int topoParseData(const uint8_t *buff, int len) { int res,ncs,msize; const struct nodeID **n; const void *m; if (!buff || buff[0] == MSG_TYPE_TOPOLOGY) { res = psample_parse_data(context,buff,len); if (counter <= TMAN_MAX_IDLE) counter++; } if (counter >= TMAN_MAX_IDLE && (!buff || buff[0] == MSG_TYPE_TMAN)) { n = psample_get_cache(context,&ncs); m = psample_get_metadata(context,&msize); res = tmanParseData(buff,len,n,ncs,m,msize); } return res; }
static void *cycle_loop(void *p) { int done = 0; int cnt = 0; while (!done) { const int tout = 1; pthread_mutex_lock(&neigh_lock); psample_parse_data(context, NULL, 0); pthread_mutex_unlock(&neigh_lock); if (cnt % 10 == 0) { const struct nodeID **neighbours; int n, i; pthread_mutex_lock(&neigh_lock); neighbours = psample_get_cache(context, &n); printf("I have %d neighbours:\n", n); for (i = 0; i < n; i++) { printf("\t%d: %s\n", i, node_addr(neighbours[i])); } fflush(stdout); if (fprefix) { FILE *f; char fname[64]; sprintf(fname, "%s-%d.txt", fprefix, port); f = fopen(fname, "w"); if (f) fprintf(f, "#Cache size: %d\n", n); for (i = 0; i < n; i++) { if (f) fprintf(f, "%d\t\t%d\t%s\n", port, i, node_addr(neighbours[i])); } fclose(f); } pthread_mutex_unlock(&neigh_lock); } cnt++; sleep(tout); } return NULL; }
static void loop(struct context *con) { int done = 0; #define BUFFSIZE 1024 static uint8_t buff[BUFFSIZE]; int cnt = 0; psample_parse_data(con->ps_context, NULL, 0); while (!done) { int len; int news; int data_source; const struct timeval tout = {1, 0}; struct timeval t1; t1 = tout; news = wait4any_threaded(con->net_context, con->cloud_context, &t1, NULL, &data_source); if (news > 0) { struct nodeID *remote = NULL; // printf("Got data from: %d\n", data_source); if (data_source == DATA_SOURCE_NET) len = recv_from_peer(con->net_context, &remote, buff, BUFFSIZE); else if (data_source == DATA_SOURCE_CLOUD) len = recv_from_cloud(con->cloud_context, buff, BUFFSIZE); psample_parse_data(con->ps_context, buff, len); if (remote) nodeid_free(remote); } else { if (psample_parse_data(con->ps_context, NULL, 0) != 0){ fprintf(stderr, "Error parsing data... quitting\n"); exit(1); } if (cnt % 10 == 0) { const struct nodeID *const *neighbourhoods; char addr[256]; int n, i; neighbourhoods = psample_get_cache(con->ps_context, &n); printf("I have %d neighbours:\n", n); for (i = 0; i < n; i++) { node_addr(neighbourhoods[i], addr, 256); printf("\t%d: %s\n", i, addr); } fflush(stdout); if (fprefix) { FILE *f; char fname[64]; fprintf(stderr, "ci sono??? %s\n", fprefix); sprintf(fname, "%s-%d.txt", fprefix, port); f = fopen(fname, "w"); if (f) fprintf(f, "#Cache size: %d\n", n); for (i = 0; i < n; i++) { node_addr(neighbourhoods[i], addr, 256); if (f) fprintf(f, "%d\t\t%d\t%s\n", port, i, addr); } fclose(f); } } cnt++; } } }