celix_status_t discovery_start(discovery_pt discovery) { celix_status_t status = CELIX_SUCCESS; char *port = NULL; char *path = NULL; logHelper_start(discovery->loghelper); bundleContext_getProperty(discovery->context, DISCOVERY_SERVER_PORT, &port); if (port == NULL) { port = DEFAULT_SERVER_PORT; } bundleContext_getProperty(discovery->context, DISCOVERY_SERVER_PATH, &path); if (path == NULL) { path = DEFAULT_SERVER_PATH; } status = endpointDiscoveryPoller_create(discovery, discovery->context, &discovery->poller); if (status != CELIX_SUCCESS) { return CELIX_BUNDLE_EXCEPTION; } status = endpointDiscoveryServer_create(discovery, discovery->context, &discovery->server); if (status != CELIX_SUCCESS) { return CELIX_BUNDLE_EXCEPTION; } status = etcdWatcher_create(discovery, discovery->context, &discovery->watcher); if (status != CELIX_SUCCESS) { return CELIX_BUNDLE_EXCEPTION; } return status; }
celix_status_t etcdWatcher_create(node_discovery_pt node_discovery, bundle_context_pt context, etcd_watcher_pt *watcher) { celix_status_t status = CELIX_SUCCESS; char* etcd_server = NULL; char* etcd_port_string = NULL; int etcd_port = 0; if (node_discovery == NULL) { return CELIX_BUNDLE_EXCEPTION; } (*watcher) = calloc(1, sizeof(struct etcd_watcher)); if (*watcher) { (*watcher)->node_discovery = node_discovery; if ((bundleContext_getProperty(context, CFG_ETCD_SERVER_IP, &etcd_server) != CELIX_SUCCESS) || !etcd_server) { etcd_server = DEFAULT_ETCD_SERVER_IP; } if ((bundleContext_getProperty(context, CFG_ETCD_SERVER_PORT, &etcd_port_string) != CELIX_SUCCESS) || !etcd_port_string) { etcd_port = DEFAULT_ETCD_SERVER_PORT; } else { char* endptr = etcd_port_string; errno = 0; etcd_port = strtol(etcd_port_string, &endptr, 10); if (*endptr || errno != 0) { etcd_port = DEFAULT_ETCD_SERVER_PORT; } } if (etcd_init(etcd_server, etcd_port) == false) { status = CELIX_BUNDLE_EXCEPTION; } else { etcdWatcher_addOwnNode(*watcher); if ((status = celixThreadMutex_create(&(*watcher)->watcherLock, NULL)) != CELIX_SUCCESS) { return status; } if ((status = celixThreadMutex_lock(&(*watcher)->watcherLock)) != CELIX_SUCCESS) { return status; } if ((status = celixThread_create(&(*watcher)->watcherThread, NULL, etcdWatcher_run, *watcher)) != CELIX_SUCCESS) { return status; } (*watcher)->running = true; if ((status = celixThreadMutex_unlock(&(*watcher)->watcherLock)) != CELIX_SUCCESS) { return status; } } } else { status = CELIX_ENOMEM; } return status; }
celix_status_t bundleActivator_start(void * userData, bundle_context_pt context) { celix_status_t status = CELIX_SUCCESS; service_tracker_customizer_pt queueCustomizer = NULL; service_tracker_customizer_pt dataStoreCustomizer = NULL; bundle_activator_pt activator = (bundle_activator_pt) userData; char *name = NULL; char *uuid = NULL; char uuidName[128]; memset(uuidName, 0, 128); bundleContext_getProperty(context, "inaetics.demonstrator.processor.name", &name); bundleContext_getProperty(context, OSGI_FRAMEWORK_FRAMEWORK_UUID, &uuid); if (name != NULL) { snprintf(uuidName, 128, "%s %s",PROCESSOR_STATS_SERVICE_NAME_PREFIX, name); } else if (name == NULL && uuid != NULL) { snprintf(uuidName, 128, "%s %.8s",PROCESSOR_STATS_SERVICE_NAME_PREFIX, uuid); } else { snprintf(uuidName, 128, "%s (unknown ID)",PROCESSOR_STATS_SERVICE_NAME_PREFIX); } printf("PROCESSOR: Starting bundle...\n"); status = processor_create(uuidName, &activator->processor); if (status == CELIX_SUCCESS) { bundleActivator_createStatsService(activator); if (activator->processorStatsService != NULL) { bundleActivator_registerStatsService(activator); } if (activator->processorStatsService == NULL || activator->processorStatsRegistration == NULL) { printf("PROCESSOR: Error creating/registering statService\n"); } else { printf("PROCESSOR: Created processor with name %s\n", uuidName); } } /*Track QueueService*/ serviceTrackerCustomizer_create(activator->processor, NULL, processor_queueServiceAdded, NULL, processor_queueServiceRemoved, &queueCustomizer); serviceTracker_create(context, INAETICS_DEMONSTRATOR_API__SAMPLE_QUEUE_SERVICE_NAME, queueCustomizer, &activator->queueTracker); serviceTracker_open(activator->queueTracker); /*Track DataStoreService*/ serviceTrackerCustomizer_create(activator->processor, NULL, processor_dataStoreServiceAdded, NULL, processor_dataStoreServiceRemoved, &dataStoreCustomizer); serviceTracker_create(context, INAETICS_DEMONSTRATOR_API__DATA_STORE_SERVICE_NAME, dataStoreCustomizer, &activator->dataStoreTracker); serviceTracker_open(activator->dataStoreTracker); return status; }
celix_status_t deploymentAdmin_create(apr_pool_t *pool, bundle_context_pt context, deployment_admin_pt *admin) { celix_status_t status = CELIX_SUCCESS; apr_pool_t *subpool; apr_pool_create(&subpool, pool); *admin = apr_palloc(subpool, sizeof(**admin)); if (!*admin) { status = CELIX_ENOMEM; } else { (*admin)->pool = subpool; (*admin)->running = true; (*admin)->context = context; (*admin)->current = NULL; (*admin)->packages = hashMap_create(utils_stringHash, NULL, utils_stringEquals, NULL); (*admin)->targetIdentification = NULL; (*admin)->pollUrl = NULL; (*admin)->auditlogUrl = NULL; bundleContext_getProperty(context, IDENTIFICATION_ID, &(*admin)->targetIdentification); (*admin)->auditlogId = apr_time_now(); (*admin)->aditlogSeqNr = 0; if ((*admin)->targetIdentification == NULL ) { printf("Target name must be set using \"deployment_admin_identification\"\n"); status = CELIX_ILLEGAL_ARGUMENT; } else { char *url = NULL; bundleContext_getProperty(context, DISCOVERY_URL, &url); if (url == NULL) { printf("URL must be set using \"deployment_admin_url\"\n"); status = CELIX_ILLEGAL_ARGUMENT; } else { (*admin)->pollUrl = apr_pstrcat(subpool, url, "/deployment/", (*admin)->targetIdentification, VERSIONS, NULL); (*admin)->auditlogUrl = apr_pstrcat(subpool, url, "/auditlog", NULL); // log_store_pt store = NULL; // log_pt log = NULL; // log_sync_pt sync = NULL; // logStore_create(subpool, &store); // log_create(subpool, store, &log); // logSync_create(subpool, (*admin)->targetIdentification, store, &sync); // // log_log(log, 20000, NULL); apr_thread_create(&(*admin)->poller, NULL, deploymentAdmin_poll, *admin, subpool); } } } return status; }
celix_status_t bundleActivator_start(void * userData, bundle_context_pt context) { celix_status_t status = CELIX_SUCCESS; struct activator *activator = userData; //Creating data store Component //Setting up name for data store. char *name = NULL; char *uuid = NULL; char uuidName[128]; memset(uuidName,0,128); bundleContext_getProperty(context, "inaetics.demonstrator.datastore.name", &name); bundleContext_getProperty(context, OSGI_FRAMEWORK_FRAMEWORK_UUID, &uuid); if(name!=NULL){ snprintf(uuidName,128,"%s %s",DATASTORE_STATS_SERVICE_NAME_PREFIX, name); } else if (name == NULL && uuid != NULL) { snprintf(uuidName,128,"%s %.8s",DATASTORE_STATS_SERVICE_NAME_PREFIX, uuid); } else { snprintf(uuidName,128,"%s (unknown ID)",DATASTORE_STATS_SERVICE_NAME_PREFIX); } status = dataStore_create(uuidName, &activator->dataStore); if(status==CELIX_SUCCESS) { //Creating service structs for services provided by the component bundleActivator_createDataStoreService(activator); bundleActivator_createStatsService(activator); if (activator->dataStoreService != NULL) { bundleActivator_registerDataStoreService(activator); } if (activator->dataStoreStatsService != NULL) { bundleActivator_registerStatsService(activator); } if ( activator->dataStoreService == NULL || activator->dataStoreStatsService == NULL || activator->dataStoreRegistration == NULL || activator->dataStoreStatsRegistration == NULL) { printf("DATA STORE: Error creating/registering services\n"); status = CELIX_BUNDLE_EXCEPTION; } else{ printf("DATA STORE: Created datastore with name %s\n",uuidName); } } return status; }
celix_status_t importRegistrationFactory_open(import_registration_factory_pt registration_factory) { celix_status_t status = CELIX_SUCCESS; char *bundleStore = NULL; bundleContext_getProperty(registration_factory->context, BUNDLE_STORE_PROPERTY_NAME, &bundleStore); if (bundleStore == NULL) { bundleStore = DEFAULT_BUNDLE_STORE; } char *name = apr_pstrcat(registration_factory->pool, bundleStore, "/", registration_factory->serviceName, "_proxy.zip", NULL); status = bundleContext_installBundle(registration_factory->context, name, ®istration_factory->bundle); if (status == CELIX_SUCCESS) { status = bundle_start(registration_factory->bundle); if (status == CELIX_SUCCESS) { printf("%s sucessfully started\n", name); } } else { printf("%s could not be installed.\n", name); } return status; }
celix_status_t importRegistrationFactory_open(import_registration_factory_pt registration_factory) { celix_status_t status; char *bundleStore = NULL; bundleContext_getProperty(registration_factory->context, BUNDLE_STORE_PROPERTY_NAME, &bundleStore); if (bundleStore == NULL) { bundleStore = DEFAULT_BUNDLE_STORE; } char name[256]; snprintf(name, 256, "%s/%s_proxy.zip", bundleStore, registration_factory->serviceName); status = bundleContext_installBundle(registration_factory->context, name, ®istration_factory->bundle); if (status == CELIX_SUCCESS) { status = bundle_start(registration_factory->bundle); if (status == CELIX_SUCCESS) { logHelper_log(registration_factory->loghelper, OSGI_LOGSERVICE_INFO, "%s successfully started.", name); } } else { logHelper_log(registration_factory->loghelper, OSGI_LOGSERVICE_ERROR, "%s could not be installed.", name); } return status; }
celix_status_t bundleActivator_create(bundle_context_pt context, void **userData) { celix_status_t status = CELIX_SUCCESS; struct publisherActivator * act = malloc(sizeof(*act)); const char* fwUUID = NULL; bundleContext_getProperty(context,OSGI_FRAMEWORK_FRAMEWORK_UUID,&fwUUID); if(fwUUID == NULL){ printf("MP_PUBLISHER: Cannot retrieve fwUUID.\n"); status = CELIX_INVALID_BUNDLE_CONTEXT; } if (status == CELIX_SUCCESS){ bundle_pt bundle = NULL; long bundleId = 0; bundleContext_getBundle(context,&bundle); bundle_getBundleId(bundle,&bundleId); arrayList_create(&(act->trackerList)); act->client = publisher_create(act->trackerList,fwUUID,bundleId); *userData = act; } else { free(act); } return status; }
static const char* etcdWriter_getRootPath(bundle_context_pt context) { const char* rootPath = NULL; bundleContext_getProperty(context, CFG_ETCD_ROOT_PATH, &rootPath); if(rootPath == NULL) { rootPath = DEFAULT_ETCD_ROOTPATH; } return rootPath; }
celix_status_t pubsub_discovery_stop(pubsub_discovery_pt ps_discovery) { celix_status_t status = CELIX_SUCCESS; const char* fwUUID = NULL; bundleContext_getProperty(ps_discovery->context, OSGI_FRAMEWORK_FRAMEWORK_UUID, &fwUUID); if (fwUUID == NULL) { printf("PSD: Cannot retrieve fwUUID.\n"); return CELIX_INVALID_BUNDLE_CONTEXT; } celixThreadMutex_lock(&ps_discovery->watchersMutex); hash_map_iterator_pt iter = hashMapIterator_create(ps_discovery->watchers); while (hashMapIterator_hasNext(iter)) { struct watcher_info * wi = hashMapIterator_nextValue(iter); etcdWatcher_stop(wi->watcher); } hashMapIterator_destroy(iter); celixThreadMutex_lock(&ps_discovery->discoveredPubsMutex); /* Unexport all publishers for the local framework, and also delete from ETCD publisher belonging to the local framework */ iter = hashMapIterator_create(ps_discovery->discoveredPubs); while (hashMapIterator_hasNext(iter)) { array_list_pt pubEP_list = (array_list_pt) hashMapIterator_nextValue(iter); int i; for (i = 0; i < arrayList_size(pubEP_list); i++) { pubsub_endpoint_pt pubEP = (pubsub_endpoint_pt) arrayList_get(pubEP_list, i); if (strcmp(pubEP->frameworkUUID, fwUUID) == 0) { etcdWriter_deletePublisherEndpoint(ps_discovery->writer, pubEP); } else { pubsub_discovery_informPublishersListeners(ps_discovery, pubEP, false); arrayList_remove(pubEP_list, i); pubsubEndpoint_destroy(pubEP); i--; } } } hashMapIterator_destroy(iter); celixThreadMutex_unlock(&ps_discovery->discoveredPubsMutex); etcdWriter_destroy(ps_discovery->writer); iter = hashMapIterator_create(ps_discovery->watchers); while (hashMapIterator_hasNext(iter)) { struct watcher_info * wi = hashMapIterator_nextValue(iter); etcdWatcher_destroy(wi->watcher); } hashMapIterator_destroy(iter); hashMap_destroy(ps_discovery->watchers, true, true); celixThreadMutex_unlock(&ps_discovery->watchersMutex); return status; }
celix_status_t etcdWriter_addPublisherEndpoint(etcd_writer_pt writer, pubsub_endpoint_pt pubEP, bool storeEP){ celix_status_t status = CELIX_BUNDLE_EXCEPTION; if(storeEP){ const char *fwUUID = NULL; bundleContext_getProperty(writer->pubsub_discovery->context, OSGI_FRAMEWORK_FRAMEWORK_UUID, &fwUUID); if(fwUUID && strcmp(pubEP->frameworkUUID, fwUUID) == 0) { celixThreadMutex_lock(&writer->localPubsLock); pubsub_endpoint_pt p = NULL; pubsubEndpoint_clone(pubEP, &p); arrayList_add(writer->localPubs,p); celixThreadMutex_unlock(&writer->localPubsLock); } } char *key; const char* ttlStr = NULL; int ttl = 0; // determine ttl if ((bundleContext_getProperty(writer->pubsub_discovery->context, CFG_ETCD_TTL, &ttlStr) != CELIX_SUCCESS) || !ttlStr) { ttl = DEFAULT_ETCD_TTL; } else { char* endptr = NULL; errno = 0; ttl = strtol(ttlStr, &endptr, 10); if (*endptr || errno != 0) { ttl = DEFAULT_ETCD_TTL; } } const char *rootPath = etcdWriter_getRootPath(writer->pubsub_discovery->context); asprintf(&key,"%s/%s/%s/%s/%ld",rootPath,pubEP->scope,pubEP->topic,pubEP->frameworkUUID,pubEP->serviceID); if(!etcd_set(key,pubEP->endpoint,ttl,false)){ status = CELIX_ILLEGAL_ARGUMENT; } FREE_MEM(key); return status; }
celix_status_t wiringAdmin_exportWiringEndpoint(wiring_admin_pt admin, wiring_endpoint_description_pt* wEndpointDescription) { celix_status_t status = CELIX_SUCCESS; celixThreadMutex_lock(&admin->exportedWiringEndpointLock); if (hashMap_size(admin->wiringReceiveTracker) == 0) { status = wiringAdmin_startWebserver(admin->context, &admin); } if (status == CELIX_SUCCESS) { char* fwuuid = NULL; status = bundleContext_getProperty(admin->context, OSGI_FRAMEWORK_FRAMEWORK_UUID, &fwuuid); if (status == CELIX_SUCCESS) { char* wireId = NULL; properties_pt props = properties_create(); printf("%s: HTTP Wiring Endpoint running at %s\n", TAG, admin->url); status = wiringEndpointDescription_create(NULL, props, wEndpointDescription); properties_set(props, WIRING_ADMIN_PROPERTIES_CONFIG_KEY, WIRING_ADMIN_PROPERTIES_CONFIG_VALUE); properties_set(props, WIRING_ENDPOINT_DESCRIPTION_HTTP_URL_KEY, admin->url); properties_set(props, (char*) OSGI_RSA_ENDPOINT_FRAMEWORK_UUID, fwuuid); wireId = properties_get(props, WIRING_ENDPOINT_DESCRIPTION_WIRE_ID_KEY); printf("%s: wiringEndpointDescription_create w/ wireId %s started\n", TAG, wireId); if (status == CELIX_SUCCESS) { service_tracker_pt tracker = NULL; status = wiringAdmin_createWiringReceiveTracker(admin, &tracker, wireId); if (status == CELIX_SUCCESS) { status = serviceTracker_open(tracker); if (status == CELIX_SUCCESS) { hashMap_put(admin->wiringReceiveTracker, *wEndpointDescription, tracker); printf("%s: WiringReceiveTracker w/ wireId %s started\n", TAG, wireId); } else { serviceTracker_destroy(tracker); } } } } } else { printf("%s: Cannot export Wiring Endpoint\n", TAG); } celixThreadMutex_unlock(&admin->exportedWiringEndpointLock); return status; }
// note that the rootNode shouldn't have a leading slash static celix_status_t etcdWatcher_getRootPath(bundle_context_pt context, char* rootNode) { celix_status_t status = CELIX_SUCCESS; char* rootPath = NULL; if (((bundleContext_getProperty(context, CFG_ETCD_ROOT_PATH, &rootPath)) != CELIX_SUCCESS) || (!rootPath)) { strncpy(rootNode, DEFAULT_ETCD_ROOTPATH, MAX_ROOTNODE_LENGTH); } else { strncpy(rootNode, rootPath, MAX_ROOTNODE_LENGTH); } return status; }
static celix_status_t etcdWatcher_addOwnFramework(etcd_watcher_pt watcher) { celix_status_t status = CELIX_BUNDLE_EXCEPTION; char localNodePath[MAX_LOCALNODE_LENGTH]; char value[MAX_VALUE_LENGTH]; char action[MAX_VALUE_LENGTH]; char url[MAX_VALUE_LENGTH]; int modIndex; char* endpoints = NULL; char* ttlStr = NULL; int ttl; bundle_context_pt context = watcher->discovery->context; endpoint_discovery_server_pt server = watcher->discovery->server; // register own framework if ((status = etcdWatcher_getLocalNodePath(context, &localNodePath[0])) != CELIX_SUCCESS) { return status; } if (endpointDiscoveryServer_getUrl(server, &url[0]) != CELIX_SUCCESS) { snprintf(url, MAX_VALUE_LENGTH, "http://%s:%s/%s", DEFAULT_SERVER_IP, DEFAULT_SERVER_PORT, DEFAULT_SERVER_PATH); } endpoints = &url[0]; if ((bundleContext_getProperty(context, CFG_ETCD_TTL, &ttlStr) != CELIX_SUCCESS) || !ttlStr) { ttl = DEFAULT_ETCD_TTL; } else { char* endptr = ttlStr; errno = 0; ttl = strtol(ttlStr, &endptr, 10); if (*endptr || errno != 0) { ttl = DEFAULT_ETCD_TTL; } } if (etcd_get(localNodePath, &value[0], &action[0], &modIndex) != true) { etcd_set(localNodePath, endpoints, ttl, false); } else if (etcd_set(localNodePath, endpoints, ttl, true) == false) { logHelper_log(*watcher->loghelper, OSGI_LOGSERVICE_WARNING, "Cannot register local discovery"); } else { status = CELIX_SUCCESS; } return status; }
celix_status_t etcdWatcher_addOwnNode(etcd_watcher_pt watcher) { celix_status_t status = CELIX_BUNDLE_EXCEPTION; char localNodePath[MAX_LOCALNODE_LENGTH]; char* ttlStr = NULL; int ttl; node_discovery_pt node_discovery = watcher->node_discovery; bundle_context_pt context = node_discovery->context; node_description_pt ownNodeDescription = node_discovery->ownNode; // register own framework status = etcdWatcher_getLocalNodePath(context, ownNodeDescription, &localNodePath[0]); // determine ttl if ((bundleContext_getProperty(context, CFG_ETCD_TTL, &ttlStr) != CELIX_SUCCESS) || !ttlStr) { ttl = DEFAULT_ETCD_TTL; } else { char* endptr = ttlStr; errno = 0; ttl = strtol(ttlStr, &endptr, 10); if (*endptr || errno != 0) { ttl = DEFAULT_ETCD_TTL; } } // register every wiring endpoint array_list_iterator_pt endpointIter = arrayListIterator_create(ownNodeDescription->wiring_ep_descriptions_list); while (status == CELIX_SUCCESS && arrayListIterator_hasNext(endpointIter)) { wiring_endpoint_description_pt wiringEndpointDesc = arrayListIterator_next(endpointIter); if (wiringEndpointDesc == NULL) { status = CELIX_ILLEGAL_ARGUMENT; } else { char etcdKey[MAX_LOCALNODE_LENGTH]; char etcdValue[MAX_LOCALNODE_LENGTH]; char* wireId = properties_get(wiringEndpointDesc->properties, WIRING_ENDPOINT_DESCRIPTION_WIRE_ID_KEY); wiringEndpoint_properties_store(wiringEndpointDesc->properties, &etcdValue[0]); snprintf(etcdKey, MAX_LOCALNODE_LENGTH, "%s/%s", localNodePath, wireId); // TODO : implement update etcd_set(etcdKey, etcdValue, ttl, false); } } arrayListIterator_destroy(endpointIter); return status; }
celix_status_t bundleActivator_start(void * userData, bundle_context_pt context) { celix_status_t status = CELIX_SUCCESS; struct activator *activator = userData; char *uuid = NULL; status = bundleContext_getProperty(context, OSGI_FRAMEWORK_FRAMEWORK_UUID, &uuid); if (!uuid) { printf("DISCOVERY_CONFIGURED: no framework UUID defined?!\n"); return CELIX_ILLEGAL_STATE; } size_t len = 11 + strlen(OSGI_FRAMEWORK_OBJECTCLASS) + strlen(OSGI_RSA_ENDPOINT_FRAMEWORK_UUID) + strlen(uuid); char *scope = malloc(len); if (!scope) { return CELIX_ENOMEM; } sprintf(scope, "(&(%s=*)(%s=%s))", OSGI_FRAMEWORK_OBJECTCLASS, OSGI_RSA_ENDPOINT_FRAMEWORK_UUID, uuid); scope[len] = 0; printf("DISCOVERY_CONFIGURED: using scope %s.\n", scope); endpoint_listener_pt endpointListener = malloc(sizeof(struct endpoint_listener)); if (!endpointListener) { return CELIX_ENOMEM; } endpointListener->handle = activator->discovery; endpointListener->endpointAdded = discovery_endpointAdded; endpointListener->endpointRemoved = discovery_endpointRemoved; properties_pt props = properties_create(); properties_set(props, "DISCOVERY", "true"); properties_set(props, (char *) OSGI_ENDPOINT_LISTENER_SCOPE, scope); status = bundleContext_registerService(context, (char *) OSGI_ENDPOINT_LISTENER_SERVICE, endpointListener, props, &activator->endpointListenerService); if (status == CELIX_SUCCESS) { status = serviceTracker_open(activator->endpointListenerTracker); } if (status == CELIX_SUCCESS) { status = discovery_start(activator->discovery); } // We can release the scope, as properties_set makes a copy of the key & value... free(scope); return status; }
celix_status_t bundleActivator_start(void * userData, bundle_context_pt context) { celix_status_t status; struct disc_mock_activator * act = userData; const char *uuid = NULL; act->reg = NULL; status = bundleContext_registerService(context, DISC_MOCK_SERVICE_NAME, act->serv, NULL, &act->reg); bundleContext_getProperty(context, OSGI_FRAMEWORK_FRAMEWORK_UUID, &uuid); if (!uuid) { return CELIX_ILLEGAL_STATE; } size_t len = 11 + strlen(OSGI_FRAMEWORK_OBJECTCLASS) + strlen(OSGI_RSA_ENDPOINT_FRAMEWORK_UUID) + strlen(uuid); char *scope = malloc(len + 1); if (!scope) { return CELIX_ENOMEM; } sprintf(scope, "(&(%s=*)(%s=%s))", OSGI_FRAMEWORK_OBJECTCLASS, OSGI_RSA_ENDPOINT_FRAMEWORK_UUID, uuid); scope[len] = 0; properties_pt props = properties_create(); properties_set(props, "DISCOVERY", "true"); properties_set(props, (char *) OSGI_ENDPOINT_LISTENER_SCOPE, scope); if (status == CELIX_SUCCESS) { endpoint_listener_pt endpointListener = calloc(1, sizeof(struct endpoint_listener)); if (endpointListener) { endpointListener->handle = act; endpointListener->endpointAdded = discovery_endpointAdded; endpointListener->endpointRemoved = discovery_endpointRemoved; status = bundleContext_registerService(context, (char *) OSGI_ENDPOINT_LISTENER_SERVICE, endpointListener, props, &act->endpointListenerService); if (status == CELIX_SUCCESS) { act->endpointListener = endpointListener; } else { free(endpointListener); } } } // We can release the scope, as properties_set makes a copy of the key & value... free(scope); return status; }
static celix_status_t etcdWatcher_getLocalNodePath(bundle_context_pt context, char* localNodePath) { celix_status_t status = CELIX_SUCCESS; char rootPath[MAX_ROOTNODE_LENGTH]; char* uuid = NULL; if ((etcdWatcher_getRootPath(context, &rootPath[0]) != CELIX_SUCCESS)) { status = CELIX_ILLEGAL_STATE; } else if (((bundleContext_getProperty(context, OSGI_FRAMEWORK_FRAMEWORK_UUID, &uuid)) != CELIX_SUCCESS) || (!uuid)) { status = CELIX_ILLEGAL_STATE; } else if (rootPath[strlen(&rootPath[0]) - 1] == '/') { snprintf(localNodePath, MAX_LOCALNODE_LENGTH, "%s%s", &rootPath[0], uuid); } else { snprintf(localNodePath, MAX_LOCALNODE_LENGTH, "%s/%s", &rootPath[0], uuid); } return status; }
static int bundleActivator_getProperty(bundle_instance_pt bi, bundle_context_pt context, char* propertyName, int defaultValue) { char *strValue = NULL; int value; bundleContext_getProperty(context, propertyName, &strValue); if (strValue != NULL) { char* endptr = strValue; errno = 0; value = strtol(strValue, &endptr, 10); if (*endptr || errno != 0) { logHelper_log(bi->loghelper, OSGI_LOGSERVICE_WARNING, "incorrect format for %s", propertyName); value = defaultValue; } } else { value = defaultValue; } return value; }
celix_status_t wiringAdmin_startWebserver(bundle_context_pt context, wiring_admin_pt *admin) { celix_status_t status = CELIX_SUCCESS; unsigned int port_counter = 0; char *port = NULL; char *ip = NULL; char *detectedIp = NULL; bundleContext_getProperty(context, NODE_DISCOVERY_NODE_WA_PORT, &port); if (port == NULL) { port = (char *) DEFAULT_WA_PORT; } bundleContext_getProperty(context, NODE_DISCOVERY_NODE_WA_ADDRESS, &ip); if (ip == NULL) { char *interface = NULL; bundleContext_getProperty(context, NODE_DISCOVERY_NODE_WA_ITF, &interface); if ((interface != NULL) && (wiring_getIpAddress(interface, &detectedIp) != CELIX_SUCCESS)) { printf("%s: Could not retrieve IP adress for interface %s\n", TAG, interface); } if (ip == NULL) { wiring_getIpAddress(NULL, &detectedIp); } ip = detectedIp; } // Prepare callbacks structure. We have only one callback, the rest are NULL. struct mg_callbacks callbacks; memset(&callbacks, 0, sizeof(callbacks)); callbacks.begin_request = wiringAdmin_callback; do { char newPort[10]; const char *options[] = { "listening_ports", port, NULL }; (*admin)->ctx = mg_start(&callbacks, (*admin), options); if ((*admin)->ctx == NULL) { char* endptr = port; int currentPort = strtol(port, &endptr, 10); errno = 0; if (*endptr || errno != 0) { currentPort = strtol(DEFAULT_WA_PORT, NULL, 10); } port_counter++; snprintf(&newPort[0], 6, "%d", (currentPort + 1)); printf("%s: Error while starting WIRING_ADMIN server on port %s - retrying on port %s...\n", TAG, port, newPort); port = newPort; } } while (((*admin)->ctx == NULL) && (port_counter < MAX_NUMBER_OF_RESTARTS)); if (ip != NULL) { snprintf((*admin)->url, MAX_URL_LENGTH, "http://%s:%s", ip, port); } else { printf("%s: No IP address for HTTP Wiring Endpint set. Using %s\n", TAG, DEFAULT_WA_ADDRESS); snprintf((*admin)->url, MAX_URL_LENGTH, "http://%s:%s", (char*) DEFAULT_WA_ADDRESS, port); } if (detectedIp != NULL) { free(detectedIp); } return status; }
celix_status_t pubsubAdmin_create(bundle_context_pt context, pubsub_admin_pt *admin) { celix_status_t status = CELIX_SUCCESS; #ifdef BUILD_WITH_ZMQ_SECURITY if (!zsys_has_curve()){ printf("PSA_ZMQ: zeromq curve unsupported\n"); return CELIX_SERVICE_EXCEPTION; } #endif *admin = calloc(1, sizeof(**admin)); if (!*admin) { status = CELIX_ENOMEM; } else{ const char *ip = NULL; char *detectedIp = NULL; (*admin)->bundle_context= context; (*admin)->localPublications = hashMap_create(utils_stringHash, NULL, utils_stringEquals, NULL); (*admin)->subscriptions = hashMap_create(utils_stringHash, NULL, utils_stringEquals, NULL); (*admin)->pendingSubscriptions = hashMap_create(utils_stringHash, NULL, utils_stringEquals, NULL); (*admin)->externalPublications = hashMap_create(utils_stringHash, NULL, utils_stringEquals, NULL); (*admin)->topicSubscriptionsPerSerializer = hashMap_create(NULL, NULL, NULL, NULL); (*admin)->topicPublicationsPerSerializer = hashMap_create(NULL, NULL, NULL, NULL); arrayList_create(&((*admin)->noSerializerSubscriptions)); arrayList_create(&((*admin)->noSerializerPublications)); arrayList_create(&((*admin)->serializerList)); celixThreadMutex_create(&(*admin)->localPublicationsLock, NULL); celixThreadMutex_create(&(*admin)->subscriptionsLock, NULL); celixThreadMutex_create(&(*admin)->externalPublicationsLock, NULL); celixThreadMutex_create(&(*admin)->serializerListLock, NULL); celixThreadMutex_create(&(*admin)->usedSerializersLock, NULL); celixThreadMutexAttr_create(&(*admin)->noSerializerPendingsAttr); celixThreadMutexAttr_settype(&(*admin)->noSerializerPendingsAttr, CELIX_THREAD_MUTEX_RECURSIVE); celixThreadMutex_create(&(*admin)->noSerializerPendingsLock, &(*admin)->noSerializerPendingsAttr); celixThreadMutexAttr_create(&(*admin)->pendingSubscriptionsAttr); celixThreadMutexAttr_settype(&(*admin)->pendingSubscriptionsAttr, CELIX_THREAD_MUTEX_RECURSIVE); celixThreadMutex_create(&(*admin)->pendingSubscriptionsLock, &(*admin)->pendingSubscriptionsAttr); if (logHelper_create(context, &(*admin)->loghelper) == CELIX_SUCCESS) { logHelper_start((*admin)->loghelper); } bundleContext_getProperty(context,PSA_IP , &ip); #ifndef ANDROID if (ip == NULL) { const char *interface = NULL; bundleContext_getProperty(context, PSA_ITF, &interface); if (pubsubAdmin_getIpAdress(interface, &detectedIp) != CELIX_SUCCESS) { logHelper_log((*admin)->loghelper, OSGI_LOGSERVICE_WARNING, "PSA_ZMQ: Could not retrieve IP adress for interface %s", interface); } ip = detectedIp; } #endif if (ip != NULL) { logHelper_log((*admin)->loghelper, OSGI_LOGSERVICE_INFO, "PSA_ZMQ: Using %s for service annunciation", ip); (*admin)->ipAddress = strdup(ip); } else { logHelper_log((*admin)->loghelper, OSGI_LOGSERVICE_WARNING, "PSA_ZMQ: No IP address for service annunciation set. Using %s", DEFAULT_IP); (*admin)->ipAddress = strdup(DEFAULT_IP); } if (detectedIp != NULL) { free(detectedIp); } const char* basePortStr = NULL; const char* maxPortStr = NULL; char* endptrBase = NULL; char* endptrMax = NULL; bundleContext_getPropertyWithDefault(context, PSA_ZMQ_BASE_PORT, "PSA_ZMQ_DEFAULT_BASE_PORT", &basePortStr); bundleContext_getPropertyWithDefault(context, PSA_ZMQ_MAX_PORT, "PSA_ZMQ_DEFAULT_MAX_PORT", &maxPortStr); (*admin)->basePort = strtol(basePortStr, &endptrBase, 10); (*admin)->maxPort = strtol(maxPortStr, &endptrMax, 10); if (*endptrBase != '\0') { (*admin)->basePort = PSA_ZMQ_DEFAULT_BASE_PORT; } if (*endptrMax != '\0') { (*admin)->maxPort = PSA_ZMQ_DEFAULT_MAX_PORT; } printf("PSA Using base port %u to max port %u\n", (*admin)->basePort, (*admin)->maxPort); // Disable Signal Handling by CZMQ setenv("ZSYS_SIGHANDLER", "false", true); const char *nrZmqThreads = NULL; bundleContext_getProperty(context, "PSA_NR_ZMQ_THREADS", &nrZmqThreads); if(nrZmqThreads != NULL) { char *endPtr = NULL; unsigned int nrThreads = strtoul(nrZmqThreads, &endPtr, 10); if(endPtr != nrZmqThreads && nrThreads > 0 && nrThreads < 50) { zsys_set_io_threads(nrThreads); logHelper_log((*admin)->loghelper, OSGI_LOGSERVICE_INFO, "PSA_ZMQ: Using %d threads for ZMQ", nrThreads); printf("PSA_ZMQ: Using %d threads for ZMQ\n", nrThreads); } } #ifdef BUILD_WITH_ZMQ_SECURITY // Setup authenticator zactor_t* auth = zactor_new (zauth, NULL); zstr_sendx(auth, "VERBOSE", NULL); // Load all public keys of subscribers into the application // This step is done for authenticating subscribers char curve_folder_path[MAX_KEY_FOLDER_PATH_LENGTH]; char* keys_bundle_dir = pubsub_getKeysBundleDir(context); snprintf(curve_folder_path, MAX_KEY_FOLDER_PATH_LENGTH, "%s/META-INF/keys/subscriber/public", keys_bundle_dir); zstr_sendx (auth, "CURVE", curve_folder_path, NULL); free(keys_bundle_dir); (*admin)->zmq_auth = auth; #endif } return status; }
celix_status_t remoteServiceAdmin_create(bundle_context_pt context, remote_service_admin_pt *admin) { celix_status_t status = CELIX_SUCCESS; *admin = calloc(1, sizeof(**admin)); if (!*admin) { status = CELIX_ENOMEM; } else { unsigned int port_counter = 0; char *port = NULL; char *ip = NULL; char *detectedIp = NULL; (*admin)->context = context; (*admin)->exportedServices = hashMap_create(NULL, NULL, NULL, NULL); arrayList_create(&(*admin)->importedServices); celixThreadMutex_create(&(*admin)->exportedServicesLock, NULL); celixThreadMutex_create(&(*admin)->importedServicesLock, NULL); if (logHelper_create(context, &(*admin)->loghelper) == CELIX_SUCCESS) { logHelper_start((*admin)->loghelper); dynCommon_logSetup((void *)remoteServiceAdmin_log, *admin, 1); dynType_logSetup((void *)remoteServiceAdmin_log, *admin, 1); dynFunction_logSetup((void *)remoteServiceAdmin_log, *admin, 1); dynInterface_logSetup((void *)remoteServiceAdmin_log, *admin, 1); jsonSerializer_logSetup((void *)remoteServiceAdmin_log, *admin, 1); jsonRpc_logSetup((void *)remoteServiceAdmin_log, *admin, 1); } bundleContext_getProperty(context, "RSA_PORT", &port); if (port == NULL) { port = (char *)DEFAULT_PORT; } bundleContext_getProperty(context, "RSA_IP", &ip); if (ip == NULL) { char *interface = NULL; bundleContext_getProperty(context, "RSA_INTERFACE", &interface); if ((interface != NULL) && (remoteServiceAdmin_getIpAdress(interface, &detectedIp) != CELIX_SUCCESS)) { logHelper_log((*admin)->loghelper, OSGI_LOGSERVICE_WARNING, "RSA: Could not retrieve IP adress for interface %s", interface); } if (ip == NULL) { remoteServiceAdmin_getIpAdress(NULL, &detectedIp); } ip = detectedIp; } if (ip != NULL) { logHelper_log((*admin)->loghelper, OSGI_LOGSERVICE_INFO, "RSA: Using %s for service annunciation", ip); (*admin)->ip = strdup(ip); } else { logHelper_log((*admin)->loghelper, OSGI_LOGSERVICE_WARNING, "RSA: No IP address for service annunciation set. Using %s", DEFAULT_IP); (*admin)->ip = strdup((char*) DEFAULT_IP); } if (detectedIp != NULL) { free(detectedIp); } // Prepare callbacks structure. We have only one callback, the rest are NULL. struct mg_callbacks callbacks; memset(&callbacks, 0, sizeof(callbacks)); callbacks.begin_request = remoteServiceAdmin_callback; do { char newPort[10]; const char *options[] = { "listening_ports", port, "num_threads", "5", NULL}; (*admin)->ctx = mg_start(&callbacks, (*admin), options); if ((*admin)->ctx != NULL) { logHelper_log((*admin)->loghelper, OSGI_LOGSERVICE_INFO, "RSA: Start webserver: %s", port); (*admin)->port = strdup(port); } else { char* endptr = port; int currentPort = strtol(port, &endptr, 10); errno = 0; if (*endptr || errno != 0) { currentPort = strtol(DEFAULT_PORT, NULL, 10); } port_counter++; snprintf(&newPort[0], 6, "%d", (currentPort+1)); logHelper_log((*admin)->loghelper, OSGI_LOGSERVICE_ERROR, "Error while starting rsa server on port %s - retrying on port %s...", port, newPort); port = newPort; } } while(((*admin)->ctx == NULL) && (port_counter < MAX_NUMBER_OF_RESTARTS)); } return status; }
celix_status_t pubsubAdmin_removePublication(pubsub_admin_pt admin,pubsub_endpoint_pt pubEP){ celix_status_t status = CELIX_SUCCESS; int count = 0; printf("PSA_ZMQ: Removing publication [FWUUID=%s bundleID=%ld topic=%s]\n",pubEP->frameworkUUID,pubEP->serviceID,pubEP->topic); const char* fwUUID = NULL; bundleContext_getProperty(admin->bundle_context,OSGI_FRAMEWORK_FRAMEWORK_UUID,&fwUUID); if(fwUUID==NULL){ printf("PSA_ZMQ: Cannot retrieve fwUUID.\n"); return CELIX_INVALID_BUNDLE_CONTEXT; } char *scope_topic = createScopeTopicKey(pubEP->scope, pubEP->topic); if(strcmp(pubEP->frameworkUUID,fwUUID)==0){ celixThreadMutex_lock(&admin->localPublicationsLock); service_factory_pt factory = (service_factory_pt)hashMap_get(admin->localPublications,scope_topic); if(factory!=NULL){ topic_publication_pt pub = (topic_publication_pt)factory->handle; pubsub_topicPublicationRemovePublisherEP(pub,pubEP); } celixThreadMutex_unlock(&admin->localPublicationsLock); if(factory==NULL){ /* Maybe the endpoint was pending */ celixThreadMutex_lock(&admin->noSerializerPendingsLock); if(!arrayList_removeElement(admin->noSerializerPublications, pubEP)){ status = CELIX_ILLEGAL_STATE; } celixThreadMutex_unlock(&admin->noSerializerPendingsLock); } } else{ celixThreadMutex_lock(&admin->externalPublicationsLock); array_list_pt ext_pub_list = (array_list_pt)hashMap_get(admin->externalPublications,scope_topic); if(ext_pub_list!=NULL){ int i; bool found = false; for(i=0;!found && i<arrayList_size(ext_pub_list);i++){ pubsub_endpoint_pt p = (pubsub_endpoint_pt)arrayList_get(ext_pub_list,i); found = pubsubEndpoint_equals(pubEP,p); if (found){ arrayList_remove(ext_pub_list,i); } } // Check if there are more publishers on the same endpoint (happens when 1 celix-instance with multiple bundles publish in same topic) for(i=0; i<arrayList_size(ext_pub_list);i++) { pubsub_endpoint_pt p = (pubsub_endpoint_pt)arrayList_get(ext_pub_list,i); if (strcmp(pubEP->endpoint,p->endpoint) == 0) { count++; } } if(arrayList_size(ext_pub_list)==0){ hash_map_entry_pt entry = hashMap_getEntry(admin->externalPublications,scope_topic); char* topic = (char*)hashMapEntry_getKey(entry); array_list_pt list = (array_list_pt)hashMapEntry_getValue(entry); hashMap_remove(admin->externalPublications,topic); arrayList_destroy(list); free(topic); } } celixThreadMutex_unlock(&admin->externalPublicationsLock); } /* Check if this publisher was connected to one of our subscribers*/ celixThreadMutex_lock(&admin->subscriptionsLock); topic_subscription_pt sub = (topic_subscription_pt)hashMap_get(admin->subscriptions,scope_topic); if(sub!=NULL && pubEP->endpoint!=NULL && count == 0){ pubsub_topicSubscriptionAddDisconnectPublisherToPendingList(sub,pubEP->endpoint); } /* And check also for ANY subscription */ topic_subscription_pt any_sub = (topic_subscription_pt)hashMap_get(admin->subscriptions,PUBSUB_ANY_SUB_TOPIC); if(any_sub!=NULL && pubEP->endpoint!=NULL && count == 0){ pubsub_topicSubscriptionAddDisconnectPublisherToPendingList(any_sub,pubEP->endpoint); } free(scope_topic); celixThreadMutex_unlock(&admin->subscriptionsLock); return status; }
celix_status_t bundleActivator_start(void * userData, bundle_context_pt context) { celix_status_t status; struct activator *activator = userData; endpoint_listener_pt endpointListener = malloc(sizeof(*endpointListener)); endpointListener->handle = activator->manager; endpointListener->endpointAdded = topologyManager_addImportedService; endpointListener->endpointRemoved = topologyManager_removeImportedService; activator->endpointListener = endpointListener; char *uuid = NULL; status = bundleContext_getProperty(activator->context, (char *) OSGI_FRAMEWORK_FRAMEWORK_UUID, &uuid); if (!uuid) { logHelper_log(activator->loghelper, OSGI_LOGSERVICE_ERROR, "TOPOLOGY_MANAGER: no framework UUID defined?!"); return CELIX_ILLEGAL_STATE; } size_t len = 14 + strlen(OSGI_FRAMEWORK_OBJECTCLASS) + strlen(OSGI_RSA_ENDPOINT_FRAMEWORK_UUID) + strlen(uuid); char *scope = malloc(len); if (!scope) { return CELIX_ENOMEM; } snprintf(scope, len, "(&(%s=*)(!(%s=%s)))", OSGI_FRAMEWORK_OBJECTCLASS, OSGI_RSA_ENDPOINT_FRAMEWORK_UUID, uuid); logHelper_log(activator->loghelper, OSGI_LOGSERVICE_INFO, "TOPOLOGY_MANAGER: endpoint listener scope is %s", scope); properties_pt props = properties_create(); properties_set(props, (char *) OSGI_ENDPOINT_LISTENER_SCOPE, scope); // We can release the scope, as properties_set makes a copy of the key & value... free(scope); bundleContext_registerService(context, (char *) OSGI_ENDPOINT_LISTENER_SERVICE, endpointListener, props, &activator->endpointListenerService); listener_hook_service_pt hookService = malloc(sizeof(*hookService)); hookService->handle = activator->manager; hookService->added = topologyManager_listenerAdded; hookService->removed = topologyManager_listenerRemoved; activator->hookService = hookService; bundleContext_registerService(context, (char *) OSGI_FRAMEWORK_LISTENER_HOOK_SERVICE_NAME, hookService, NULL, &activator->hook); bundleContext_addServiceListener(context, activator->serviceListener, "(service.exported.interfaces=*)"); if (status == CELIX_SUCCESS) { serviceTracker_open(activator->remoteServiceAdminTracker); } if (status == CELIX_SUCCESS) { status = serviceTracker_open(activator->endpointListenerTracker); } bundleContext_registerService(context, (char *) TOPOLOGYMANAGER_SCOPE_SERVICE, activator->scopeService, NULL, &activator->scopeReg); array_list_pt references = NULL; bundleContext_getServiceReferences(context, NULL, "(service.exported.interfaces=*)", &references); int i; for (i = 0; i < arrayList_size(references); i++) { service_reference_pt reference = arrayList_get(references, i); char *serviceId = NULL; status = CELIX_DO_IF(status, serviceReference_getProperty(reference, (char *)OSGI_FRAMEWORK_SERVICE_ID, &serviceId)); CELIX_DO_IF(status, topologyManager_addExportedService(activator->manager, reference, serviceId)); } arrayList_destroy(references); return status; }
/** * Allocates memory and initializes a new endpoint_discovery_poller instance. */ celix_status_t endpointDiscoveryPoller_create(discovery_pt discovery, bundle_context_pt context, endpoint_discovery_poller_pt *poller) { celix_status_t status; *poller = malloc(sizeof(struct endpoint_discovery_poller)); if (!*poller) { return CELIX_ENOMEM; } (*poller)->loghelper = &discovery->loghelper; status = celixThreadMutex_create(&(*poller)->pollerLock, NULL); if (status != CELIX_SUCCESS) { return status; } char* interval = NULL; status = bundleContext_getProperty(context, DISCOVERY_POLL_INTERVAL, &interval); if (!interval) { interval = DEFAULT_POLL_INTERVAL; } char* endpoints = NULL; status = bundleContext_getProperty(context, DISCOVERY_POLL_ENDPOINTS, &endpoints); if (!endpoints) { endpoints = DEFAULT_POLL_ENDPOINTS; } // we're going to mutate the string with strtok, so create a copy... endpoints = strdup(endpoints); (*poller)->poll_interval = atoi(interval); (*poller)->discovery = discovery; (*poller)->running = false; (*poller)->entries = hashMap_create(utils_stringHash, NULL, utils_stringEquals, NULL); const char* sep = ","; char *save_ptr = NULL; char* tok = strtok_r(endpoints, sep, &save_ptr); while (tok) { endpointDiscoveryPoller_addDiscoveryEndpoint(*poller, utils_stringTrim(tok)); tok = strtok_r(NULL, sep, &save_ptr); } // Clean up after ourselves... free(endpoints); status = celixThreadMutex_lock(&(*poller)->pollerLock); if (status != CELIX_SUCCESS) { return CELIX_BUNDLE_EXCEPTION; } (*poller)->running = true; status = celixThread_create(&(*poller)->pollerThread, NULL, endpointDiscoveryPoller_performPeriodicPoll, *poller); if (status != CELIX_SUCCESS) { return status; } status = celixThreadMutex_unlock(&(*poller)->pollerLock); return status; }
/* * the ectdWatcher needs to have access to the endpoint_discovery_poller and therefore is only * allowed to be created after the endpoint_discovery_poller */ celix_status_t etcdWatcher_create(discovery_pt discovery, bundle_context_pt context, etcd_watcher_pt *watcher) { celix_status_t status = CELIX_SUCCESS; char* etcd_server = NULL; char* etcd_port_string = NULL; int etcd_port = 0; if (discovery == NULL) { return CELIX_BUNDLE_EXCEPTION; } (*watcher) = calloc(1, sizeof(struct etcd_watcher)); if (!*watcher) { return CELIX_ENOMEM; } else { (*watcher)->discovery = discovery; (*watcher)->loghelper = &discovery->loghelper; (*watcher)->entries = hashMap_create(utils_stringHash, NULL, utils_stringEquals, NULL); } if ((bundleContext_getProperty(context, CFG_ETCD_SERVER_IP, &etcd_server) != CELIX_SUCCESS) || !etcd_server) { etcd_server = DEFAULT_ETCD_SERVER_IP; } if ((bundleContext_getProperty(context, CFG_ETCD_SERVER_PORT, &etcd_port_string) != CELIX_SUCCESS) || !etcd_port_string) { etcd_port = DEFAULT_ETCD_SERVER_PORT; } else { char* endptr = etcd_port_string; errno = 0; etcd_port = strtol(etcd_port_string, &endptr, 10); if (*endptr || errno != 0) { etcd_port = DEFAULT_ETCD_SERVER_PORT; } } status = etcd_init(etcd_server, etcd_port); printf(" ININT\n"); if (status == CELIX_SUCCESS) { etcdWatcher_addOwnFramework(*watcher); status = celixThreadMutex_create(&(*watcher)->watcherLock, NULL); printf(" 111\n"); } if (status == CELIX_SUCCESS) { if (celixThreadMutex_lock(&(*watcher)->watcherLock) == CELIX_SUCCESS) { status = celixThread_create(&(*watcher)->watcherThread, NULL, etcdWatcher_run, *watcher); if (status == CELIX_SUCCESS) { printf(" STARTEDTSTARTED\n"); (*watcher)->running = true; } celixThreadMutex_unlock(&(*watcher)->watcherLock); } } printf(" DONEDONE\n"); return status; }
celix_status_t pubsubAdmin_addPublication(pubsub_admin_pt admin, pubsub_endpoint_pt pubEP) { celix_status_t status = CELIX_SUCCESS; printf("PSA_ZMQ: Received publication [FWUUID=%s bundleID=%ld scope=%s, topic=%s]\n", pubEP->frameworkUUID, pubEP->serviceID, pubEP->scope, pubEP->topic); const char* fwUUID = NULL; bundleContext_getProperty(admin->bundle_context, OSGI_FRAMEWORK_FRAMEWORK_UUID, &fwUUID); if (fwUUID == NULL) { printf("PSA_ZMQ: Cannot retrieve fwUUID.\n"); return CELIX_INVALID_BUNDLE_CONTEXT; } char *scope_topic = createScopeTopicKey(pubEP->scope, pubEP->topic); if ((strcmp(pubEP->frameworkUUID, fwUUID) == 0) && (pubEP->endpoint == NULL)) { celixThreadMutex_lock(&admin->localPublicationsLock); service_factory_pt factory = (service_factory_pt) hashMap_get(admin->localPublications, scope_topic); if (factory == NULL) { topic_publication_pt pub = NULL; pubsub_serializer_service_t *best_serializer = NULL; if( (status=pubsubAdmin_getBestSerializer(admin, pubEP, &best_serializer)) == CELIX_SUCCESS){ status = pubsub_topicPublicationCreate(admin->bundle_context, pubEP, best_serializer, admin->ipAddress, admin->basePort, admin->maxPort, &pub); } else{ printf("PSA_ZMQ: Cannot find a serializer for publishing topic %s. Adding it to pending list.\n", pubEP->topic); celixThreadMutex_lock(&admin->noSerializerPendingsLock); arrayList_add(admin->noSerializerPublications,pubEP); celixThreadMutex_unlock(&admin->noSerializerPendingsLock); } if (status == CELIX_SUCCESS) { status = pubsub_topicPublicationStart(admin->bundle_context, pub, &factory); if (status == CELIX_SUCCESS && factory != NULL) { hashMap_put(admin->localPublications, strdup(scope_topic), factory); connectTopicPubSubToSerializer(admin, best_serializer, pub, true); } } else { printf("PSA_ZMQ: Cannot create a topicPublication for scope=%s, topic=%s (bundle %ld).\n", pubEP->scope, pubEP->topic, pubEP->serviceID); } } else { //just add the new EP to the list topic_publication_pt pub = (topic_publication_pt) factory->handle; pubsub_topicPublicationAddPublisherEP(pub, pubEP); } celixThreadMutex_unlock(&admin->localPublicationsLock); } else{ celixThreadMutex_lock(&admin->externalPublicationsLock); array_list_pt ext_pub_list = (array_list_pt) hashMap_get(admin->externalPublications, scope_topic); if (ext_pub_list == NULL) { arrayList_create(&ext_pub_list); hashMap_put(admin->externalPublications, strdup(scope_topic), ext_pub_list); } arrayList_add(ext_pub_list, pubEP); celixThreadMutex_unlock(&admin->externalPublicationsLock); } /* Re-evaluate the pending subscriptions */ celixThreadMutex_lock(&admin->pendingSubscriptionsLock); hash_map_entry_pt pendingSub = hashMap_getEntry(admin->pendingSubscriptions, scope_topic); if (pendingSub != NULL) { //There were pending subscription for the just published topic. Let's connect them. char* topic = (char*) hashMapEntry_getKey(pendingSub); array_list_pt pendingSubList = (array_list_pt) hashMapEntry_getValue(pendingSub); int i; for (i = 0; i < arrayList_size(pendingSubList); i++) { pubsub_endpoint_pt subEP = (pubsub_endpoint_pt) arrayList_get(pendingSubList, i); pubsubAdmin_addSubscription(admin, subEP); } hashMap_remove(admin->pendingSubscriptions, scope_topic); arrayList_clear(pendingSubList); arrayList_destroy(pendingSubList); free(topic); } celixThreadMutex_unlock(&admin->pendingSubscriptionsLock); /* Connect the new publisher to the subscription for his topic, if there is any */ celixThreadMutex_lock(&admin->subscriptionsLock); topic_subscription_pt sub = (topic_subscription_pt) hashMap_get(admin->subscriptions, scope_topic); if (sub != NULL && pubEP->endpoint != NULL) { pubsub_topicSubscriptionAddConnectPublisherToPendingList(sub, pubEP->endpoint); } /* And check also for ANY subscription */ topic_subscription_pt any_sub = (topic_subscription_pt) hashMap_get(admin->subscriptions, PUBSUB_ANY_SUB_TOPIC); if (any_sub != NULL && pubEP->endpoint != NULL) { pubsub_topicSubscriptionAddConnectPublisherToPendingList(any_sub, pubEP->endpoint); } free(scope_topic); celixThreadMutex_unlock(&admin->subscriptionsLock); return status; }