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; }
cms_service cms_serviceNew( const c_char* name, const c_char* uri) { cms_service service; c_bool success; const c_char* init; C_STRUCT(v_participantQos) q; struct sockaddr_in addr; socklen_t len; os_result errcode; char* ipTagStr = NULL; char* xmlStr = NULL; u_result result; os_result res; os_ifAttributes *ifList; os_uint32 nofIf, i; service = NULL; if(uri != NULL) { init = cmx_initialise(); if(strcmp(init, CMS_RESULT_OK) == 0) { service = cms_service(os_malloc(C_SIZEOF(cms_service))); cms_object(service)->kind = CMS_SERVICE; service->terminate = FALSE; service->leaseThread = NULL; service->clients = NULL; service->soap = NULL; service->configuration = NULL; service->garbageCollector = NULL; service->uservice = u_serviceNew(uri, CMSERVICE_ATTACH_TIMEOUT, name, NULL, U_SERVICE_CMSOAP, NULL); if(service->uservice != NULL) { /*disable all events.*/ u_dispatcherSetEventMask(u_dispatcher(service->uservice), 0); u_entityAction(u_entity(service->uservice), cms_serviceActionGroups, NULL); u_serviceChangeState(service->uservice, STATE_INITIALISING); success = cms_serviceInit(name, service); result = u_participantQosInit((v_participantQos)&q); if (result == U_RESULT_OK) { /* Insert service information in userData QoS */ len = sizeof(struct sockaddr); errcode = os_sockGetsockname (service->soap->master, (struct sockaddr*)&addr, len); if(errcode == os_resultSuccess) { OS_REPORT_1(OS_INFO, CMS_CONTEXT, 0, "SOAP service is reachable via port %d",ntohs(addr.sin_port)); ifList = os_malloc(MAX_INTERFACES * sizeof(*ifList)); #ifdef WITH_IPV6 res = os_sockQueryIPv6Interfaces(ifList, (os_uint32)MAX_INTERFACES, &nofIf); #else res = os_sockQueryInterfaces(ifList, (os_uint32)MAX_INTERFACES, &nofIf); #endif /* SOAP userdata layout: * <TunerService> * <Ip>x.x.x.x:port</Ip> [<Ip>x.x.x.x</Ip>]... * </TunerService> */ if (res == os_resultSuccess) { os_char tmp[64]; int chars; for (i = 0; i < nofIf; i++) { /* ignore the local loopback interface */ if (!os_sockaddrIsLoopback((os_sockaddr*)&ifList[i].address)) { os_sprintf(tmp,"%s",inet_ntoa(((os_sockaddr_in*)&ifList[i].address)->sin_addr)); if (strcmp(tmp,"0.0.0.0") != 0) { chars = os_sprintf(tmp, IP_TAG, inet_ntoa(((os_sockaddr_in*)&ifList[i].address)->sin_addr), ntohs(addr.sin_port)); if (chars > 0) { if (ipTagStr) { ipTagStr = os_realloc(ipTagStr, strlen(ipTagStr) + chars + 1); } else { ipTagStr = os_malloc(chars + 1); *ipTagStr = '\0'; } ipTagStr = os_strcat(ipTagStr, tmp); } } } } } else { if(service->configuration->verbosity >= 1) { OS_REPORT(OS_WARNING, CMS_CONTEXT, 0,"Could not get SOAP ip address."); } ipTagStr = os_malloc((strlen(IP_TAG) + INET6_ADDRSTRLEN_EXTENDED)); os_sprintf (ipTagStr, IP_TAG, "127.0.0.1", ntohs(addr.sin_port)); } os_free(ifList); xmlStr = os_malloc(strlen(ipTagStr) + strlen(SOAP_TAG)+1); os_sprintf (xmlStr, SOAP_TAG, ipTagStr); os_free(ipTagStr); q.userData.size = strlen(xmlStr); q.userData.value = os_malloc(q.userData.size); memcpy(q.userData.value, xmlStr, q.userData.size); if(service->configuration->verbosity >= 5) { OS_REPORT_1(OS_INFO, CMS_CONTEXT, 0, "SOAP userData: %s", xmlStr); } os_free(xmlStr); } else { q.userData.size = 0; q.userData.value = NULL; if(service->configuration->verbosity >= 1) { OS_REPORT(OS_WARNING, CMS_CONTEXT, 0, "Could not get SOAP port."); } } result = u_entitySetQoS(u_entity(service->uservice), (v_qos)&q); if (result != U_RESULT_OK) { if(service->configuration->verbosity >= 1) { OS_REPORT(OS_WARNING, CMS_CONTEXT, 0,"Could not update the participantQos for publication of the SOAP ip address and port."); } } os_free(q.userData.value); } else { if(service->configuration->verbosity >= 1) { OS_REPORT(OS_WARNING, CMS_CONTEXT, 0,"Could not initiate participantQos for SOAP service ip address and port publication."); } } if(success == FALSE) { cms_serviceFree(service); service = NULL; } else { u_serviceWatchSpliceDaemon(service->uservice, cms_serviceSplicedaemonListener, service); u_serviceChangeState(service->uservice, STATE_OPERATIONAL); } } else { OS_REPORT(OS_ERROR, CMS_CONTEXT, 0, "cms_serviceNew: user layer service could not be created."); cms_serviceFree(service); service = NULL; } } else { if(service && service->configuration->verbosity >= 1) { OS_REPORT(OS_ERROR, CMS_CONTEXT, 0, "cms_serviceNew: C&M API could not be initialized."); } } } else { if(service && service->configuration->verbosity > 0) { OS_REPORT(OS_ERROR, CMS_CONTEXT, 0, "cms_serviceNew: no uri supplied."); } } return service; }
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); }