| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#include <stdio.h> |
| 5 |
#include <stdlib.h> |
| 6 |
#include <string.h> |
| 7 |
|
| 8 |
#include "common.h" |
| 9 |
|
| 10 |
#define MAX_LINE_LENGTH 1024 * 32 |
| 11 |
|
| 12 |
static void do_publish(amqp_connection_state_t conn, char *exchange, |
| 13 |
char *routing_key, amqp_basic_properties_t *props, |
| 14 |
amqp_bytes_t body) { |
| 15 |
int res = amqp_basic_publish(conn, 1, cstring_bytes(exchange), |
| 16 |
cstring_bytes(routing_key), 0, 0, props, body); |
| 17 |
die_amqp_error(res, "basic.publish"); |
| 18 |
} |
| 19 |
|
| 20 |
int main(int argc, const char **argv) { |
| 21 |
amqp_connection_state_t conn; |
| 22 |
static char *exchange = NULL; |
| 23 |
static char *routing_key = NULL; |
| 24 |
static char *content_type = NULL; |
| 25 |
static char *content_encoding = NULL; |
| 26 |
static char **headers = NULL; |
| 27 |
static char *reply_to = NULL; |
| 28 |
static char *body = NULL; |
| 29 |
amqp_basic_properties_t props; |
| 30 |
amqp_bytes_t body_bytes; |
| 31 |
static int delivery = 1; |
| 32 |
static int line_buffered = 0; |
| 33 |
static char **pos; |
| 34 |
|
| 35 |
struct poptOption options[] = { |
| 36 |
INCLUDE_OPTIONS(connect_options), |
| 37 |
{"exchange", 'e', POPT_ARG_STRING, &exchange, 0, |
| 38 |
"the exchange to publish to", "exchange"}, |
| 39 |
{"routing-key", 'r', POPT_ARG_STRING, &routing_key, 0, |
| 40 |
"the routing key to publish with", "routing key"}, |
| 41 |
{"persistent", 'p', POPT_ARG_VAL, &delivery, 2, |
| 42 |
"use the persistent delivery mode", NULL}, |
| 43 |
{"content-type", 'C', POPT_ARG_STRING, &content_type, 0, |
| 44 |
"the content-type for the message", "content type"}, |
| 45 |
{"reply-to", 't', POPT_ARG_STRING, &reply_to, 0, |
| 46 |
"the replyTo to use for the message", "reply to"}, |
| 47 |
{"line-buffered", 'l', POPT_ARG_VAL, &line_buffered, 2, |
| 48 |
"treat each line from standard in as a separate message", NULL}, |
| 49 |
{"content-encoding", 'E', POPT_ARG_STRING, &content_encoding, 0, |
| 50 |
"the content-encoding for the message", "content encoding"}, |
| 51 |
{"header", 'H', POPT_ARG_ARGV, &headers, 0, |
| 52 |
"set a message header (may be specified multiple times)", |
| 53 |
"\"key: value\""}, |
| 54 |
{"body", 'b', POPT_ARG_STRING, &body, 0, "specify the message body", |
| 55 |
"body"}, |
| 56 |
POPT_AUTOHELP{NULL, '\0', 0, NULL, 0, NULL, NULL}}; |
| 57 |
|
| 58 |
process_all_options(argc, argv, options); |
| 59 |
|
| 60 |
if (!exchange && !routing_key) { |
| 61 |
fprintf(stderr, "neither exchange nor routing key specified\n"); |
| 62 |
return 1; |
| 63 |
} |
| 64 |
|
| 65 |
memset(&props, 0, sizeof props); |
| 66 |
props._flags = AMQP_BASIC_DELIVERY_MODE_FLAG; |
| 67 |
props.delivery_mode = delivery; |
| 68 |
|
| 69 |
if (content_type) { |
| 70 |
props._flags |= AMQP_BASIC_CONTENT_TYPE_FLAG; |
| 71 |
props.content_type = amqp_cstring_bytes(content_type); |
| 72 |
} |
| 73 |
|
| 74 |
if (content_encoding) { |
| 75 |
props._flags |= AMQP_BASIC_CONTENT_ENCODING_FLAG; |
| 76 |
props.content_encoding = amqp_cstring_bytes(content_encoding); |
| 77 |
} |
| 78 |
|
| 79 |
if (reply_to) { |
| 80 |
props._flags |= AMQP_BASIC_REPLY_TO_FLAG; |
| 81 |
props.reply_to = amqp_cstring_bytes(reply_to); |
| 82 |
} |
| 83 |
|
| 84 |
if (headers) { |
| 85 |
int num = 0; |
| 86 |
for (pos = headers; *pos; pos++) { |
| 87 |
num++; |
| 88 |
} |
| 89 |
|
| 90 |
if (num > 0) { |
| 91 |
amqp_table_t *table = &props.headers; |
| 92 |
table->num_entries = num; |
| 93 |
table->entries = calloc(num, sizeof(amqp_table_entry_t)); |
| 94 |
if (table->entries == NULL) { |
| 95 |
fprintf(stderr, "Memory allocation failed\n"); |
| 96 |
return 1; |
| 97 |
} |
| 98 |
int i = 0; |
| 99 |
for (pos = headers; *pos; pos++) { |
| 100 |
char *colon = strchr(*pos, ':'); |
| 101 |
if (colon) { |
| 102 |
*colon++ = '\0'; |
| 103 |
while (*colon == ' ') colon++; |
| 104 |
table->entries[i].key = amqp_cstring_bytes(*pos); |
| 105 |
table->entries[i].value.kind = AMQP_FIELD_KIND_UTF8; |
| 106 |
table->entries[i].value.value.bytes = amqp_cstring_bytes(colon); |
| 107 |
i++; |
| 108 |
} else { |
| 109 |
fprintf(stderr, |
| 110 |
"Ignored header definition missing ':' delimiter in \"%s\"\n", |
| 111 |
*pos); |
| 112 |
} |
| 113 |
} |
| 114 |
props._flags |= AMQP_BASIC_HEADERS_FLAG; |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
conn = make_connection(); |
| 119 |
|
| 120 |
if (body) { |
| 121 |
body_bytes = amqp_cstring_bytes(body); |
| 122 |
} else { |
| 123 |
if (line_buffered) { |
| 124 |
body_bytes.bytes = (char *)malloc(MAX_LINE_LENGTH); |
| 125 |
if (body_bytes.bytes == NULL) { |
| 126 |
fprintf(stderr, "Memory allocation failed\n"); |
| 127 |
return 1; |
| 128 |
} |
| 129 |
while (fgets(body_bytes.bytes, MAX_LINE_LENGTH, stdin)) { |
| 130 |
body_bytes.len = strlen(body_bytes.bytes); |
| 131 |
do_publish(conn, exchange, routing_key, &props, body_bytes); |
| 132 |
} |
| 133 |
} else { |
| 134 |
body_bytes = read_all(0); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
if (!line_buffered) { |
| 139 |
do_publish(conn, exchange, routing_key, &props, body_bytes); |
| 140 |
} |
| 141 |
|
| 142 |
if (props.headers.num_entries > 0) { |
| 143 |
free(props.headers.entries); |
| 144 |
} |
| 145 |
|
| 146 |
if (!body) { |
| 147 |
free(body_bytes.bytes); |
| 148 |
} |
| 149 |
|
| 150 |
close_connection(conn); |
| 151 |
return 0; |
| 152 |
} |