celix-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] [celix] abroekhuis commented on a change in pull request #156: Added protocol service API and wire protocol implementation to be use…
Date Thu, 05 Mar 2020 10:18:59 GMT
abroekhuis commented on a change in pull request #156: Added protocol service API and wire
protocol implementation to be use…
URL: https://github.com/apache/celix/pull/156#discussion_r388199184
 
 

 ##########
 File path: bundles/pubsub/pubsub_protocol_wire_v1/src/pubsub_wire_protocol_impl.c
 ##########
 @@ -0,0 +1,309 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <math.h>
+#include <string.h>
+
+#include "utils.h"
+#include "celix_properties.h"
+
+#include "pubsub_wire_protocol_impl.h"
+#include "pubsub_wire_protocol_common.h"
+
+#define NETSTRING_ERROR_TOO_LONG     -1
+#define NETSTRING_ERROR_NO_COLON     -2
+#define NETSTRING_ERROR_TOO_SHORT    -3
+#define NETSTRING_ERROR_NO_COMMA     -4
+#define NETSTRING_ERROR_LEADING_ZERO -5
+#define NETSTRING_ERROR_NO_LENGTH    -6
+
+struct pubsub_protocol_wire_v1 {
+};
+
+static celix_status_t pubsubProtocol_createNetstring(const char* string, char** netstringOut);
+static int pubsubProtocol_parseNetstring(char *buffer, size_t buffer_length,
+                                  char **netstring_start, size_t *netstring_length);
+
+celix_status_t pubsubProtocol_create(pubsub_protocol_wire_v1_t **protocol) {
+    celix_status_t status = CELIX_SUCCESS;
+
+    *protocol = calloc(1, sizeof(**protocol));
+
+    if (!*protocol) {
+        status = CELIX_ENOMEM;
+    }
+    else {
+        //
+    }
+
+    return status;
+}
+
+celix_status_t pubsubProtocol_destroy(pubsub_protocol_wire_v1_t* protocol) {
+    celix_status_t status = CELIX_SUCCESS;
+
+    free(protocol);
+
+    return status;
+}
+
+celix_status_t pubsubProtocol_getSyncHeader(void* handle, void *syncHeader) {
+    for (int i = 0; i < 5; ++i) {
+        ((char *) syncHeader)[i] = '\0';
+    }
+    writeInt(syncHeader, 0, PROTOCOL_WIRE_SYNC);
+
+    return CELIX_SUCCESS;
+}
+
+celix_status_t pubsubProtocol_encodeHeader(void *handle, pubsub_protocol_message_t *message,
void **outBuffer, size_t *outLength) {
+    celix_status_t status = CELIX_SUCCESS;
+
+    *outBuffer = calloc(1, 24);
+    if (*outBuffer == NULL) {
+        status = CELIX_ENOMEM;
+    } else {
+        int idx = 0;
+        idx = writeInt(*outBuffer, idx, PROTOCOL_WIRE_SYNC);
+        idx = writeInt(*outBuffer, idx, PROTOCOL_WIRE_ENVELOPE_VERSION);
+        idx = writeInt(*outBuffer, idx, message->header.msgId);
+        idx = writeShort(*outBuffer, idx, message->header.msgMajorVersion);
+        idx = writeShort(*outBuffer, idx, message->header.msgMinorVersion);
+        idx = writeInt(*outBuffer, idx, message->header.payloadSize);
+        idx = writeInt(*outBuffer, idx, message->header.metadataSize);
+
+        *outLength = idx;
+    }
+
+    return status;
+}
+
+celix_status_t pubsubProtocol_encodePayload(void *handle, pubsub_protocol_message_t *message,
void **outBuffer, size_t *outLength) {
+    *outBuffer = message->payload.payload;
+    *outLength = message->payload.length;
+
+    return CELIX_SUCCESS;
+}
+
+celix_status_t pubsubProtocol_encodeMetadata(void *handle, pubsub_protocol_message_t *message,
void **outBuffer, size_t *outLength) {
+    celix_status_t status = CELIX_SUCCESS;
+
+    char *line = calloc(1, 4);
+    size_t idx = 4;
+    size_t len = 0;
+
+    const char *key;
+    if (message->metadata.metadata != NULL && celix_properties_size(message->metadata.metadata)
> 0) {
+        CELIX_PROPERTIES_FOR_EACH(message->metadata.metadata, key) {
+            const char *val = celix_properties_get(message->metadata.metadata, key, "!Error!");
+            char *keyNetString = NULL;
+            char *valueNetString = NULL;
+
+            status = pubsubProtocol_createNetstring(key, &keyNetString);
+            if (status != CELIX_SUCCESS) {
+                break;
+            }
+            status = pubsubProtocol_createNetstring(val, &valueNetString);
+            if (status != CELIX_SUCCESS) {
+                break;
+            }
+
+            len += strlen(keyNetString);
+            len += strlen(valueNetString);
+            char *tmp = realloc(line, len + sizeof(uint32_t));
+            if (!tmp) {
+                free(line);
+                status = CELIX_ENOMEM;
+                return status;
+            }
+            line = tmp;
+
+            memcpy(line + idx, keyNetString, strlen(keyNetString));
+            idx += strlen(keyNetString);
+            memcpy(line + idx, valueNetString, strlen(keyNetString));
+            idx += strlen(valueNetString);
+
+            free(keyNetString);
+            free(valueNetString);
+        }
+    }
+    int size = celix_properties_size(message->metadata.metadata);
+    memcpy(line, &size, sizeof(int32_t));
+
+    *outBuffer = line;
+    *outLength = idx;
+
+    return status;
+}
+
+celix_status_t pubsubProtocol_decodeHeader(void* handle, void *data, size_t length, pubsub_protocol_message_t
*message) {
+    celix_status_t status = CELIX_SUCCESS;
+
+    int idx = 0;
+    unsigned int sync;
+    idx = readInt(data, idx, &sync);
+    if (sync != PROTOCOL_WIRE_SYNC) {
+        status = CELIX_ILLEGAL_ARGUMENT;
+    } else {
+        unsigned int envelopeVersion;
+        idx = readInt(data, idx, &envelopeVersion);
+        if (envelopeVersion != PROTOCOL_WIRE_ENVELOPE_VERSION) {
+            status = CELIX_ILLEGAL_ARGUMENT;
+        } else {
+            idx = readInt(data, idx, &message->header.msgId);
+            idx = readShort(data, idx, &message->header.msgMajorVersion);
+            idx = readShort(data, idx, &message->header.msgMinorVersion);
+            idx = readInt(data, idx, &message->header.payloadSize);
+            readInt(data, idx, &message->header.metadataSize);
+        }
+    }
+
+    return status;
+}
+
+celix_status_t pubsubProtocol_decodePayload(void* handle, void *data, size_t length, pubsub_protocol_message_t
*message){
+    message->payload.payload = data;
+    message->payload.length = length;
+
+    return CELIX_SUCCESS;
+}
+
+celix_status_t pubsubProtocol_decodeMetadata(void* handle, void *data, size_t length, pubsub_protocol_message_t
*message) {
+    celix_status_t status = CELIX_SUCCESS;
+
+    uint32_t nOfElements;
+    size_t idx = readInt(data, 0, &nOfElements);
+    char *netstring = data + idx;
+    int netstringLen = length - idx;
+
+    message->metadata.metadata = celix_properties_create();
+    while (idx < length) {
+        size_t outlen;
+        status = pubsubProtocol_parseNetstring(netstring, netstringLen, &netstring, &outlen);
+        if (status != CELIX_SUCCESS) {
+            break;
+        }
+        char *key = strndup(netstring, outlen);
+        netstring += outlen + 1;
+        idx += outlen + 3;
+
+        status = pubsubProtocol_parseNetstring(netstring, netstringLen, &netstring, &outlen);
+        if (status != CELIX_SUCCESS) {
+            break;
+        }
+        char *value = strndup(netstring, outlen);
+        netstring += outlen + 1;
+        idx += outlen + 3;
+
+        celix_properties_setWithoutCopy(message->metadata.metadata, key, value);
+    }
+
+    return status;
+}
+
+static celix_status_t pubsubProtocol_createNetstring(const char* string, char** netstringOut)
{
+    celix_status_t status = CELIX_SUCCESS;
+
+    size_t str_len = strlen(string);
+    if (str_len == 0) {
+        // 0:,
+        *netstringOut = calloc(1, 4);
+        if (*netstringOut == NULL) {
+            status = CELIX_ENOMEM;
+        } else {
+            *netstringOut[0] = '0';
+            *netstringOut[1] = ':';
+            *netstringOut[2] = ',';
+            *netstringOut[3] = '\0';
+        }
+    } else {
+        size_t numlen = ceil(log10(str_len + 1));
+        *netstringOut = calloc(1, numlen + str_len + 3);
+        if (*netstringOut == NULL) {
+            status = CELIX_ENOMEM;
+        } else {
+            sprintf(*netstringOut, "%zu:%s,", str_len, string);
+        }
+    }
+
+    return status;
+}
+
+/* Reads a netstring from a `buffer` of length `buffer_length`. Writes
 
 Review comment:
   Code comes from https://github.com/PeterScott/netstring-c, which does not list a license,
and only mentions "public domain". S0 not needed I think?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

Mime
View raw message