cms_client cms_clientNew( unsigned long ip, cms_service service) { cms_client client; os_result osr; client = os_malloc(sizeof *client); if(client != NULL){ if (cms_threadInit(cms_thread(client), "cms_client", &service->configuration->clientScheduling)) { cms_object(client)->kind = CMS_CLIENT; cms_thread(client)->did = service->did; cms_thread(client)->uri = os_strdup(service->uri); client->ip = ip; client->initCount = 0; client->service = service; client->internalFree = FALSE; osr = os_mutexInit(&client->soapMutex, NULL); client->soapEnvs = c_iterNew(NULL); if(osr == os_resultSuccess){ osr = os_mutexInit(&client->conditionMutex, NULL); if(osr == os_resultSuccess){ osr = os_condInit(&client->condition, &client->conditionMutex, NULL ); if(osr == os_resultSuccess){ osr = os_mutexInit(&client->threadMutex, NULL); if(osr == os_resultSuccess){ client->threads = c_iterNew(NULL); } else { cms_clientFree(client); } } } else { cms_clientFree(client); } } else { cms_clientFree(client); } } else { cms_clientFree(client); client = NULL; } } if(client == NULL){ if(service->configuration->verbosity >= 1){ OS_REPORT(OS_ERROR, CMS_CONTEXT, 0, "cms_clientNew: client could not be initialized."); } } return client; }
int main() { oes oes_object ; cms cms_object(oes_object) ; auto pcms = std::addressof(cms_object) ; oes_object.connect(pcms) ; for( int i = 0 ; i < 10 ; ++i ) std::thread( &cms::send_data, pcms, i + 10 ).detach() ; std::this_thread::sleep_for( std::chrono::seconds(3) ); cms_object.print() ; }
cms_soapThread cms_soapThreadNew( const c_char* name, cms_client client) { cms_soapThread thread; os_mutexAttr attr; os_condAttr condAttr; os_result osr; thread = NULL; osr = os_resultInvalid; if(client != NULL){ thread = cms_soapThread(os_malloc(C_SIZEOF(cms_soapThread))); if (thread != NULL) { cms_object(thread)->kind = CMS_SOAPTHREAD; cms_threadInit(cms_thread(thread), name, &client->service->configuration->clientScheduling); cms_thread(thread)->uri = os_strdup(cms_thread(client)->uri); thread->client = client; thread->soap = NULL; osr = os_mutexAttrInit(&attr); if(osr == os_resultSuccess){ attr.scopeAttr = OS_SCOPE_PRIVATE; osr = os_mutexInit(&thread->soapMutex, &attr); if(osr == os_resultSuccess){ osr = os_condAttrInit(&condAttr); if(osr == os_resultSuccess){ condAttr.scopeAttr = OS_SCOPE_PRIVATE; osr = os_condInit(&thread->condition, &thread->soapMutex, &condAttr ); } } } } } if (osr != os_resultSuccess) { cms_soapThreadFree(thread); return NULL; } return thread; }
cms_client cms_clientNew( unsigned long ip, cms_service service, const c_char* uri) { cms_client client; os_result osr; os_mutexAttr attr; os_condAttr condAttr; client = cms_client(os_malloc(C_SIZEOF(cms_client))); cms_threadInit(cms_thread(client), "cms_client", &service->configuration->clientScheduling); if(client != NULL){ cms_object(client)->kind = CMS_CLIENT; cms_thread(client)->uri = os_strdup(uri); client->ip = ip; client->initCount = 0; client->service = service; client->internalFree = FALSE; osr = os_mutexAttrInit(&attr); if(osr == os_resultSuccess){ attr.scopeAttr = OS_SCOPE_PRIVATE; osr = os_mutexInit(&client->soapMutex, &attr); client->soapEnvs = c_iterNew(NULL); if(osr == os_resultSuccess){ osr = os_condAttrInit(&condAttr); if(osr == os_resultSuccess){ osr = os_mutexInit(&client->conditionMutex, &attr); if(osr == os_resultSuccess){ condAttr.scopeAttr = OS_SCOPE_PRIVATE; osr = os_condInit(&client->condition, &client->conditionMutex, &condAttr ); if(osr == os_resultSuccess){ osr = os_mutexInit(&client->threadMutex, &attr); if(osr == os_resultSuccess){ client->threads = c_iterNew(NULL); } else { cms_clientFree(client); } } } else { cms_clientFree(client); } } else { cms_clientFree(client); } } else { cms_clientFree(client); } } else { cms_clientFree(client); } } if(client == NULL){ if(service->configuration->verbosity >= 1){ OS_REPORT(OS_ERROR, CMS_CONTEXT, 0, "cms_clientNew: client could not be initialized."); } } return client; }
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; }