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_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_create(bundle_context_pt context, void **userData) { celix_status_t status = CELIX_SUCCESS; apr_pool_t *pool = NULL; apr_pool_t *parentPool; struct activator *activator; status = bundleContext_getMemoryPool(context, &parentPool); if( status == CELIX_SUCCESS ) { if(apr_pool_create(&pool,parentPool) != APR_SUCCESS) { status = CELIX_BUNDLE_EXCEPTION; } else { activator = apr_palloc(pool,sizeof(*activator)); activator->pool = pool; activator->registration = NULL; logHelper_create(context, &activator->loghelper); *userData = activator; event_admin_pt event_admin = NULL; event_admin_service_pt event_admin_service = NULL; status = eventAdmin_create(activator->pool,context, &event_admin); if(status == CELIX_SUCCESS) { activator->event_admin = event_admin; event_admin_service = apr_palloc(activator->pool, sizeof(event_admin_service)); if(!event_admin_service) { status = CELIX_ENOMEM; } else { event_admin->context = context; event_admin->loghelper = &activator->loghelper; event_admin_service->eventAdmin = event_admin; event_admin_service->postEvent = eventAdmin_postEvent; event_admin_service->sendEvent = eventAdmin_sendEvent; event_admin_service->createEvent = eventAdmin_createEvent; event_admin_service->containsProperty = eventAdmin_containsProperty; event_admin_service->event_equals = eventAdmin_event_equals; event_admin_service->getProperty = eventAdmin_getProperty; event_admin_service->getPropertyNames = eventAdmin_getPropertyNames; event_admin_service->getTopic = eventAdmin_getTopic; event_admin_service->hashCode = eventAdmin_hashCode; event_admin_service->matches = eventAdmin_matches; event_admin_service->toString = eventAdmin_toString; } } activator->event_admin_service = event_admin_service; } } return status; }
celix_status_t eventPublisherCreate(apr_pool_t *pool, bundle_context_pt context, event_publisher_pt *event_publisher) { celix_status_t status = CELIX_SUCCESS; *event_publisher = apr_palloc(pool, sizeof(**event_publisher)); if (!*event_publisher) { status = CELIX_ENOMEM; } else { (*event_publisher)->event_admin_service = NULL; (*event_publisher)->pool = NULL; (*event_publisher)->eventAdminAdded = false; (*event_publisher)->running = false; (*event_publisher)->pool = pool; (*event_publisher)->context = context; logHelper_create(context, &(*event_publisher)->loghelper); } return status; }
celix_status_t shell_create(bundle_context_pt context_ptr, shell_service_pt *shell_service_ptr) { celix_status_t status = CELIX_SUCCESS; if (!context_ptr || !shell_service_ptr) { status = CELIX_ILLEGAL_ARGUMENT; } if (status == CELIX_SUCCESS) { *shell_service_ptr = calloc(1, sizeof(**shell_service_ptr)); if (!*shell_service_ptr) { status = CELIX_ENOMEM; } } if (status == CELIX_SUCCESS) { (*shell_service_ptr)->shell = calloc(1, sizeof(*(*shell_service_ptr)->shell)); if (!(*shell_service_ptr)->shell) { status = CELIX_ENOMEM; } } if (status == CELIX_SUCCESS) { (*shell_service_ptr)->shell->bundle_context_ptr = context_ptr; (*shell_service_ptr)->shell->command_name_map_ptr = hashMap_create(utils_stringHash, NULL, utils_stringEquals, NULL); (*shell_service_ptr)->shell->command_reference_map_ptr = hashMap_create(NULL, NULL, NULL, NULL); (*shell_service_ptr)->getCommands = shell_getCommands; (*shell_service_ptr)->getCommandDescription = shell_getCommandDescription; (*shell_service_ptr)->getCommandUsage = shell_getCommandUsage; (*shell_service_ptr)->getCommandReference = shell_getCommandReference; (*shell_service_ptr)->executeCommand = shell_executeCommand; status = logHelper_create(context_ptr, &(*shell_service_ptr)->shell->logHelper); } if (status != CELIX_SUCCESS) { shell_destroy(shell_service_ptr); } 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 discovery_create(bundle_context_pt context, discovery_pt *discovery) { celix_status_t status = CELIX_SUCCESS; *discovery = malloc(sizeof(struct discovery)); if (!*discovery) { return CELIX_ENOMEM; } (*discovery)->context = context; (*discovery)->poller = NULL; (*discovery)->server = NULL; (*discovery)->listenerReferences = hashMap_create(serviceReference_hashCode, NULL, serviceReference_equals2, NULL); (*discovery)->discoveredServices = hashMap_create(utils_stringHash, NULL, utils_stringEquals, NULL); status = celixThreadMutex_create(&(*discovery)->listenerReferencesMutex, NULL); status = celixThreadMutex_create(&(*discovery)->discoveredServicesMutex, NULL); logHelper_create(context, &(*discovery)->loghelper); return status; }
celix_status_t bundleActivator_create(bundle_context_pt context, void **userData) { celix_status_t status = CELIX_SUCCESS; bundle_instance_pt bi = (bundle_instance_pt) calloc(1, sizeof(struct bundle_instance)); if (!bi) { status = CELIX_ENOMEM; } else if (userData != NULL) { bi->shellMediator = NULL; bi->remoteShell = NULL; bi->connectionListener = NULL; status = logHelper_create(context, &bi->loghelper); (*userData) = bi; } else { status = CELIX_ILLEGAL_ARGUMENT; free(bi); } 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; }