static celix_status_t rsaJsonRpcEndpoint_handleRequest()

in bundles/remote_services/rsa_rpc_json/src/rsa_json_rpc_endpoint_impl.c [207:274]


static celix_status_t rsaJsonRpcEndpoint_handleRequest(void *handle, celix_properties_t *metadata,
        const struct iovec *request, struct iovec *responseOut) {
    celix_status_t status = CELIX_SUCCESS;
    if (handle == NULL || request == NULL || request->iov_base == NULL
            || request->iov_len == 0 || responseOut == NULL || metadata == NULL) {
        return CELIX_ILLEGAL_ARGUMENT;
    }
    responseOut->iov_base = NULL;
    responseOut->iov_len = 0;
    rsa_json_rpc_endpoint_t *endpoint = (rsa_json_rpc_endpoint_t *)handle;

    long serialProtoId  = celix_properties_getAsLong(metadata, "SerialProtocolId", 0);
    if (serialProtoId != endpoint->serialProtoId) {
        celix_logHelper_error(endpoint->logHelper, "Serialization protocol ID mismatch. expect:%ld actual:%u.", serialProtoId, endpoint->serialProtoId);
        return CELIX_ILLEGAL_ARGUMENT;
    }

    json_error_t error;
    json_auto_t* jsRequest = json_loads((char *)request->iov_base, 0, &error);
    if (jsRequest == NULL) {
        celix_logHelper_error(endpoint->logHelper, "Parse request json string failed for %s.", (char *)request->iov_base);
        return CELIX_ILLEGAL_ARGUMENT;
    }
    const char *sig;
    int rc = json_unpack(jsRequest, "{s:s}", "m", &sig);
    if (rc != 0) {
        celix_logHelper_error(endpoint->logHelper, "Error requesting method for %s.", (char *)request->iov_base);
        return CELIX_ILLEGAL_ARGUMENT;
    }

    char *szResponse = NULL;
    bool cont = remoteInterceptorHandler_invokePreExportCall(endpoint->interceptorsHandler,
            endpoint->endpointDesc->properties, sig, &metadata);
    if (cont) {
        celixThreadRwlock_readLock(&endpoint->lock);
        if (endpoint->service != NULL) {
            int rc1 = jsonRpc_call(endpoint->intfType, endpoint->service, (char *)request->iov_base, &szResponse);
            status = (rc1 != 0) ? CELIX_SERVICE_EXCEPTION : CELIX_SUCCESS;
            if (rc1 != 0) {
                celix_logHelper_logTssErrors(endpoint->logHelper, CELIX_LOG_LEVEL_ERROR);
                celix_logHelper_error(endpoint->logHelper, "Error calling remote service. Got error code %d", rc1);
            }
        } else {
            status = CELIX_ILLEGAL_STATE;
            celix_logHelper_error(endpoint->logHelper, "%s is null, please try again.", endpoint->endpointDesc->serviceName);
        }
        celixThreadRwlock_unlock(&endpoint->lock);

        remoteInterceptorHandler_invokePostExportCall(endpoint->interceptorsHandler,
                endpoint->endpointDesc->properties, sig, metadata);
    } else {
        celix_logHelper_error(endpoint->logHelper, "%s has been intercepted.", endpoint->endpointDesc->serviceName);
        status = CELIX_INTERCEPTOR_EXCEPTION;
    }

    if (szResponse != NULL) {
        responseOut->iov_base = szResponse;
        responseOut->iov_len = strlen(szResponse) + 1;// make it include '\0'
    }

    if (endpoint->callsLogFile != NULL) {
        fprintf(endpoint->callsLogFile, "ENDPOINT REMOTE CALL:\n\tservice=%s\n\tservice_id=%lu\n\trequest_payload=%s\n\trequest_response=%s\n\tstatus=%i\n",
                endpoint->endpointDesc->serviceName, endpoint->endpointDesc->serviceId, (char *)request->iov_base, szResponse, status);
        fflush(endpoint->callsLogFile);
    }

    return status;
}