celix_status_t shellMediator_create(bundle_context_pt context, shell_mediator_pt *instance) { celix_status_t status = CELIX_SUCCESS; service_tracker_customizer_pt customizer = NULL; (*instance) = (shell_mediator_pt) calloc(1, sizeof(**instance)); if ((*instance) != NULL) { (*instance)->context = context; (*instance)->tracker = NULL; (*instance)->shellService = NULL; status = logHelper_create(context, &(*instance)->loghelper); status = CELIX_DO_IF(status, celixThreadMutex_create(&(*instance)->mutex, NULL)); status = CELIX_DO_IF(status, serviceTrackerCustomizer_create((*instance), shellMediator_addingService, shellMediator_addedService, shellMediator_modifiedService, shellMediator_removedService, &customizer)); status = CELIX_DO_IF(status, serviceTracker_create(context, (char * )OSGI_SHELL_SERVICE_NAME, customizer, &(*instance)->tracker)); if (status == CELIX_SUCCESS) { logHelper_start((*instance)->loghelper); serviceTracker_open((*instance)->tracker); } } else { status = CELIX_ENOMEM; } if (status != CELIX_SUCCESS) { logHelper_log((*instance)->loghelper, OSGI_LOGSERVICE_ERROR, "Error creating shell_mediator, error code is %i\n", status); } return status; }
celix_status_t bundleActivator_start(void * userData, bundle_context_pt context) { celix_status_t status = CELIX_SUCCESS; struct activator *activator = userData; event_admin_service_pt event_admin_service = NULL; apr_pool_t *pool; status = bundleContext_getMemoryPool(context, &pool); if(status == CELIX_SUCCESS) { struct activator * data = (struct activator *) userData; service_tracker_customizer_pt cust = NULL; service_tracker_pt tracker = NULL; data->context = context; serviceTrackerCustomizer_create(data->event_admin_service->eventAdmin, eventAdmin_addingService, eventAdmin_addedService, eventAdmin_modifiedService, eventAdmin_removedService, &cust); serviceTracker_create(context, (char *) EVENT_HANDLER_SERVICE, cust, &tracker); data->tracker = tracker; serviceTracker_open(tracker); properties_pt properties = NULL; properties = properties_create(); event_admin_service = activator->event_admin_service; //printf("pointer of event admin service %p\n",event_admin_service); bundleContext_registerService(context, (char *) EVENT_ADMIN_NAME, event_admin_service, properties, &activator->registration); logHelper_start(activator->loghelper); } return status; }
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 bundleActivator_create(bundle_context_pt context, void **userData) { celix_status_t status = CELIX_SUCCESS; struct activator *activator = NULL; void *scope; activator = calloc(1, sizeof(struct activator)); if (!activator) { return CELIX_ENOMEM; } activator->context = context; activator->endpointListenerService = NULL; activator->endpointListenerTracker = NULL; activator->hook = NULL; activator->manager = NULL; activator->remoteServiceAdminTracker = NULL; activator->serviceListener = NULL; activator->scopeService = calloc(1, sizeof(*(activator->scopeService))); if (activator->scopeService == NULL) { free(activator); return CELIX_ENOMEM; } activator->scopeService->addExportScope = tm_addExportScope; activator->scopeService->removeExportScope = tm_removeExportScope; activator->scopeService->addImportScope = tm_addImportScope; activator->scopeService->removeImportScope = tm_removeImportScope; activator->scopeReg = NULL; // explicitly needed, otherwise exception logHelper_create(context, &activator->loghelper); logHelper_start(activator->loghelper); status = topologyManager_create(context, activator->loghelper, &activator->manager, &scope); activator->scopeService->handle = scope; if (status == CELIX_SUCCESS) { status = bundleActivator_createEPLTracker(activator, &activator->endpointListenerTracker); if (status == CELIX_SUCCESS) { status = bundleActivator_createRSATracker(activator, &activator->remoteServiceAdminTracker); if (status == CELIX_SUCCESS) { status = bundleActivator_createServiceListener(activator, &activator->serviceListener); if (status == CELIX_SUCCESS) { *userData = activator; } } } } return status; }
celix_status_t bundleActivator_start(void * userData, bundle_context_pt context) { celix_status_t status = CELIX_SUCCESS; bundle_instance_pt bi = (bundle_instance_pt) userData; int port = bundleActivator_getPort(bi, context); int maxConn = bundleActivator_getMaximumConnections(bi, context); status = logHelper_start(bi->loghelper); status = CELIX_DO_IF(status, shellMediator_create(context, &bi->shellMediator)); status = CELIX_DO_IF(status, remoteShell_create(bi->shellMediator, maxConn, &bi->remoteShell)); status = CELIX_DO_IF(status, connectionListener_create(bi->remoteShell, port, &bi->connectionListener)); status = CELIX_DO_IF(status, connectionListener_start(bi->connectionListener)); return status; }
celix_status_t driverMatcher_create(bundle_context_pt context, driver_matcher_pt *matcher) { celix_status_t status = CELIX_SUCCESS; *matcher = calloc(1, sizeof(**matcher)); if (!*matcher) { status = CELIX_ENOMEM; } else { (*matcher)->matches = NULL; (*matcher)->context = context; (*matcher)->attributes = hashMap_create(driverMatcher_matchKeyHash, NULL, driverMatcher_matchKeyEquals, NULL); arrayList_create(&(*matcher)->matches); if(logHelper_create(context, &(*matcher)->loghelper) == CELIX_SUCCESS) { logHelper_start((*matcher)->loghelper); } } return status; }
celix_status_t eventPublisherStart(event_publisher_pt *event_publisher) { (*event_publisher)->running = true; logHelper_start((*event_publisher)->loghelper); apr_thread_create(&(*event_publisher)->sender, NULL, eventPublisherSendEventThread, event_publisher, (*event_publisher)->pool); return CELIX_SUCCESS; }
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; }