zookeeper-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From praveshjain1991 <praveshjain1...@gmail.com>
Subject Storing 2d array in z node
Date Thu, 14 Aug 2014 12:06:47 GMT
 am trying to store a 2d integer array in zookeeper znode, but upon
retrieving it, the expected data is not returned.

Here is the code:

#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <zookeeper.h>
#include <pthread.h>

static zhandle_t *zh;
static int batchMode=0;
static int shutdownThisThing=0;

pthread_mutex_t count_mutex;
pthread_cond_t count_threshold_cv;

struct timeval startTime;
struct Stat stat;

char* buffer;
int* buff_len;

int rc = 0;

int size = 12;

char *foo_get_cert_once(char* id) { return 0; }

void my_data_completion(int rc, const char *value, int value_len,
    const struct Stat *stat, const void *data) {

    int *int_value = (int *) value;     //data received from the z-node
    int *int_data = (int *) data;       //data received from the caller
    int i=0,j=0;
    for(i=0;i<size*size;i++)
        fprintf(stderr, "%d ",*(int_value+i) );
    fprintf(stderr,"\n");
    for(i=0;i<size*size;i++)
        fprintf(stderr, "%d ",*(int_data+i) );
}

void watcher(zhandle_t *zzh, int type, int state, const char *path, void
*watcherCtx) {

    fprintf(stderr, "Inside watcher function. Here type is %d and state is
%d\n", type, state);
    zoo_exists(zh, "/pravesh", 1, &stat);
}

/**
Function called in a separate thread.
Ignite wait here.
**/
void *waiting(void *arg){

    fprintf(stderr, "Inside waiting function\n");
    pthread_mutex_lock(&count_mutex);
    pthread_cond_wait(&count_threshold_cv, &count_mutex);
    pthread_mutex_unlock(&count_mutex);
    pthread_exit(NULL);
}

int main() {

    char buffer[512];
    char p[2048];
    char *cert=0;
    char appId[64];

    int string[size][size];
    int i=0,j=0,k=0;

    for(i=0;i<size;i++)
        for(j=0;j<size;j++)
            string[i][j]=k++;

    for(i=0;i<size;i++)
        for(j=0;j<size;j++)
            fprintf(stderr, "%d ", string[i][j]);
    fprintf(stderr, "\n");

    int end = 0;
    int rc = 0;

    zoo_set_debug_level(ZOO_LOG_LEVEL_INFO);

    zh = zookeeper_init("localhost:2181", watcher, 10000, 0, 0, 0);
    if (!zh) {
        return errno;
    }

    int buflen= sizeof(buffer);

    zoo_set(zh, "/pravesh", (const char*)string, size*size, -1);               
//sets the data in the z-node
    zoo_aget(zh, "/pravesh", 1, my_data_completion, (const char*)string );     
//gets the data from the z-node and passes it to my_data_completion

/**
 * Trying to implement waiting functionality using threads
 **/

    fprintf(stderr, "Starting threads\n");
    pthread_t thread;
    pthread_mutex_init(&count_mutex, NULL);
    pthread_cond_init (&count_threshold_cv, NULL);
    pthread_create(&thread, NULL, &waiting, NULL);

    fprintf(stderr, "Waiting function has been called\n");

    pthread_mutex_destroy(&count_mutex);
    pthread_cond_destroy(&count_threshold_cv);
    pthread_exit(NULL);
}

The output after printing data is correct(numbers 0-143) but the output
after printing value is unexpected (shown below):

    0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
27 28 29 30 31 32 33 34 35 0 0 1946159936 32538 176 0 37 0 1946159744 32538
21 25 0 0 132017 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Is there something wrong with the implementation? 



--
View this message in context: http://zookeeper-user.578899.n2.nabble.com/Storing-2d-array-in-z-node-tp7580158.html
Sent from the zookeeper-user mailing list archive at Nabble.com.

Mime
View raw message