コード例 #1
0
ファイル: grapes-example.c プロジェクト: nivox/master-thesis
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);
    }
  }
}
コード例 #2
0
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++;
    }
  }
}