Fixed reply size calculation
[elliptics.git] / bindings / cpp / callback.cpp
blobc1f4e93aa511d142cef8d5af16dc34c314d62632
1 /*
2 * 2008+ Copyright (c) Evgeniy Polyakov <zbr@ioremap.net>
3 * All rights reserved.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
16 #define _XOPEN_SOURCE 600
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <sys/socket.h>
21 #include <sys/mman.h>
22 #include <sys/wait.h>
24 #include <sstream>
25 #include <stdexcept>
27 #include "elliptics/cppdef.h"
29 using namespace ioremap::elliptics;
31 callback::callback() : complete(0)
33 pthread_cond_init(&wait_cond, NULL);
34 pthread_mutex_init(&lock, NULL);
37 callback::~callback()
41 int callback::handle(struct dnet_net_state *state, struct dnet_cmd *cmd)
43 pthread_mutex_lock(&lock);
44 if (is_trans_destroyed(state, cmd)) {
45 complete++;
46 pthread_cond_broadcast(&wait_cond);
47 } else if (cmd && state) {
48 data.append((const char *)dnet_state_addr(state), sizeof(struct dnet_addr));
49 data.append((const char *)cmd, sizeof(struct dnet_cmd) + cmd->size);
51 pthread_mutex_unlock(&lock);
53 return 0;
56 std::string callback::wait(int completed)
58 pthread_mutex_lock(&lock);
59 while (complete != completed)
60 pthread_cond_wait(&wait_cond, &lock);
61 pthread_mutex_unlock(&lock);
63 return data;