static void rsaJsonRpcEndpoint_addSvcWithOwner()

in bundles/remote_services/rsa_rpc_json/src/rsa_json_rpc_endpoint_impl.c [50:124]


static void rsaJsonRpcEndpoint_addSvcWithOwner(void *handle, void *service,
        const celix_properties_t *props, const celix_bundle_t *svcOwner);
static void rsaJsonRpcEndpoint_removeSvcWithOwner(void *handle, void *service,
        const celix_properties_t *props, const celix_bundle_t *svcOwner);
static celix_status_t rsaJsonRpcEndpoint_handleRequest(void *handle, celix_properties_t *metadata,
        const struct iovec *request, struct iovec *responseOut);

celix_status_t rsaJsonRpcEndpoint_create(celix_bundle_context_t* ctx, celix_log_helper_t *logHelper,
        FILE *logFile, remote_interceptors_handler_t *interceptorsHandler,
        const endpoint_description_t *endpointDesc, unsigned int serialProtoId,
        rsa_json_rpc_endpoint_t **endpointOut) {
    assert(ctx != NULL);
    assert(logHelper != NULL);
    assert(interceptorsHandler != NULL);
    assert(endpointDesc != NULL);
    assert(endpointOut != NULL);
    celix_status_t status = CELIX_SUCCESS;
    celix_autofree rsa_json_rpc_endpoint_t* endpoint = calloc(1, sizeof(*endpoint));
    if (endpoint == NULL) {
        return CELIX_ENOMEM;
    }
    endpoint->ctx = ctx;
    endpoint->logHelper = logHelper;
    endpoint->callsLogFile = logFile;
    endpoint->serialProtoId = serialProtoId;
    celix_autoptr(endpoint_description_t) endpointDescCopy = endpoint->endpointDesc = endpointDescription_clone(endpointDesc);
    if (endpoint->endpointDesc == NULL) {
        celix_logHelper_error(logHelper, "RSA json rpc endpoint: Error cloning endpoint description for %s.",
                endpointDesc->serviceName);
        return CELIX_ENOMEM;
    }

    endpoint->interceptorsHandler = interceptorsHandler;
    endpoint->service = NULL;
    endpoint->intfType = NULL;
    status = celixThreadRwlock_create(&endpoint->lock, NULL);
    if (status != CELIX_SUCCESS) {
        celix_logHelper_error(logHelper, "RSA json rpc endpoint: Error initilizing lock for %s. %d.",
                endpointDesc->serviceName, status);
        return status;
    }
    celix_autoptr(celix_thread_rwlock_t) lock = &endpoint->lock;

    char filter[32] = {0};// It is longer than the size of "service.id" + serviceId
    (void)snprintf(filter, sizeof(filter), "(%s=%ld)", CELIX_FRAMEWORK_SERVICE_ID, endpointDesc->serviceId);
    celix_service_tracking_options_t opts = CELIX_EMPTY_SERVICE_TRACKING_OPTIONS;
    opts.filter.filter = filter;
    opts.callbackHandle = endpoint;
    opts.addWithOwner = rsaJsonRpcEndpoint_addSvcWithOwner;
    opts.removeWithOwner = rsaJsonRpcEndpoint_removeSvcWithOwner;
    endpoint->svcTrackerId = celix_bundleContext_trackServicesWithOptionsAsync(endpoint->ctx, &opts);
    if (endpoint->svcTrackerId < 0) {
        celix_logHelper_error(logHelper, "RSA json rpc endpoint: Error Registering %s tracker.", endpointDesc->serviceName);
        return CELIX_ILLEGAL_STATE;
    }

    endpoint->reqHandlerSvc.handle = endpoint;
    endpoint->reqHandlerSvc.handleRequest = rsaJsonRpcEndpoint_handleRequest;
    celix_service_registration_options_t opts1 = CELIX_EMPTY_SERVICE_REGISTRATION_OPTIONS;
    opts1.serviceName = CELIX_RSA_REQUEST_HANDLER_SERVICE_NAME;
    opts1.serviceVersion = CELIX_RSA_REQUEST_HANDLER_SERVICE_VERSION;
    opts1.svc = &endpoint->reqHandlerSvc;
    celix_steal_ptr(lock);
    celix_steal_ptr(endpointDescCopy);
    endpoint->reqHandlerSvcId = celix_bundleContext_registerServiceWithOptionsAsync(endpoint->ctx, &opts1);
    if (endpoint->reqHandlerSvcId< 0) {
        celix_logHelper_error(logHelper, "Error Registering endpoint request handler service for %s.", endpointDesc->serviceName);
        celix_bundleContext_stopTrackerAsync(endpoint->ctx, endpoint->svcTrackerId,
                                             endpoint, rsaJsonRpcEndpoint_stopSvcTrackerDone);
        celix_steal_ptr(endpoint); // endpoint is freed in stopSvcTrackerDone
        return CELIX_ILLEGAL_STATE;
    }
    *endpointOut = celix_steal_ptr(endpoint);
    return CELIX_SUCCESS;
}