You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@qpid.apache.org by tr...@apache.org on 2017/12/21 13:30:25 UTC

qpid-dispatch git commit: DISPATCH-905 - Added a general get-as-string facility for proton data elements. Use this facility for the port field in the failover list. This closes #237

Repository: qpid-dispatch
Updated Branches:
  refs/heads/master 58ce3a208 -> 5ca979d53


DISPATCH-905 - Added a general get-as-string facility for proton data elements.  Use this facility for the port field in the failover list.
This closes #237


Project: http://git-wip-us.apache.org/repos/asf/qpid-dispatch/repo
Commit: http://git-wip-us.apache.org/repos/asf/qpid-dispatch/commit/5ca979d5
Tree: http://git-wip-us.apache.org/repos/asf/qpid-dispatch/tree/5ca979d5
Diff: http://git-wip-us.apache.org/repos/asf/qpid-dispatch/diff/5ca979d5

Branch: refs/heads/master
Commit: 5ca979d53c7761e36ff95052971c1009f29441fc
Parents: 58ce3a2
Author: Ted Ross <tr...@redhat.com>
Authored: Thu Dec 21 08:27:12 2017 -0500
Committer: Ted Ross <tr...@redhat.com>
Committed: Thu Dec 21 08:27:12 2017 -0500

----------------------------------------------------------------------
 include/qpid/dispatch/proton_utils.h |  34 +++++++
 src/CMakeLists.txt                   |   1 +
 src/proton_utils.c                   | 152 ++++++++++++++++++++++++++++++
 src/router_node.c                    |   5 +-
 tests/CMakeLists.txt                 |   1 +
 tests/proton_utils_tests.c           | 100 ++++++++++++++++++++
 tests/run_unit_tests.c               |   2 +
 7 files changed, 292 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/qpid-dispatch/blob/5ca979d5/include/qpid/dispatch/proton_utils.h
----------------------------------------------------------------------
diff --git a/include/qpid/dispatch/proton_utils.h b/include/qpid/dispatch/proton_utils.h
new file mode 100644
index 0000000..e128d5a
--- /dev/null
+++ b/include/qpid/dispatch/proton_utils.h
@@ -0,0 +1,34 @@
+#ifndef __proton_utils_h__
+#define __proton_utils_h__ 1
+/*
+ * 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 <proton/codec.h>
+
+/**
+ * Get the contents of a data field as a string.
+ *
+ * @param data A proton data field
+ * @return A heap allocated string representing the contents of the data.  The caller is
+ *         responsible for freeing the string when it is no longer needed.
+ */
+char *qdpn_data_as_string(pn_data_t *data);
+
+#endif
+

http://git-wip-us.apache.org/repos/asf/qpid-dispatch/blob/5ca979d5/src/CMakeLists.txt
----------------------------------------------------------------------
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 78b17b3..627f6da 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -70,6 +70,7 @@ set(qpid_dispatch_SOURCES
   parse.c
   parse_tree.c
   policy.c
+  proton_utils.c
   remote_sasl.c
   posix/threading.c
   python_embedded.c

http://git-wip-us.apache.org/repos/asf/qpid-dispatch/blob/5ca979d5/src/proton_utils.c
----------------------------------------------------------------------
diff --git a/src/proton_utils.c b/src/proton_utils.c
new file mode 100644
index 0000000..edd86ba
--- /dev/null
+++ b/src/proton_utils.c
@@ -0,0 +1,152 @@
+/*
+ * 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 <qpid/dispatch/proton_utils.h>
+#include <string.h>
+#include <inttypes.h>
+#include <time.h>
+#include <ctype.h>
+#include <stdio.h>
+
+char *qdpn_data_as_string(pn_data_t *data)
+{
+#define MAX_BUFFER 50
+    char buffer[MAX_BUFFER + 1];
+    buffer[MAX_BUFFER] = '\0';
+
+    switch(pn_data_type(data)) {
+    case PN_BOOL:
+        return strdup(pn_data_get_bool(data) ? "true" : "false");
+
+    case PN_UBYTE:
+        snprintf(buffer, MAX_BUFFER, "%"PRId8, pn_data_get_ubyte(data));
+        return strdup(buffer);
+
+    case PN_BYTE:
+        snprintf(buffer, MAX_BUFFER, "%"PRId8, pn_data_get_byte(data));
+        return strdup(buffer);
+
+    case PN_USHORT:
+        snprintf(buffer, MAX_BUFFER, "%"PRId16, pn_data_get_ushort(data));
+        return strdup(buffer);
+
+    case PN_SHORT:
+        snprintf(buffer, MAX_BUFFER, "%"PRId16, pn_data_get_short(data));
+        return strdup(buffer);
+
+    case PN_UINT:
+        snprintf(buffer, MAX_BUFFER, "%"PRId32, pn_data_get_uint(data));
+        return strdup(buffer);
+
+    case PN_INT:
+        snprintf(buffer, MAX_BUFFER, "%"PRId32, pn_data_get_int(data));
+        return strdup(buffer);
+
+    case PN_CHAR: {
+        char c = (char) pn_data_get_char(data);
+        return strndup(&c, 1);
+    }
+
+    case PN_ULONG:
+        snprintf(buffer, MAX_BUFFER, "%"PRId64, pn_data_get_ulong(data));
+        return strdup(buffer);
+
+    case PN_LONG:
+        snprintf(buffer, MAX_BUFFER, "%"PRId64, pn_data_get_long(data));
+        return strdup(buffer);
+
+    case PN_TIMESTAMP: {
+#if _POSIX_C_SOURCE || _BSD_SOURCE || _SVID_SOURCE
+        time_t t = (time_t) (pn_data_get_timestamp(data));
+        ctime_r(&t, buffer);
+        size_t len = strlen(buffer);
+        if (buffer[len - 1] == '\n')
+            buffer[len - 1] = '\0';
+        return strdup(buffer);
+#else
+        snprintf(buffer, MAX_BUFFER, "%"PRId64, pn_data_get_timestamp(data));
+        return strdup(buffer);
+#endif
+    }
+
+    case PN_FLOAT:
+        snprintf(buffer, MAX_BUFFER, "%lg", pn_data_get_float(data));
+        return strdup(buffer);
+
+    case PN_DOUBLE:
+        snprintf(buffer, MAX_BUFFER, "%lg", pn_data_get_double(data));
+        return strdup(buffer);
+
+    case PN_DECIMAL32:
+        snprintf(buffer, MAX_BUFFER, "%"PRId32, pn_data_get_decimal32(data));
+        return strdup(buffer);
+
+    case PN_DECIMAL64:
+        snprintf(buffer, MAX_BUFFER, "%"PRId64, pn_data_get_decimal64(data));
+        return strdup(buffer);
+
+    case PN_UUID: {
+        pn_uuid_t uuid = pn_data_get_uuid(data);
+        uint8_t  *u    = (uint8_t*) uuid.bytes;
+        snprintf(buffer, MAX_BUFFER,
+                 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
+                 u[0], u[1], u[2],  u[3],  u[4],  u[5],  u[6],  u[7],
+                 u[8], u[9], u[10], u[11], u[12], u[13], u[14], u[15]);
+        return strdup(buffer);
+    }
+
+    case PN_BINARY: {
+        pn_bytes_t bytes = pn_data_get_binary(data);
+
+        //
+        // Search the binary field for unprintable characters.  If found, don't return a
+        // string representation.
+        //
+        for (int i = 0; i < bytes.size; i++) {
+            if (!isprint(bytes.start[i]))
+                return 0;
+        }
+
+        return strndup(bytes.start, bytes.size);
+    }
+
+    case PN_STRING:
+        return strdup(pn_data_get_string(data).start);
+
+    case PN_SYMBOL:
+        return strdup(pn_data_get_symbol(data).start);
+
+    //
+    // Return null for the following types:
+    //
+    case PN_DECIMAL128:
+    case PN_DESCRIBED:
+    case PN_ARRAY:
+    case PN_LIST:
+    case PN_MAP:
+    case PN_NULL:
+    default:
+        break;
+    }
+
+    return 0;
+}
+
+
+

http://git-wip-us.apache.org/repos/asf/qpid-dispatch/blob/5ca979d5/src/router_node.c
----------------------------------------------------------------------
diff --git a/src/router_node.c b/src/router_node.c
index 08cafde..81437cb 100644
--- a/src/router_node.c
+++ b/src/router_node.c
@@ -27,6 +27,7 @@
 #include "entity_cache.h"
 #include "router_private.h"
 #include <qpid/dispatch/router_core.h>
+#include <qpid/dispatch/proton_utils.h>
 #include <proton/sasl.h>
 
 const char *QD_ROUTER_NODE_TYPE = "router.node";
@@ -911,9 +912,7 @@ static void AMQP_opened_handler(qd_router_t *router, qd_connection_t *conn, bool
                                         else if (sym.size == strlen(QD_CONNECTION_PROPERTY_FAILOVER_PORT_KEY) &&
                                                                             strcmp(sym.start, QD_CONNECTION_PROPERTY_FAILOVER_PORT_KEY) == 0) {
                                             pn_data_next(props);
-                                            if (pn_data_type(props) == PN_STRING) {
-                                                item->port = strdup(pn_data_get_string(props).start);
-                                            }
+                                            item->port = qdpn_data_as_string(props);
 
                                         }
                                         else if (sym.size == strlen(QD_CONNECTION_PROPERTY_FAILOVER_SCHEME_KEY) &&

http://git-wip-us.apache.org/repos/asf/qpid-dispatch/blob/5ca979d5/tests/CMakeLists.txt
----------------------------------------------------------------------
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index 0c6454c..bdff4e4 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -33,6 +33,7 @@ set(unit_test_SOURCES
     failoverlist_test.c
     timer_test.c
     parse_tree_tests
+    proton_utils_tests.c
     )
 if (USE_MEMORY_POOL)
   list(APPEND unit_test_SOURCES alloc_test.c)

http://git-wip-us.apache.org/repos/asf/qpid-dispatch/blob/5ca979d5/tests/proton_utils_tests.c
----------------------------------------------------------------------
diff --git a/tests/proton_utils_tests.c b/tests/proton_utils_tests.c
new file mode 100644
index 0000000..14d6b7e
--- /dev/null
+++ b/tests/proton_utils_tests.c
@@ -0,0 +1,100 @@
+/*
+ * 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.
+ */
+
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <assert.h>
+#include <string.h>
+#include <inttypes.h>
+#include "test_case.h"
+#include <qpid/dispatch.h>
+#include <qpid/dispatch/proton_utils.h>
+
+
+typedef struct {
+    const char     *encoded;
+    size_t          size;
+    const char     *expected;
+} as_string_vector_t;
+
+as_string_vector_t vectors0[] = {
+    {"\x40",                 1, 0},          // null
+    {"\x56\x00",             2, "false"},    // boolean
+    {"\x56\x01",             2, "true"},     // boolean
+    {"\x41",                 1, "true"},     // boolean.true
+    {"\x42",                 1, "false"},    // boolean.false
+    {"\x50\x55",             2, "85"},       // ubyte
+    {"\x51\x55",             2, "85"},       // byte
+    {"\x60\x11\x55",         3, "4437"},     // ushort
+    {"\x61\x11\x55",         3, "4437"},     // short
+    {"\x70\x00\x11\x22\x33", 5, "1122867"},  // uint
+    {"\x52\x55",             2, "85"},       // smalluint
+    {"\x43",                 1, "0"},        // uint0
+    {"\x71\x00\x11\x22\x33", 5, "1122867"},  // int
+    {"\x53\x55",             2, "85"},       // smallulong
+    {"\x55\x55",             2, "85"},       // smalllong
+    {"\x98\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f", 17, "00010203-0405-0607-0809-0a0b0c0d0e0f"}, // uuid
+    {"\xa0\x04GB\x01D",      6, 0},          // vbin8
+    {"\xa0\x04GBCD",         6, "GBCD"},     // vbin8
+    {"\xa1\x04HBCD",         6, "HBCD"},     // str8-utf8
+    {"\xa3\x04IBCD",         6, "IBCD"},     // sym8
+    {"\x45",                 1, 0},          // list0
+    {0, 0, 0}
+};
+
+#define MAX_ERROR 1000
+static char error[MAX_ERROR];
+
+static char *test_data_as_string(void *context)
+{
+    as_string_vector_t *vector = vectors0;
+
+    while (vector->encoded) {
+        pn_data_t *data = pn_data(0);
+        pn_data_decode(data, vector->encoded, vector->size);
+        char *result = qdpn_data_as_string(data);
+
+        if (result || vector->expected) {
+            if ((result == 0 || vector->expected == 0) && result != vector->expected) {
+                snprintf(error, MAX_ERROR, "Expected '%s', got '%s'", vector->expected, result);
+                return error;
+            }
+
+            if (strcmp(result, vector->expected)) {
+                snprintf(error, MAX_ERROR, "Expected '%s', got '%s'", vector->expected, result);
+                return error;
+            }
+        }
+        vector++;
+    }
+
+    return 0;
+}
+
+
+int proton_utils_tests(void)
+{
+    int result = 0;
+    char *test_group = "proton_utils_tests";
+
+    TEST_CASE(test_data_as_string, 0);
+
+    return result;
+}
+

http://git-wip-us.apache.org/repos/asf/qpid-dispatch/blob/5ca979d5/tests/run_unit_tests.c
----------------------------------------------------------------------
diff --git a/tests/run_unit_tests.c b/tests/run_unit_tests.c
index 97b699e..ff95786 100644
--- a/tests/run_unit_tests.c
+++ b/tests/run_unit_tests.c
@@ -29,6 +29,7 @@ int compose_tests(void);
 int policy_tests(void);
 int failoverlist_tests(void);
 int parse_tree_tests(void);
+int proton_utils_tests(void);
 
 int main(int argc, char** argv)
 {
@@ -61,6 +62,7 @@ int main(int argc, char** argv)
     result += policy_tests();
     result += failoverlist_tests();
     result += parse_tree_tests();
+    result += proton_utils_tests();
 
     qd_dispatch_free(qd);       // dispatch_free last.
 


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org