示例#1
0
int main(int argc, char** argv) {
  int err;
  std::string usage("Usage: " + std::string(argv[0]) +
                    " [options] <hostport>\n");
  usage += "  Runs a master node with launcher that is running on host:port.";
  google::SetUsageMessage(usage);
  google::InitGoogleLogging(argv[0]);
  google::InstallFailureSignalHandler();

  google::ParseCommandLineFlags(&argc, &argv, true);
  if (argc != 2) {
    fprintf(stderr, "Invalid number of aruments provided\n%s\n",
            google::ProgramUsage());
    exit(EXIT_FAILURE);
  }

  accept_fd = listen_to(FLAGS_address.c_str());
  CHECK_GE(accept_fd, 0) << "Could not listen on " << FLAGS_address;
  DLOG_IF(INFO, FLAGS_log_network) << "Listening on " << FLAGS_address;

  launcher_fd = connect_to(argv[1]);
  CHECK_GE(launcher_fd, 0) << "Could not connect to launcher " << argv[1];
  DLOG_IF(INFO, FLAGS_log_network) << "Connected to launcher at " << argv[1];

  // Tell the launcher what address we are listening on.
  err = send_string(launcher_fd, FLAGS_address);
  CHECK_GE(err, 0) << "Error sending master info";

  // student code
  int tick_seconds;
  master_node_init(FLAGS_max_workers, tick_seconds);

  struct timeval tick_period;
  tick_period.tv_sec = tick_seconds;
  tick_period.tv_usec = 0;

  harness_begin_main_loop(&tick_period);

  return 0;
}
示例#2
0
int main(int argc, char *argv[]) {
  const char *host = SELENE_SERVER_DEFAULT_HOST;
  int port = SELENE_SERVER_DEFAULT_PORT;
  const char *cert_path = SELENE_SERVER_DEFAULT_CERT_PATH;
  const char *key_path = NULL;
  selene_conf_t *conf = NULL;
  const char *cert = NULL;
  const char *pkey = NULL;
  int rv = 0;
  int i;

  for (i = 1; i < argc; i++) {
    /* TODO: s_server compat */
    if (!strcmp("-host", argv[i]) && argc > i + 1) {
      host = argv[i + 1];
      i++;
    } else if (!strcmp("-port", argv[i]) && argc > i + 1) {
      port = atoi(argv[i + 1]);
      i++;
    } else if (!strcmp("-listen", argv[i]) && argc > i + 1) {
      char *p;
      host = argv[i + 1];
      if ((p = strstr(host, ":")) == NULL) {
        fprintf(stderr, "no port found\n");
        exit(EXIT_FAILURE);
      }
      *(p++) = '\0';
      port = atoi(p);
      i++;
    } else {
      fprintf(stderr, "Invalid args\n");
      usage();
      exit(EXIT_FAILURE);
    }
  }

  if (host == NULL) {
    fprintf(stderr, "-host must be set\n");
    exit(EXIT_FAILURE);
  }

  if (port <= 0) {
    fprintf(stderr, "-port must be set\n");
    exit(EXIT_FAILURE);
  }

  if (key_path == NULL) {
    /* assume its a pem encoded cert + key in one */
    key_path = cert_path;
  }

  SERR(selene_conf_create(&conf));

  SERR(selene_conf_use_reasonable_defaults(conf));

  cert = load_cert(cert_path);
  pkey = load_cert(key_path);
  SERR(selene_conf_cert_chain_add(conf, cert, pkey));

  rv = listen_to(conf, host, port, stdin);

  selene_conf_destroy(conf);

  if (cert) {
    free((void *)cert);
  }

  if (pkey) {
    free((void *)pkey);
  }

  return rv;
}
示例#3
0
int main()
{
	int server = listen_to(NULL, "4242");
	printf("Server: %i\n", server);

	// The structure for two events
	struct pollfd fds[1024];

	size_t n_fds = 0;

	fds[n_fds].fd = server;
	fds[n_fds].events = POLLIN;
	n_fds++;

	while (1)
	{
		int ret = poll(fds, n_fds, 10);
		if (ret < 0)
			break;
		if (ret == 0)
			continue;

		for (size_t i = 0; i < n_fds; i++)
		{
			if (!fds[i].revents & POLLIN)
				continue;
			fds[i].revents = 0;

			if (i == 0) // server
			{
				// new client
				int client = accept(server, NULL, NULL);
				if (n_fds >= 1024)
				{
					close(client);
				}
				else
				{
					printf("+ %i\n", client);

					// append client to poll list
					fds[n_fds].fd = client;
					fds[n_fds].events = POLLIN;
					n_fds++;
				}
			}
			else // client
			{
				char buf;
				int ret = recv(fds[i].fd, &buf, 1, MSG_PEEK);
				if (ret == 0) // client disconnected
				{
					printf("- %i\n", fds[i].fd);

					// remove client from poll list (swap and pop)
					if (i != n_fds-1)
						memcpy(&fds[i], &fds[n_fds-1], sizeof(struct pollfd));
					n_fds--;
				}
				else
				{
					// receive data
					while (recv(fds[i].fd, &buf, 1, MSG_DONTWAIT) > 0);
				}
			}
		}
	}
}