You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@celix.apache.org by pn...@apache.org on 2020/03/03 20:10:55 UTC

[celix] 01/01: Refactors the RSA curl read callback function.

This is an automated email from the ASF dual-hosted git repository.

pnoltes pushed a commit to branch feature/slow_rsa
in repository https://gitbox.apache.org/repos/asf/celix.git

commit 3f728350a50ec7e8be20b00b5110d0a522db2e1a
Author: Pepijn Noltes <pe...@gmail.com>
AuthorDate: Tue Mar 3 21:10:35 2020 +0100

    Refactors the RSA curl read callback function.
    
    It was apparently copying the data byte for byte.
---
 .../src/remote_service_admin_dfi.c                 | 46 +++++++++++----------
 .../test/src/rsa_client_server_tests.cpp           | 32 ++++++++-------
 .../test/src/tst_activator.c                       | 48 ++++++++++++++++++----
 3 files changed, 82 insertions(+), 44 deletions(-)

diff --git a/bundles/remote_services/remote_service_admin_dfi/src/remote_service_admin_dfi.c b/bundles/remote_services/remote_service_admin_dfi/src/remote_service_admin_dfi.c
index eb45cf8..9fd6015 100644
--- a/bundles/remote_services/remote_service_admin_dfi/src/remote_service_admin_dfi.c
+++ b/bundles/remote_services/remote_service_admin_dfi/src/remote_service_admin_dfi.c
@@ -28,6 +28,7 @@
 #include <curl/curl.h>
 
 #include <jansson.h>
+#include <assert.h>
 #include "json_serializer.h"
 #include "utils.h"
 
@@ -76,12 +77,13 @@ struct remote_service_admin {
 
 struct post {
     const char *readptr;
-    int size;
+    size_t size;
+    size_t read;
 };
 
 struct get {
     char *writeptr;
-    int size;
+    size_t size;
 };
 
 #define OSGI_RSA_REMOTE_PROXY_FACTORY   "remote_proxy_factory"
@@ -700,16 +702,16 @@ celix_status_t remoteServiceAdmin_removeImportedService(remote_service_admin_t *
     return status;
 }
 
-
 static celix_status_t remoteServiceAdmin_send(void *handle, endpoint_description_t *endpointDescription, char *request, char **reply, int* replyStatus) {
     remote_service_admin_t * rsa = handle;
     struct post post;
     post.readptr = request;
     post.size = strlen(request);
+    post.read = 0;
 
     struct get get;
     get.size = 0;
-    get.writeptr = malloc(1);
+    get.writeptr = NULL;
 
     const char *serviceUrl = celix_properties_get(endpointDescription->properties, (char*) RSA_DFI_ENDPOINT_URL, NULL);
     char url[256];
@@ -760,35 +762,37 @@ static celix_status_t remoteServiceAdmin_send(void *handle, endpoint_description
     return status;
 }
 
-static size_t remoteServiceAdmin_readCallback(void *ptr, size_t size, size_t nmemb, void *userp) {
+static size_t remoteServiceAdmin_readCallback(void *voidBuffer, size_t size, size_t nmemb, void *userp) {
     struct post *post = userp;
+    char *buffer = voidBuffer;
 
-    if (post->size) {
-        *(char *) ptr = post->readptr[0];
-        post->readptr++;
-        post->size--;
-        return 1;
+    if (post->read == post->size) {
+        return 0;
+    } else {
+        size_t buffSize = size * nmemb;
+        size_t startRead = post->read;
+        for (size_t i = 0; i < buffSize && post->size != post->read; ++i) {
+            buffer[i] = post->readptr[post->read++];
+        }
+        return post->read - startRead;
     }
-
-    return 0;
 }
 
 static size_t remoteServiceAdmin_write(void *contents, size_t size, size_t nmemb, void *userp) {
     size_t realsize = size * nmemb;
     struct get *mem = (struct get *)userp;
 
-    mem->writeptr = realloc(mem->writeptr, mem->size + realsize + 1);
+    mem->writeptr =malloc(realsize + 1);
     if (mem->writeptr == NULL) {
         /* out of memory! */
-        printf("not enough memory (realloc returned NULL)");
-        exit(EXIT_FAILURE);
+        fprintf(stderr, "not enough memory (malloc returned NULL)");
+        return 0;
+    } else {
+        memcpy(&(mem->writeptr[mem->size]), contents, realsize);
+        mem->size += realsize;
+        mem->writeptr[mem->size] = 0;
+        return realsize;
     }
-
-    memcpy(&(mem->writeptr[mem->size]), contents, realsize);
-    mem->size += realsize;
-    mem->writeptr[mem->size] = 0;
-
-    return realsize;
 }
 
 
diff --git a/bundles/remote_services/remote_service_admin_dfi/test/src/rsa_client_server_tests.cpp b/bundles/remote_services/remote_service_admin_dfi/test/src/rsa_client_server_tests.cpp
index 3622235..e4b9452 100644
--- a/bundles/remote_services/remote_service_admin_dfi/test/src/rsa_client_server_tests.cpp
+++ b/bundles/remote_services/remote_service_admin_dfi/test/src/rsa_client_server_tests.cpp
@@ -75,29 +75,31 @@ extern "C" {
         bool discovered = tst->isCalcDiscovered(tst->handle);
         CHECK_TRUE(discovered);
 
-//        ok = tst->testCalculator(tst->handle);
-//        CHECK_TRUE(ok);
+        ok = tst->testCalculator(tst->handle);
+        CHECK_TRUE(ok);
 
         discovered = tst->isRemoteExampleDiscovered(tst->handle);
         CHECK_TRUE(discovered);
 
-//        ok = tst->testRemoteString(tst->handle);
-//        CHECK_TRUE(ok);
-//
-//        ok = tst->testRemoteConstString(tst->handle);
-//        CHECK_TRUE(ok);
+        ok = tst->testRemoteComplex(tst->handle);
+        CHECK_TRUE(ok);
+
+        ok = tst->testRemoteAction(tst->handle);
+        CHECK_TRUE(ok);
 
         ok = tst->testRemoteNumbers(tst->handle);
         CHECK_TRUE(ok);
 
-//        ok = tst->testRemoteEnum(tst->handle);
-//        CHECK_TRUE(ok);
-//
-//        ok = tst->testRemoteAction(tst->handle);
-//        CHECK_TRUE(ok);
-//
-//        ok = tst->testRemoteComplex(tst->handle);
-//        CHECK_TRUE(ok);
+        ok = tst->testRemoteString(tst->handle);
+        CHECK_TRUE(ok);
+
+        ok = tst->testRemoteConstString(tst->handle);
+        CHECK_TRUE(ok);
+
+#ifndef __APPLE__ //TODO fix for apple dfi handling, see issue #91
+        ok = tst->testRemoteEnum(tst->handle);
+        CHECK_TRUE(ok);
+#endif
     };
 
     static void test(void) {
diff --git a/bundles/remote_services/remote_service_admin_dfi/test/src/tst_activator.c b/bundles/remote_services/remote_service_admin_dfi/test/src/tst_activator.c
index 5894c73..5c48d18 100644
--- a/bundles/remote_services/remote_service_admin_dfi/test/src/tst_activator.c
+++ b/bundles/remote_services/remote_service_admin_dfi/test/src/tst_activator.c
@@ -19,6 +19,7 @@
 
 #include <stdlib.h>
 #include <string.h>
+#include <time.h>
 
 #include <celix_api.h>
 
@@ -27,6 +28,31 @@
 #include "remote_example.h"
 #include <unistd.h>
 
+static double diffBetweenTimeSpecs(struct timespec *b, struct timespec *e)
+{
+    struct timespec diff;
+    if ((e->tv_nsec - b->tv_nsec) < 0) {
+        diff.tv_sec = e->tv_sec - b->tv_sec - 1;
+        diff.tv_nsec = e->tv_nsec - b->tv_nsec + 1000000000;
+    } else {
+        diff.tv_sec = e->tv_sec - b->tv_sec;
+        diff.tv_nsec = e->tv_nsec - b->tv_nsec;
+    }
+
+    return ((double)diff.tv_sec) + diff.tv_nsec /  1000000000.0;
+}
+
+//note exports double diff variable (time in ms)
+#define TIMED_EXPR(expr) \
+    double diff; \
+    do { \
+        struct timespec _begin, _end; \
+        clock_gettime(CLOCK_MONOTONIC, &_begin); \
+        expr; \
+        clock_gettime(CLOCK_MONOTONIC, &_end); \
+        diff = diffBetweenTimeSpecs(&_begin, &_end) * 1000.0; \
+    } while(0)
+
 struct activator {
     long svcId;
     struct tst_service testSvc;
@@ -105,8 +131,8 @@ static bool bndTestCalculator(void *handle) {
     pthread_mutex_lock(&act->mutex);
     int rc = 1;
     if (act->calc != NULL) {
-        rc = act->calc->sqrt(act->calc->handle, 4, &result);
-        printf("calc result is %f\n", result);
+        TIMED_EXPR(rc = act->calc->sqrt(act->calc->handle, 4, &result));
+        printf("calc result is %f. Call took %f ms\n", result, diff);
     } else {
         printf("calc not ready\n");
     }
@@ -121,10 +147,11 @@ static bool bndTestRemoteString(void *handle) {
 
     pthread_mutex_lock(&act->mutex);
     if (act->remoteExample != NULL) {
-        //test string call with taking ownership
+        //test string Call with taking ownership
         char *tmp = strndup("test1", 1024);
         char *result = NULL;
-        act->remoteExample->setName1(act->remoteExample->handle, tmp, &result);
+        TIMED_EXPR(act->remoteExample->setName1(act->remoteExample->handle, tmp, &result));
+        printf("Call setName1 took %f ms\n", diff);
         //note setName1 should take ownership of tmp, so no free(tmp) needed.
         ok = strncmp("test1", result, 1024) == 0;
         free(result);
@@ -146,7 +173,8 @@ static bool bndTestRemoteConstString(void *handle) {
         //test pow
         const char *name = "name2";
         char *result = NULL;
-        act->remoteExample->setName2(act->remoteExample->handle, name, &result);
+        TIMED_EXPR(act->remoteExample->setName2(act->remoteExample->handle, name, &result));
+        printf("Call setName2 took %f ms\n", diff);
         ok = strncmp(result, "name2", 1024) == 0;
         free(result);
     } else {
@@ -168,14 +196,16 @@ static bool bndTestRemoteNumbers(void *handle) {
         if (ok) {
             //test pow
             double p;
-            act->remoteExample->pow(act->remoteExample->handle, 2, 2, &p);
+            TIMED_EXPR(act->remoteExample->pow(act->remoteExample->handle, 2, 2, &p));
+            printf("Call pow took %f ms\n", diff);
             ok = (p == 4.0);
         }
 
         if (ok) {
             //test fib
             int32_t f;
-            act->remoteExample->fib(act->remoteExample->handle, 4, &f);
+            TIMED_EXPR(act->remoteExample->fib(act->remoteExample->handle, 4, &f));
+            printf("Call fib took %f ms\n", diff);
             ok = (f == 3);
         }
     } else {
@@ -236,7 +266,9 @@ static bool bndTestRemoteComplex(void *handle) {
        exmpl.name = "name";
        exmpl.e = ENUM_EXAMPLE_VAL3;
        struct complex_output_example* result = NULL;
-       int rc = act->remoteExample->setComplex(act->remoteExample->handle, &exmpl, &result);
+       int rc;
+       TIMED_EXPR(rc = act->remoteExample->setComplex(act->remoteExample->handle, &exmpl, &result));
+       printf("Call setComplex took %f ms\n", diff);
        ok = rc == 0 && result->pow == 8 && result->fib == 5 && strncmp("name", result->name, 64) == 0;
        if (rc == 0) {
            free(result->name);