int main(int argc, char* argv[]) { if (argc != 3) { printf("Usage: vpxcams <cam_id> <port>\n"); return -1; } int cam_id = atoi(argv[1]); int port = atoi(argv[2]); int width = CAM_WIDTH; int height = CAM_HEIGHT; if (!cam_open(cam_id, width, height, true)) { printf("Could not open camera: %d\n", cam_id); return -1; } vpx_init(width, height); server* s = new server(width, height, port); boost::thread server_thread(&server::run, s); cam_loop(width, height, s); vpx_cleanup(); server_thread.join(); return 0; }
int main() { const static int PORT=8070; // renderer RenderLoop app; // server boost::asio::io_service server_io; msgpack::rpc::asio::server server(server_io, [&app]( const msgpack::object &msg, std::shared_ptr<msgpack::rpc::asio::session> session) { app.enqueue(msg, session); }); server.listen(boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), PORT)); boost::thread server_thread([&server_io](){ server_io.run(); }); // blocking app.run(); server_io.stop(); server_thread.join(); return 0; }
void test_unbuffered() { unlink("/tmp/.socket_stream_test"); auto server = fd::easy::unix_socket::server("/tmp/.socket_stream_test"); auto client = fd::easy::unix_socket::client("/tmp/.socket_stream_test"); auto conn = server.accept(); auto start = std::chrono::high_resolution_clock::now(); std::thread server_thread(unbuffered_server, std::ref(conn)); std::thread client_thread(unbuffered_client, std::ref(client)); server_thread.join(); client_thread.join(); auto end = std::chrono::high_resolution_clock::now(); std::chrono::duration<double> diff = end - start; double throughput = loop_count * sizeof(message) / diff.count(); std::cout << "---- Unbuffered ----\n" << "Elapsed: " << diff.count() << " s\n" << "Throughput: " << throughput / 1e6 << " mb/s\n"; }
int wmain(int argc, wchar_t* argv[]) { SetPriorityClass(GetCurrentProcess(), IDLE_PRIORITY_CLASS); wchar_t *pipename = L"\\\\.\\pipe\\DWindowThumbnailPipe"; SetUnhandledExceptionFilter(my_handler); server_thread(argc>1?argv[1]:pipename); return 0; }
void Process::Run(Game& game) { // ZeroMQ context zmq::context_t zcontext(1); // Components Api api(zcontext, 5555); Notifier notifier(zcontext, 5556); Server server(zcontext, 5557); // TODO: Read workers from file // TODO: Initialize workers properly and distribute players // TODO: Use a worker vector Worker worker; // Game log Log log("match/log"); World* world; if(utils::FileExists("match/world.txt")) { std::ifstream stream("match/world.txt"); // Read existing world world = game.ReadWorld(stream); } else { // TODO: This should be API-driven // Generate a new world world = game.GenerateWorld(); log.Clear(); } // Print current world std::ostringstream stream; world->PrintStructure(stream); debug::Println("MASTER", "Current world:"); debug::Print(stream.str()); // Start servers std::thread api_thread(run_api, std::ref(api), std::ref(worker), std::ref(*world), std::ref(log)); std::thread server_thread(run_server, std::ref(server), std::ref(worker), std::ref(game), std::ref(*world), std::ref(notifier), std::ref(log)); // Run players worker.Run(); // Run game loop run_game_loop(*world, notifier, log); }
int main (int argc, char ** argv) { int ch, ret, seed = 0, d = 0; /* set default value */ memset (&tcpgen, 0, sizeof (tcpgen)); tcpgen.flow_dist = FLOWDIST_SAME; tcpgen.flow_num = 1; tcpgen.data_len = 984; /* 1024 byte packet excluding ether header */ while ((ch = getopt (argc, argv, "d:B:scn:t:x:i:l:rm:pDv")) != -1) { switch (ch) { case 'd' : ret = inet_pton (AF_INET, optarg, &tcpgen.dst); if (ret < 0) { D ("invalid dst address %s", optarg); perror ("inet_pton"); return -1; } break; case 'B' : ret = inet_pton (AF_INET, optarg, &tcpgen.src); if (ret < 0) { D ("invalid src address %s", optarg); perror ("inet_pton"); return -1; } break; case 's' : tcpgen.server_mode = 1; break; case 'c' : tcpgen.client_mode = 1; break; case 'n' : tcpgen.flow_num = atoi (optarg); if (tcpgen.flow_num > MAX_FLOWNUM) { D ("max number of flows is %d", MAX_FLOWNUM); return -1; } break; case 't' : if (strncmp (optarg, "same", 4) == 0) tcpgen.flow_dist = FLOWDIST_SAME; else if (strncmp (optarg, "random", 6) == 0) tcpgen.flow_dist = FLOWDIST_RANDOM; else if (strncmp (optarg, "power", 5) == 0) tcpgen.flow_dist = FLOWDIST_POWER; else { D ("invalid distribution patter %s", optarg); return -1; } break; case 'x' : tcpgen.count = atoi (optarg); break; case 'i' : tcpgen.interval = atoi (optarg); break; case 'l' : tcpgen.data_len = atoi (optarg); break; case 'r' : tcpgen.randomized = 1; break; case 'm' : seed = atoi (optarg); break; case 'p' : tcpgen.thread_mode = 1; break; case 'D' : d = 1; break; case 'v' : tcpgen.verbose = 1; break; default : usage (); return -1; } } if (seed) srand (seed); else srand (time (NULL)); if (d) daemon (1, 0); if (tcpgen.server_mode) server_thread (NULL); else if (tcpgen.client_mode) client_thread (NULL); D ("tcpgen finished"); return 0; }
int main (int argc, char * const argv[]) { char* config_file = NULL; // bool debug_mode = false; // bool daemon_mode = false; // bool check_mode = false; Config conf_params; Router router(&conf_params); /* * Initialisation * * Read the config file (-f option): * - bind_address * - bind_port (default: 555) * - data_path (default: /var/lib/ows) * - etc_path (default: /etc/ows) */ if ( argc < 3 ) { usage(); return EXIT_FAILURE; } for ( int i = 1 ; i < argc ; i++ ) { if ( strcmp(argv[i], "-f" ) == 0 && i < argc ) { config_file = argv[i+1]; continue; } /* if ( strcmp(argv[i], "-v" ) == 0 ) { debug_mode = true; continue; } if ( strcmp(argv[i], "-d" ) == 0 ) { daemon_mode = true; continue; } */ } if ( config_file == NULL ) { std::cerr << "No config file given" << std::endl; usage(); return EXIT_FAILURE; } if ( conf_params.parse_file(config_file) == false ) { std::cout << "Cannot parse " << config_file << std::endl; return EXIT_FAILURE; } // if (daemon_mode == true) // daemonize(); /* * Peers Discovery * * - Get the (host, public key) list * - Call Router.reach_master() : if true -> continue, else waiting loop (30 seconds and retry) * */ router.update_peers_list(); while ( router.get_reachable_peers_number() < 1 ) { std::cout << "Cannot reach any peer" << std::endl; router.update_peers_list(); sleep(30); } while ( router.reach_master() == false ) { std::cout << "Cannot reach the master" << std::endl; sleep(30); } /* * Planning loading * * - Call Rpc_Client.get_planning() until it succeeds * - Create the domain * - Call Domain.load_planning(); */ rpc::t_node node; Domain domain(&conf_params); switch (conf_params.get_running_mode()) { case P2P: { /* * Check if the domain contains data * Or ask the direct peers for data */ if ( domain.contains_data(conf_params.get_param("node_name")->c_str()) == false ) { p_m_host_keys_iter iters = router.get_direct_peers(); rpc::t_node node; node.name = conf_params.get_param("node_name")->c_str(); for ( auto iter = iters.first ; iter != iters.second ; iter++ ) { if ( router.get_node(*conf_params.get_param("domain_name"), node, iter->second.c_str()) == true ) break; } if ( domain.add_node(conf_params.get_param("domain_name")->c_str(), node) == false ) { std::cerr << "Cannot load the planning" << std::endl; return EXIT_FAILURE; } } break; } case ACTIVE: { while ( router.get_node(conf_params.get_param("domain_name")->c_str(), node, router.get_master_node()->c_str()) == false ) { std::cout << "Cannot get the planning" << std::endl; sleep(30); } if ( domain.add_node(conf_params.get_param("domain_name")->c_str(), node) == false ) { std::cerr << "Cannot load the planning" << std::endl; return EXIT_FAILURE; } break; } case PASSIVE: { /* * Nothing is done. The master gives orders */ break; } } /* * Ports listening * * - create a Rpc_Server object * - create a dedicated thread */ Rpc_Server server(&domain, &conf_params, &router); boost::thread server_thread(boost::bind(&Rpc_Server::run, &server)); /* * Domain preparation * * - Try to reach the node needed by the planning : build the routing table */ /* * Domain routine * * - Check for ready jobs every minute * - Or wait master's orders */ v_jobs jobs; boost::thread_group running_jobs; switch (conf_params.get_running_mode()) { case (P2P): {break;}; case (ACTIVE): { while (1) { domain.get_ready_jobs(jobs, conf_params.get_param("node_name")->c_str()); std::cout << "jobs size: " << jobs.size() << std::endl; // TODO: remove it BOOST_FOREACH(Job j, jobs) { running_jobs.create_thread(boost::bind(&Job::run, &j)); } sleep(60); } break; } case (PASSIVE): { break; } }
static void server_thread_wrap(void *arg) { thread_args *args = arg; server_thread(args); }
int main(int argc, const char *argv[]) { try { LogPolicy::GetInstance().Unmute(); bool use_shared_memory = false, trial_run = false; std::string ip_address; int ip_port, requested_thread_num; ServerPaths server_paths; const unsigned init_result = GenerateServerProgramOptions(argc, argv, server_paths, ip_address, ip_port, requested_thread_num, use_shared_memory, trial_run); if (init_result == INIT_OK_DO_NOT_START_ENGINE) { return 0; } if (init_result == INIT_FAILED) { return 1; } #ifdef __linux__ const int lock_flags = MCL_CURRENT | MCL_FUTURE; if (-1 == mlockall(lock_flags)) { SimpleLogger().Write(logWARNING) << argv[0] << " could not be locked to RAM"; } #endif SimpleLogger().Write() << "starting up engines, " << g_GIT_DESCRIPTION << ", " << "compiled at " << __DATE__ << ", " __TIME__; if (use_shared_memory) { SimpleLogger().Write(logDEBUG) << "Loading from shared memory"; } else { SimpleLogger().Write() << "HSGR file:\t" << server_paths["hsgrdata"]; SimpleLogger().Write(logDEBUG) << "Nodes file:\t" << server_paths["nodesdata"]; SimpleLogger().Write(logDEBUG) << "Edges file:\t" << server_paths["edgesdata"]; SimpleLogger().Write(logDEBUG) << "Geometry file:\t" << server_paths["geometries"]; SimpleLogger().Write(logDEBUG) << "RAM file:\t" << server_paths["ramindex"]; SimpleLogger().Write(logDEBUG) << "Index file:\t" << server_paths["fileindex"]; SimpleLogger().Write(logDEBUG) << "Names file:\t" << server_paths["namesdata"]; SimpleLogger().Write(logDEBUG) << "Timestamp file:\t" << server_paths["timestamp"]; SimpleLogger().Write(logDEBUG) << "Threads:\t" << requested_thread_num; SimpleLogger().Write(logDEBUG) << "IP address:\t" << ip_address; SimpleLogger().Write(logDEBUG) << "IP port:\t" << ip_port; } #ifndef _WIN32 int sig = 0; sigset_t new_mask; sigset_t old_mask; sigfillset(&new_mask); pthread_sigmask(SIG_BLOCK, &new_mask, &old_mask); #endif OSRM osrm_lib(server_paths, use_shared_memory); Server *routing_server = ServerFactory::CreateServer(ip_address, ip_port, requested_thread_num); routing_server->GetRequestHandlerPtr().RegisterRoutingMachine(&osrm_lib); if (trial_run) { SimpleLogger().Write() << "trial run, quitting after successful initialization"; } else { boost::thread server_thread(std::bind(&Server::Run, routing_server)); #ifndef _WIN32 sigset_t wait_mask; pthread_sigmask(SIG_SETMASK, &old_mask, 0); sigemptyset(&wait_mask); sigaddset(&wait_mask, SIGINT); sigaddset(&wait_mask, SIGQUIT); sigaddset(&wait_mask, SIGTERM); pthread_sigmask(SIG_BLOCK, &wait_mask, 0); SimpleLogger().Write() << "running and waiting for requests"; sigwait(&wait_mask, &sig); #else // Set console control handler to allow server to be stopped. console_ctrl_function = std::bind(&Server::Stop, routing_server); SetConsoleCtrlHandler(console_ctrl_handler, TRUE); SimpleLogger().Write() << "running and waiting for requests"; routing_server->Run(); #endif SimpleLogger().Write() << "initiating shutdown"; routing_server->Stop(); SimpleLogger().Write() << "stopping threads"; if (!server_thread.timed_join(boost::posix_time::seconds(2))) { SimpleLogger().Write(logWARNING) << "Didn't exit within 2 seconds. Hard abort!"; } } SimpleLogger().Write() << "freeing objects"; delete routing_server; SimpleLogger().Write() << "shutdown completed"; } catch (const std::exception &e) { SimpleLogger().Write(logWARNING) << "exception: " << e.what(); return 1; } #ifdef __linux__ munlockall(); #endif return 0; }
// NOTE: input_args is free'd with `free` so must be alloc'd with malloc. // This call loops forever. DLL_EXPORT int main_server(struct adb_main_input * input_args) { server_thread((void *)input_args); return 0; }
int main(int argc, const char *argv[]) { try { LogPolicy::GetInstance().Unmute(); bool trial_run = false; std::string ip_address; int ip_port, requested_thread_num; libosrm_config lib_config; const unsigned init_result = GenerateServerProgramOptions( argc, argv, lib_config.server_paths, ip_address, ip_port, requested_thread_num, lib_config.use_shared_memory, trial_run, lib_config.max_locations_distance_table, lib_config.max_locations_map_matching); if (init_result == INIT_OK_DO_NOT_START_ENGINE) { return 0; } if (init_result == INIT_FAILED) { return 1; } #ifdef __linux__ const int lock_flags = MCL_CURRENT | MCL_FUTURE; if (-1 == mlockall(lock_flags)) { SimpleLogger().Write(logWARNING) << argv[0] << " could not be locked to RAM"; } #endif SimpleLogger().Write() << "starting up engines, " << g_GIT_DESCRIPTION; if (lib_config.use_shared_memory) { SimpleLogger().Write(logDEBUG) << "Loading from shared memory"; } SimpleLogger().Write(logDEBUG) << "Threads:\t" << requested_thread_num; SimpleLogger().Write(logDEBUG) << "IP address:\t" << ip_address; SimpleLogger().Write(logDEBUG) << "IP port:\t" << ip_port; #ifndef _WIN32 int sig = 0; sigset_t new_mask; sigset_t old_mask; sigfillset(&new_mask); pthread_sigmask(SIG_BLOCK, &new_mask, &old_mask); #endif OSRM osrm_lib(lib_config); auto routing_server = Server::CreateServer(ip_address, ip_port, requested_thread_num); routing_server->GetRequestHandlerPtr().RegisterRoutingMachine(&osrm_lib); if (trial_run) { SimpleLogger().Write() << "trial run, quitting after successful initialization"; } else { std::packaged_task<int()> server_task([&]() -> int { routing_server->Run(); return 0; }); auto future = server_task.get_future(); std::thread server_thread(std::move(server_task)); #ifndef _WIN32 sigset_t wait_mask; pthread_sigmask(SIG_SETMASK, &old_mask, 0); sigemptyset(&wait_mask); sigaddset(&wait_mask, SIGINT); sigaddset(&wait_mask, SIGQUIT); sigaddset(&wait_mask, SIGTERM); pthread_sigmask(SIG_BLOCK, &wait_mask, 0); SimpleLogger().Write() << "running and waiting for requests"; sigwait(&wait_mask, &sig); #else // Set console control handler to allow server to be stopped. console_ctrl_function = std::bind(&Server::Stop, routing_server); SetConsoleCtrlHandler(console_ctrl_handler, TRUE); SimpleLogger().Write() << "running and waiting for requests"; routing_server->Run(); #endif SimpleLogger().Write() << "initiating shutdown"; routing_server->Stop(); SimpleLogger().Write() << "stopping threads"; auto status = future.wait_for(std::chrono::seconds(2)); if (status == std::future_status::ready) { server_thread.join(); } else { SimpleLogger().Write(logWARNING) << "Didn't exit within 2 seconds. Hard abort!"; server_task.reset(); // just kill it } } SimpleLogger().Write() << "freeing objects"; routing_server.reset(); SimpleLogger().Write() << "shutdown completed"; } catch (const std::exception &e) { SimpleLogger().Write(logWARNING) << "exception: " << e.what(); return 1; } #ifdef __linux__ munlockall(); #endif return 0; }