Beispiel #1
0
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);
    }
  }
}
Beispiel #2
0
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;
}
Beispiel #3
0
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;
}
Beispiel #4
0
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;
}
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++;
    }
  }
}