static int camqp_create_exchange (camqp_config_t *conf) /* {{{ */ { amqp_exchange_declare_ok_t *ed_ret; if (conf->exchange_type == NULL) return (0); ed_ret = amqp_exchange_declare (conf->connection, /* channel = */ CAMQP_CHANNEL, /* exchange = */ amqp_cstring_bytes (conf->exchange), /* type = */ amqp_cstring_bytes (conf->exchange_type), /* passive = */ 0, /* durable = */ 0, /* auto_delete = */ 1, /* arguments = */ AMQP_EMPTY_TABLE); if ((ed_ret == NULL) && camqp_is_error (conf)) { char errbuf[1024]; ERROR ("amqp plugin: amqp_exchange_declare failed: %s", camqp_strerror (conf, errbuf, sizeof (errbuf))); camqp_close_connection (conf); return (-1); } INFO ("amqp plugin: Successfully created exchange \"%s\" " "with type \"%s\".", conf->exchange, conf->exchange_type); return (0); } /* }}} int camqp_create_exchange */
int main(int argc, char const *const *argv) { char const *hostname; int port, status; char const *exchange; char const *exchangetype; amqp_socket_t *socket; amqp_connection_state_t conn; if (argc < 5) { fprintf(stderr, "Usage: amqps_exchange_declare host port exchange " "exchangetype [cacert.pem [key.pem cert.pem]]\n"); return 1; } hostname = argv[1]; port = atoi(argv[2]); exchange = argv[3]; exchangetype = argv[4]; conn = amqp_new_connection(); socket = amqp_ssl_socket_new(conn); if (!socket) { die("creating SSL/TLS socket"); } if (argc > 5) { status = amqp_ssl_socket_set_cacert(socket, argv[5]); if (status) { die("setting CA certificate"); } } if (argc > 7) { status = amqp_ssl_socket_set_key(socket, argv[7], argv[6]); if (status) { die("setting client key/cert"); } } status = amqp_socket_open(socket, hostname, port); if (status) { die("opening SSL/TLS connection"); } die_on_amqp_error(amqp_login(conn, "/", 0, 131072, 0, AMQP_SASL_METHOD_PLAIN, "guest", "guest"), "Logging in"); amqp_channel_open(conn, 1); die_on_amqp_error(amqp_get_rpc_reply(conn), "Opening channel"); amqp_exchange_declare(conn, 1, amqp_cstring_bytes(exchange), amqp_cstring_bytes(exchangetype), 0, 0, amqp_empty_table); die_on_amqp_error(amqp_get_rpc_reply(conn), "Declaring exchange"); die_on_amqp_error(amqp_channel_close(conn, 1, AMQP_REPLY_SUCCESS), "Closing channel"); die_on_amqp_error(amqp_connection_close(conn, AMQP_REPLY_SUCCESS), "Closing connection"); die_on_error(amqp_destroy_connection(conn), "Ending connection"); return 0; }
int main(int argc, const char **argv) { const char *hostname; int port; const char *exchange; const char *routingkey; const char *messagebody; const char *exchangetype = "direct"; if (argc < 6) { fprintf(stderr, "Usage: emit_log_direct host port exchange routingkey messagebody\n"); return 1; } hostname = argv[1]; port = atoi(argv[2]); exchange = argv[3]; routingkey = argv[4]; messagebody = argv[5]; int sockfd; int channelid = 1; amqp_connection_state_t conn; conn = amqp_new_connection(); die_on_error(sockfd = amqp_open_socket(hostname, port), "Opening socket"); amqp_set_sockfd(conn, sockfd); die_on_amqp_error(amqp_login(conn, "/", 0, 131072, 0, AMQP_SASL_METHOD_PLAIN, "guest", "guest"),"Logging in"); amqp_channel_open(conn, channelid); die_on_amqp_error(amqp_get_rpc_reply(conn), "Opening channel"); amqp_exchange_declare(conn,channelid,amqp_cstring_bytes(exchange),amqp_cstring_bytes(exchangetype),0,1, amqp_empty_table); die_on_amqp_error(amqp_get_rpc_reply(conn),"Declaring exchange"); { amqp_basic_properties_t props; props._flags = AMQP_BASIC_DELIVERY_MODE_FLAG; /*props.content_type = amqp_cstring_bytes("text/plain");*/ props.delivery_mode = 2; /* persistent delivery mode */ die_on_error(amqp_basic_publish(conn, channelid, amqp_cstring_bytes(exchange), amqp_cstring_bytes(routingkey), 0, 0, &props, amqp_cstring_bytes(messagebody)), "Publishing"); } die_on_amqp_error(amqp_channel_close(conn, 1, AMQP_REPLY_SUCCESS), "Closing channel"); die_on_amqp_error(amqp_connection_close(conn, AMQP_REPLY_SUCCESS), "Closing connection"); die_on_error(amqp_destroy_connection(conn), "Ending connection"); return 0; }
void MQChannel::DeclareExchange(const MQExchangeParams ¶ms) { amqp_exchange_declare( mConn, mChannel, amqp_cstring_bytes(params.name.c_str()), amqp_cstring_bytes(params.type.c_str()), params.passive, params.durable, params.args); }
static gboolean afamqp_dd_connect(AMQPDestDriver *self, gboolean reconnect) { int sockfd; amqp_rpc_reply_t ret; if (reconnect && self->conn) { ret = amqp_get_rpc_reply(self->conn); if (ret.reply_type == AMQP_RESPONSE_NORMAL) return TRUE; } self->conn = amqp_new_connection(); sockfd = amqp_open_socket(self->host, self->port); if (sockfd < 0) { gchar *errstr = amqp_error_string(-sockfd); msg_error("Error connecting to AMQP server", evt_tag_str("driver", self->super.super.super.id), evt_tag_str("error", errstr), evt_tag_int("time_reopen", self->super.time_reopen), NULL); g_free(errstr); return FALSE; } amqp_set_sockfd(self->conn, sockfd); ret = amqp_login(self->conn, self->vhost, 0, 131072, 0, AMQP_SASL_METHOD_PLAIN, self->user, self->password); if (!afamqp_is_ok(self, "Error during AMQP login", ret)) return FALSE; amqp_channel_open(self->conn, 1); ret = amqp_get_rpc_reply(self->conn); if (!afamqp_is_ok(self, "Error during AMQP channel open", ret)) return FALSE; if (self->declare) { amqp_exchange_declare(self->conn, 1, amqp_cstring_bytes(self->exchange), amqp_cstring_bytes(self->exchange_type), 0, 0, amqp_empty_table); ret = amqp_get_rpc_reply(self->conn); if (!afamqp_is_ok(self, "Error during AMQP exchange declaration", ret)) return FALSE; } msg_debug ("Connecting to AMQP succeeded", evt_tag_str("driver", self->super.super.super.id), NULL); return TRUE; }
bool initAmqp(amqp_connection_state_t &conn, amqp_socket_t *socket, int &status, amqp_bytes_t &queuename) { conn = amqp_new_connection(); socket = amqp_tcp_socket_new(conn); if (!socket) { die("creating TCP socket"); return false; } status = amqp_socket_open(socket, hostname, port); if (status) { die("opening TCP socket"); return false; } if (!die_on_amqp_error(amqp_login(conn, "/", 0, 131072, 0, AMQP_SASL_METHOD_PLAIN, amqp_user, amqp_pass), "Logging in")) { return false; }; amqp_channel_open(conn, 1); if (!die_on_amqp_error(amqp_get_rpc_reply(conn), "Opening channel")) { return false; } // passive 0 durable 1 auto-delete 0 internal 0 exchange amqp_exchange_declare_ok_t_ *er = amqp_exchange_declare(conn, 1, amqp_cstring_bytes(exchange), amqp_cstring_bytes(exchange_type), 0, 1, 0, 0, amqp_empty_table); if (!die_on_amqp_error(amqp_get_rpc_reply(conn), "Declaring exchange")) { return false; } // passive 0 durable 1 exclusive 0 auto-delete 0 queue amqp_queue_declare_ok_t *r = amqp_queue_declare(conn, 1, amqp_cstring_bytes(bindingkey), 0, 1, 0, 0, amqp_empty_table); if (!die_on_amqp_error(amqp_get_rpc_reply(conn), "Declaring queue")) { return false; } queuename = amqp_bytes_malloc_dup(r->queue); if (queuename.bytes == NULL) { fprintf(stderr, "Out of memory while copying queue name"); return false; } amqp_queue_bind(conn, 1, queuename, amqp_cstring_bytes(exchange), amqp_cstring_bytes(bindingkey), amqp_empty_table); if (!die_on_amqp_error(amqp_get_rpc_reply(conn), "Binding queue")) { return false; } // no-local 0 no-ack 0 exclusive 0 consumer amqp_basic_consume(conn, 1, queuename, amqp_cstring_bytes(consumer_name), 1, 0, 0, amqp_empty_table); if (!die_on_amqp_error(amqp_get_rpc_reply(conn), "Consuming")) { return false; } return true; }
int main(int argc, char const *const *argv) { char const *hostname; int port, status; char const *exchange; char const *exchangetype; amqp_socket_t *socket = NULL; amqp_connection_state_t conn; if (argc < 5) { fprintf(stderr, "Usage: amqp_exchange_declare host port exchange exchangetype\n"); return 1; } hostname = argv[1]; port = atoi(argv[2]); exchange = argv[3]; exchangetype = argv[4]; conn = amqp_new_connection(); socket = amqp_tcp_socket_new(); if (!socket) { die("creating TCP socket"); } status = amqp_socket_open(socket, hostname, port); if (status) { die("opening TCP socket"); } amqp_set_socket(conn, socket); die_on_amqp_error(amqp_login(conn, "/", 0, 131072, 0, AMQP_SASL_METHOD_PLAIN, "guest", "guest"), "Logging in"); amqp_channel_open(conn, 1); die_on_amqp_error(amqp_get_rpc_reply(conn), "Opening channel"); amqp_exchange_declare(conn, 1, amqp_cstring_bytes(exchange), amqp_cstring_bytes(exchangetype), 0, 0, amqp_empty_table); die_on_amqp_error(amqp_get_rpc_reply(conn), "Declaring exchange"); die_on_amqp_error(amqp_channel_close(conn, 1, AMQP_REPLY_SUCCESS), "Closing channel"); die_on_amqp_error(amqp_connection_close(conn, AMQP_REPLY_SUCCESS), "Closing connection"); die_on_error(amqp_destroy_connection(conn), "Ending connection"); return 0; }
void RabbitInBoundConnectionPoint::ConnectPoint() throw (ConnectException) { ConnectRabbit(); amqp_exchange_declare(_conn, 1, amqp_cstring_bytes(_exchange), amqp_cstring_bytes(_exchangetype), 0, 1, amqp_empty_table); GetRabbitError("Declaracion Exchange"); amqp_queue_declare(_conn, 1, amqp_cstring_bytes(_queue), 0, 1, 0, 0, amqp_empty_table); GetRabbitError("Declaracion Queue"); amqp_queue_bind(_conn, 1, amqp_cstring_bytes(_queue), amqp_cstring_bytes(_exchange), amqp_cstring_bytes(_routingKey), amqp_empty_table); GetRabbitError("Binding Queue"); amqp_basic_consume(_conn, 1, amqp_cstring_bytes(_queue), amqp_empty_bytes, 0, 0, 0, amqp_empty_table); GetRabbitError("Binding Queue"); }
static int camqp_create_exchange (camqp_config_t *conf) /* {{{ */ { amqp_exchange_declare_ok_t *ed_ret; amqp_table_t argument_table; struct amqp_table_entry_t_ argument_table_entries[1]; if (conf->exchange_type == NULL) return (0); /* Valid arguments: "auto_delete", "internal" */ argument_table.num_entries = STATIC_ARRAY_SIZE (argument_table_entries); argument_table.entries = argument_table_entries; argument_table_entries[0].key = amqp_cstring_bytes ("auto_delete"); argument_table_entries[0].value.kind = AMQP_FIELD_KIND_BOOLEAN; argument_table_entries[0].value.value.boolean = 1; ed_ret = amqp_exchange_declare (conf->connection, /* channel = */ CAMQP_CHANNEL, /* exchange = */ amqp_cstring_bytes (conf->exchange), /* type = */ amqp_cstring_bytes (conf->exchange_type), /* passive = */ 0, /* durable = */ 0, #if defined(AMQP_VERSION) && AMQP_VERSION >= 0x00060000 /* auto delete = */ 0, /* internal = */ 0, #endif /* arguments = */ argument_table); if ((ed_ret == NULL) && camqp_is_error (conf)) { char errbuf[1024]; ERROR ("amqp plugin: amqp_exchange_declare failed: %s", camqp_strerror (conf, errbuf, sizeof (errbuf))); camqp_close_connection (conf); return (-1); } INFO ("amqp plugin: Successfully created exchange \"%s\" " "with type \"%s\".", conf->exchange, conf->exchange_type); return (0); } /* }}} int camqp_create_exchange */
void AMQPExchange::sendDeclareCommand() { checkType(); amqp_bytes_t exchange = amqp_cstring_bytes(name.c_str()); amqp_bytes_t exchangetype = amqp_cstring_bytes(type.c_str()); amqp_table_t args; args.num_entries = 0; args.entries = NULL; amqp_boolean_t passive = (parms & AMQP_PASSIVE) ? 1:0; //amqp_boolean_t autodelete = (parms & AMQP_AUTODELETE) ? 1:0; amqp_boolean_t durable = (parms & AMQP_DURABLE) ? 1:0; //amqp_exchange_declare(*cnn, (amqp_channel_t) 1, exchange, exchangetype, passive, durable, autodelete, args ); //for some reason rabbitmq-c doesn't have auto-delete now... amqp_exchange_declare(*cnn, (amqp_channel_t) 1, exchange, exchangetype, passive, durable, args ); amqp_rpc_reply_t res =amqp_get_rpc_reply(*cnn); AMQPBase::checkReply(&res); }
void RabbitMQConnection::declareExchange (const std::string &exchange_name, const std::string &type, bool durable, const int ttl) { amqp_bytes_t exchange = amqp_cstring_bytes (exchange_name.c_str() ); amqp_bytes_t exchange_type = amqp_cstring_bytes (type.c_str() ); amqp_table_entry_t entries[1]; amqp_table_t table; table.entries = entries; if (ttl > 0) { table.num_entries = 1; entries[0].key = amqp_cstring_bytes ("x-expires"); entries[0].value.kind = AMQP_FIELD_KIND_I32; entries[0].value.value.i32 = ttl; } else { table.num_entries = 0; } amqp_exchange_declare (conn, 1, exchange, exchange_type, /* passive */ false, durable, table); exception_on_error (amqp_get_rpc_reply (conn), "Declaring exchange"); }
static int lc_bus_exchange_init(amqp_connection_state_t *conn, uint32_t exchange_id) { amqp_exchange_declare_ok_t *res = NULL; amqp_rpc_reply_t ret; if (exchange_id >= NUM_LC_BUS_EXCHANGE) { return LC_BUS_ERR; } res = amqp_exchange_declare( *conn, LC_BUS_CHANNEL, amqp_cstring_bytes(lc_bus_exchange[exchange_id][0]), amqp_cstring_bytes(lc_bus_exchange[exchange_id][1]), 0, /* passive */ 1, /* durable */ 0, 0, amqp_empty_table /* arguments */ ); if (!res) { ret = amqp_get_rpc_reply(*conn); if (ret.reply_type != AMQP_RESPONSE_NORMAL) { LB_SYSLOG(LOG_ERR, "[%s,%s] failed, channel=%u %s\n", lc_bus_exchange[exchange_id][0], lc_bus_exchange[exchange_id][1], LC_BUS_CHANNEL, amqp_rpc_reply_string(&ret)); return LC_BUS_ERR; } } LB_SYSLOG(LOG_INFO, "[%s,%s] succeed, channel=%u.\n", lc_bus_exchange[exchange_id][0], lc_bus_exchange[exchange_id][1], LC_BUS_CHANNEL); return LC_BUS_OK; }
/************************************************************************* ************************************************************************** * * Namn : rmq_connect * * Typ : int * * Typ Parameter IOGF Beskrivning * * Beskrivning : Connect to RabbitMQ broker. * ************************************************************************** **************************************************************************/ int rmq_connect() { int sts; amqp_rpc_reply_t rep; amqp_channel_open_ok_t* co; if (!ctx->conn) { ctx->conn = amqp_new_connection(); // printf( "Connection : %u\n", (unsigned int)ctx->conn); } if (!ctx->socket) { ctx->socket = (amqp_socket_t*)amqp_tcp_socket_new(ctx->conn); if (!ctx->socket) { printf("Socket error\n"); return 0; } sts = amqp_socket_open(ctx->socket, ctx->op->Server, ctx->op->Port); if (sts) { printf("Socket open error %d\n", sts); ctx->socket = 0; return 0; } } rep = amqp_login(ctx->conn, "/", 0, 131072, 0, AMQP_SASL_METHOD_PLAIN, ctx->op->User, ctx->op->Password); if (rep.reply_type != AMQP_RESPONSE_NORMAL) { if (rep.reply_type == AMQP_RESPONSE_LIBRARY_EXCEPTION) printf("Login failure, not authorized? %d\n", rep.reply_type); else printf("Login failure: %d\n", rep.reply_type); return 0; } if (!ctx->channel) { if (ctx->op->Channel == 0) ctx->channel = 1; else ctx->channel = ctx->op->Channel; co = amqp_channel_open(ctx->conn, ctx->channel); if (!co) { printf("Channel not open\n"); ctx->channel = 0; } else { printf("Channel open %s\n", (char*)co->channel_id.bytes); } } /* Declare send queue */ if (ctx->is_producer) { // 0 passive 0 durable 0 exclusive 0 auto-delete amqp_queue_declare_ok_t* qd = amqp_queue_declare(ctx->conn, ctx->channel, amqp_cstring_bytes(ctx->op->SendQueue), 0, ctx->op->Durable, 0, 0, amqp_empty_table); if (!qd) { printf("SendQueue not declared\n"); } else { printf("SendQueue %s message cnt %d, consumer cnt %d\n", (char*)qd->queue.bytes, qd->message_count, qd->consumer_count); } } /* Declare receive queue */ if (ctx->is_consumer) { // 0 passive 0 durable 0 exclusive 0 auto-delete amqp_queue_declare_ok_t* qd = amqp_queue_declare(ctx->conn, ctx->channel, amqp_cstring_bytes(ctx->op->ReceiveQueue), 0, ctx->op->Durable, 0, 0, amqp_empty_table); if (!qd) { printf("ReceiveQueue not declared\n"); } else { printf("ReceiveQueue %s message cnt %d, consumer cnt %d\n", (char*)qd->queue.bytes, qd->message_count, qd->consumer_count); } } if (ctx->is_producer && !streq(ctx->op->Exchange, "")) #if AMQP_VERSION_MAJOR == 0 && AMQP_VERSION_MINOR < 6 amqp_exchange_declare(ctx->conn, ctx->channel, amqp_cstring_bytes(ctx->op->Exchange), amqp_cstring_bytes("fanout"), 0, ctx->op->Durable, amqp_empty_table); #else amqp_exchange_declare(ctx->conn, ctx->channel, amqp_cstring_bytes(ctx->op->Exchange), amqp_cstring_bytes("fanout"), 0, ctx->op->Durable, 0, 0, amqp_empty_table); #endif if (ctx->is_producer && !streq(ctx->op->Exchange, "")) amqp_queue_bind(ctx->conn, ctx->channel, amqp_cstring_bytes(ctx->op->SendQueue), amqp_cstring_bytes(ctx->op->Exchange), amqp_cstring_bytes("exchange-key"), amqp_empty_table); amqp_basic_consume_ok_t* bc; // 0 no-local 1 no-ack 0 exclusive if (ctx->is_consumer) { bc = amqp_basic_consume(ctx->conn, ctx->channel, amqp_cstring_bytes(ctx->op->ReceiveQueue), amqp_empty_bytes, 0, !ctx->op->Acknowledge, 0, amqp_empty_table); if (!bc) printf("Consumer error\n"); else printf("Consumer tag: %s\n", (char*)bc->consumer_tag.bytes); } return 1; }
/* Transport implementation */ int janus_rabbitmq_init(janus_transport_callbacks *callback, const char *config_path) { if(g_atomic_int_get(&stopping)) { /* Still stopping from before */ return -1; } if(callback == NULL || config_path == NULL) { /* Invalid arguments */ return -1; } /* This is the callback we'll need to invoke to contact the Janus core */ gateway = callback; /* Read configuration */ char filename[255]; g_snprintf(filename, 255, "%s/%s.jcfg", config_path, JANUS_RABBITMQ_PACKAGE); JANUS_LOG(LOG_VERB, "Configuration file: %s\n", filename); janus_config *config = janus_config_parse(filename); if(config == NULL) { JANUS_LOG(LOG_WARN, "Couldn't find .jcfg configuration file (%s), trying .cfg\n", JANUS_RABBITMQ_PACKAGE); g_snprintf(filename, 255, "%s/%s.cfg", config_path, JANUS_RABBITMQ_PACKAGE); JANUS_LOG(LOG_VERB, "Configuration file: %s\n", filename); config = janus_config_parse(filename); } if(config != NULL) janus_config_print(config); janus_config_category *config_general = janus_config_get_create(config, NULL, janus_config_type_category, "general"); janus_config_category *config_admin = janus_config_get_create(config, NULL, janus_config_type_category, "admin"); janus_config_item *item = janus_config_get(config, config_general, janus_config_type_item, "json"); if(item && item->value) { /* Check how we need to format/serialize the JSON output */ if(!strcasecmp(item->value, "indented")) { /* Default: indented, we use three spaces for that */ json_format = JSON_INDENT(3) | JSON_PRESERVE_ORDER; } else if(!strcasecmp(item->value, "plain")) { /* Not indented and no new lines, but still readable */ json_format = JSON_INDENT(0) | JSON_PRESERVE_ORDER; } else if(!strcasecmp(item->value, "compact")) { /* Compact, so no spaces between separators */ json_format = JSON_COMPACT | JSON_PRESERVE_ORDER; } else { JANUS_LOG(LOG_WARN, "Unsupported JSON format option '%s', using default (indented)\n", item->value); json_format = JSON_INDENT(3) | JSON_PRESERVE_ORDER; } } /* Check if we need to send events to handlers */ janus_config_item *events = janus_config_get(config, config_general, janus_config_type_item, "events"); if(events != NULL && events->value != NULL) notify_events = janus_is_true(events->value); if(!notify_events && callback->events_is_enabled()) { JANUS_LOG(LOG_WARN, "Notification of events to handlers disabled for %s\n", JANUS_RABBITMQ_NAME); } /* Handle configuration, starting from the server details */ item = janus_config_get(config, config_general, janus_config_type_item, "host"); if(item && item->value) rmqhost = g_strdup(item->value); else rmqhost = g_strdup("localhost"); int rmqport = AMQP_PROTOCOL_PORT; item = janus_config_get(config, config_general, janus_config_type_item, "port"); if(item && item->value) rmqport = atoi(item->value); /* Credentials and Virtual Host */ item = janus_config_get(config, config_general, janus_config_type_item, "vhost"); if(item && item->value) vhost = g_strdup(item->value); else vhost = g_strdup("/"); item = janus_config_get(config, config_general, janus_config_type_item, "username"); if(item && item->value) username = g_strdup(item->value); else username = g_strdup("guest"); item = janus_config_get(config, config_general, janus_config_type_item, "password"); if(item && item->value) password = g_strdup(item->value); else password = g_strdup("guest"); /* SSL config*/ gboolean ssl_enabled = FALSE; gboolean ssl_verify_peer = FALSE; gboolean ssl_verify_hostname = FALSE; item = janus_config_get(config, config_general, janus_config_type_item, "ssl_enabled"); if(item == NULL) { /* Try legacy property */ item = janus_config_get(config, config_general, janus_config_type_item, "ssl_enable"); if (item && item->value) { JANUS_LOG(LOG_WARN, "Found deprecated 'ssl_enable' property, please update it to 'ssl_enabled' instead\n"); } } if(!item || !item->value || !janus_is_true(item->value)) { JANUS_LOG(LOG_INFO, "RabbitMQ SSL support disabled\n"); } else { ssl_enabled = TRUE; item = janus_config_get(config, config_general, janus_config_type_item, "ssl_cacert"); if(item && item->value) ssl_cacert_file = g_strdup(item->value); item = janus_config_get(config, config_general, janus_config_type_item, "ssl_cert"); if(item && item->value) ssl_cert_file = g_strdup(item->value); item = janus_config_get(config, config_general, janus_config_type_item, "ssl_key"); if(item && item->value) ssl_key_file = g_strdup(item->value); item = janus_config_get(config, config_general, janus_config_type_item, "ssl_verify_peer"); if(item && item->value && janus_is_true(item->value)) ssl_verify_peer = TRUE; item = janus_config_get(config, config_general, janus_config_type_item, "ssl_verify_hostname"); if(item && item->value && janus_is_true(item->value)) ssl_verify_hostname = TRUE; } /* Now check if the Janus API must be supported */ item = janus_config_get(config, config_general, janus_config_type_item, "enabled"); if(item == NULL) { /* Try legacy property */ item = janus_config_get(config, config_general, janus_config_type_item, "enable"); if (item && item->value) { JANUS_LOG(LOG_WARN, "Found deprecated 'enable' property, please update it to 'enabled' instead\n"); } } if(!item || !item->value || !janus_is_true(item->value)) { JANUS_LOG(LOG_WARN, "RabbitMQ support disabled (Janus API)\n"); } else { /* Parse configuration */ item = janus_config_get(config, config_general, janus_config_type_item, "to_janus"); if(!item || !item->value) { JANUS_LOG(LOG_FATAL, "Missing name of incoming queue for RabbitMQ integration...\n"); goto error; } to_janus = g_strdup(item->value); item = janus_config_get(config, config_general, janus_config_type_item, "from_janus"); if(!item || !item->value) { JANUS_LOG(LOG_FATAL, "Missing name of outgoing queue for RabbitMQ integration...\n"); goto error; } from_janus = g_strdup(item->value); item = janus_config_get(config, config_general, janus_config_type_item, "janus_exchange"); if(!item || !item->value) { JANUS_LOG(LOG_INFO, "Missing name of outgoing exchange for RabbitMQ integration, using default\n"); } else { janus_exchange = g_strdup(item->value); } if (janus_exchange == NULL) { JANUS_LOG(LOG_INFO, "RabbitMQ support for Janus API enabled, %s:%d (%s/%s)\n", rmqhost, rmqport, to_janus, from_janus); } else { JANUS_LOG(LOG_INFO, "RabbitMQ support for Janus API enabled, %s:%d (%s/%s) exch: (%s)\n", rmqhost, rmqport, to_janus, from_janus, janus_exchange); } rmq_janus_api_enabled = TRUE; } /* Do the same for the admin API */ item = janus_config_get(config, config_admin, janus_config_type_item, "admin_enabled"); if(item == NULL) { /* Try legacy property */ item = janus_config_get(config, config_general, janus_config_type_item, "admin_enable"); if (item && item->value) { JANUS_LOG(LOG_WARN, "Found deprecated 'admin_enable' property, please update it to 'admin_enabled' instead\n"); } } if(!item || !item->value || !janus_is_true(item->value)) { JANUS_LOG(LOG_WARN, "RabbitMQ support disabled (Admin API)\n"); } else { /* Parse configuration */ item = janus_config_get(config, config_admin, janus_config_type_item, "to_janus_admin"); if(!item || !item->value) { JANUS_LOG(LOG_FATAL, "Missing name of incoming queue for RabbitMQ integration...\n"); goto error; } to_janus_admin = g_strdup(item->value); item = janus_config_get(config, config_admin, janus_config_type_item, "from_janus_admin"); if(!item || !item->value) { JANUS_LOG(LOG_FATAL, "Missing name of outgoing queue for RabbitMQ integration...\n"); goto error; } from_janus_admin = g_strdup(item->value); JANUS_LOG(LOG_INFO, "RabbitMQ support for Admin API enabled, %s:%d (%s/%s)\n", rmqhost, rmqport, to_janus_admin, from_janus_admin); rmq_admin_api_enabled = TRUE; } if(!rmq_janus_api_enabled && !rmq_admin_api_enabled) { JANUS_LOG(LOG_WARN, "RabbitMQ support disabled for both Janus and Admin API, giving up\n"); goto error; } else { /* FIXME We currently support a single application, create a new janus_rabbitmq_client instance */ rmq_client = g_malloc0(sizeof(janus_rabbitmq_client)); /* Connect */ rmq_client->rmq_conn = amqp_new_connection(); amqp_socket_t *socket = NULL; int status; JANUS_LOG(LOG_VERB, "Creating RabbitMQ socket...\n"); if (ssl_enabled) { socket = amqp_ssl_socket_new(rmq_client->rmq_conn); if(socket == NULL) { JANUS_LOG(LOG_FATAL, "Can't connect to RabbitMQ server: error creating socket...\n"); goto error; } if(ssl_verify_peer) { amqp_ssl_socket_set_verify_peer(socket, 1); } else { amqp_ssl_socket_set_verify_peer(socket, 0); } if(ssl_verify_hostname) { amqp_ssl_socket_set_verify_hostname(socket, 1); } else { amqp_ssl_socket_set_verify_hostname(socket, 0); } if(ssl_cacert_file) { status = amqp_ssl_socket_set_cacert(socket, ssl_cacert_file); if(status != AMQP_STATUS_OK) { JANUS_LOG(LOG_FATAL, "Can't connect to RabbitMQ server: error setting CA certificate... (%s)\n", amqp_error_string2(status)); goto error; } } if(ssl_cert_file && ssl_key_file) { status = amqp_ssl_socket_set_key(socket, ssl_cert_file, ssl_key_file); if(status != AMQP_STATUS_OK) { JANUS_LOG(LOG_FATAL, "Can't connect to RabbitMQ server: error setting key... (%s)\n", amqp_error_string2(status)); goto error; } } } else { socket = amqp_tcp_socket_new(rmq_client->rmq_conn); if(socket == NULL) { JANUS_LOG(LOG_FATAL, "Can't connect to RabbitMQ server: error creating socket...\n"); goto error; } } JANUS_LOG(LOG_VERB, "Connecting to RabbitMQ server...\n"); status = amqp_socket_open(socket, rmqhost, rmqport); if(status != AMQP_STATUS_OK) { JANUS_LOG(LOG_FATAL, "Can't connect to RabbitMQ server: error opening socket... (%s)\n", amqp_error_string2(status)); goto error; } JANUS_LOG(LOG_VERB, "Logging in...\n"); amqp_rpc_reply_t result = amqp_login(rmq_client->rmq_conn, vhost, 0, 131072, 0, AMQP_SASL_METHOD_PLAIN, username, password); if(result.reply_type != AMQP_RESPONSE_NORMAL) { JANUS_LOG(LOG_FATAL, "Can't connect to RabbitMQ server: error logging in... %s, %s\n", amqp_error_string2(result.library_error), amqp_method_name(result.reply.id)); goto error; } rmq_client->rmq_channel = 1; JANUS_LOG(LOG_VERB, "Opening channel...\n"); amqp_channel_open(rmq_client->rmq_conn, rmq_client->rmq_channel); result = amqp_get_rpc_reply(rmq_client->rmq_conn); if(result.reply_type != AMQP_RESPONSE_NORMAL) { JANUS_LOG(LOG_FATAL, "Can't connect to RabbitMQ server: error opening channel... %s, %s\n", amqp_error_string2(result.library_error), amqp_method_name(result.reply.id)); goto error; } rmq_client->janus_exchange = amqp_empty_bytes; if(janus_exchange != NULL) { JANUS_LOG(LOG_VERB, "Declaring exchange...\n"); rmq_client->janus_exchange = amqp_cstring_bytes(janus_exchange); amqp_exchange_declare(rmq_client->rmq_conn, rmq_client->rmq_channel, rmq_client->janus_exchange, amqp_cstring_bytes(JANUS_RABBITMQ_EXCHANGE_TYPE), 0, 0, 0, 0, amqp_empty_table); result = amqp_get_rpc_reply(rmq_client->rmq_conn); if(result.reply_type != AMQP_RESPONSE_NORMAL) { JANUS_LOG(LOG_FATAL, "Can't connect to RabbitMQ server: error diclaring exchange... %s, %s\n", amqp_error_string2(result.library_error), amqp_method_name(result.reply.id)); goto error; } } rmq_client->janus_api_enabled = FALSE; if(rmq_janus_api_enabled) { rmq_client->janus_api_enabled = TRUE; JANUS_LOG(LOG_VERB, "Declaring incoming queue... (%s)\n", to_janus); rmq_client->to_janus_queue = amqp_cstring_bytes(to_janus); amqp_queue_declare(rmq_client->rmq_conn, rmq_client->rmq_channel, rmq_client->to_janus_queue, 0, 0, 0, 0, amqp_empty_table); result = amqp_get_rpc_reply(rmq_client->rmq_conn); if(result.reply_type != AMQP_RESPONSE_NORMAL) { JANUS_LOG(LOG_FATAL, "Can't connect to RabbitMQ server: error declaring queue... %s, %s\n", amqp_error_string2(result.library_error), amqp_method_name(result.reply.id)); goto error; } JANUS_LOG(LOG_VERB, "Declaring outgoing queue... (%s)\n", from_janus); rmq_client->from_janus_queue = amqp_cstring_bytes(from_janus); amqp_queue_declare(rmq_client->rmq_conn, rmq_client->rmq_channel, rmq_client->from_janus_queue, 0, 0, 0, 0, amqp_empty_table); result = amqp_get_rpc_reply(rmq_client->rmq_conn); if(result.reply_type != AMQP_RESPONSE_NORMAL) { JANUS_LOG(LOG_FATAL, "Can't connect to RabbitMQ server: error declaring queue... %s, %s\n", amqp_error_string2(result.library_error), amqp_method_name(result.reply.id)); goto error; } amqp_basic_consume(rmq_client->rmq_conn, rmq_client->rmq_channel, rmq_client->to_janus_queue, amqp_empty_bytes, 0, 1, 0, amqp_empty_table); result = amqp_get_rpc_reply(rmq_client->rmq_conn); if(result.reply_type != AMQP_RESPONSE_NORMAL) { JANUS_LOG(LOG_FATAL, "Can't connect to RabbitMQ server: error consuming... %s, %s\n", amqp_error_string2(result.library_error), amqp_method_name(result.reply.id)); goto error; } } rmq_client->admin_api_enabled = FALSE; if(rmq_admin_api_enabled) { rmq_client->admin_api_enabled = TRUE; JANUS_LOG(LOG_VERB, "Declaring incoming queue... (%s)\n", to_janus_admin); rmq_client->to_janus_admin_queue = amqp_cstring_bytes(to_janus_admin); amqp_queue_declare(rmq_client->rmq_conn, rmq_client->rmq_channel, rmq_client->to_janus_admin_queue, 0, 0, 0, 0, amqp_empty_table); result = amqp_get_rpc_reply(rmq_client->rmq_conn); if(result.reply_type != AMQP_RESPONSE_NORMAL) { JANUS_LOG(LOG_FATAL, "Can't connect to RabbitMQ server: error declaring queue... %s, %s\n", amqp_error_string2(result.library_error), amqp_method_name(result.reply.id)); goto error; } JANUS_LOG(LOG_VERB, "Declaring outgoing queue... (%s)\n", from_janus_admin); rmq_client->from_janus_admin_queue = amqp_cstring_bytes(from_janus_admin); amqp_queue_declare(rmq_client->rmq_conn, rmq_client->rmq_channel, rmq_client->from_janus_admin_queue, 0, 0, 0, 0, amqp_empty_table); result = amqp_get_rpc_reply(rmq_client->rmq_conn); if(result.reply_type != AMQP_RESPONSE_NORMAL) { JANUS_LOG(LOG_FATAL, "Can't connect to RabbitMQ server: error declaring queue... %s, %s\n", amqp_error_string2(result.library_error), amqp_method_name(result.reply.id)); goto error; } amqp_basic_consume(rmq_client->rmq_conn, rmq_client->rmq_channel, rmq_client->to_janus_admin_queue, amqp_empty_bytes, 0, 1, 0, amqp_empty_table); result = amqp_get_rpc_reply(rmq_client->rmq_conn); if(result.reply_type != AMQP_RESPONSE_NORMAL) { JANUS_LOG(LOG_FATAL, "Can't connect to RabbitMQ server: error consuming... %s, %s\n", amqp_error_string2(result.library_error), amqp_method_name(result.reply.id)); goto error; } } rmq_client->messages = g_async_queue_new(); rmq_client->destroy = 0; /* Prepare the transport session (again, just one) */ rmq_session = janus_transport_session_create(rmq_client, NULL); /* Start the threads */ GError *error = NULL; rmq_client->in_thread = g_thread_try_new("rmq_in_thread", &janus_rmq_in_thread, rmq_client, &error); if(error != NULL) { /* Something went wrong... */ JANUS_LOG(LOG_FATAL, "Got error %d (%s) trying to launch the RabbitMQ incoming thread...\n", error->code, error->message ? error->message : "??"); janus_transport_session_destroy(rmq_session); g_free(rmq_client); janus_config_destroy(config); return -1; } rmq_client->out_thread = g_thread_try_new("rmq_out_thread", &janus_rmq_out_thread, rmq_client, &error); if(error != NULL) { /* Something went wrong... */ JANUS_LOG(LOG_FATAL, "Got error %d (%s) trying to launch the RabbitMQ outgoing thread...\n", error->code, error->message ? error->message : "??"); janus_transport_session_destroy(rmq_session); g_free(rmq_client); janus_config_destroy(config); return -1; } janus_mutex_init(&rmq_client->mutex); /* Done */ JANUS_LOG(LOG_INFO, "Setup of RabbitMQ integration completed\n"); /* Notify handlers about this new transport */ if(notify_events && gateway->events_is_enabled()) { json_t *info = json_object(); json_object_set_new(info, "event", json_string("connected")); gateway->notify_event(&janus_rabbitmq_transport, rmq_session, info); } } janus_config_destroy(config); config = NULL; /* Done */ g_atomic_int_set(&initialized, 1); JANUS_LOG(LOG_INFO, "%s initialized!\n", JANUS_RABBITMQ_NAME); return 0; error: /* If we got here, something went wrong */ g_free(rmq_client); g_free(rmqhost); g_free(vhost); g_free(username); g_free(password); g_free(janus_exchange); g_free(to_janus); g_free(from_janus); g_free(to_janus_admin); g_free(from_janus_admin); g_free(ssl_cacert_file); g_free(ssl_cert_file); g_free(ssl_key_file); if(config) janus_config_destroy(config); return -1; }
static gboolean afamqp_dd_connect(AMQPDestDriver *self, gboolean reconnect) { int sockfd_ret; amqp_rpc_reply_t ret; if (reconnect && self->conn) { ret = amqp_get_rpc_reply(self->conn); if (ret.reply_type == AMQP_RESPONSE_NORMAL) { return TRUE; } else { _amqp_connection_disconnect(self); } } self->conn = amqp_new_connection(); if (self->conn == NULL) { msg_error("Error allocating AMQP connection.", NULL); goto exception_amqp_dd_connect_failed_init; } self->sockfd = amqp_tcp_socket_new(self->conn); struct timeval delay; delay.tv_sec = 1; delay.tv_usec = 0; sockfd_ret = amqp_socket_open_noblock(self->sockfd, self->host, self->port, &delay); if (sockfd_ret != AMQP_STATUS_OK) { msg_error("Error connecting to AMQP server", evt_tag_str("driver", self->super.super.super.id), evt_tag_str("error", amqp_error_string2(-sockfd_ret)), evt_tag_int("time_reopen", self->super.time_reopen), NULL); goto exception_amqp_dd_connect_failed_init; } ret = amqp_login(self->conn, self->vhost, 0, 131072, 0, AMQP_SASL_METHOD_PLAIN, self->user, self->password); if (!afamqp_is_ok(self, "Error during AMQP login", ret)) { goto exception_amqp_dd_connect_failed_init; } amqp_channel_open(self->conn, 1); ret = amqp_get_rpc_reply(self->conn); if (!afamqp_is_ok(self, "Error during AMQP channel open", ret)) { goto exception_amqp_dd_connect_failed_channel; } if (self->declare) { amqp_exchange_declare(self->conn, 1, amqp_cstring_bytes(self->exchange), amqp_cstring_bytes(self->exchange_type), 0, 0, 0, 0, amqp_empty_table); ret = amqp_get_rpc_reply(self->conn); if (!afamqp_is_ok(self, "Error during AMQP exchange declaration", ret)) { goto exception_amqp_dd_connect_failed_exchange; } } msg_debug ("Connecting to AMQP succeeded", evt_tag_str("driver", self->super.super.super.id), NULL); return TRUE; /* Exceptions */ exception_amqp_dd_connect_failed_exchange: amqp_channel_close(self->conn, 1, AMQP_REPLY_SUCCESS); exception_amqp_dd_connect_failed_channel: amqp_connection_close(self->conn, AMQP_REPLY_SUCCESS); exception_amqp_dd_connect_failed_init: _amqp_connection_deinit(self); return FALSE; }
int main(int argc, const char **argv) { const char *hostname; int port; const char *exchange; const char *routingkey; const char *exchangetype = "direct"; if (argc < 5) { fprintf(stderr, "Usage: receive_logs_direct host port exchange routingkeys...\n"); return 1; } hostname = argv[1]; port = atoi(argv[2]); exchange = argv[3]; int sockfd; int channelid = 1; amqp_connection_state_t conn; conn = amqp_new_connection(); die_on_error(sockfd = amqp_open_socket(hostname, port), "Opening socket"); amqp_set_sockfd(conn, sockfd); die_on_amqp_error(amqp_login(conn, "/", 0, 131072, 0, AMQP_SASL_METHOD_PLAIN, "guest", "guest"),"Logging in"); amqp_channel_open(conn, channelid); die_on_amqp_error(amqp_get_rpc_reply(conn), "Opening channel"); amqp_exchange_declare(conn,channelid,amqp_cstring_bytes(exchange),amqp_cstring_bytes(exchangetype),0,1, amqp_empty_table); die_on_amqp_error(amqp_get_rpc_reply(conn),"Declaring exchange"); amqp_queue_declare_ok_t *r = amqp_queue_declare(conn,channelid,amqp_empty_bytes,0,0,1,0,amqp_empty_table); int i; for(i = 4;i < argc;i++) { routingkey = argv[i]; amqp_queue_bind(conn,channelid,amqp_bytes_malloc_dup(r->queue),amqp_cstring_bytes(exchange), amqp_cstring_bytes(routingkey),amqp_empty_table); } amqp_basic_qos(conn,channelid,0,1,0); amqp_basic_consume(conn,channelid,amqp_bytes_malloc_dup(r->queue),amqp_empty_bytes,0,0,0,amqp_empty_table); die_on_amqp_error(amqp_get_rpc_reply(conn), "Consuming"); { amqp_frame_t frame; int result; amqp_basic_deliver_t *d; amqp_basic_properties_t *p; size_t body_target; size_t body_received; while (1) { amqp_maybe_release_buffers(conn); result = amqp_simple_wait_frame(conn, &frame); printf("Result %d\n", result); if (result < 0) break; printf("Frame type %d, channel %d\n", frame.frame_type, frame.channel); if (frame.frame_type != AMQP_FRAME_METHOD) continue; printf("Method %s\n", amqp_method_name(frame.payload.method.id)); if (frame.payload.method.id != AMQP_BASIC_DELIVER_METHOD) continue; d = (amqp_basic_deliver_t *) frame.payload.method.decoded; printf("Delivery %u, exchange %.*s routingkey %.*s\n",(unsigned) d->delivery_tag, (int) d->exchange.len, (char *) d->exchange.bytes, (int) d->routing_key.len, (char *) d->routing_key.bytes); result = amqp_simple_wait_frame(conn, &frame); if (result < 0) break; if (frame.frame_type != AMQP_FRAME_HEADER) { fprintf(stderr, "Expected header!"); abort(); } p = (amqp_basic_properties_t *) frame.payload.properties.decoded; if (p->_flags & AMQP_BASIC_CONTENT_TYPE_FLAG) { printf("Content-type: %.*s\n", (int) p->content_type.len, (char *) p->content_type.bytes); } body_target = frame.payload.properties.body_size; body_received = 0; int sleep_seconds = 0; while (body_received < body_target) { result = amqp_simple_wait_frame(conn, &frame); if (result < 0) break; if (frame.frame_type != AMQP_FRAME_BODY) { fprintf(stderr, "Expected body!"); abort(); } body_received += frame.payload.body_fragment.len; assert(body_received <= body_target); int i; for(i = 0; i<frame.payload.body_fragment.len; i++) { printf("%c",*((char*)frame.payload.body_fragment.bytes+i)); if(*((char*)frame.payload.body_fragment.bytes+i) == '.') sleep_seconds++; } printf("\n"); } if (body_received != body_target) { /* Can only happen when amqp_simple_wait_frame returns <= 0 */ /* We break here to close the connection */ break; } /* do something */ sleep(sleep_seconds); amqp_basic_ack(conn,channelid,d->delivery_tag,0); } } die_on_amqp_error(amqp_channel_close(conn, 1, AMQP_REPLY_SUCCESS), "Closing channel"); die_on_amqp_error(amqp_connection_close(conn, AMQP_REPLY_SUCCESS), "Closing connection"); die_on_error(amqp_destroy_connection(conn), "Ending connection"); return 0; }
void * SWITCH_THREAD_FUNC mod_amqp_logging_thread(switch_thread_t *thread, void *data) { mod_amqp_message_t *msg = NULL; switch_status_t status = SWITCH_STATUS_SUCCESS; mod_amqp_logging_profile_t *profile = (mod_amqp_logging_profile_t *)data; amqp_boolean_t passive = 0; amqp_boolean_t durable = 1; while (profile->running) { if (!profile->conn_active) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Amqp no connection- reconnecting...\n"); status = mod_amqp_connection_open(profile->conn_root, &(profile->conn_active), profile->name, profile->custom_attr); if ( status == SWITCH_STATUS_SUCCESS ) { // Ensure that the exchange exists, and is of the correct type amqp_exchange_declare(profile->conn_active->state, 1, amqp_cstring_bytes(profile->exchange), amqp_cstring_bytes(profile->exchange_type), passive, durable, amqp_empty_table); if (!mod_amqp_log_if_amqp_error(amqp_get_rpc_reply(profile->conn_active->state), "Declaring exchange")) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Amqp reconnect successful- connected\n"); continue; } } switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Profile[%s] failed to connect with code(%d), sleeping for %dms\n", profile->name, status, profile->reconnect_interval_ms); switch_sleep(profile->reconnect_interval_ms * 1000); continue; } if (!msg && switch_queue_pop_timeout(profile->send_queue, (void**)&msg, 1000000) != SWITCH_STATUS_SUCCESS) { continue; } if (msg) { switch (mod_amqp_logging_send(profile, msg)) { case SWITCH_STATUS_SUCCESS: /* Success: prepare for next message */ mod_amqp_util_msg_destroy(&msg); break; case SWITCH_STATUS_NOT_INITALIZED: switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Send failed with 'not initialised'\n"); break; case SWITCH_STATUS_SOCKERR: switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Send failed with 'socket error'\n"); break; default: switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Send failed with a generic error\n"); /* Send failed and closed the connection; reconnect will happen at the beginning of the loop * NB: do we need a delay here to prevent a fast reconnect-send-fail loop? */ break; } } } /* Abort the current message */ mod_amqp_util_msg_destroy(&msg); // Terminate the thread switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Event sender thread stopped\n"); switch_thread_exit(thread, SWITCH_STATUS_SUCCESS); return NULL; }
void create(int argc, char*argv[]) { ex_name = argv[2]; qu_name = argv[3]; rt_key = argv[4]; /* // 预清理测试 amqp_exchange_delete(conn, channel_id, amqp_cstring_bytes(ex_name.c_str()), 0); check_amqp_reply("amqp delete exchange failed.", "amqp delete exchange succ."); amqp_queue_delete(conn, channel_id, amqp_cstring_bytes(qu_name.c_str()), 0, 0); check_amqp_reply("amqp delete queue failed.", "amqp delete queue succ."); */ amqp_exchange_declare(conn, /* state */ channel_id, /* channel */ amqp_cstring_bytes(ex_name.c_str()), /* exchange */ amqp_cstring_bytes(ex_type.c_str()), /* type */ 0, /* passive */ 1, /* durable */ 0, /* auto_delete */ 0, /* internal */ amqp_empty_table); check_amqp_reply("amqp declare exchange failed.", "amqp declare exchange succ"); // 设置队列的优先级属性 amqp_table_t table; table.num_entries = 1; table.entries = (amqp_table_entry_t*)calloc(1, sizeof(amqp_table_entry_t)); table.entries[0].key = amqp_cstring_bytes("x-max-priority"); table.entries[0].value.kind = AMQP_FIELD_KIND_I16; table.entries[0].value.value.i16 = 10; amqp_queue_declare(conn, /* state */ channel_id, /* channel */ amqp_cstring_bytes(qu_name.c_str()), /* queue */ 0, /* passive */ 1, /* durable */ 0, /* exclusive */ 0, /* auto_delete */ //amqp_empty_table); table); check_amqp_reply("amqp declare queue failed.", "amqp declare queue succ."); amqp_queue_bind(conn, channel_id, amqp_cstring_bytes(qu_name.c_str()), amqp_cstring_bytes(ex_name.c_str()), amqp_cstring_bytes(rt_key.c_str()), amqp_empty_table); check_amqp_reply("amqp bind queue failed.", "amqp bind queue succ"); return; }
/** * Internal function used to initialize the connection to * the RabbitMQ server. Also used to reconnect to the server * in case the connection fails and to redeclare exchanges * and queues if they are lost * */ static int init_conn(MQ_INSTANCE *my_instance) { int rval = 0; int amqp_ok = AMQP_STATUS_OK; if(my_instance->use_ssl){ if((my_instance->sock = amqp_ssl_socket_new(my_instance->conn)) != NULL){ if((amqp_ok = amqp_ssl_socket_set_cacert(my_instance->sock,my_instance->ssl_CA_cert)) != AMQP_STATUS_OK){ skygw_log_write(LOGFILE_ERROR, "Error : Failed to set CA certificate: %s", amqp_error_string2(amqp_ok)); goto cleanup; } if((amqp_ok = amqp_ssl_socket_set_key(my_instance->sock, my_instance->ssl_client_cert, my_instance->ssl_client_key)) != AMQP_STATUS_OK){ skygw_log_write(LOGFILE_ERROR, "Error : Failed to set client certificate and key: %s", amqp_error_string2(amqp_ok)); goto cleanup; } }else{ amqp_ok = AMQP_STATUS_SSL_CONNECTION_FAILED; skygw_log_write(LOGFILE_ERROR, "Error : SSL socket creation failed."); goto cleanup; } /**SSL is not used, falling back to TCP*/ }else if((my_instance->sock = amqp_tcp_socket_new(my_instance->conn)) == NULL){ skygw_log_write(LOGFILE_ERROR, "Error : TCP socket creation failed."); goto cleanup; } /**Socket creation was successful, trying to open the socket*/ if((amqp_ok = amqp_socket_open(my_instance->sock,my_instance->hostname,my_instance->port)) != AMQP_STATUS_OK){ skygw_log_write(LOGFILE_ERROR, "Error : Failed to open socket: %s", amqp_error_string2(amqp_ok)); goto cleanup; } amqp_rpc_reply_t reply; reply = amqp_login(my_instance->conn,my_instance->vhost,0,AMQP_DEFAULT_FRAME_SIZE,0,AMQP_SASL_METHOD_PLAIN,my_instance->username,my_instance->password); if(reply.reply_type != AMQP_RESPONSE_NORMAL){ skygw_log_write(LOGFILE_ERROR, "Error : Login to RabbitMQ server failed."); goto cleanup; } amqp_channel_open(my_instance->conn,my_instance->channel); reply = amqp_get_rpc_reply(my_instance->conn); if(reply.reply_type != AMQP_RESPONSE_NORMAL){ skygw_log_write(LOGFILE_ERROR, "Error : Channel creation failed."); goto cleanup; } amqp_exchange_declare(my_instance->conn,my_instance->channel, amqp_cstring_bytes(my_instance->exchange), amqp_cstring_bytes(my_instance->exchange_type), 0, 1, amqp_empty_table); reply = amqp_get_rpc_reply(my_instance->conn); if(reply.reply_type != AMQP_RESPONSE_NORMAL){ skygw_log_write(LOGFILE_ERROR, "Error : Exchange declaration failed,trying to redeclare the exchange."); if(reply.reply_type == AMQP_RESPONSE_SERVER_EXCEPTION){ if(reply.reply.id == AMQP_CHANNEL_CLOSE_METHOD){ amqp_send_method(my_instance->conn,my_instance->channel,AMQP_CHANNEL_CLOSE_OK_METHOD,NULL); }else if(reply.reply.id == AMQP_CONNECTION_CLOSE_METHOD){ amqp_send_method(my_instance->conn,my_instance->channel,AMQP_CONNECTION_CLOSE_OK_METHOD,NULL); } my_instance->channel++; amqp_channel_open(my_instance->conn,my_instance->channel); amqp_exchange_delete(my_instance->conn,my_instance->channel,amqp_cstring_bytes(my_instance->exchange),0); amqp_exchange_declare(my_instance->conn,my_instance->channel, amqp_cstring_bytes(my_instance->exchange), amqp_cstring_bytes(my_instance->exchange_type), 0, 1, amqp_empty_table); reply = amqp_get_rpc_reply(my_instance->conn); } if(reply.reply_type != AMQP_RESPONSE_NORMAL){ skygw_log_write(LOGFILE_ERROR, "Error : Exchange redeclaration failed."); goto cleanup; } } if(my_instance->queue){ amqp_queue_declare(my_instance->conn,my_instance->channel, amqp_cstring_bytes(my_instance->queue), 0, 1, 0, 0, amqp_empty_table); reply = amqp_get_rpc_reply(my_instance->conn); if(reply.reply_type != AMQP_RESPONSE_NORMAL){ skygw_log_write(LOGFILE_ERROR, "Error : Queue declaration failed."); goto cleanup; } amqp_queue_bind(my_instance->conn,my_instance->channel, amqp_cstring_bytes(my_instance->queue), amqp_cstring_bytes(my_instance->exchange), amqp_cstring_bytes(my_instance->key), amqp_empty_table); reply = amqp_get_rpc_reply(my_instance->conn); if(reply.reply_type != AMQP_RESPONSE_NORMAL){ skygw_log_write(LOGFILE_ERROR, "Error : Failed to bind queue to exchange."); goto cleanup; } } rval = 1; cleanup: return rval; }
void * SWITCH_THREAD_FUNC mod_amqp_command_thread(switch_thread_t *thread, void *data) { mod_amqp_command_profile_t *profile = (mod_amqp_command_profile_t *) data; while (profile->running) { amqp_queue_declare_ok_t *recv_queue; amqp_bytes_t queueName = { 0, NULL }; /* Ensure we have an AMQP connection */ if (!profile->conn_active) { switch_status_t status; switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Amqp no connection- reconnecting...\n"); status = mod_amqp_connection_open(profile->conn_root, &(profile->conn_active), profile->name, profile->custom_attr); if ( status != SWITCH_STATUS_SUCCESS ) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Profile[%s] failed to connect with code(%d), sleeping for %dms\n", profile->name, status, profile->reconnect_interval_ms); switch_sleep(profile->reconnect_interval_ms * 1000); continue; } /* Check if exchange already exists */ amqp_exchange_declare(profile->conn_active->state, 1, amqp_cstring_bytes(profile->exchange), amqp_cstring_bytes("topic"), 0, /* passive */ 1, /* durable */ amqp_empty_table); if (mod_amqp_log_if_amqp_error(amqp_get_rpc_reply(profile->conn_active->state), "Checking for command exchange")) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Profile[%s] failed to create missing command exchange", profile->name); continue; } /* Ensure we have a queue */ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Creating command queue"); recv_queue = amqp_queue_declare(profile->conn_active->state, // state 1, // channel profile->queue ? amqp_cstring_bytes(profile->queue) : amqp_empty_bytes, // queue name 0, 0, // passive, durable 0, 1, // exclusive, auto-delete amqp_empty_table); // args if (mod_amqp_log_if_amqp_error(amqp_get_rpc_reply(profile->conn_active->state), "Declaring queue")) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Profile[%s] failed to connect with code(%d), sleeping for %dms\n", profile->name, status, profile->reconnect_interval_ms); switch_sleep(profile->reconnect_interval_ms * 1000); continue; } if (queueName.bytes) { amqp_bytes_free(queueName); } queueName = amqp_bytes_malloc_dup(recv_queue->queue); if (!queueName.bytes) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Out of memory while copying queue name"); break; } switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Created command queue %.*s", (int)queueName.len, (char *)queueName.bytes); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Binding command queue to exchange %s", profile->exchange); /* Bind the queue to the exchange */ amqp_queue_bind(profile->conn_active->state, // state 1, // channel queueName, // queue amqp_cstring_bytes(profile->exchange), // exchange amqp_cstring_bytes(profile->binding_key), // routing key amqp_empty_table); // args if (mod_amqp_log_if_amqp_error(amqp_get_rpc_reply(profile->conn_active->state), "Binding queue")) { mod_amqp_connection_close(profile->conn_active); profile->conn_active = NULL; switch_sleep(profile->reconnect_interval_ms * 1000); continue; } switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Amqp reconnect successful- connected\n"); continue; } // Start a command amqp_basic_consume(profile->conn_active->state, // state 1, // channel queueName, // queue amqp_empty_bytes, // command tag 0, 1, 0, // no_local, no_ack, exclusive amqp_empty_table); // args if (mod_amqp_log_if_amqp_error(amqp_get_rpc_reply(profile->conn_active->state), "Creating a command")) { mod_amqp_connection_close(profile->conn_active); profile->conn_active = NULL; switch_sleep(profile->reconnect_interval_ms * 1000); continue; } while (profile->running && profile->conn_active) { amqp_rpc_reply_t res; amqp_envelope_t envelope; struct timeval timeout = {0}; char command[1024]; enum ECommandFormat { COMMAND_FORMAT_UNKNOWN, COMMAND_FORMAT_PLAINTEXT } commandFormat = COMMAND_FORMAT_PLAINTEXT; char *fs_resp_exchange = NULL, *fs_resp_key = NULL; amqp_maybe_release_buffers(profile->conn_active->state); timeout.tv_usec = 500 * 1000; res = amqp_consume_message(profile->conn_active->state, &envelope, &timeout, 0); if (res.reply_type == AMQP_RESPONSE_LIBRARY_EXCEPTION) { if (res.library_error == AMQP_STATUS_UNEXPECTED_STATE) { /* Unexpected frame. Discard it then continue */ amqp_frame_t decoded_frame; amqp_simple_wait_frame(profile->conn_active->state, &decoded_frame); } if (res.library_error == AMQP_STATUS_SOCKET_ERROR) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "A socket error occurred. Tearing down and reconnecting\n"); break; } if (res.library_error == AMQP_STATUS_CONNECTION_CLOSED) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "AMQP connection was closed. Tearing down and reconnecting\n"); break; } if (res.library_error == AMQP_STATUS_TCP_ERROR) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "A TCP error occurred. Tearing down and reconnecting\n"); break; } if (res.library_error == AMQP_STATUS_TIMEOUT) { // nop } /* Try consuming again */ continue; } if (res.reply_type != AMQP_RESPONSE_NORMAL) { break; } switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Delivery:%u, exchange:%.*s routingkey:%.*s\n", (unsigned) envelope.delivery_tag, (int) envelope.exchange.len, (char *) envelope.exchange.bytes, (int) envelope.routing_key.len, (char *) envelope.routing_key.bytes); if (envelope.message.properties._flags & AMQP_BASIC_CONTENT_TYPE_FLAG) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Content-type: %.*s\n", (int) envelope.message.properties.content_type.len, (char *) envelope.message.properties.content_type.bytes); if (strncasecmp("text/plain", envelope.message.properties.content_type.bytes, strlen("text/plain")) == 0) { commandFormat = COMMAND_FORMAT_PLAINTEXT; } else { commandFormat = COMMAND_FORMAT_UNKNOWN; } } if (envelope.message.properties.headers.num_entries) { int x = 0; for ( x = 0; x < envelope.message.properties.headers.num_entries; x++) { char *header_key = (char *)envelope.message.properties.headers.entries[x].key.bytes; char *header_value = (char *)envelope.message.properties.headers.entries[x].value.value.bytes.bytes; switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "AMQP message custom header key[%s] value[%s]\n", header_key, header_value); if ( !strncmp(header_key, "x-fs-api-resp-exchange", 22)) { fs_resp_exchange = header_value; } else if (!strncmp(header_key, "x-fs-api-resp-key", 17)) { fs_resp_key = header_value; } else { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Ignoring unrecognized event header [%s]\n", header_key); } } } if (commandFormat == COMMAND_FORMAT_PLAINTEXT) { switch_stream_handle_t stream = { 0 }; /* Collects the command output */ /* Convert amqp bytes to c-string */ snprintf(command, sizeof(command), "%.*s", (int) envelope.message.body.len, (char *) envelope.message.body.bytes); /* Execute the command */ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Executing: %s\n", command); SWITCH_STANDARD_STREAM(stream); if ( fs_resp_exchange && fs_resp_key ) { switch_status_t status = switch_console_execute(command, 0, &stream); mod_amqp_command_response(profile, command, stream, fs_resp_exchange, fs_resp_key, status); } else { if (switch_console_execute(command, 0, &stream) != SWITCH_STATUS_SUCCESS) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Remote command failed:\n%s\n", (char *) stream.data); } else { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Remote command succeeded:\n%s\n", (char *) stream.data); } } switch_safe_free(stream.data); } /* Tidy up */ amqp_destroy_envelope(&envelope); } amqp_bytes_free(queueName); queueName.bytes = NULL; mod_amqp_connection_close(profile->conn_active); profile->conn_active = NULL; if (profile->running) { /* We'll reconnect, but sleep to avoid hammering resources */ switch_sleep(500); } } /* Terminate the thread */ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Command listener thread stopped\n"); switch_thread_exit(thread, SWITCH_STATUS_SUCCESS); return NULL; }
void amqp_cache_purge(struct chained_cache *queue[], int index) { struct pkt_primitives *data = NULL; struct pkt_bgp_primitives *pbgp = NULL; struct pkt_nat_primitives *pnat = NULL; struct pkt_mpls_primitives *pmpls = NULL; char *pcust = NULL; struct pkt_bgp_primitives empty_pbgp; struct pkt_nat_primitives empty_pnat; struct pkt_mpls_primitives empty_pmpls; char *empty_pcust = NULL; struct amqp_basic_properties_t_ amqp_msg_props; char src_mac[18], dst_mac[18], src_host[INET6_ADDRSTRLEN], dst_host[INET6_ADDRSTRLEN], ip_address[INET6_ADDRSTRLEN]; char rd_str[SRVBUFLEN], misc_str[SRVBUFLEN], dyn_amqp_routing_key[SRVBUFLEN], *orig_amqp_routing_key = NULL; char default_amqp_exchange[] = "pmacct", default_amqp_exchange_type[] = "direct"; char default_amqp_routing_key[] = "acct"; int i, j, amqp_status, batch_idx, is_routing_key_dyn = FALSE; time_t start, duration; pid_t writer_pid = getpid(); amqp_connection_state_t amqp_conn; amqp_socket_t *amqp_socket = NULL; amqp_rpc_reply_t amqp_ret; /* setting some defaults */ if (!config.sql_db) config.sql_db = default_amqp_exchange; if (!config.sql_table) config.sql_table = default_amqp_routing_key; else { if (strchr(config.sql_table, '$')) { is_routing_key_dyn = TRUE; orig_amqp_routing_key = config.sql_table; config.sql_table = dyn_amqp_routing_key; } } if (!config.amqp_exchange_type) config.amqp_exchange_type = default_amqp_exchange_type; empty_pcust = malloc(config.cpptrs.len); if (!empty_pcust) { Log(LOG_ERR, "ERROR ( %s/%s ): Unable to malloc() empty_pcust. Exiting.\n", config.name, config.type); exit_plugin(1); } memset(&empty_pbgp, 0, sizeof(struct pkt_bgp_primitives)); memset(&empty_pnat, 0, sizeof(struct pkt_nat_primitives)); memset(&empty_pmpls, 0, sizeof(struct pkt_mpls_primitives)); memset(empty_pcust, 0, config.cpptrs.len); memset(&amqp_msg_props, 0, sizeof(amqp_msg_props)); amqp_conn = amqp_new_connection(); amqp_socket = amqp_tcp_socket_new(amqp_conn); if (!socket) { Log(LOG_ERR, "ERROR ( %s/%s ): Connection failed to RabbitMQ: no socket\n", config.name, config.type); return; } if (config.sql_host) amqp_status = amqp_socket_open(amqp_socket, config.sql_host, 5672/* default port */); else amqp_status = amqp_socket_open(amqp_socket, "127.0.0.1", 5672 /* default port */); if (amqp_status) { Log(LOG_ERR, "ERROR ( %s/%s ): Connection failed to RabbitMQ: unable to open socket\n", config.name, config.type); return; } amqp_ret = amqp_login(amqp_conn, "/", 0, 131072, 0, AMQP_SASL_METHOD_PLAIN, config.sql_user, config.sql_passwd); if (amqp_ret.reply_type != AMQP_RESPONSE_NORMAL) { Log(LOG_ERR, "ERROR ( %s/%s ): Connection failed to RabbitMQ: login\n", config.name, config.type); return; } amqp_channel_open(amqp_conn, 1); amqp_ret = amqp_get_rpc_reply(amqp_conn); if (amqp_ret.reply_type != AMQP_RESPONSE_NORMAL) { Log(LOG_ERR, "ERROR ( %s/%s ): Connection failed to RabbitMQ: unable to open channel\n", config.name, config.type); return; } amqp_exchange_declare(amqp_conn, 1, amqp_cstring_bytes(config.sql_db), amqp_cstring_bytes(config.amqp_exchange_type), 0, 0, amqp_empty_table); amqp_ret = amqp_get_rpc_reply(amqp_conn); if (amqp_ret.reply_type != AMQP_RESPONSE_NORMAL) { Log(LOG_ERR, "ERROR ( %s/%s ): Connection failed to RabbitMQ: exchange declare\n", config.name, config.type); return; } if (config.amqp_persistent_msg) { amqp_msg_props._flags = (AMQP_BASIC_CONTENT_TYPE_FLAG | AMQP_BASIC_DELIVERY_MODE_FLAG); amqp_msg_props.content_type = amqp_cstring_bytes("text/json"); amqp_msg_props.delivery_mode = 2; /* persistent delivery */ } Log(LOG_INFO, "INFO ( %s/%s ): *** Purging cache - START (PID: %u) ***\n", config.name, config.type, writer_pid); start = time(NULL); for (j = 0; j < index; j++) { char *json_str; data = &queue[j]->primitives; if (queue[j]->pbgp) pbgp = queue[j]->pbgp; else pbgp = &empty_pbgp; if (queue[j]->pnat) pnat = queue[j]->pnat; else pnat = &empty_pnat; if (queue[j]->pmpls) pmpls = queue[j]->pmpls; else pmpls = &empty_pmpls; if (queue[j]->pcust) pcust = queue[j]->pcust; else pcust = empty_pcust; if (queue[j]->valid == PRINT_CACHE_FREE) continue; json_str = compose_json(config.what_to_count, config.what_to_count_2, queue[j]->flow_type, &queue[j]->primitives, pbgp, pnat, pmpls, pcust, queue[j]->bytes_counter, queue[j]->packet_counter, queue[j]->flow_counter, queue[j]->tcp_flags, &queue[j]->basetime); if (json_str) { if (is_routing_key_dyn) amqp_handle_routing_key_dyn_strings(config.sql_table, SRVBUFLEN, orig_amqp_routing_key, queue[j]); if (config.debug) Log(LOG_DEBUG, "DEBUG ( %s/%s ): publishing [E=%s RK=%s DM=%u]: %s\n", config.name, config.type, config.sql_db, config.sql_table, amqp_msg_props.delivery_mode, json_str); amqp_basic_publish(amqp_conn, 1, amqp_cstring_bytes(config.sql_db), amqp_cstring_bytes(config.sql_table), 0, 0, &amqp_msg_props, amqp_cstring_bytes(json_str)); amqp_ret = amqp_get_rpc_reply(amqp_conn); if (amqp_ret.reply_type != AMQP_RESPONSE_NORMAL) { Log(LOG_ERR, "ERROR ( %s/%s ): Connection failed to RabbitMQ: publishing\n", config.name, config.type); return; } free(json_str); } } amqp_channel_close(amqp_conn, 1, AMQP_REPLY_SUCCESS); amqp_connection_close(amqp_conn, AMQP_REPLY_SUCCESS); amqp_destroy_connection(amqp_conn); duration = time(NULL)-start; Log(LOG_INFO, "INFO ( %s/%s ): *** Purging cache - END (PID: %u, QN: %u, ET: %u) ***\n", config.name, config.type, writer_pid, index, duration); if (config.sql_trigger_exec) P_trigger_exec(config.sql_trigger_exec); }
static void rabbitmq_add_queue(amqp_connection_state_t conn, amqp_channel_t* channel_ref, const char *stream) { size_t n = strlen(stream) + 1; char app[n], env[n]; memset(app, 0, n); memset(env, 0, n); sscanf(stream, "%[^-]-%[^-]", app, env); if (strcmp(env, rabbit_env)) { printf("[I] skipping: %s-%s\n", app, env); return; } // printf("[D] processing stream %s-%s\n", app, env); char exchange[n+16]; memset(exchange, 0, n+16); sprintf(exchange, "request-stream-%s-%s", app, env); // printf("[D] exchange: %s\n", exchange); char queue[n+15]; memset(queue, 0, n+15); sprintf(queue, "logjam-device-%s-%s", app, env); // printf("[D] queue: %s\n", queue); printf("[I] binding: %s ==> %s\n", exchange, queue); amqp_channel_t channel = ++(*channel_ref); if (channel > 1) { // printf("[D] opening channel %d\n", channel); amqp_channel_open(conn, channel); die_on_amqp_error(amqp_get_rpc_reply(conn), "Opening AMQP channel"); } // amqp_exchange_declare(amqp_connection_state_t state, amqp_channel_t channel, amqp_bytes_t exchange, // amqp_bytes_t type, amqp_boolean_t passive, amqp_boolean_t durable, // amqp_boolean_t auto_delete, amqp_boolean_t internal, amqp_table_t arguments); amqp_exchange_declare(conn, channel, amqp_cstring_bytes(exchange), amqp_cstring_bytes("topic"), 0, 1, 0, 0, amqp_empty_table); die_on_amqp_error(amqp_get_rpc_reply(conn), "declaring exchange"); amqp_bytes_t queuename = amqp_cstring_bytes(queue); struct amqp_table_entry_t_ queue_arg_table_entries[1] = { { amqp_cstring_bytes("x-message-ttl"), { AMQP_FIELD_KIND_I32, { 60 * 1000 } } } }; amqp_table_t queue_argument_table = {1, queue_arg_table_entries}; // amqp_queue_declare(amqp_connection_state_t state, amqp_channel_t channel, amqp_bytes_t queue, // amqp_boolean_t passive, amqp_boolean_t durable, amqp_boolean_t exclusive, amqp_boolean_t auto_delete, // amqp_table_t arguments); amqp_queue_declare(conn, channel, queuename, 0, 0, 0, 1, queue_argument_table); die_on_amqp_error(amqp_get_rpc_reply(conn), "declaring queue"); // amqp_queue_bind(amqp_connection_state_t state, amqp_channel_t channel, amqp_bytes_t queue, // amqp_bytes_t exchange, amqp_bytes_t routing_key, amqp_table_t arguments) const char *routing_key = "#"; amqp_queue_bind(conn, channel, queuename, amqp_cstring_bytes(exchange), amqp_cstring_bytes(routing_key), amqp_empty_table); die_on_amqp_error(amqp_get_rpc_reply(conn), "binding queue"); // amqp_basic_consume(amqp_connection_state_t state, amqp_channel_t channel, amqp_bytes_t queue, amqp_bytes_t consumer_tag, // amqp_boolean_t no_local, amqp_boolean_t no_ack, amqp_boolean_t exclusive, amqp_table_t arguments) amqp_basic_consume(conn, channel, queuename, amqp_empty_bytes, 0, 1, 0, amqp_empty_table); die_on_amqp_error(amqp_get_rpc_reply(conn), "consuming"); }
/* {{{ proto AMQPExchange::declareExchange(); declare Exchange */ static PHP_METHOD(amqp_exchange_class, declareExchange) { PHP5to7_READ_PROP_RV_PARAM_DECL; amqp_channel_resource *channel_resource; amqp_table_t *arguments; if (zend_parse_parameters_none() == FAILURE) { return; } channel_resource = PHP_AMQP_GET_CHANNEL_RESOURCE(PHP_AMQP_READ_THIS_PROP("channel")); PHP_AMQP_VERIFY_CHANNEL_RESOURCE(channel_resource, "Could not declare exchange."); /* Check that the exchange has a name */ if (PHP_AMQP_READ_THIS_PROP_STRLEN("name") < 1) { zend_throw_exception(amqp_exchange_exception_class_entry, "Could not declare exchange. Exchanges must have a name.", 0 TSRMLS_CC); return; } /* Check that the exchange has a name */ if (PHP_AMQP_READ_THIS_PROP_STRLEN("type") < 1) { zend_throw_exception(amqp_exchange_exception_class_entry, "Could not declare exchange. Exchanges must have a type.", 0 TSRMLS_CC); return; } arguments = convert_zval_to_amqp_table(PHP_AMQP_READ_THIS_PROP("arguments") TSRMLS_CC); #if AMQP_VERSION_MAJOR * 100 + AMQP_VERSION_MINOR * 10 + AMQP_VERSION_PATCH > 52 amqp_exchange_declare( channel_resource->connection_resource->connection_state, channel_resource->channel_id, amqp_cstring_bytes(PHP_AMQP_READ_THIS_PROP_STR("name")), amqp_cstring_bytes(PHP_AMQP_READ_THIS_PROP_STR("type")), PHP_AMQP_READ_THIS_PROP_BOOL("passive"), PHP_AMQP_READ_THIS_PROP_BOOL("durable"), PHP_AMQP_READ_THIS_PROP_BOOL("auto_delete"), PHP_AMQP_READ_THIS_PROP_BOOL("internal"), *arguments ); #else amqp_exchange_declare( channel_resource->connection_resource->connection_state, channel_resource->channel_id, amqp_cstring_bytes(PHP_AMQP_READ_THIS_PROP_STR("name")), amqp_cstring_bytes(PHP_AMQP_READ_THIS_PROP_STR("type")), PHP_AMQP_READ_THIS_PROP_BOOL("passive"), PHP_AMQP_READ_THIS_PROP_BOOL("durable"), *arguments ); #endif amqp_rpc_reply_t res = amqp_get_rpc_reply(channel_resource->connection_resource->connection_state); php_amqp_free_amqp_table(arguments); if (PHP_AMQP_MAYBE_ERROR(res, channel_resource)) { php_amqp_zend_throw_exception_short(res, amqp_exchange_exception_class_entry TSRMLS_CC); php_amqp_maybe_release_buffers_on_channel(channel_resource->connection_resource, channel_resource); return; } php_amqp_maybe_release_buffers_on_channel(channel_resource->connection_resource, channel_resource); RETURN_TRUE; }
switch_status_t mod_amqp_logging_create(char *name, switch_xml_t cfg) { mod_amqp_logging_profile_t *profile = NULL; switch_xml_t params, param, connections, connection; switch_threadattr_t *thd_attr = NULL; char *exchange = NULL, *exchange_type = NULL; int exchange_durable = 1; /* durable */ switch_memory_pool_t *pool; if (switch_core_new_memory_pool(&pool) != SWITCH_STATUS_SUCCESS) { goto err; } profile = switch_core_alloc(pool, sizeof(mod_amqp_logging_profile_t)); profile->pool = pool; profile->name = switch_core_strdup(profile->pool, name); profile->running = 1; profile->conn_root = NULL; profile->conn_active = NULL; profile->log_level_mask = 0; profile->send_queue_size = 5000; if ((params = switch_xml_child(cfg, "params")) != NULL) { for (param = switch_xml_child(params, "param"); param; param = param->next) { char *var = (char *) switch_xml_attr_soft(param, "name"); char *val = (char *) switch_xml_attr_soft(param, "value"); if (!var) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Profile[%s] param missing 'name' attribute\n", profile->name); continue; } if (!val) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Profile[%s] param[%s] missing 'value' attribute\n", profile->name, var); continue; } if (!strncmp(var, "reconnect_interval_ms", 21)) { int interval = atoi(val); if ( interval && interval > 0 ) { profile->reconnect_interval_ms = interval; } } else if (!strncmp(var, "send_queue_size", 15)) { int interval = atoi(val); if ( interval && interval > 0 ) { profile->send_queue_size = interval; } } else if (!strncmp(var, "exchange-type", 13)) { exchange_type = switch_core_strdup(profile->pool, val); } else if (!strncmp(var, "exchange-name", 13)) { exchange = switch_core_strdup(profile->pool, val); } else if (!strncmp(var, "exchange-durable", 16)) { exchange_durable = switch_true(val); } else if (!strncmp(var, "log-levels", 10)) { profile->log_level_mask = switch_log_str2mask(val); } } /* params for loop */ } /* Handle defaults of string types */ profile->exchange = exchange ? exchange : switch_core_strdup(profile->pool, "TAP.Events"); profile->exchange_type = exchange_type ? exchange_type : switch_core_strdup(profile->pool, "topic"); profile->exchange_durable = exchange_durable; if ((connections = switch_xml_child(cfg, "connections")) != NULL) { for (connection = switch_xml_child(connections, "connection"); connection; connection = connection->next) { if ( ! profile->conn_root ) { /* Handle first root node */ if (mod_amqp_connection_create(&(profile->conn_root), connection, profile->pool) != SWITCH_STATUS_SUCCESS) { /* Handle connection create failure */ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Profile[%s] failed to create connection\n", profile->name); continue; } profile->conn_active = profile->conn_root; } else { if (mod_amqp_connection_create(&(profile->conn_active->next), connection, profile->pool) != SWITCH_STATUS_SUCCESS) { /* Handle connection create failure */ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Profile[%s] failed to create connection\n", profile->name); continue; } profile->conn_active = profile->conn_active->next; } } } profile->conn_active = NULL; if ( mod_amqp_connection_open(profile->conn_root, &(profile->conn_active), profile->name, profile->custom_attr) != SWITCH_STATUS_SUCCESS) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Profile[%s] was unable to connect to any connection\n", profile->name); goto err; } amqp_exchange_declare(profile->conn_active->state, 1, amqp_cstring_bytes(profile->exchange), amqp_cstring_bytes(profile->exchange_type), 0, /* passive */ profile->exchange_durable, amqp_empty_table); if (mod_amqp_log_if_amqp_error(amqp_get_rpc_reply(profile->conn_active->state), "Declaring exchange")) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Profile[%s] failed to create exchange\n", profile->name); goto err; } /* Create a bounded FIFO queue for sending messages */ if (switch_queue_create(&(profile->send_queue), profile->send_queue_size, profile->pool) != SWITCH_STATUS_SUCCESS) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Cannot create send queue of size %d!\n", profile->send_queue_size); goto err; } /* Start the event send thread. This will set up the initial connection */ switch_threadattr_create(&thd_attr, profile->pool); switch_threadattr_stacksize_set(thd_attr, SWITCH_THREAD_STACKSIZE); if (switch_thread_create(&profile->logging_thread, thd_attr, mod_amqp_logging_thread, profile, profile->pool)) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Cannot create 'amqp event sender' thread!\n"); goto err; } if ( switch_core_hash_insert(globals.logging_hash, name, (void *) profile) != SWITCH_STATUS_SUCCESS) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Failed to insert new profile [%s] into mod_amqp profile hash\n", name); goto err; } switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Profile[%s] Successfully started\n", profile->name); return SWITCH_STATUS_SUCCESS; err: /* Cleanup */ mod_amqp_logging_destroy(&profile); return SWITCH_STATUS_GENERR; }