bool GlobalServer_start ( GlobalServer *self ) { #ifdef WIN32 SetConsoleTitle ("GlobalServer"); #endif // WIN32 special ("======================"); special ("=== Global server ===="); special ("======================"); GlobalServerStartupInfo *info = &self->info; zpoller_t *poller; bool isRunning = true; ServerStartupInfo serverInfo; // =================================== // Initialize CLI connection // =================================== // CLI should communicates through BSD sockets zsock_set_router_raw (self->cliConnection, true); if (zsock_bind (self->cliConnection, GLOBAL_SERVER_CLI_ENDPOINT, info->ip, info->cliPort) == -1) { error ("Failed to bind CLI port."); return false; } info ("CLI connection binded on port %s.", zsys_sprintf (GLOBAL_SERVER_CLI_ENDPOINT, info->ip, info->cliPort)); // =================================== // Initialize Zones connection // =================================== if ((info->zonesPort = zsock_bind (self->zonesConnection, GLOBAL_SERVER_ZONES_ENDPOINT, info->ip)) == -1) { error ("Failed to bind zones port."); return false; } info ("Zones connection binded on port %s.", zsys_sprintf (GLOBAL_SERVER_ZONES_ENDPOINT, info->ip, info->zonesPort)); // =================================== // Initialize 1 Barrack Server // =================================== if (!(ServerFactory_initServerInfo (&serverInfo, SERVER_TYPE_BARRACK, BARRACK_SERVER_ROUTER_ID, info->barrackServerIp, info->barrackServerPortCount, info->barrackServerPort, info->zoneWorkersCount, info->ip, info->cliPort, info->sqlInfo.hostname, info->sqlInfo.login, info->sqlInfo.password, info->sqlInfo.database, info->redisInfo.hostname, info->redisInfo.port) )) { error ("[Barrack] Cannot create a new ServerInfo."); return false; } if (!(Server_createProcess (&serverInfo, ZONE_SERVER_EXECUTABLE_NAME))) { error ("[Barrack] Can't launch a new Server process."); return false; } // =================================== // Initialize N Social Server // =================================== for (uint16_t routerId = 0; routerId < info->socialServersCount; routerId++) { ServerFactory_initServerInfo (&serverInfo, SERVER_TYPE_SOCIAL, SOCIAL_SERVER_ROUTER_ID - routerId, info->socialServersIp[routerId], 1, &info->socialServersPorts[routerId], // Only 1 port for each social server info->socialWorkersCount, info->ip, info->cliPort, info->sqlInfo.hostname, info->sqlInfo.login, info->sqlInfo.password, info->sqlInfo.database, info->redisInfo.hostname, info->redisInfo.port ); if (!(Server_createProcess (&serverInfo, ZONE_SERVER_EXECUTABLE_NAME))) { error ("[routerId=%d] Can't launch a new Server process.", routerId); return false; } } // =================================== // Initialize N Zone Server // =================================== for (uint16_t routerId = 0; routerId < info->zoneServersCount; routerId++) { ServerFactory_initServerInfo (&serverInfo, SERVER_TYPE_ZONE, routerId, info->zoneServersIp[routerId], 1, &info->zoneServersPorts[routerId], // Only 1 port for each Zone server info->zoneWorkersCount, info->ip, info->cliPort, info->sqlInfo.hostname, info->sqlInfo.login, info->sqlInfo.password, info->sqlInfo.database, info->redisInfo.hostname, info->redisInfo.port ); if (!(Server_createProcess (&serverInfo, ZONE_SERVER_EXECUTABLE_NAME))) { error ("[routerId=%d] Can't launch a new Server process.", routerId); return false; } } // Define a poller with the zones and the CLI sockets if (!(poller = zpoller_new (self->cliConnection, self->zonesConnection, NULL))) { error ("Global server cannot create a poller."); return false; } // Listens to requests info ("GlobalServer is ready and running."); while (isRunning) { zsock_t *actor = zpoller_wait (poller, -1); typedef int (*GlobalServerRequestHandler) (GlobalServer *self, zsock_t *actor); GlobalServerRequestHandler handler; // Get the correct handler based on the actor if (actor == self->zonesConnection) { handler = GlobalServer_handleZonesRequest; } else if (actor == self->cliConnection) { handler = GlobalServer_handleCliRequest; } else { warning ("An unknown actor talked to the Global Server. Maybe SIGINT signal ?"); break; } switch (handler (self, actor)) { case -1: // ERROR error ("Global Server encountered an error when handling a request."); case -2: // Connection stopped isRunning = false; break; case 0: // OK break; } } return true; }
/// // Set socket option `router_raw`. void QZsock::setRouterRaw (int routerRaw) { zsock_set_router_raw (self, routerRaw); }
JNIEXPORT void JNICALL Java_org_zeromq_czmq_Zsock__1_1setRouterRaw (JNIEnv *env, jclass c, jlong self, jint router_raw) { zsock_set_router_raw ((zsock_t *) (intptr_t) self, (int) router_raw); }