int main(int argc, char *argv[]) { struct nodeID *cloudHost; struct context *con; if (cmdline_parse(argc, argv) != 0){ fprintf(stderr, "Error parsing parameters!\n"); return 1; } if (!cloud_conf){ fprintf(stderr, "No cloud configuration provided!\n"); return 1; } con = init(); if (!con){ fprintf(stderr, "Error initializing!\n"); return 1; } cloudHost = get_cloud_node(con->cloud_context, 0); psample_add_peer(con->ps_context, cloudHost, NULL, 0); nodeid_free(cloudHost); loop(con); fprintf(stderr, "Quitting!\n"); return 0; }
static enum signaling_type sig_receive(struct nodeID *my_sock, struct nodeID **r, int *id, struct chunkID_set **cset) { static uint8_t buff[BUFFSIZE]; int ret; struct nodeID *owner; struct nodeID *remote; enum signaling_type sig_type; int max_deliver = 0; uint16_t trans_id = 0; ret = recv_from_peer(my_sock, &remote, buff, BUFFSIZE); /* TODO: Error check! */ if (buff[0] != MSG_TYPE_SIGNALLING) { fprintf(stderr, "Wrong message type!!!\n"); return -1; } fprintf(stdout,"Received message of %d bytes\n",ret); ret = parseSignaling(buff + 1, ret - 1, &owner, cset, &max_deliver, &trans_id, &sig_type); fprintf(stdout,"Parsed %d bytes: trans_id %d, Max_deliver %d\n", ret, trans_id, max_deliver); switch (sig_type) { case sig_accept: fprintf(stdout, "Accept of %d chunks\n", chunkID_set_size(*cset)); break; case sig_deliver: fprintf(stdout, "Deliver of %d chunks\n", chunkID_set_size(*cset)); break; case sig_send_buffermap: fprintf(stdout, "I received a buffer of %d chunks\n", chunkID_set_size(*cset)); break; } if(owner)nodeid_free(owner); if (r) { *r = remote; } else { nodeid_free(remote); } if (id) *id = trans_id; return sig_type; }
void cache_free(struct peer_cache *c) { int i; for (i = 0; i < c->current_size; i++) { if(c->entries[i].id) { nodeid_free(c->entries[i].id); } } free(c->entries); free(c->metadata); free(c); }
int main(int argc, char *argv[]) { struct nodeID *my_sock; int ret; cmdline_parse(argc, argv); my_sock = init(); ret = 0; if (dst_port != 0) { ret = client_side(my_sock); } else { ret = server_side(my_sock); } nodeid_free(my_sock); return ret; }
int main(int argc, char *argv[]) { struct nodeID *local_node; struct cloud_helper_context *cloud_ctx; struct psample_context *ps_ctx; uint8_t buff[1024]; local_node = net_helper_init("127.0.0.1", 1234, ""); cloud_ctx = cloud_helper_init(local_node, "provder=delegate,delegate_lib=my_helper.so," \ "param1=value1,..."); ps_ctx = psample_init(local_node, NULL, 0, "protocol=cloudcast,cache_size=25"); /* Perform first cycle to initialize the protocol */ psample_parse_data(con->ps_context, NULL, 0); while(1) { const struct timeval tout = {1, 0}; int data_source = -1; int news = 0; /* Wait for incoming data either from peers or cloud */ news = wait4any_threaded(local_node, cloud_ctx, &tout, NULL, &data_source); if (news > 0) { struct nodeID *remote = NULL; int len = 0; /* Incoming data available, perform passive thread */ if (data_source == DATA_SOURCE_NET) { len = recv_from_peer(local_node, &remote, buff, 1024); } else if (data_source == DATA_SOURCE_CLOUD) { len = recv_from_cloud(cloud_ctx, buff, 1024); } psample_parse_data(con->ps_context, buff, len); if (remote) nodeid_free(remote); } else { /* Let the peer sampler handles active cycles and sleep periods */ psample_parse_data(ps_ctx, NULL, 0); } } }
static int cache_insert(struct peer_cache *c, struct cache_entry *e, const void *meta) { int i, position; if (c->current_size == c->cache_size) { return -2; } position = 0; for (i = 0; i < c->current_size; i++) { assert(e->id); assert(c->entries[i].id); if (c->entries[i].timestamp <= e->timestamp) { position = i + 1; } if (nodeid_equal(e->id, c->entries[i].id)) { if (c->entries[i].timestamp > e->timestamp) { assert(i >= position); nodeid_free(c->entries[i].id); c->entries[i] = *e; memcpy(c->metadata + i * c->metadata_size, meta, c->metadata_size); if (position != i) { memmove(c->entries + position + 1, c->entries + position, sizeof(struct cache_entry) * (i - position)); memmove(c->metadata + (position + 1) * c->metadata_size, c->metadata + position * c->metadata_size, (i -position) * c->metadata_size); } return position; } return -1; } } if (position != c->current_size) { memmove(c->entries + position + 1, c->entries + position, sizeof(struct cache_entry) * (c->current_size - position)); memmove(c->metadata + (position + 1) * c->metadata_size, c->metadata + position * c->metadata_size, (c->current_size - position) * c->metadata_size); } c->current_size++; c->entries[position] = *e; memcpy(c->metadata + position * c->metadata_size, meta, c->metadata_size); return position; }
static void *recv_loop(void *p) { struct nodeID *s = p; int done = 0; #define BUFFSIZE 1024 static uint8_t buff[BUFFSIZE]; while (!done) { int len; struct nodeID *remote; len = recv_from_peer(s, &remote, buff, BUFFSIZE); pthread_mutex_lock(&neigh_lock); psample_parse_data(context, buff, len); pthread_mutex_unlock(&neigh_lock); nodeid_free(remote); } return NULL; }
void cache_delay(struct peer_cache *c, int dts) { int i; for (i = 0; i < c->current_size; i++) { if (c->max_timestamp && (c->entries[i].timestamp + dts > c->max_timestamp)) { int j = i; while(j < c->current_size && c->entries[j].id) { nodeid_free(c->entries[j].id); c->entries[j++].id = NULL; } c->current_size = i; /* The cache is ordered by timestamp... all the other entries wiil be older than this one, so remove all of them */ } else { c->entries[i].timestamp = c->entries[i].timestamp + dts > 0 ? c->entries[i].timestamp + dts : 0; } } }
int cache_del(struct peer_cache *c, const struct nodeID *neighbour) { int i; int found = 0; for (i = 0; i < c->current_size; i++) { if (nodeid_equal(c->entries[i].id, neighbour)) { nodeid_free(c->entries[i].id); c->current_size--; found = 1; if (c->metadata_size && (i < c->current_size)) { memmove(c->metadata + c->metadata_size * i, c->metadata + c->metadata_size * (i + 1), c->metadata_size * (c->current_size - i)); } } if (found && (i < c->current_size)) { c->entries[i] = c->entries[i + 1]; } } return c->current_size; }
int server_side(struct nodeID *my_sock) { int trans_id = 0; int chunktosend; struct chunkID_set *cset = NULL; struct chunkID_set *rcset; struct nodeID *remote; enum signaling_type sig_type; sig_type = sig_receive(my_sock, &remote, &trans_id, &cset); switch(sig_type) { case sig_offer: fprintf(stdout, "1) Message OFFER: peer offers %d chunks\n", chunkID_set_size(cset)); chunktosend = chunkID_set_get_latest(cset); printChunkID_set(cset); rcset = chunkID_set_init("size=1"); fprintf(stdout, "2) Acceping only latest chunk #%d\n", chunktosend); chunkID_set_add_chunk(rcset, chunktosend); acceptChunks(remote, rcset, trans_id++); break; case sig_request: fprintf(stdout, "1) Message REQUEST: peer requests %d chunks\n", chunkID_set_size(cset)); printChunkID_set(cset); chunktosend = chunkID_set_get_earliest(cset); rcset = chunkID_set_init("size=1"); fprintf(stdout, "2) Deliver only earliest chunk #%d\n", chunktosend); chunkID_set_add_chunk(rcset, chunktosend); deliverChunks(remote, rcset, trans_id++); break; case sig_send_buffermap: fprintf(stdout, "1) Message SEND_BMAP: I received a buffer of %d chunks\n", chunkID_set_size(cset)); printChunkID_set(cset); rcset = chunkID_set_init("size=15,type=bitmap"); if (!rcset) { fprintf(stderr,"Unable to allocate memory for rcset\n"); return -1; } fillChunkID_set(rcset, random_bmap); fprintf(stdout, "2) Message SEND_BMAP: I send my buffer of %d chunks\n", chunkID_set_size(rcset)); printChunkID_set(rcset); sendBufferMap(remote, my_sock, rcset, 0, trans_id++); break; case sig_request_buffermap: fprintf(stdout, "1) Message REQUEST_BMAP: Someone requeste my buffer map [%d]\n", (cset == NULL)); rcset = chunkID_set_init("size=15,type=bitmap"); if (!rcset) { fprintf(stderr,"Unable to allocate memory for rcset\n"); return -1; } fillChunkID_set(rcset, random_bmap); sendBufferMap(remote, my_sock, rcset, 0, trans_id++); break; } nodeid_free(remote); if (cset) chunkID_set_free(cset); if (rcset) chunkID_set_free(rcset); return 1; }
int client_side(struct nodeID *my_sock) { struct nodeID *dst; struct chunkID_set *cset = NULL; struct chunkID_set *rcset = NULL; int ret; enum signaling_type sig_type; dst = create_node(dst_ip, dst_port); fprintf(stdout,"Sending signal\n"); switch (sig) { case offer: cset = chunkID_set_init("size=10"); if (!cset) { fprintf(stderr,"Unable to allocate memory for cset\n"); nodeid_free(dst); return -1; } fillChunkID_set(cset, random_bmap); fprintf(stdout, "\"OFFER\" "); ret = offerChunks(dst, cset, 8, 22); break; case request: cset = chunkID_set_init("size=10"); if (!cset) { fprintf(stderr,"Unable to allocate memory for cset\n"); nodeid_free(dst); return -1; } fprintf(stdout, "\"REQUEST\" "); fillChunkID_set(cset,random_bmap); printChunkID_set(cset); ret = requestChunks(dst, cset, 10, 44); break; case sendbmap: cset = chunkID_set_init("type=bitmap,size=10"); if (!cset) { fprintf(stderr,"Unable to allocate memory for cset\n"); nodeid_free(dst); return -1; } fillChunkID_set(cset, random_bmap); fprintf(stdout, "\"SEND BMAP\" "); ret = sendBufferMap(dst, dst, cset, 0, 88); break; case reqbmap: fprintf(stdout, "\"REQUEST BMAP\" "); ret = requestBufferMap(dst, NULL, 99); break; default: printf("Please select one operation (O)ffer, (R)equest, send (B)map, request (b)map\n"); nodeid_free(dst); return -1; } fprintf(stdout, "done: %d\n", ret); nodeid_free(dst); sig_type = sig_receive(my_sock, NULL, NULL, &rcset); printChunkID_set(rcset); if (cset) chunkID_set_free(cset); if (rcset) chunkID_set_free(rcset); return 1; }
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++; } } }