iTunes COM for Windows SDK IITUserPlaylist::AddFile access violation - c

I am just try to write simple program for some practice. But iTunes COM for Windows SDK threw me a little surprise.
I am want to create playlist and add file to it but when i call IITUserPlaylist::AddFile program crash with access violation. I can all other methods like Delete etc.
Project (MSVC10): http://www76.zippyshare.com/v/34795637/file.html
#include <iostream>
#include <windows.h>
#include <comutil.h>
#include "iTunesCOMInterface.h"
int main(int argc, char* argv[])
{
CoInitialize(0);
HRESULT hRes;
IiTunes* itunes = 0;
// note - CLSID_iTunesApp and IID_IiTunes are defined in iTunesCOMInterface_i.c
hRes = ::CoCreateInstance(CLSID_iTunesApp, NULL, CLSCTX_LOCAL_SERVER, IID_IiTunes, (PVOID *)&itunes);
if(hRes != S_OK && itunes == nullptr)
{
std::cout << "Can not create iTunesApp instance" << std::endl;
CoUninitialize();
return 1;
}
IITLibraryPlaylist* mainLibrary = 0;
hRes = itunes->get_LibraryPlaylist(&mainLibrary);
if(hRes != S_OK || mainLibrary == 0)
{
std::cout << "Can not get main library" << std::endl;
itunes->Release();
CoUninitialize();
return 1;
}
IITSource* mainLibrarySource = 0;
hRes = itunes->get_LibrarySource(&mainLibrarySource);
if(hRes != S_OK || mainLibrarySource == 0)
{
std::cout << "Can not get library source" << std::endl;
mainLibrary->Release();
itunes->Release();
CoUninitialize();
return 1;
}
IITPlaylistCollection* playlistCollection = 0;
hRes = mainLibrarySource->get_Playlists(&playlistCollection);
if(hRes != S_OK || playlistCollection == 0)
{
std::cout << "Can not get source playlists" << std::endl;
mainLibrarySource->Release();
mainLibrary->Release();
itunes->Release();
CoUninitialize();
return 1;
}
BSTR playlistName = SysAllocString(L"SoundFrost playlist");
IITPlaylist* ownPlaylist = 0;
//TODO: But there can be additional playlists with the same name, since playlist names are not unique
hRes = playlistCollection->get_ItemByName(playlistName, &ownPlaylist);
if(hRes != S_OK || ownPlaylist == 0)
{
//just create new
hRes = itunes->CreatePlaylist(playlistName, &ownPlaylist);
if(hRes != S_OK || ownPlaylist == 0)
{
std::cout << "Can not create playlist";
SysFreeString(playlistName);
playlistCollection->Release();
mainLibrarySource->Release();
mainLibrary->Release();
itunes->Release();
CoUninitialize();
return 1;
}
}
SysFreeString(playlistName);
ITPlaylistKind playlistKind;
hRes = ownPlaylist->get_Kind(&playlistKind);
if(hRes == S_OK && playlistKind == ITPlaylistKindUser)
{
IITUserPlaylist* ownUserPlaylist = static_cast<IITUserPlaylist*>(ownPlaylist);
if(ownUserPlaylist != 0)
{
BSTR filePath = SysAllocString(L"C:\\test.mp3");
IITOperationStatus* status;
//hRes = mainLibrary->AddFile(filePath, &status);
//hRes = ownUserPlaylist->AddFile(filePath, &status);
SysFreeString(filePath);
}
}
playlistCollection->Release();
ownPlaylist->Release();
mainLibrarySource->Release();
mainLibrary->Release();
itunes->Release();
CoUninitialize();
return 0;
}
If i call IITLibraryPlaylist::AddFile all was right.
Just uncomment 95 or 96 line for test.
Simple C# code works fine:
iTunesApp itunes = new iTunesLib.iTunesApp();
IITUserPlaylist playlist = itunes.CreatePlaylist("C# playlist") as IITUserPlaylist;
IITOperationStatus status = playlist.AddFile("C:\\test.mp3");
I can not understand why this is happening.

You can't really do this with COM:
IITUserPlaylist* ownUserPlaylist = static_cast<IITUserPlaylist*>(ownPlaylist);
You're just casting an interface pointer into another interface pointer. You must do a QueryInterface instead:
HRESULT hr = ownPlaylist->QueryInterface(IID_IITUserPlaylist, (void**)&ownUserPlaylist);

Related

How to authenticate to Active Directory using gsasl gssapi?

I'm trying to authenticate to an Active Directory domain using gsasl. I've already kinit'd as the Administrator. I've tried to follow the test code in gsasl tests/gssapi.c, but the code below is failing with GSASL_GSSAPI_INIT_SEC_CONTEXT_ERROR when calling gsasl_step64().
static int callback(Gsasl *ctx, Gsasl_session *sctx, Gsasl_property prop)
{
int ret = GSASL_NO_CALLBACK;
switch (prop) {
case GSASL_AUTHZID:
gsasl_property_set(sctx, GSASL_AUTHZID, "Administrator");
ret = GSASL_OK;
break;
case GSASL_SERVICE:
gsasl_property_set(sctx, prop, "host");
ret = GSASL_OK;
break;
case GSASL_HOSTNAME:
char hostname[HOST_NAME_MAX];
gethostname(hostname, HOST_NAME_MAX);
gsasl_property_set(sctx, prop, hostname);
ret = GSASL_OK;
break;
default:
break;
}
return ret;
}
int main()
{
Gsasl *ctx = NULL;
Gsasl_session *session;
char *s1 = NULL, *s2 = NULL;
int ret;
ret = gsasl_init(&ctx);
if (ret != GSASL_OK) {
cerr << "gsasl_init failed" << endl;
return ret;
}
if (!gsasl_client_support_p(ctx, "GSSAPI")) {
cerr << "No support for GSSAPI." << endl;
return 77;
}
gsasl_callback_set(ctx, callback);
ret = gsasl_client_start(ctx, "GSSAPI", &session);
if (ret != GSASL_OK) {
cerr << "gsasl_client_start failed" << endl;
return ret;
}
do {
ret = gsasl_step64(session, s2, &s1);
gsasl_free(s2);
if (ret != GSASL_OK && ret != GSASL_NEEDS_MORE) {
cerr << "gsasl_step64 failed " << ret << endl;
return ret;
}
} while (ret != GSASL_OK);
if (s1) {
gsasl_free(s1);
}
gsasl_finish(session);
}
Does anyone see what I'm doing wrong?
Not really a clear answer because I don't use AD currently, so I can't verify my answer. But to localize the issue:
I would try the examples first. Maybe this test is desired to fail. If it doesn't work with the examples, I would investigate the source of library of the GNU SASL library.
Here is an extract from the source of the library, which throws this error:
maj_stat = gss_init_sec_context (&min_stat,
GSS_C_NO_CREDENTIAL,
&state->context,
state->service,
state->mech_oid,
GSS_C_MUTUAL_FLAG,
0,
&state->cb,
buf,
&actual_mech_type,
&state->token, &ret_flags, NULL);
if (maj_stat != GSS_S_COMPLETE && maj_stat != GSS_S_CONTINUE_NEEDED)
return GSASL_GSSAPI_INIT_SEC_CONTEXT_ERROR;
As you can see, the function gss_init_sec_context() is called, In the if clause only GSS_S_COMPLETE and GSS_S_CONTINUE_NEEDED will be tested. But gss_init_sec_context() has many more status codes. So I would change the code of the library and print out a clearer error code, why it doesn't work.
There was also an issue in WINBIND/SAMBA, don't know if you use SAMBA, which throws an unspecified error in this specific function.

ffmpeg rtsp on C

I need to write RTSP steram from IP-cam to file. I use FFMPEG to do this. I found code example on C++, but i need to use only C. Can anyone help me?
i have problems in file operations. how can i write stream to file?
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <sstream>
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavformat/avio.h>
#include <libswscale/swscale.h>
}
int main(int argc, char** argv) {
// Open the initial context variables that are needed
SwsContext *img_convert_ctx;
AVFormatContext* format_ctx = avformat_alloc_context();
AVCodecContext* codec_ctx = NULL;
int video_stream_index;
// Register everything
av_register_all();
avformat_network_init();
//open RTSP
if (avformat_open_input(&format_ctx, "rtsp://134.169.178.187:8554/h264.3gp",
NULL, NULL) != 0) {
return EXIT_FAILURE;
}
if (avformat_find_stream_info(format_ctx, NULL) < 0) {
return EXIT_FAILURE;
}
//search video stream
for (int i = 0; i < format_ctx->nb_streams; i++) {
if (format_ctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
video_stream_index = i;
}
AVPacket packet;
av_init_packet(&packet);
//open output file
AVFormatContext* output_ctx = avformat_alloc_context();
AVStream* stream = NULL;
int cnt = 0;
//start reading packets from stream and write them to file
av_read_play(format_ctx); //play RTSP
// Get the codec
AVCodec *codec = NULL;
codec = avcodec_find_decoder(AV_CODEC_ID_H264);
if (!codec) {
exit(1);
}
// Add this to allocate the context by codec
codec_ctx = avcodec_alloc_context3(codec);
avcodec_get_context_defaults3(codec_ctx, codec);
avcodec_copy_context(codec_ctx, format_ctx->streams[video_stream_index]->codec);
std::ofstream output_file;
if (avcodec_open2(codec_ctx, codec, NULL) < 0)
exit(1);
img_convert_ctx = sws_getContext(codec_ctx->width, codec_ctx->height,
codec_ctx->pix_fmt, codec_ctx->width, codec_ctx->height, AV_PIX_FMT_RGB24,
SWS_BICUBIC, NULL, NULL, NULL);
int size = avpicture_get_size(AV_PIX_FMT_YUV420P, codec_ctx->width,
codec_ctx->height);
uint8_t* picture_buffer = (uint8_t*) (av_malloc(size));
AVFrame* picture = av_frame_alloc();
AVFrame* picture_rgb = av_frame_alloc();
int size2 = avpicture_get_size(AV_PIX_FMT_RGB24, codec_ctx->width,
codec_ctx->height);
uint8_t* picture_buffer_2 = (uint8_t*) (av_malloc(size2));
avpicture_fill((AVPicture *) picture, picture_buffer, AV_PIX_FMT_YUV420P,
codec_ctx->width, codec_ctx->height);
avpicture_fill((AVPicture *) picture_rgb, picture_buffer_2, AV_PIX_FMT_RGB24,
codec_ctx->width, codec_ctx->height);
while (av_read_frame(format_ctx, &packet) >= 0 && cnt < 1000) { //read ~ 1000 frames
std::cout << "1 Frame: " << cnt << std::endl;
if (packet.stream_index == video_stream_index) { //packet is video
std::cout << "2 Is Video" << std::endl;
if (stream == NULL) { //create stream in file
std::cout << "3 create stream" << std::endl;
stream = avformat_new_stream(output_ctx,
format_ctx->streams[video_stream_index]->codec->codec);
avcodec_copy_context(stream->codec,
format_ctx->streams[video_stream_index]->codec);
stream->sample_aspect_ratio =
format_ctx->streams[video_stream_index]->codec->sample_aspect_ratio;
}
int check = 0;
packet.stream_index = stream->id;
std::cout << "4 decoding" << std::endl;
int result = avcodec_decode_video2(codec_ctx, picture, &check, &packet);
std::cout << "Bytes decoded " << result << " check " << check
<< std::endl;
if (cnt > 100) //cnt < 0)
{
sws_scale(img_convert_ctx, picture->data, picture->linesize, 0,
codec_ctx->height, picture_rgb->data, picture_rgb->linesize);
std::stringstream file_name;
file_name << "test" << cnt << ".ppm";
output_file.open(file_name.str().c_str());
output_file << "P3 " << codec_ctx->width << " " << codec_ctx->height
<< " 255\n";
for (int y = 0; y < codec_ctx->height; y++) {
for (int x = 0; x < codec_ctx->width * 3; x++)
output_file
<< (int) (picture_rgb->data[0]
+ y * picture_rgb->linesize[0])[x] << " ";
}
output_file.close();
}
cnt++;
}
av_free_packet(&packet);
av_init_packet(&packet);
}
av_free(picture);
av_free(picture_rgb);
av_free(picture_buffer);
av_free(picture_buffer_2);
av_read_pause(format_ctx);
avio_close(output_ctx->pb);
avformat_free_context(output_ctx);
return (EXIT_SUCCESS);
}
please help me to compile this by C-compiler.

How to set extended controls in V4L2 correctly?

I want to record a video from a V4L2 device (from the Raspberry Pi camera) in C.
The recording itself works and I can save the video to a file.
However I need to change the bitrate of the video. From the strace output of the v4l2-ctl --set-ctrl video_bitrate=10000000 command I know that the extended controls API of v4l2 is used to achieve this.
Here's my code which doesn't work so far:
#include <iostream>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/mman.h> //mmap
#include <fcntl.h>
#include <unistd.h>
#include <linux/videodev2.h>
using namespace std;
#define numbuffers 3
struct picturebuffer
{
void *startadress;
size_t length;
};
//array in which the buffer pointer are being stored
picturebuffer pb[numbuffers];
int main()
{
//open camera
int fd;
fd = open("/dev/video0", O_RDWR);
if(fd < 0)
{
cout << "error during opening the camera device!";
cout.flush();
}
cout << "camera opened";
//read capabilities
struct v4l2_capability caps;
if(ioctl(fd, VIDIOC_QUERYCAP, &caps) < 0)
{
cout << "error while reading the capabilities!";
cout.flush();
}
cout << "Capabilities " << caps.capabilities << endl;
//ToDo: check for required capabilities
//set image data
struct v4l2_format format;
format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
format.fmt.pix.pixelformat = V4L2_PIX_FMT_H264;
format.fmt.pix.width = 1920;
format.fmt.pix.height = 1080;
if(ioctl(fd, VIDIOC_S_FMT, &format) < 0)
{
cout << "error in the image format";
}
cout << "Image properties set" << endl;
//Todo: check if width and height fit together (VIDIOC_ENUM_FRAMESIZES)
//set extended Controls
struct v4l2_ext_controls ecs;
struct v4l2_ext_control ec;
memset(&ecs, 0, sizeof(ecs));
memset(&ec, 0, sizeof(ec));
ec.id = V4L2_CID_MPEG_VIDEO_BITRATE;
ec.value = 10000000;
ec.size = 0;
ecs.controls = &ec;
ecs.count = 1;
ecs.ctrl_class = V4L2_CTRL_CLASS_MPEG;
if(ioctl(fd, VIDIOC_S_EXT_CTRLS, &ecs) < 0)
{
cout << "error in extended controls bitrate";
cout.flush();
}
//allocate buffer in the kernel
struct v4l2_requestbuffers req;
req.count = numbuffers;
req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
req.memory = V4L2_MEMORY_MMAP;
if(ioctl(fd, VIDIOC_REQBUFS, &req) < 0)
{
cout << "errro while allocating buffer";
cout.flush();
}
cout << "number of buffers: " << req.count << endl;
cout.flush();
//map buffers into userspace
for(int i=0; i<numbuffers; i++)
{
struct v4l2_buffer bufferinfo;
memset(&bufferinfo, 0, sizeof(bufferinfo));
bufferinfo.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
bufferinfo.memory = V4L2_MEMORY_MMAP;
bufferinfo.index = i;
if(ioctl(fd, VIDIOC_QUERYBUF, &bufferinfo) < 0)
{
cout << "error while querying bufferinfo";
cout.flush();
}
pb[i].startadress = mmap(NULL, bufferinfo.length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, bufferinfo.m.offset);
pb[i].length = bufferinfo.length;
if(pb[i].startadress == MAP_FAILED)
{
cout << "error during mmap" << endl;
}
memset(pb[i].startadress, 0, bufferinfo.length);
cout << "size of buffer: " << bufferinfo.length << endl;
}
cout << "buffers mapped into userspace" << endl;
cout.flush();
//queue in the buffers
for(int i=0; i<numbuffers; i++)
{
struct v4l2_buffer bufferinfo;
memset(&bufferinfo, 0, sizeof(bufferinfo));
bufferinfo.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
bufferinfo.memory = V4L2_MEMORY_MMAP;
bufferinfo.index = i;
if(ioctl(fd, VIDIOC_QBUF, &bufferinfo) < 0)
{
cout << "error while queueing the buffers in" << endl;
}
}
//since that point the driver starts capturing the pics
int type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
if(ioctl(fd, VIDIOC_STREAMON, &type) < 0)
{
cout << "error while starting the stream" << endl;
}
int file;
if((file = open("/home/pi/image.h264", O_WRONLY | O_CREAT, 0660)) < 0)
{
cout << "error while writing the file";
}
//loop for managing the pics
for(int i=0; i<100; i++)
{
struct v4l2_buffer bufferinfo;
memset(&bufferinfo, 0, sizeof(bufferinfo));
bufferinfo.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
bufferinfo.memory = V4L2_MEMORY_MMAP;
if(ioctl(fd, VIDIOC_DQBUF, &bufferinfo) < 0)
{
cout << "error while getting the buffer!" << endl;
}
//do anything with the pic
char buf[pb[bufferinfo.index].length];
memcpy(&buf, pb[bufferinfo.index].startadress, pb[bufferinfo.index].length);
cout << bufferinfo.index << endl;
cout.flush();
//write picture into the file
write(file, pb[bufferinfo.index].startadress, pb[bufferinfo.index].length);
if(ioctl(fd, VIDIOC_QBUF, &bufferinfo) < 0)
{
cout << "error while enqueuing the buffer" << endl;
}
}
close(file);
if(ioctl(fd, VIDIOC_STREAMOFF, &type) < 0)
{
cout << "error while stopping the stream" << endl;
}
//clean up
for(int i=0; i<numbuffers; i++)
{
if(munmap(pb[i].startadress, pb[i].length) < 0)
{
cout << "error during unmap";
}
}
//close camera file
close(fd);
cout << "!!!Hello World!!!" << endl;
cout.flush();
return 0;
}
The ioctl call seems to succeed, however my output file always has the same size as of 199,2 MB. Does someone know what´s wrong in the code ?
You need to check if the camera driver supports that IOCTL command. If the driver doesn't support the IOCTL command by not implementing it, you still can execute the command and it is routed to v4l2 default implementation, no actual changes are applied to the camera setting
Try to change the lines:
pb[bufferinfo.index].length
By:
pb[bufferinfo.index].bytesused
For example:
write(file, pb[bufferinfo.index].startadress, pb[bufferinfo.index].bytesused);

avformat_write_header fails with error "invalid argument

I'm trying to write a chunk of a mp4 file to a .ts file, which does not yet exist.
So first I create AVFormatContext this way:
AVOutputFormat *oformat = av_guess_format( NULL,
"mpegts",
"video/x-mpegts" );
AVFormatContext * outFormatContext = NULL;
avformat_alloc_output_context2( &outFormatContext, oformat, NULL, dst_file)
outFormatContext is created and outFormatContext->oformat is set to oformat. Then I open I/O context on outFormatContext:
avio_open(&outFormatContext->pb, dst_file, AVIO_FLAG_WRITE)
avformat_write_header( outFormatContext, NULL );
according to this this is enough for avformat_write_header to work but it fails with error "invalid argument".
What else needs to be done to successfully write the header?
Although it's a little bit late, I hope it would be helpful.
Here is my way to call avformat_write_header, you should have a stream provided to write the header.
AVFormatContext* pOutFormatContext;
AVOutputFormat* avOutputFormat;
if ((avOutputFormat = av_guess_format(NULL, "mp4", "video/mp4")) == NULL) {
cerr << "Could not guess output format" << endl;
return -1;
}
avformat_alloc_output_context2(&pOutFormatContext,
av_guess_format("mp4", NULL, "video/mp4"), NULL, NULL);
if (pOutFormatContext == NULL) {
cerr << "Could not allocate output context" << endl;
return -1;
}
for (int i = 0; i < avFormatContext->nb_streams; i++) {
AVStream* inAVStream = avFormatContext->streams[i];
AVStream* outAVStream = avformat_new_stream(pOutFormatContext, inAVStream->codec->codec);
if (avcodec_copy_context(outAVStream->codec, inAVStream->codec) < 0) {
cerr << "Failed to copy codec context" << endl;
return -1;
}
outAVStream->codec->codec_tag = 0;
if (pOutFormatContext->oformat->flags & AVFMT_GLOBALHEADER) {
outAVStream->codec->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
}
}
avio_open(&pOutFormatContext->pb, "test.mp4", AVIO_FLAG_READ_WRITE);
if (pOutFormatContext->pb == NULL) {
cerr << "Could not open for writing" << endl;
return -1;
}
if (avformat_write_header(pOutFormatContext, NULL) != 0) {
cerr << "Could not write header" << endl;
return -1;
}

Example Code for MemCached in C

I am looking for some sample C code for using memcache to set a value
Connecting to Server/Port
using multiple memcache_set
Close
I have the app running in PHP in 5 lines of code but can not find any good memcache samples in C which I need to port to.
This is a great memcached sample in C
#include <libmemcached/memcached.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv) {
//memcached_servers_parse (char *server_strings);
memcached_server_st *servers = NULL;
memcached_st *memc;
memcached_return rc;
char *key = "keystring";
char *value = "keyvalue";
char *retrieved_value;
size_t value_length;
uint32_t flags;
memc = memcached_create(NULL);
servers = memcached_server_list_append(servers, "localhost", 11211, &rc);
rc = memcached_server_push(memc, servers);
if (rc == MEMCACHED_SUCCESS)
fprintf(stderr, "Added server successfully\n");
else
fprintf(stderr, "Couldn't add server: %s\n", memcached_strerror(memc, rc));
rc = memcached_set(memc, key, strlen(key), value, strlen(value), (time_t)0, (uint32_t)0);
if (rc == MEMCACHED_SUCCESS)
fprintf(stderr, "Key stored successfully\n");
else
fprintf(stderr, "Couldn't store key: %s\n", memcached_strerror(memc, rc));
retrieved_value = memcached_get(memc, key, strlen(key), &value_length, &flags, &rc);
printf("Yay!\n");
if (rc == MEMCACHED_SUCCESS) {
fprintf(stderr, "Key retrieved successfully\n");
printf("The key '%s' returned value '%s'.\n", key, retrieved_value);
free(retrieved_value);
}
else
fprintf(stderr, "Couldn't retrieve key: %s\n", memcached_strerror(memc, rc));
return 0;
}
Here is a slightly more complete answer, which also shows how to get and delete from a memcached server. The code is technically in C++, but doesn't use any features that would be unfamiliar to most C programmers.
To compile: g++ sample.cc -o sample -ggdb -O3 -lmemcached
To run: ./sample -s localhost -p 11211
The functions whose names begin with mcd_ show the basics of inserting, deleting, and getting, as well as connecting and disconnecting from memcached. The remaining code makes use of those functions to insert 50 key/value pairs, verify they all are in the server, remove half the pairs, and then verify that the right keys remain / are gone.
#include <iostream>
#include <string>
#include <unistd.h>
#include <vector>
#include <libmemcached/memcached.h>
// NB: I know that `using namespace std` at global scope is bad form, and that
// global variables are bad form. However, they help keep this answer under
// 200 lines.
using namespace std;
/// The connection to memcached
memcached_st *mcd;
/// A global set of key/value pairs that we manufacture for the sake of this
/// example
vector<pair<string, string>> kv_pairs;
/// Put a key/value pair into memcached with expiration 0, no special flags
bool mcd_set(const string &key, const string &val) {
auto rc = memcached_set(mcd, key.c_str(), key.length(), val.c_str(),
val.length(), (time_t)0, (uint32_t)0);
if (rc == MEMCACHED_SUCCESS)
return true;
cout << "Error in mcd_set(): " << memcached_strerror(mcd, rc) << endl;
return false;
}
/// Delete a key/value pair from memcached. return true on success
/// NB: `(time_t)0` is an expiration of `0`, i.e., immediately
bool mcd_delete(const string &key) {
auto rc = memcached_delete(mcd, key.c_str(), key.length(), (time_t)0);
if (rc == MEMCACHED_SUCCESS)
return true;
cout << "Error in mcd_delete(): " << memcached_strerror(mcd, rc) << endl;
return false;
}
/// Get a value from the kv store, using its key to do the lookup. return
/// true on success. On success, the by-ref out parameter `val` will be set.
bool mcd_get(const string &key, string &val) {
memcached_return rc;
size_t len;
uint32_t flags = 0;
char *res = memcached_get(mcd, key.c_str(), key.length(), &len, &flags, &rc);
if (rc == MEMCACHED_SUCCESS) {
val = string(res, len);
free(res);
return true;
}
// NB: skip next line, because we don't want error messages on a failed get:
//
// cout << "Error in mcd_get(): " << memcached_strerror(mcd, rc) << endl;
return false;
}
/// Connect to a single memcached server on the provided port
bool mcd_connect(const string &servername, const int port) {
mcd = memcached_create(nullptr);
memcached_return rc;
memcached_server_st *servers = nullptr;
servers =
memcached_server_list_append(servers, servername.c_str(), port, &rc);
rc = memcached_server_push(mcd, servers);
if (rc == MEMCACHED_SUCCESS) {
cout << " Successfully connected to " << servername << ":" << port << endl;
return true;
}
cout << "Error in mcd_connect(): " << memcached_strerror(mcd, rc) << endl;
return false;
}
/// Close the connection to memcached
void mcd_shutdown() {
memcached_free(mcd);
cout << " Successfully disconnected\n";
}
/// Create a bunch of key/value pairs
void build_kv_pairs(int howmany) {
for (int i = 0; i < howmany; ++i) {
string key = "key" + to_string(i) + "_______";
string val = "val" + to_string(i);
for (int i = 0; i < 100; ++i)
val += ("_" + to_string(i));
kv_pairs.push_back({key, val});
}
}
/// insert a bunch of k/v pairs into memcached
bool put_kv_pairs(int howmany) {
for (int i = 0; i < howmany; ++i) {
if (!mcd_set(kv_pairs[i].first, kv_pairs[i].second)) {
cout << "Error inserting key `" << kv_pairs[i].first << "`\n";
return false;
}
}
cout << " put_kv_pairs(" << howmany << ") completed successfully\n";
return true;
}
/// Remove a sequence of keys from memcached
///
/// NB: Here and below, we use first/last/stride so that we can vary wich
/// key/value pairs are operated on
bool delete_kv_pairs(int first, int last, int stride) {
for (int i = first; i <= last; i += stride) {
if (!mcd_delete(kv_pairs[i].first)) {
cout << "Error removing key `" << kv_pairs[i].first << "`\n";
return false;
}
}
cout << " delete_kv_pairs(" << first << ", " << last << ", " << stride
<< ") completed successfully\n";
return true;
}
/// Verify that a sequence of keys is in memcached, with the right expected
/// values
bool check_present_pairs(int first, int last, int stride) {
for (int i = first; i <= last; i += stride) {
string value;
if (!mcd_get(kv_pairs[i].first, value)) {
cout << "Error getting key `" << kv_pairs[i].first
<< "`: key not found\n";
return false;
}
if (value != kv_pairs[i].second) {
cout << "Value error while getting key `" << kv_pairs[i].first << "`\n";
cout << " Expected: `" << kv_pairs[i].second << "`\n";
cout << " Found: `" << value << "`\n";
return false;
}
}
cout << " check_present_pairs(" << first << ", " << last << ", " << stride
<< ") completed successfully\n";
return true;
}
/// Verify that a sequence of keys is *not* in memcached
bool check_missing_pairs(int first, int last, int stride) {
for (int i = first; i <= last; i += stride) {
string value;
if (mcd_get(kv_pairs[i].first, value)) {
cout << "Error getting key `" << kv_pairs[i].first
<< "`: key unexpectedly found\n";
return false;
}
}
cout << " check_missing_pairs(" << first << ", " << last << ", " << stride
<< ") completed successfully\n";
return true;
}
int main(int argc, char **argv) {
// Parse args to get server name (-s) and port (-p)
string servername = "";
int port;
long opt;
while ((opt = getopt(argc, argv, "s:p:")) != -1) {
switch (opt) {
case 's':
servername = string(optarg);
break;
case 'p':
port = atoi(optarg);
break;
}
}
// Create a bunch of key/value pairs to use for the experiments
int howmany = 50;
build_kv_pairs(howmany);
// Connect to memcached
mcd_connect(servername, port);
// insert all the pairs, make sure they are all present
put_kv_pairs(howmany);
check_present_pairs(0, howmany - 1, 1);
// Delete the even pairs
delete_kv_pairs(0, howmany - 2, 2);
// ensure the odd pairs are present
check_present_pairs(1, howmany - 1, 2);
// ensure the even pairs are not present
check_missing_pairs(0, howmany, 2);
mcd_shutdown();
}
The code should produce output like the following:
Successfully connected to localhost:11211
put_kv_pairs(50) completed successfully
check_present_pairs(0, 49, 1) completed successfully
delete_kv_pairs(0, 48, 2) completed successfully
check_present_pairs(1, 49, 2) completed successfully
check_missing_pairs(0, 50, 2) completed successfully
Successfully disconnected

Resources