Example #1
0
static int	check_options(t_server *server, int ac, char *av[])
{
  if (ac == 1 || ac > optind)
    {
      fprintf(stderr, "usage: %s [[[-p port] -p port] ...]"
	      " [-x world_x] [-y world_y] [-c max_clients]"
	      " [-t speed] [-d] -n team_name_1 team_name_2 ...\n", av[0]);
      return (EXIT_FAILURE);
    }
  if (!server->listener && listen_on_port(server, "4242", SOCK_STREAM))
    return (EXIT_FAILURE);
  if (list_size(server->game.teams) <= 1)
    {
      fprintf(stderr, "%s: you must create at least two teams\n", av[0]);
      return (EXIT_FAILURE);
    }
  return (check_team_names(server->game.teams, av[0], server));
}
Example #2
0
int		parse_command_line(t_server *server, int ac, char *av[])
{
  char		c;
  t_team	*t;

  init_serv(server);
  while ((c = getopt(ac, av, "dp:x:y:c:t:n:")) != -1)
    {
      if (c == '?')
	return (EXIT_FAILURE);
      if (c == 'p' && listen_on_port(server, optarg, SOCK_STREAM))
	return (EXIT_FAILURE);
      if (add_numbers(c, server, optarg))
	return (EXIT_FAILURE);
      if (c == 'd')
	server->debug = 1;
      if (c == 'n' && (t = malloc(sizeof(t_team))))
	add_team_names(t, server, ac, av);
    }
  return (check_options(server, ac, av));
}
Example #3
0
int main( int argc, char** argv )
{
   // parse command-line options
   boost::program_options::options_description option_config("Allowed options");
   option_config.add_options()("data-dir", boost::program_options::value<std::string>(), "configuration data directory")
                              ("help", "display this help message")
                              ("p2p", "enable p2p mode")
                              ("port", boost::program_options::value<uint16_t>(), "set port to listen on")
                              ("connect-to", boost::program_options::value<std::string>(), "set remote host to connect to")
                              ("server", "enable JSON-RPC server")
                              ("rpcuser", boost::program_options::value<std::string>(), "username for JSON-RPC")
                              ("rpcpassword", boost::program_options::value<std::string>(), "password for JSON-RPC")
                              ("rpcport", boost::program_options::value<uint16_t>(), "port to listen for JSON-RPC connections")
                              ("httpport", boost::program_options::value<uint16_t>(), "port to listen for HTTP JSON-RPC connections")
                              ("trustee-private-key", boost::program_options::value<std::string>(), "act as a trustee using the given private key")
                              ("trustee-address", boost::program_options::value<std::string>(), "trust the given BTS address to generate blocks")
                              ("genesis-json", boost::program_options::value<std::string>(), "generate a genesis block with the given json file (only for testing, only accepted when the blockchain is empty)");

   boost::program_options::positional_options_description positional_config;
   positional_config.add("data-dir", 1);

   boost::program_options::variables_map option_variables;
   try
   {
     boost::program_options::store(boost::program_options::command_line_parser(argc, argv).
       options(option_config).positional(positional_config).run(), option_variables);
     boost::program_options::notify(option_variables);
   }
   catch (boost::program_options::error&)
   {
     std::cerr << "Error parsing command-line options\n\n";
     std::cerr << option_config << "\n";
     return 1;
   }

   if (option_variables.count("help"))
   {
     std::cout << option_config << "\n";
     return 0;
   }
   
   bool p2p_mode = option_variables.count("p2p") != 0;

   try {
      print_banner();
      fc::path datadir = get_data_dir(option_variables);
      ::configure_logging(datadir);

      auto cfg   = load_config(datadir);
      auto chain = load_and_configure_chain_database(datadir, option_variables);
      auto wall  = std::make_shared<bts::wallet::wallet>();
      wall->set_data_directory( datadir );

      auto c = std::make_shared<bts::client::client>(p2p_mode);
      c->set_chain( chain );
      c->set_wallet( wall );

      if (option_variables.count("trustee-private-key"))
      {
         auto key = fc::variant(option_variables["trustee-private-key"].as<std::string>()).as<fc::ecc::private_key>();
         c->run_trustee(key);
      }
      else if( fc::exists( "trustee.key" ) )
      {
         auto key = fc::json::from_file( "trustee.key" ).as<fc::ecc::private_key>();
         c->run_trustee(key);
      }

      bts::rpc::rpc_server_ptr rpc_server = std::make_shared<bts::rpc::rpc_server>();
      rpc_server->set_client(c);


      if( option_variables.count("server") )
      {
        // the user wants us to launch the RPC server.
        // First, override any config parameters they 
        bts::rpc::rpc_server::config rpc_config(cfg.rpc);
        if (option_variables.count("rpcuser"))
          rpc_config.rpc_user = option_variables["rpcuser"].as<std::string>();
        if (option_variables.count("rpcpassword"))
          rpc_config.rpc_password = option_variables["rpcpassword"].as<std::string>();
        // for now, force binding to localhost only
        if (option_variables.count("rpcport"))
          rpc_config.rpc_endpoint = fc::ip::endpoint(fc::ip::address("127.0.0.1"), option_variables["rpcport"].as<uint16_t>());
        if (option_variables.count("httpport"))
          rpc_config.httpd_endpoint = fc::ip::endpoint(fc::ip::address("127.0.0.1"), option_variables["httpport"].as<uint16_t>());
        std::cerr<<"starting json rpc server on "<< std::string( rpc_config.rpc_endpoint ) <<"\n";
        std::cerr<<"starting http json rpc server on "<< std::string( rpc_config.httpd_endpoint ) <<"\n";
        rpc_server->configure(rpc_config);
      }
      
      if (p2p_mode)
      {
        c->configure( datadir );
        if (option_variables.count("port"))
          c->listen_on_port(option_variables["port"].as<uint16_t>());
        c->connect_to_p2p_network();
        if (option_variables.count("connect-to"))
          c->connect_to_peer(option_variables["connect-to"].as<std::string>());
      }
      else
      {
        if (option_variables.count("connect-to"))
          c->add_node(option_variables["connect-to"].as<std::string>());
        else
           c->add_node( "127.0.0.1:4569" );
      }

      auto cli = std::make_shared<bts::cli::cli>( c, rpc_server );
      cli->wait();

   } 
   catch ( const fc::exception& e ) 
   {
      wlog( "${e}", ("e", e.to_detail_string() ) );
   }
   return 0;
}
Example #4
0
/***************************************************************
 * How this program works:
 *  - Allocate and initialize system variables (as DB tables)
 *  - Open socket to listen for DB config commands
 *  - select() loop
 **************************************************************/
int
main()
{
  fd_set   rfds;       /* read bit masks for select statement */
  fd_set   wfds;       /* write bit masks for select statement */
  int      mxfd;       /* Maximum FD for the select statement */
  int      newui_fd = -1; /* FD to TCP socket accept UI conns */
  int      i;          /* generic loop counter */
  UI      *pui;        /* pointer to a UI struct */
  UI      *nextpui;    /* points to next UI in list */



  /* Init */
  ConnHead = (UI *) NULL;
  DemoHead =  (DEMOLIST *) NULL;

  for (i = 0; i < nuitables; i++)
  {
    rta_add_table(&UITables[i]);
  }

  while (1)
  {
    /* Build the fd_set for the select call.  This includes the listen
       port for new UI connections and any existing UI connections.  We 
       also look for the ability to write to the clients if data is
       queued.  */
    FD_ZERO(&rfds);
    FD_ZERO(&wfds);
    mxfd = 0;

    /* open UI/DB/manager listener if needed */
    if (newui_fd < 0)
    {
      newui_fd = listen_on_port(DB_PORT);
    }
    FD_SET(newui_fd, &rfds);
    mxfd = (newui_fd > mxfd) ? newui_fd : mxfd;

    /* for each UI conn .... */
    pui = ConnHead;
    while (pui)
    {
      if (pui->rspfree < MXRSP) /* Data to send? */
      {
        FD_SET(pui->fd, &wfds);
        mxfd = (pui->fd > mxfd) ? pui->fd : mxfd;
      }
      else
      {
        FD_SET(pui->fd, &rfds);
        mxfd = (pui->fd > mxfd) ? pui->fd : mxfd;
      }
      pui = pui->nextconn;
    }

    /* Wait for some something to do */
    (void) select(mxfd + 1, &rfds, &wfds,
      (fd_set *) 0, (struct timeval *) 0);

    /* ....after the select call.  We have activity. Search through
       the open fd's to find what to do. */

    /* Handle new UI/DB/manager connection requests */
    if ((newui_fd >= 0) && (FD_ISSET(newui_fd, &rfds)))
    {
      accept_ui_session(newui_fd);
    }

    /* process request from or data to one of the UI programs */
    pui = ConnHead;
    while (pui)
    {
      /* Get next UI now since pui struct may be freed in handle_ui.. */
      nextpui = pui->nextconn;
      if (FD_ISSET(pui->fd, &rfds))
      {
        handle_ui_request(pui);
      }
      else if (FD_ISSET(pui->fd, &wfds))
      {
        handle_ui_output(pui);
      }
      pui = nextpui;
    }
  }
}