Exemplo n.º 1
0
int main(int argc, char **argv)
{
  conn_rec *conns;
  int conns_count;
  host_map *hosts_roles;

  char **hosts;
  int hosts_count;
  char **roles;
  int roles_count;

  if (argc < 4) {
    fprintf(stderr, "Not enough arguments\n");
    fprintf(stderr, "Usage: %s hostfile scribblefile outputfile\n", argv[0]);
    fprintf(stderr, "       use `-' for stdout\n");
    return EXIT_FAILURE;
  }

  printf("Host file: %s\nScribble file: %s\nOutput connection configuration: %s\n", argv[1], argv[2], argv[3]);

  hosts_count = connmgr_load_hosts(argv[1], &hosts);
  roles_count = connmgr_load_roles(argv[2], &roles);

  conns_count = connmgr_init(&conns, &hosts_roles, roles, roles_count, hosts, hosts_count, 6666);
  connmgr_write(argv[3], conns, conns_count, hosts_roles, roles_count);
  return EXIT_SUCCESS;
}
Exemplo n.º 2
0
static int upnp_renderer_init(void)
{
	static struct service *upnp_services[4];
	upnp_services[0] = upnp_transport_get_service();
	upnp_services[1] = upnp_connmgr_get_service();
	upnp_services[2] = upnp_control_get_service();
	upnp_services[3] = NULL;
	render_device.services = upnp_services;
        return connmgr_init();
}
Exemplo n.º 3
0
static int upnp_renderer_init(void)
{
    int err_no = 0;
	
	err_no = control_init();
    if (err_no != 0)
    {
        return err_no;
    }
	
	err_no = transport_init();
	if (err_no != 0)
    {
        return err_no;
    }
	
    return connmgr_init();
}
Exemplo n.º 4
0
int main(int argc, char *argv[])
{
    srand(time(0));
    log_init("httpsrv.log", 1);
    log_set_level(LOG_LEVEL_DEBUG);
    DEBUG("httpsrv has started.");
    signal(SIGINT, sig_handler);
    alarm_init();
    if(net_create(&net, "8080"))
        return -1;
    connmgr_init(net);
    while(1)
        net_process(net, -1);
    connmgr_close();
    net_free(&net);
    alarm_close();
    log_close();
    return 0;
}
Exemplo n.º 5
0
static int upnp_renderer_init(void)
{
        return connmgr_init();
}
Exemplo n.º 6
0
void session_init(int *argc, char ***argv, session **s, const char *scribble)
{
  unsigned int role_idx;
#ifdef __DEBUG__
  sc_print_version();
  DEBUG_prog_start_time = sc_time();
#endif

  st_tree *tree = st_tree_init((st_tree *)malloc(sizeof(st_tree)));

  // Get meta information from Scribble protocol.
  if ((yyin = fopen(scribble, "r")) == NULL) {
    fprintf(stderr, "Warning: Cannot open %s, reading from stdin\n", scribble);
  }
  yyparse(tree);

#ifdef __DEBUG__
  printf("The local protocol:");
  st_tree_print(tree);
#endif


  // Sanity check.
  if (tree->info->global) {
    fprintf(stderr, "Error: %s is a Global protocol\n", scribble);
    return;
  }

  // Parse arguments.
  int option;
  char *config_file = NULL;
  char *hosts_file = NULL;
  char *protocol_file = NULL;

  // Invoke getopt to extract arguments we need
  while (1) {
    static struct option long_options[] = {
      {"conf",     required_argument, 0, 'c'},
      {"hosts",    required_argument, 0, 's'},
      {"protocol", required_argument, 0, 'p'},
      {0, 0, 0, 0}
    };

    int option_idx = 0;
    option = getopt_long(*argc, *argv, "c:s:p:", long_options, &option_idx);

    if (option == -1) break;

    switch (option) {
      case 'c':
        config_file = (char *)calloc(sizeof(char), strlen(optarg)+1);
        strcpy(config_file, optarg);
        fprintf(stderr, "Using configuration file %s\n", config_file);
        break;
      case 's':
        hosts_file = (char *)calloc(sizeof(char), strlen(optarg)+1);
        strcpy(hosts_file, optarg);
        fprintf(stderr, "Using hosts file %s\n", hosts_file);
        break;
      case 'p':
        protocol_file = (char *)calloc(sizeof(char), strlen(optarg)+1);
        strcpy(protocol_file, optarg);
        fprintf(stderr, "Using protocol file %s\n", protocol_file);
        break;
    }
  }

  *argc -= optind-1;
  (*argv)[optind-1] = (*argv)[0];
  *argv += optind-1;

  conn_rec *conns;
  int nconns;
  char **hosts;
  int nhosts;
  char **roles;
  int nroles;
  host_map *hosts_roles;
  int conn_idx;

  if (config_file == NULL) { // Generate dynamic connection parameters (config file absent).

    if (hosts_file == NULL) {
      hosts_file = "hosts";
      fprintf(stderr, "Warning: host file not specified (-s), reading from `%s'\n", hosts_file);
    }
    nhosts = connmgr_load_hosts(hosts_file, &hosts);
    if (protocol_file == NULL) {
      protocol_file = "Protocol.spr";
      fprintf(stderr, "Warning: protocol file not specified (-p), reading from `%s'\n", protocol_file);
    }

    nroles = connmgr_load_roles(protocol_file, &roles);
    nconns = connmgr_init(&conns, &hosts_roles, roles, nroles, hosts, nhosts, 7777);

  } else { // Use config file.

    nconns = connmgr_read(config_file, &conns, &hosts_roles, &nroles);

  }

#ifdef __DEBUG__
  printf("\n------Conn Mgr------\n");
  connmgr_write("-", conns, nconns, hosts_roles, nroles);
  printf("--------------------\n");
#endif

  *s = (session *)malloc(sizeof(session));
  session *sess = *s;

  sess->name = (char *)calloc(sizeof(char), strlen(tree->info->myrole)+1);
  strcpy(sess->name, tree->info->myrole);

  // Direct connections (p2p).
  sess->nrole = tree->info->nrole;
  sess->roles = (role **)malloc(sizeof(role *) * sess->nrole);
  sess->ctx = zmq_init(1);

  for (role_idx=0; role_idx<sess->nrole; role_idx++) {
    sess->roles[role_idx] = (role *)malloc(sizeof(role));
    sess->roles[role_idx]->type = SESSION_ROLE_P2P;
    sess->roles[role_idx]->s = sess;
    sess->roles[role_idx]->p2p = (struct role_endpoint *)malloc(sizeof(struct role_endpoint));

    sess->roles[role_idx]->p2p->name = (char *)calloc(sizeof(char), strlen(tree->info->roles[role_idx])+1);
    strcpy(sess->roles[role_idx]->p2p->name, tree->info->roles[role_idx]);

    for (conn_idx=0; conn_idx<nconns; conn_idx++) { // Look for matching connection parameter

      if ((CONNMGR_TYPE_P2P == conns[conn_idx].type) && strcmp(conns[conn_idx].to, sess->roles[role_idx]->p2p->name) == 0 && strcmp(conns[conn_idx].from, sess->name) == 0) { // As a client.
        assert(strlen(conns[conn_idx].host) < 255 && conns[conn_idx].port < 65536);
        if (strstr(conns[conn_idx].host, "ipc:") != NULL) {
          sprintf(sess->roles[role_idx]->p2p->uri, "ipc:///tmp/sessionc-%u", conns[conn_idx].port);
        } else {
          sprintf(sess->roles[role_idx]->p2p->uri, "tcp://%s:%u", conns[conn_idx].host, conns[conn_idx].port);
        }
#ifdef __DEBUG__
        fprintf(stderr, "Connection (as client) %s -> %s is %s\n",
            conns[conn_idx].from,
            conns[conn_idx].to,
            sess->roles[role_idx]->p2p->uri);
#endif
        if ((sess->roles[role_idx]->p2p->ptr = zmq_socket(sess->ctx, ZMQ_PAIR)) == NULL) perror("zmq_socket");
        if (zmq_connect(sess->roles[role_idx]->p2p->ptr, sess->roles[role_idx]->p2p->uri) != 0) perror("zmq_connect");

        break;
      }

      if ((CONNMGR_TYPE_P2P == conns[conn_idx].type) && strcmp(conns[conn_idx].from, sess->roles[role_idx]->p2p->name) == 0 && strcmp(conns[conn_idx].to, sess->name) == 0) { // As a server.
        assert(conns[conn_idx].port < 65536);
        if (strstr(conns[conn_idx].host, "ipc:") != NULL) {
          sprintf(sess->roles[role_idx]->p2p->uri, "ipc:///tmp/sessionc-%u", conns[conn_idx].port);
        } else {
          sprintf(sess->roles[role_idx]->p2p->uri, "tcp://*:%u",conns[conn_idx].port);
        }
  #ifdef __DEBUG__
        fprintf(stderr, "Connection (as server) %s -> %s is %s\n",
            conns[conn_idx].from,
            conns[conn_idx].to,
            sess->roles[role_idx]->p2p->uri);
  #endif
        if ((sess->roles[role_idx]->p2p->ptr = zmq_socket(sess->ctx, ZMQ_PAIR)) == NULL) perror("zmq_socket");
        if (zmq_bind(sess->roles[role_idx]->p2p->ptr, sess->roles[role_idx]->p2p->uri) != 0) perror("zmq_bind");

        break;
      }

    }

  }

  // Add a _Others group role.
  sess->nrole++;
  sess->roles = (role **)realloc(sess->roles, sizeof(role *) * sess->nrole);
  sess->roles[sess->nrole-1] = (role *)malloc(sizeof(role)); // A group role at roles[last_index]
  sess->roles[sess->nrole-1]->type = SESSION_ROLE_GRP;
  sess->roles[sess->nrole-1]->s = sess;
  sess->roles[sess->nrole-1]->grp = (struct role_group *)malloc(sizeof(struct role_group));

  sess->roles[sess->nrole-1]->grp->name = "_Others";
  sess->roles[sess->nrole-1]->grp->nendpoint = tree->info->nrole;
  sess->roles[sess->nrole-1]->grp->endpoints
      = (struct role_endpoint **)malloc(sizeof(struct role_endpoint *) * sess->roles[sess->nrole-1]->grp->nendpoint);
  // Copy endpoints to our global group role.
  unsigned int endpoint_idx;
  for (endpoint_idx=0; endpoint_idx<sess->roles[sess->nrole-1]->grp->nendpoint; endpoint_idx++) {
    if (sess->roles[endpoint_idx]->type == SESSION_ROLE_P2P) {
      sess->roles[sess->nrole-1]->grp->endpoints[endpoint_idx] = sess->roles[endpoint_idx]->p2p;
    }
  }

  sess->roles[sess->nrole-1]->grp->in  = (struct role_endpoint *)malloc(sizeof(struct role_endpoint));
  sess->roles[sess->nrole-1]->grp->out = (struct role_endpoint *)malloc(sizeof(struct role_endpoint));

  // Setup a SUB (broadcast-in) socket
  if ((sess->roles[sess->nrole-1]->grp->in->ptr = zmq_socket(sess->ctx, ZMQ_SUB)) == NULL) perror("zmq_socket");
  // Setup a series of PUB (broadcast-out) socket
  if ((sess->roles[sess->nrole-1]->grp->out->ptr = zmq_socket(sess->ctx, ZMQ_PUB)) == NULL) perror("zmq_socket");

  for (conn_idx=0; conn_idx<nconns; conn_idx++) { // Look for the broadcast socket
    if ((CONNMGR_TYPE_GRP == conns[conn_idx].type) && (strcmp(conns[conn_idx].to, sess->name) == 0)) {
      sprintf(sess->roles[sess->nrole-1]->grp->in->uri, "tcp://*:%u", conns[conn_idx].port);
#ifdef __DEBUG__
      fprintf(stderr, "Broadcast in-socket: %s\n",
        sess->roles[sess->nrole-1]->grp->in->uri);
#endif
      if (zmq_bind(sess->roles[sess->nrole-1]->grp->in->ptr, sess->roles[sess->nrole-1]->grp->in->uri) != 0) perror("zmq_bind");
      break;
    }
  }

  for (conn_idx=0; conn_idx<nconns; conn_idx++) { // Look for the broadcast socket
    if ((CONNMGR_TYPE_GRP == conns[conn_idx].type) && (strcmp(conns[conn_idx].to, sess->name) != 0)) {
      sprintf(sess->roles[sess->nrole-1]->grp->out->uri, "tcp://%s:%u", conns[conn_idx].host, conns[conn_idx].port);
#ifdef __DEBUG__
      fprintf(stderr, "Broadcast out-socket: %s\n",
        sess->roles[sess->nrole-1]->grp->out->uri);
#endif
      if (zmq_connect(sess->roles[sess->nrole-1]->grp->out->ptr, sess->roles[sess->nrole-1]->grp->out->uri) != 0) perror("zmq_connect");
    }
  }
  zmq_setsockopt(sess->roles[sess->nrole-1]->grp->in->ptr, ZMQ_SUBSCRIBE, "", 0);
#ifdef ZMQ_LINGER
  zmq_setsockopt(sess->roles[sess->nrole-1]->grp->out->ptr, ZMQ_LINGER, 0, 0);
#endif

  sess->r = &find_role_in_session;

  free(tree);
#ifdef __DEBUG__
  DEBUG_sess_start_time = sc_time();
#endif
}