summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlan Antonuk <alan.antonuk@gmail.com>2014-12-25 21:11:55 -0800
committerAlan Antonuk <alan.antonuk@gmail.com>2015-05-05 20:54:17 -0700
commit858576bba0e60a4e4f11feaf26e6376f424352f3 (patch)
treeae4418dd7c179f92c7777239228c0c4d0af2d0a3
parentba9d8ba48fa099a0430584e1e92fe14d8b8ab3c0 (diff)
downloadrabbitmq-c-858576bba0e60a4e4f11feaf26e6376f424352f3.tar.gz
Add convenience funcs for creating table entries.
-rw-r--r--librabbitmq/amqp_socket.c45
-rw-r--r--librabbitmq/amqp_table.c40
-rw-r--r--librabbitmq/amqp_table.h82
3 files changed, 137 insertions, 30 deletions
diff --git a/librabbitmq/amqp_socket.c b/librabbitmq/amqp_socket.c
index 545f2cd..6425508 100644
--- a/librabbitmq/amqp_socket.c
+++ b/librabbitmq/amqp_socket.c
@@ -1187,36 +1187,22 @@ static amqp_rpc_reply_t amqp_login_inner(amqp_connection_state_t state,
goto error_res;
}
- default_properties[0].key = amqp_cstring_bytes("product");
- default_properties[0].value.kind = AMQP_FIELD_KIND_UTF8;
- default_properties[0].value.value.bytes =
- amqp_cstring_bytes("rabbitmq-c");
-
- /* version */
- default_properties[1].key = amqp_cstring_bytes("version");
- default_properties[1].value.kind = AMQP_FIELD_KIND_UTF8;
- default_properties[1].value.value.bytes =
- amqp_cstring_bytes(AMQP_VERSION_STRING);
-
- /* platform */
- default_properties[2].key = amqp_cstring_bytes("platform");
- default_properties[2].value.kind = AMQP_FIELD_KIND_UTF8;
- default_properties[2].value.value.bytes =
- amqp_cstring_bytes(AMQ_PLATFORM);
-
- /* copyright */
- default_properties[3].key = amqp_cstring_bytes("copyright");
- default_properties[3].value.kind = AMQP_FIELD_KIND_UTF8;
- default_properties[3].value.value.bytes =
- amqp_cstring_bytes(AMQ_COPYRIGHT);
-
- default_properties[4].key = amqp_cstring_bytes("information");
- default_properties[4].value.kind = AMQP_FIELD_KIND_UTF8;
- default_properties[4].value.value.bytes =
- amqp_cstring_bytes("See https://github.com/alanxz/rabbitmq-c");
+
+
+ default_properties[0] =
+ amqp_table_construct_utf8_entry("product", "rabbitmq-c");
+ default_properties[1] =
+ amqp_table_construct_utf8_entry("version", AMQP_VERSION_STRING);
+ default_properties[2] =
+ amqp_table_construct_utf8_entry("platform", AMQ_PLATFORM);
+ default_properties[3] =
+ amqp_table_construct_utf8_entry("copyright", AMQ_COPYRIGHT);
+ default_properties[4] = amqp_table_construct_utf8_entry(
+ "information", "See https://github.com/alanxz/rabbitmq-c");
default_table.entries = default_properties;
- default_table.num_entries = sizeof(default_properties) / sizeof(amqp_table_entry_t);
+ default_table.num_entries =
+ sizeof(default_properties) / sizeof(amqp_table_entry_t);
if (0 == client_properties->num_entries) {
s.client_properties = default_table;
@@ -1261,8 +1247,7 @@ static amqp_rpc_reply_t amqp_login_inner(amqp_connection_state_t state,
s.mechanism = sasl_method_name(sasl_method);
s.response = response_bytes;
- s.locale.bytes = "en_US";
- s.locale.len = 5;
+ s.locale = amqp_cstring_bytes("en_US");
res = amqp_send_method(state, 0, AMQP_CONNECTION_START_OK_METHOD, &s);
if (res < 0) {
diff --git a/librabbitmq/amqp_table.c b/librabbitmq/amqp_table.c
index 8cc312a..d45dd85 100644
--- a/librabbitmq/amqp_table.c
+++ b/librabbitmq/amqp_table.c
@@ -39,6 +39,7 @@
#endif
#include "amqp_private.h"
+#include "amqp_table.h"
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
@@ -612,3 +613,42 @@ amqp_table_clone(amqp_table_t *original, amqp_table_t *clone, amqp_pool_t *pool)
error_out1:
return res;
}
+
+amqp_table_entry_t amqp_table_construct_utf8_entry(const char *key,
+ const char *value) {
+ amqp_table_entry_t ret;
+ ret.key = amqp_cstring_bytes(key);
+ ret.value.kind = AMQP_FIELD_KIND_UTF8;
+ ret.value.value.bytes = amqp_cstring_bytes(value);
+ return ret;
+}
+
+amqp_table_entry_t amqp_table_construct_table_entry(const char *key,
+ const amqp_table_t *value) {
+ amqp_table_entry_t ret;
+ ret.key = amqp_cstring_bytes(key);
+ ret.value.kind = AMQP_FIELD_KIND_TABLE;
+ ret.value.value.table = *value;
+ return ret;
+}
+
+amqp_table_entry_t amqp_table_construct_bool_entry(const char *key,
+ const int value) {
+ amqp_table_entry_t ret;
+ ret.key = amqp_cstring_bytes(key);
+ ret.value.kind = AMQP_FIELD_KIND_BOOLEAN;
+ ret.value.value.boolean = value;
+ return ret;
+}
+
+amqp_table_entry_t *amqp_table_get_entry_by_key(const amqp_table_t *table,
+ const amqp_bytes_t key) {
+ int i;
+ assert(table != NULL);
+ for (i = 0; i < table->num_entries; ++i) {
+ if (amqp_bytes_equal(table->entries[i].key, key)) {
+ return &table->entries[i];
+ }
+ }
+ return NULL;
+}
diff --git a/librabbitmq/amqp_table.h b/librabbitmq/amqp_table.h
new file mode 100644
index 0000000..1eef1b1
--- /dev/null
+++ b/librabbitmq/amqp_table.h
@@ -0,0 +1,82 @@
+/* vim:set ft=c ts=2 sw=2 sts=2 et cindent: */
+/*
+ * ***** BEGIN LICENSE BLOCK *****
+ * Version: MIT
+ *
+ * Portions created by Alan Antonuk are Copyright (c) 2014 Alan Antonuk.
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use, copy,
+ * modify, merge, publish, distribute, sublicense, and/or sell copies
+ * of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ * ***** END LICENSE BLOCK *****
+ */
+#ifndef AMQP_TABLE_H
+#define AMQP_TABLE_H
+
+#include "amqp.h"
+#include "amqp_private.h"
+
+/**
+ * Initializes a table entry with utf-8 string type value.
+ *
+ * \param [in] key the table entry key. The string must remain valid for the
+ * life of the resulting amqp_table_entry_t.
+ * \param [in] value the string value. The string must remain valid for the life
+ * of the resulting amqp_table_entry_t.
+ * \returns An initialized table entry.
+ */
+amqp_table_entry_t amqp_table_construct_utf8_entry(const char *key,
+ const char *value);
+
+/**
+ * Initializes a table entry with table type value.
+ *
+ * \param [in] key the table entry key. The string must remain value for the
+ * life of the resulting amqp_table_entry_t.
+ * \param [in] value the amqp_table_t value. The table must remain valid for the
+ * life of the resulting amqp_table_entry_t.
+ * \returns An initialized table entry.
+ */
+amqp_table_entry_t amqp_table_construct_table_entry(const char *key,
+ const amqp_table_t *value);
+
+/**
+ * Initializes a table entry with boolean type value.
+ *
+ * \param [in] key the table entry key. The string must remain value for the
+ * life of the resulting amqp_table_entry_t.
+ * \param [in] value the boolean value. 0 means false, any other value is true.
+ * \returns An initialized table entry.
+ */
+amqp_table_entry_t amqp_table_construct_bool_entry(const char *key,
+ const int value);
+
+/**
+ * Searches a table for an entry with a matching key.
+ *
+ * \param [in] table the table to search.
+ * \param [in] key the string to search with.
+ * \returns a pointer to the table entry in the table if a matching key can be
+ * found, NULL otherwise.
+ */
+amqp_table_entry_t *amqp_table_get_entry_by_key(const amqp_table_t *table,
+ const amqp_bytes_t key);
+
+#endif /* AMQP_TABLE_H */