/************************************************************** * constructor/destructor **************************************************************/ u_service u_serviceNew( const c_char *uri, c_long timeout, const c_char *name, const c_char *extendedStateName, u_serviceKind kind, v_qos qos) { u_domain domain; v_kernel kk; v_service ks; v_serviceManager sm; u_service s; u_result r; os_result osr; c_bool serviceTermHandlerRequired = FALSE; ks = NULL; r = u_domainOpen(&domain, uri, timeout); if (r != U_RESULT_OK) { OS_REPORT_1(OS_ERROR,"u_serviceNew",0, "Failure to open the kernel - return code %d", r); return NULL; } s = NULL; if (domain != NULL) { r = u_entityWriteClaim(u_entity(domain),(v_entity*)(&kk)); if (r == U_RESULT_OK) { assert(kk); sm = v_getServiceManager(kk); if (sm != NULL) { #ifndef INTEGRITY if (lockPages(kk, name)) { osr = os_procMLockAll(OS_MEMLOCK_CURRENT|OS_MEMLOCK_FUTURE); } else { osr = os_resultSuccess; } if (osr == os_resultSuccess) { #endif switch(kind){ case U_SERVICE_DDSI: case U_SERVICE_DDSIE: case U_SERVICE_NETWORKING: ks = v_service(v_networkingNew(sm, name, extendedStateName, (v_participantQos)qos)); serviceTermHandlerRequired = TRUE; break; case U_SERVICE_DURABILITY: ks = v_service(v_durabilityNew(sm, name, extendedStateName, (v_participantQos)qos)); serviceTermHandlerRequired = TRUE; break; case U_SERVICE_CMSOAP: ks = v_service(v_cmsoapNew(sm, name, extendedStateName, (v_participantQos)qos)); serviceTermHandlerRequired = TRUE; break; case U_SERVICE_RNR: ks = v_service(v_rnrNew(sm, name, extendedStateName, (v_participantQos)qos)); serviceTermHandlerRequired = TRUE; break; case U_SERVICE_DBMSCONNECT: case U_SERVICE_INCOGNITO: ks = v_serviceNew(sm, name, extendedStateName, (v_participantQos)qos, NULL); serviceTermHandlerRequired = TRUE; break; case U_SERVICE_SPLICED: break; default: OS_REPORT(OS_WARNING,"u_serviceNew",0, "Failed to start an unknown service kind"); break; } } else { OS_REPORT(OS_ERROR,"u_serviceNew",0, "Failed to lock memory pages for current process"); } /* Install the service signal handlers if spliced is not within this * same process. i.e. only do this if each service is in its own * process so signal handlers won't interfere */ if (serviceTermHandlerRequired && !u_splicedInProcess()) { os_procSetTerminationHandler(serviceTermHandler); } #ifndef INTEGRITY } else { OS_REPORT(OS_ERROR,"u_serviceNew",0, "Failed to retrieve the Service Manager"); } #endif if (ks != NULL) { s = u_entityAlloc(NULL,u_service,ks,TRUE); r = u_serviceInit(s, kind, domain); if (r != U_RESULT_OK) { OS_REPORT_1(OS_ERROR,"u_serviceNew",0, "Failed to initialize service: %s", name); u_serviceFree(s); s = NULL; } callbackService = s; (void) os_signalHandlerSetExceptionCallback(u__serviceExceptionCallbackWrapper); } else { OS_REPORT(OS_WARNING,"u_serviceNew",0, "Failed to retrieve the Service Manager"); } r = u_entityRelease(u_entity(domain)); } } return s; }
int main( int argc, char* argv[]) { int result = 0; v_participantQos participantQos; u_result uresult; os_boolean success; v_subscriberQos subscriberQos; c_time resolution; c_base base; v_kernel kernel; /* Necessary to initialize the user layer. Do this just once per process.*/ mlv_init (); uresult = u_userInitialise(); mlv_setforreal (1); if(uresult == U_RESULT_OK){ /* Allocate default participant qos*/ participantQos = u_participantQosNew(NULL); { os_mutexAttr mattr; os_mutexAttrInit (&mattr); mattr.scopeAttr = OS_SCOPE_PRIVATE; os_mutexInit (&gluelock, &mattr); } if(participantQos){ if(argc > 1){ SERVICE_NAME = argv[1]; } if(argc > 2){ SERVICE_URI = argv[2]; } /*create participant*/ participant = u_participant(u_serviceNew( SERVICE_URI, 0, SERVICE_NAME, NULL, U_SERVICE_NETWORKING, (v_qos)participantQos)); if(participant){ struct cfgst *cfgst; ddsi2_participant_gid = u_entityGid (u_entity (participant)); /*Notify kernel that I am initializing. */ u_serviceChangeState(u_service(participant),STATE_INITIALISING); /*Start monitoring the splicedaemon state. I need to terminate if he says so*/ u_serviceWatchSpliceDaemon( u_service(participant), in_discoveryWatchSpliced, &terminate); if ((cfgst = config_init (participant, SERVICE_NAME)) != NULL) { unsigned rtps_flags = 0; struct nn_servicelease *servicelease; open_tracing_file (); /* Dependencies between default values is not handled automatically by the config processing (yet) */ if (config.many_sockets_mode) { if (config.max_participants == 0) config.max_participants = 100; } if (NN_STRICT_P) { /* Should not be sending invalid messages when strict */ config.respond_to_rti_init_zero_ack_with_invalid_heartbeat = 0; config.acknack_numbits_emptyset = 1; } config_print_and_free_cfgst (cfgst); servicelease = nn_servicelease_new (participant); nn_servicelease_start_renewing (servicelease); myNetworkId = getNetworkId (); u_entityAction(u_entity(participant), resolveKernelService, NULL); base = c_getBase(service); kernel = v_object(service)->kernel; rtps_init (base, kernel, config.domainId, config.participantIndex, rtps_flags, config.networkAddressString, config.peers); /* Initialize entity administration. */ success = in_entityAdminInit(participant); if(success){ /*Create subscriber to receive client writers' messages.*/ subscriberQos = u_subscriberQosNew(NULL); os_free(subscriberQos->partition); subscriberQos->partition = NULL; clientSubscriber = u_subscriberNew( participant, "clientSubscriber", subscriberQos, TRUE); if(clientSubscriber){ /*Create networkReader to be able to receive client writers' messages.*/ clientReader = u_networkReaderNew( clientSubscriber, "clientReader", NULL, TRUE); if(clientReader){ resolution.seconds = 0; resolution.nanoseconds = 10 * 1000 * 1000; /*10 ms*/ /*Create network queue*/ uresult = u_networkReaderCreateQueue( clientReader, 1000, 0, FALSE, FALSE, resolution, TRUE, &queueId, "DDSi"); if(uresult == U_RESULT_OK){ struct builtin_datareader_set drset; u_entityAction(u_entity(clientReader), resolveKernelReader, NULL); uresult = create_builtin_readers (&drset, participant); if (uresult == U_RESULT_OK) { u_serviceChangeState(u_service(participant),STATE_OPERATIONAL); uresult = attachAndMonitor(participant, &drset); if((uresult != U_RESULT_OK) && (uresult != U_RESULT_DETACHING)) { nn_log (LC_ERROR, "Abnormal termination...\n"); result = -1; } else { nn_log (LC_INFO, "Deleting entities...\n"); } destroy_builtin_readers (&drset); } else { nn_log (LC_FATAL, "Could not create subscription + readers for builtin topics.\n"); result = -1; } terminate = TRUE; v_networkReaderTrigger(vclientReader, queueId); os_threadWaitExit(clientWriterThread, NULL); } else { nn_log (LC_FATAL, "Could not create networkQueue.\n"); result = -1; } /*Clean up networkReader*/ os_mutexLock (&gluelock); u_networkReaderFree(clientReader); clientReader = NULL; vclientReader = NULL; os_mutexUnlock (&gluelock); } else { nn_log (LC_FATAL, "Could not create networkReader.\n"); result = -1; } /*Clean up subscriber*/ u_subscriberFree(clientSubscriber); } else { nn_log (LC_FATAL, "Could not create subscriber.\n"); result = -1; } /*Clean up entity administration*/ in_entityAdminDestroy(); } else { nn_log (LC_FATAL, "Could not initialize entity adminstration.\n"); result = -1; } /* RTPS layer now defines types, cleanup before detaching */ rtps_term(); /*Notify kernel that I've terminated*/ u_serviceChangeState(u_service(participant),STATE_TERMINATED); nn_servicelease_free (servicelease); /*Delete participant*/ uresult = u_serviceFree(u_service(participant)); if(uresult != U_RESULT_OK){ nn_log (LC_FATAL, "Deletion of participant failed.\n"); result = -1; } } else { nn_log (LC_FATAL, "Initialization of configuration failed.\n"); result = -1; } } else { nn_log (LC_FATAL, "Could not create participant.\n"); result = -1; } u_participantQosFree (participantQos); } else { nn_log (LC_FATAL, "Could not allocate participantQos.\n"); result = -1; } os_mutexDestroy (&gluelock); /* Detach user layer */ mlv_setforreal (0); uresult = u_userDetach(); mlv_fini (); if(uresult != U_RESULT_OK){ nn_log (LC_FATAL, "Detachment of user layer failed.\n"); result = -1; } } else { nn_log (LC_FATAL, "Initialization of user layer failed.\n"); result = -1; } nn_log (LC_INFO, "Finis.\n"); /* Must be really late, or nn_log becomes unhappy -- but it should be before os_osExit (which appears to be called from u_userExit(), which is not called by u_userDetach but by an exit handler, it appears.) */ config_fini (); return result; }
void cms_serviceFree( cms_service cms) { cms_client client; c_iter clientCopy; c_ulong i, size; if(cms != NULL) { if(cms->configuration != NULL) { if(cms->configuration->verbosity >= 2) { OS_REPORT(OS_INFO, CMS_CONTEXT, 0, "Terminating CMSOAP service..."); } } if(cms->uservice != NULL) { u_serviceChangeState(cms->uservice, STATE_TERMINATING); } cms->terminate = TRUE; if(cms->leaseThread != NULL) { cms_threadFree(cms->leaseThread); } if(cms->garbageCollector != NULL) { cms_threadFree(cms->garbageCollector); } if(cms->soap != NULL) { cms->soap->attributes = NULL; soap_destroy(cms->soap); soap_end(cms->soap); soap_done(cms->soap); free(cms->soap); } if(cms->clients != NULL) { os_mutexLock(&cms->clientMutex); clientCopy = c_iterCopy(cms->clients); os_mutexUnlock(&cms->clientMutex); if(c_iterLength(cms->clients) > 0) { if(cms->configuration->verbosity >= 2) { OS_REPORT_1(OS_WARNING, CMS_CONTEXT, 0, "Terminating CMSOAPService, but %d client(s) is/are still connected.", c_iterLength(cms->clients)); } } size = c_iterLength(clientCopy); for(i=0; i<size; i++) { client = cms_client(c_iterObject(clientCopy, i)); cms_thread(client)->terminate = TRUE; } for(i=0; i<size; i++) { client = cms_client(c_iterObject(clientCopy, i)); cms_clientFree(client); } c_iterFree(clientCopy); os_mutexLock(&cms->clientMutex); c_iterFree(cms->clients); os_mutexUnlock(&cms->clientMutex); os_mutexDestroy(&cms->clientMutex); } if(cms->configuration != NULL) { if(cms->configuration->verbosity >= 4) { OS_REPORT(OS_INFO, CMS_CONTEXT, 0, "CMSOAP service terminated."); } } cms_configurationFree(cms->configuration); if(cms->uservice != NULL) { cmx_deregisterAllEntities(); u_serviceChangeState(cms->uservice, STATE_TERMINATED); u_serviceFree(cms->uservice); } cmx_detach(); os_free(cms); } }
static void nw_serviceMain( const char *serviceName, const char *URI) { u_serviceManager serviceManager; os_time sleepTime; os_result waitResult; nw_termination terminate; os_mutexAttr termMtxAttr; os_condAttr termCvAttr; c_bool fatal = FALSE; v_duration leasePeriod; terminate.terminate = FALSE; os_mutexAttrInit( & termMtxAttr ); os_condAttrInit( & termCvAttr ); termMtxAttr.scopeAttr = OS_SCOPE_PRIVATE; termCvAttr.scopeAttr = OS_SCOPE_PRIVATE; os_mutexInit( &terminate.mtx, &termMtxAttr ); os_condInit( &terminate.cv, &terminate.mtx, &termCvAttr ); /* Create networking service with kernel */ service = u_serviceNew(URI, NW_ATTACH_TIMEOUT, serviceName, NULL, U_SERVICE_NETWORKING, NULL); /* Initialize configuration */ nw_configurationInitialize(service, serviceName, URI); /* Ask service manager for splicedaemon state */ serviceManager = u_serviceManagerNew(u_participant(service)); /* Create the controller which starts the updating */ /* and calls the listener on a fatal error */ controller = nw_controllerNew(service,controller_onfatal,&fatal); if (controller) { os_procAtExit(on_exit_handler); /* Start the actual engine */ NW_REPORT_INFO(1, "Networking started"); NW_TRACE(Mainloop, 1, "Networking started"); nw_controllerStart(controller); /* Change state for spliced */ u_serviceChangeState(service, STATE_INITIALISING); u_serviceChangeState(service, STATE_OPERATIONAL); /* Get sleeptime from configuration */ nw_retrieveLeaseSettings(&leasePeriod, &sleepTime); /*sleepTime.tv_sec = 1; */ /* Loop until termination is requested */ u_serviceWatchSpliceDaemon(service, nw_splicedaemonListener, &terminate); os_mutexLock( &terminate.mtx ); while ((!(int)terminate.terminate) && (!(int)fatal) && (!(int)f_exit)) { /* Assert my liveliness and the Splicedaemon's liveliness*/ u_serviceRenewLease(service, leasePeriod); /* Check if anybody is still remotely interested */ nw_controllerUpdateHeartbeats(controller); /* Wait before renewing again */ waitResult = os_condTimedWait( &terminate.cv, &terminate.mtx, &sleepTime ); if (waitResult == os_resultFail) { OS_REPORT(OS_CRITICAL, "nw_serviceMain", 0, "os_condTimedWait failed - thread will terminate"); fatal = TRUE; } /* QAC EXPECT 2467; Control variable, terminate, not modified inside loop. That is correct, it is modified by another thread */ } os_mutexUnlock( &terminate.mtx ); /* keep process here waiting for the exit processing */ while ((int)f_exit){os_nanoSleep(sleepTime);} if (!(int)fatal ) { leasePeriod.seconds = 20; leasePeriod.nanoseconds = 0; u_serviceRenewLease(service, leasePeriod); u_serviceChangeState(service, STATE_TERMINATING); nw_controllerStop(controller); nw_controllerFree(controller); controller = NULL; NW_REPORT_INFO(1, "Networking stopped"); NW_TRACE(Mainloop, 1, "Networking stopped"); } } if (!(int)fatal ) { nw_configurationFinalize(); /* Clean up */ u_serviceChangeState(service, STATE_TERMINATED); u_serviceManagerFree(serviceManager); u_serviceFree(service); } }
void in_serviceMain( const os_char* serviceName, const os_char* uri) { u_service service; in_config config; in_result result; u_serviceManager serviceManager; in_controller controller; v_duration leasePeriod; os_time sleepTime; os_boolean terminate = OS_FALSE; in_connectivityAdmin admin; assert(serviceName); assert(uri); /* Create networking service with kernel */ service = u_serviceNew( uri, IN_ATTACH_TIMEOUT, serviceName, NULL, U_SERVICE_NETWORKING, NULL); assert(service); /* Initialize configuration */ config = in_configGetInstance(); result = in_configConvertDomTree(config, uri, service); if(result == IN_RESULT_OK) { /* Ask service manager for splicedaemon state */ serviceManager = u_serviceManagerNew(u_participant(service)); admin = in_connectivityAdminGetInstance(); /* Create the controller which starts the updating */ controller = in_controllerNew(service); if (controller) { /* Start the actual engine */ IN_REPORT_INFO(1, "DDSI networking started"); IN_TRACE(Mainloop, 1, "DDSI networking started"); in_controllerStart(controller); /* Change state for spliced */ u_serviceChangeState(service, STATE_INITIALISING); u_serviceChangeState(service, STATE_OPERATIONAL); /* Get sleeptime from configuration */ in_retrieveLeaseSettings(&leasePeriod, &sleepTime); u_serviceRenewLease(service, leasePeriod); /* Loop until termination is requested */ u_serviceWatchSpliceDaemon( service, in_splicedaemonListener, &terminate); /* terminate flag is modified by the splice deamon listener thread*/ while (!terminate) { /* Assert my liveliness and the Splicedaemon's liveliness */ u_serviceRenewLease(service, leasePeriod); /* Wait before renewing again */ os_nanoSleep(sleepTime); } leasePeriod.seconds = 20; u_serviceRenewLease(service, leasePeriod); u_serviceChangeState(service, STATE_TERMINATING); in_controllerStop(controller); in_controllerFree(controller); IN_REPORT_INFO(1, "DDSI networking stopped"); IN_TRACE(Mainloop, 1, "DDSI networking stopped"); } u_serviceChangeState(service, STATE_TERMINATED); u_serviceManagerFree(serviceManager); in_objectFree(in_object(admin)); } /* Clean up */ in_configFree(config); u_serviceFree(service); }