amqp_connection_state_t make_connection(void) { int s; struct amqp_connection_info ci; amqp_connection_state_t conn; init_connection_info(&ci); s = amqp_open_socket(ci.host, ci.port); die_amqp_error(s, "opening socket to %s:%d", ci.host, ci.port); conn = amqp_new_connection(); amqp_set_sockfd(conn, s); die_rpc(amqp_login(conn, ci.vhost, 0, 131072, 0, AMQP_SASL_METHOD_PLAIN, ci.user, ci.password), "logging in to AMQP server"); if (!amqp_channel_open(conn, 1)) { die_rpc(amqp_get_rpc_reply(conn), "opening channel"); } return conn; }
void close_connection(amqp_connection_state_t conn) { int res; die_rpc(amqp_channel_close(conn, 1, AMQP_REPLY_SUCCESS), "closing channel"); die_rpc(amqp_connection_close(conn, AMQP_REPLY_SUCCESS), "closing connection"); res = amqp_destroy_connection(conn); die_amqp_error(res, "closing connection"); }
amqp_connection_state_t make_connection(void) { int status; amqp_socket_t *socket = NULL; struct amqp_uri *uri = alloc_amqp_uri(); amqp_connection_state_t conn; init_connection_info(uri); conn = amqp_new_connection(); if (uri->ssl) { #ifdef WITH_SSL socket = amqp_ssl_socket_new(conn); if (!socket) { die("creating SSL/TLS socket"); } if (amqp_cacert) { amqp_ssl_socket_set_cacert(socket, amqp_cacert); } if (amqp_key) { amqp_ssl_socket_set_key(socket, amqp_cert, amqp_key); } #else die("librabbitmq was not built with SSL/TLS support"); #endif } else { socket = amqp_tcp_socket_new(conn); if (!socket) { die("creating TCP socket (out of memory)"); } } host_port_t * hp = ((host_port_t *)uri->host_port_array.elts); status = amqp_socket_open(socket, hp->host, hp->port); if (status) { die("opening socket to %s:%d", hp->host, hp->port); } die_rpc(amqp_login(conn, uri->vhost, 0, 131072, amqp_heartbeat, AMQP_SASL_METHOD_PLAIN, uri->user, uri->password), "logging in to AMQP server"); if (!amqp_channel_open(conn, 1)) { die_rpc(amqp_get_rpc_reply(conn), "opening channel"); } free_amqp_uri(uri); return conn; }
amqp_connection_state_t make_connection(void) { int status; amqp_socket_t *socket = NULL; struct amqp_connection_info ci; amqp_connection_state_t conn; init_connection_info(&ci); conn = amqp_new_connection(); if (ci.ssl) { #ifdef WITH_SSL socket = amqp_ssl_socket_new(); if (!socket) { die("creating SSL/TLS socket"); } if (amqp_cacert) { amqp_ssl_socket_set_cacert(socket, amqp_cacert); } if (amqp_key) { amqp_ssl_socket_set_key(socket, amqp_cert, amqp_key); } #else die("librabbitmq was not built with SSL/TLS support"); #endif } else { socket = amqp_tcp_socket_new(); if (!socket) { die("creating TCP socket (out of memory)"); } } status = amqp_socket_open(socket, ci.host, ci.port); if (status) { die("opening socket to %s:%d", ci.host, ci.port); } amqp_set_socket(conn, socket); die_rpc(amqp_login(conn, ci.vhost, 0, 131072, 0, AMQP_SASL_METHOD_PLAIN, ci.user, ci.password), "logging in to AMQP server"); if (!amqp_channel_open(conn, 1)) { die_rpc(amqp_get_rpc_reply(conn), "opening channel"); } return conn; }
static int do_get(amqp_connection_state_t conn, char *queue) { amqp_rpc_reply_t r = amqp_basic_get(conn, 1, cstring_bytes(queue), 1); die_rpc(r, "basic.get"); if (r.reply.id == AMQP_BASIC_GET_EMPTY_METHOD) return 0; copy_body(conn, 1); return 1; }
int main(int argc, const char **argv) { amqp_connection_state_t conn; char *queue = NULL; int durable = 0; struct poptOption options[] = { INCLUDE_OPTIONS(connect_options), { "queue", 'q', POPT_ARG_STRING, &queue, 0, "the queue name to declare, or the empty string", "queue" }, { "durable", 'd', POPT_ARG_VAL, &durable, 1, "declare a durable queue", NULL }, POPT_AUTOHELP { NULL, '\0', 0, NULL, 0, NULL, NULL } }; process_all_options(argc, argv, options); if (queue == NULL) { fprintf(stderr, "queue name not specified\n"); return 1; } conn = make_connection(); { amqp_queue_declare_ok_t *reply = amqp_queue_declare(conn, 1, cstring_bytes(queue), 0, durable, 0, 0, amqp_empty_table); if (reply == NULL) { die_rpc(amqp_get_rpc_reply(conn), "queue.declare"); } printf("%.*s\n", (int)reply->queue.len, (char *)reply->queue.bytes); } close_connection(conn); return 0; }
int main(int argc, const char **argv) { amqp_connection_state_t conn; char *queue = NULL; int if_unused = 0; int if_empty = 0; struct poptOption options[] = { INCLUDE_OPTIONS(connect_options), {"queue", 'q', POPT_ARG_STRING, &queue, 0, "the queue name to delete", "queue"}, {"if-unused", 'u', POPT_ARG_VAL, &if_unused, 1, "do not delete unless queue is unused", NULL}, {"if-empty", 'e', POPT_ARG_VAL, &if_empty, 1, "do not delete unless queue is empty", NULL}, POPT_AUTOHELP { NULL, 0, 0, NULL, 0 } }; process_all_options(argc, argv, options); if (queue == NULL || *queue == '\0') { fprintf(stderr, "queue name not specified\n"); return 1; } conn = make_connection(); { amqp_queue_delete_ok_t *reply = amqp_queue_delete(conn, 1, cstring_bytes(queue), if_unused, if_empty); if (reply == NULL) { die_rpc(amqp_get_rpc_reply(conn), "queue.delete"); } printf("%u\n", reply->message_count); } close_connection(conn); return 0; }
int lc_bus_publish_unicast(const char *buf, int buf_len, uint32_t queue_id) { amqp_connection_state_t *conn = NULL; amqp_bytes_t body = { len: buf_len, bytes: (void *)buf }; amqp_basic_properties_t props; int res = 0; if (!buf || queue_id >= NUM_LC_BUS_QUEUE) { return LC_BUS_ERR; } conn = lc_bus_get_connection(); if (!conn) { return LC_BUS_ERR; } memset(&props, 0, sizeof props); props._flags = AMQP_BASIC_DELIVERY_MODE_FLAG; props.delivery_mode = 2; /* persistent delivery mode */ res = amqp_basic_publish( *conn, LC_BUS_CHANNEL, amqp_cstring_bytes(lc_bus_exchange[LC_BUS_EXCHANGE_DIRECT][0]), amqp_cstring_bytes(lc_bus_queue[queue_id]), 0, /* mandatory */ 0, /* immediate */ &props, body ); if (res) { LB_SYSLOG(LOG_ERR, "failed channel=%u queue=%u ret=%d\n", LC_BUS_CHANNEL, queue_id, res); return LC_BUS_ERR; } else { LB_SYSLOG(LOG_INFO, "succeed channel=%u queue=%u\n", LC_BUS_CHANNEL, queue_id); } return LC_BUS_OK; } int lc_bus_publish_broadcast(const char *buf, int buf_len) { /* TODO */ return LC_BUS_OK; } int lc_bus_publish_multicast(const char *buf, int buf_len) { /* TODO */ return LC_BUS_OK; } static int consumer_queueids[NUM_LC_BUS_QUEUE]; static int lc_bus_init_consumer_atomic(amqp_connection_state_t *conn, uint32_t flag, uint32_t queue_id) { amqp_basic_consume_ok_t *res_consume = NULL; amqp_rpc_reply_t ret; if (queue_id >= NUM_LC_BUS_QUEUE) { return LC_BUS_ERR; } #if 0 amqp_basic_qos_ok_t *res_qos = NULL; if (count > 0 && count <= 65535 && !(res_qos = amqp_basic_qos( *conn, 1, 0, count, 0 ))) { die_rpc(amqp_get_rpc_reply(*conn), "basic.qos"); return LC_BUS_ERR; } #endif res_consume = amqp_basic_consume( *conn, LC_BUS_CHANNEL, amqp_cstring_bytes(lc_bus_queue[queue_id]), amqp_empty_bytes, /* consumer_tag */ 0, /* no_local */ (flag & LC_BUS_CONSUME_WITH_ACK) ? 0 : 1, /* no_ack */ 0, /* exclusive */ amqp_empty_table /* arguments */ ); if (!res_consume) { ret = amqp_get_rpc_reply(*conn); if (ret.reply_type != AMQP_RESPONSE_NORMAL) { LB_SYSLOG(LOG_ERR, "[%s] failed, channel=%u %s\n", lc_bus_queue[queue_id], LC_BUS_CHANNEL, amqp_rpc_reply_string(&ret)); return LC_BUS_ERR; } } consumer_queueids[queue_id] = 1; LB_SYSLOG(LOG_INFO, "[%s] succeed channel=%u.\n", lc_bus_queue[queue_id], LC_BUS_CHANNEL); return LC_BUS_OK; }