void init() { conn = amqp_new_connection(); amqp_socket_t* pSocket = amqp_tcp_socket_new(conn); if (pSocket == NULL) { ABORT("amqp create socket failed."); } printf("socket create succ.\n"); int state = amqp_socket_open(pSocket, hostip.c_str(), port); if (state < 0) { ABORT("amqp open socket failed."); } printf("socket open succ.\n"); amqp_rpc_reply_t reply = amqp_login(conn, "/", 0, 131072, 0, AMQP_SASL_METHOD_PLAIN, username.c_str(), password.c_str()); if (reply.reply_type != AMQP_RESPONSE_NORMAL) { ABORT("amqp login failed."); } printf("amqp login succ.\n"); // 初始化 amqp_channel_open(conn, channel_id); check_amqp_reply("amqp open channel failed.", "amqp channel channel success."); }
Channel::Channel(const std::string &host, int port, const std::string &username, const std::string &password, const std::string &vhost, int frame_max) : m_impl(new Detail::ChannelImpl) { m_impl->m_connection = amqp_new_connection(); if (NULL == m_impl->m_connection) { throw std::bad_alloc(); } try { amqp_socket_t *socket = amqp_tcp_socket_new(m_impl->m_connection); int sock = amqp_socket_open(socket, host.c_str(), port); m_impl->CheckForError(sock); m_impl->DoLogin(username, password, vhost, frame_max); } catch (...) { amqp_destroy_connection(m_impl->m_connection); throw; } m_impl->SetIsConnected(true); }
/* * Initialize RabbitMQ connection */ static rsRetVal initRabbitMQ(instanceData *pData) { amqp_socket_t *asocket; amqp_exchange_declare_t edReq; DEFiRet; DBGPRINTF("omrabbitmq: trying connect to '%s' at port %d\n", pData->host, pData->port); pData->conn = amqp_new_connection(); asocket = amqp_tcp_socket_new(pData->conn); if (!asocket) { errmsg.LogError(0, RS_RET_ERR, "omrabbitmq: Error allocating tcp socket"); pData->conn = NULL; ABORT_FINALIZE(RS_RET_SUSPENDED); } if (die_on_error(amqp_socket_open(asocket, (char*) pData->host, pData->port), "Opening socket")) { pData->conn = NULL; ABORT_FINALIZE(RS_RET_SUSPENDED); } if (die_on_amqp_error(amqp_login(pData->conn, (char*) pData->vhost, 0, 131072, 0, AMQP_SASL_METHOD_PLAIN, pData->user, pData->password), "Logging in")) { pData->conn = NULL; ABORT_FINALIZE(RS_RET_SUSPENDED); } amqp_channel_open(pData->conn, RABBITMQ_CHANNEL); if (die_on_amqp_error(amqp_get_rpc_reply(pData->conn), "Opening channel")) { pData->conn = NULL; ABORT_FINALIZE(RS_RET_SUSPENDED); } if(pData->exchange_type != NULL) { edReq.ticket = 0; edReq.exchange = amqp_cstring_bytes(pData->exchange); edReq.type = amqp_cstring_bytes(pData->exchange_type); edReq.passive = 0; edReq.durable = pData->durable; edReq.auto_delete = pData->auto_delete; edReq.internal = 0; edReq.nowait = 0; edReq.arguments = amqp_empty_table; amqp_simple_rpc_decoded(pData->conn, RABBITMQ_CHANNEL, AMQP_EXCHANGE_DECLARE_METHOD, AMQP_EXCHANGE_DECLARE_OK_METHOD, &edReq); if(die_on_amqp_error(amqp_get_rpc_reply(pData->conn), "Declaring exchange")) { pData->conn = NULL; ABORT_FINALIZE(RS_RET_SUSPENDED); } } finalize_it: RETiRet; }
void amqp_set_sockfd(amqp_connection_state_t state, int sockfd) { amqp_socket_t *socket = amqp_tcp_socket_new(state); if (!socket) { amqp_abort("%s", strerror(errno)); } amqp_tcp_socket_set_sockfd(socket, sockfd); }
int create_queue_item(void *buf, size_t len) { printf("length: %zu\n", len); amqp_connection_state_t conn = amqp_new_connection(); amqp_socket_t *socket = amqp_tcp_socket_new(conn); if (!socket) { printf("Error creating TCP socket.\n"); return -1; } int status = amqp_socket_open(socket, "localhost", 5672); if (status) { printf("Error opening TCP socket.\n"); return -1; } // Open channel if (handle_amqp_error(amqp_login(conn, "/", 0, 131072, 0, AMQP_SASL_METHOD_PLAIN, "guest", "guest"), "Login")) { return -1; } amqp_channel_open(conn, 1); if (handle_amqp_error(amqp_get_rpc_reply(conn), "Get Reply")) { return -1; } // Send message { amqp_basic_properties_t props; props._flags = AMQP_BASIC_CONTENT_TYPE_FLAG | AMQP_BASIC_DELIVERY_MODE_FLAG; props.content_type = amqp_cstring_bytes("text/plain"); props.delivery_mode = 2; /* persistent delivery mode */ status = amqp_basic_publish(conn, 1, amqp_cstring_bytes(""), amqp_cstring_bytes("test-route"), 0, 0, &props, amqp_cstring_bytes(buf)); if (status < 0) { printf("Error publishing\n"); return -1; } printf("Published the thingy\n"); } // Close channel amqp_channel_close(conn, 1, AMQP_REPLY_SUCCESS); amqp_connection_close(conn, AMQP_REPLY_SUCCESS); amqp_destroy_connection(conn); return 0; }
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; amqp_socket_t *socket; amqp_connection_state_t conn; struct timeval tval; struct timeval *tv; if (argc < 3) { fprintf(stderr, "Usage: amqp_connect_timeout host port [timeout_sec [timeout_usec=0]]\n"); return 1; } if (argc > 3) { tv = &tval; tv->tv_sec = atoi(argv[3]); if (argc > 4 ) { tv->tv_usec = atoi(argv[4]); } else { tv->tv_usec = 0; } } else { tv = NULL; } hostname = argv[1]; port = atoi(argv[2]); conn = amqp_new_connection(); socket = amqp_tcp_socket_new(conn); if (!socket) { die("creating TCP socket"); } die_on_error(amqp_socket_open_noblock(socket, hostname, port, tv), "opening TCP socket"); die_on_amqp_error(amqp_login(conn, "/", 0, 131072, 0, AMQP_SASL_METHOD_PLAIN, "guest", "guest"), "Logging in"); die_on_amqp_error(amqp_connection_close(conn, AMQP_REPLY_SUCCESS), "Closing connection"); die_on_error(amqp_destroy_connection(conn), "Ending connection"); printf ("Done\n"); return 0; }
int main(int argc, char const *const *argv) { char const *hostname; int port, status; char const *exchange; char const *bindingkey; char const *queue; amqp_socket_t *socket = NULL; amqp_connection_state_t conn; if (argc < 6) { fprintf(stderr, "Usage: amqp_bind host port exchange bindingkey queue\n"); return 1; } hostname = argv[1]; port = atoi(argv[2]); exchange = argv[3]; bindingkey = argv[4]; queue = argv[5]; 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_queue_bind(conn, 1, amqp_cstring_bytes(queue), amqp_cstring_bytes(exchange), amqp_cstring_bytes(bindingkey), amqp_empty_table); die_on_amqp_error(amqp_get_rpc_reply(conn), "Unbinding"); 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 RabbitMQConnection::_rabbitMQConnection(const std::string& hostname, int port, const std::string& vhost, const std::string& user, const std::string& password) { this->channel_n=0; this->conn = amqp_new_connection(); amqp_socket_t * socket = amqp_tcp_socket_new(conn); if (!socket) { die("creating TCP socket"); } int status = amqp_socket_open(socket, hostname.c_str(), port); if (status) { die("opening TCP socket"); } die_on_amqp_error(amqp_login(this->conn, vhost.c_str(), 0, 131072, 60, AMQP_SASL_METHOD_PLAIN, user.c_str(), password.c_str()), "Logging in"); }
static amqp_connection_state_t get_amqp_connection(void) { int status; amqp_connection_state_t conn; amqp_rpc_reply_t reply; amqp_socket_t *socket = NULL; conn = amqp_new_connection(); if (!conn) { fprintf(stderr, "[!] Error getting ampq connection\n"); return NULL; } socket = amqp_tcp_socket_new(conn); if (!socket) { amqp_destroy_connection(conn); fprintf(stderr, "[!] Error creating the TCP socket!\n"); return NULL; } status = amqp_socket_open(socket, "localhost", 5672); if (status) { amqp_connection_close(conn, AMQP_REPLY_SUCCESS); amqp_destroy_connection(conn); fprintf(stderr, "[!] Error opening the TCP socket!\n"); return NULL; } reply = amqp_login(conn, "/", 0, 131072, 0, AMQP_SASL_METHOD_PLAIN, "guest", "guest"); if (reply.reply_type != AMQP_RESPONSE_NORMAL) { amqp_connection_close(conn, AMQP_REPLY_SUCCESS); amqp_destroy_connection(conn); fprintf(stderr, "[!] Error loggging into server\n"); return NULL; } if (!amqp_channel_open(conn, 1)) { amqp_channel_close(conn, 1, AMQP_REPLY_SUCCESS); amqp_connection_close(conn, AMQP_REPLY_SUCCESS); fprintf(stderr, "[!] Error opening channel 1\n"); return NULL; } return conn; }
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; }
void AMQP::sockConnect() { cnn = amqp_new_connection(); sockfd = amqp_tcp_socket_new(cnn); if (!sockfd) { amqp_destroy_connection(cnn); throw AMQPException("AMQP cannot create socket descriptor"); } //cout << "sockfd="<< sockfd << " pid=" << getpid() <<endl; int status = amqp_socket_open(sockfd, host.c_str(), port); if (status != AMQP_STATUS_OK) { amqp_connection_close(cnn, AMQP_REPLY_SUCCESS); amqp_destroy_connection(cnn); throw AMQPException("AMQP cannot open socket"); } }
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; }
int main(int argc, char const *const *argv) { char const *hostname; int port, status; int rate_limit; int message_count; amqp_socket_t *socket = NULL; amqp_connection_state_t conn; if (argc < 5) { fprintf(stderr, "Usage: amqp_producer host port rate_limit message_count\n"); return 1; } hostname = argv[1]; port = atoi(argv[2]); rate_limit = atoi(argv[3]); message_count = atoi(argv[4]); conn = amqp_new_connection(); socket = amqp_tcp_socket_new(conn); if (!socket) { die("creating TCP socket"); } status = amqp_socket_open(socket, hostname, port); if (status) { die("opening TCP 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"); send_batch(conn, "test queue", rate_limit, message_count); 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; }
amqp_connection_state_t setup_connection_and_channel(void) { amqp_connection_state_t connection_state_ = amqp_new_connection(); amqp_socket_t *socket = amqp_tcp_socket_new(connection_state_); assert(socket); int rc = amqp_socket_open(socket, "localhost", AMQP_PROTOCOL_PORT); assert(rc == AMQP_STATUS_OK); amqp_rpc_reply_t rpc_reply = amqp_login( connection_state_, "/", 1, AMQP_DEFAULT_FRAME_SIZE, AMQP_DEFAULT_HEARTBEAT, AMQP_SASL_METHOD_PLAIN, "guest", "guest"); assert(rpc_reply.reply_type == AMQP_RESPONSE_NORMAL); amqp_channel_open_ok_t *res = amqp_channel_open(connection_state_, fixed_channel_id); assert(res != NULL); return connection_state_; }
static amqp_connection_state_t setup_amqp_connection() { amqp_connection_state_t connection = amqp_new_connection(); amqp_socket_t *socket = amqp_tcp_socket_new(connection); #ifdef FIX_SIG_PIPE // why doesn't librabbitmq do this, eh? int sockfd = amqp_get_sockfd(connection); int one = 1; // setsockopt(sockfd, SOL_SOCKET, SO_NOSIGPIPE, &one, sizeof(one)); #endif int status = amqp_socket_open(socket, rabbit_host, rabbit_port); assert_x(!status, "Opening RabbitMQ socket", __FILE__, __LINE__); die_on_amqp_error(amqp_login(connection, "/", 0, OUR_FRAME_MAX, 0, AMQP_SASL_METHOD_PLAIN, "guest", "guest"), "Logging in to RabbitMQ"); amqp_channel_open(connection, 1); die_on_amqp_error(amqp_get_rpc_reply(connection), "Opening AMQP channel"); return connection; }
void AMQP::sockConnect() { int status; cnn = amqp_new_connection(); switch(proto) { case AMQPS_proto: { sockfd = amqp_ssl_socket_new(cnn); status = amqp_ssl_socket_set_cacert(sockfd, cacert_path.c_str()); if (status) { throw AMQPException("AMQP cannot set CA certificate"); } status = amqp_ssl_socket_set_key(sockfd, client_cert_path.c_str(), client_key_path.c_str()); if (status) { throw AMQPException("AMQP cannot set client certificate or key"); } amqp_ssl_socket_set_verify_peer(sockfd, verify_peer ? 1 : 0); amqp_ssl_socket_set_verify_hostname(sockfd, verify_hostname ? 1 : 0); } break; case AMQP_proto: default: sockfd = amqp_tcp_socket_new(cnn); break; } status = amqp_socket_open(sockfd, host.c_str(), port); if (status){ amqp_destroy_connection(cnn); throw AMQPException("AMQP cannot create socket"); } }
static int send(struct stats_buffer *sf) { int status; char const *exchange; char const *routingkey; amqp_socket_t *socket = NULL; amqp_connection_state_t conn; exchange = "amq.direct"; routingkey = "tacc_stats"; conn = amqp_new_connection(); socket = amqp_tcp_socket_new(conn); status = amqp_socket_open(socket, sf->sf_host, atoi(sf->sf_port)); amqp_login(conn, "/", 0, 131072, 0, AMQP_SASL_METHOD_PLAIN, "guest", "guest"); amqp_channel_open(conn, 1); amqp_get_rpc_reply(conn); { amqp_basic_properties_t props; props._flags = AMQP_BASIC_CONTENT_TYPE_FLAG | AMQP_BASIC_DELIVERY_MODE_FLAG; props.content_type = amqp_cstring_bytes("text/plain"); props.delivery_mode = 2; /* persistent delivery mode */ amqp_basic_publish(conn, 1, amqp_cstring_bytes(exchange), amqp_cstring_bytes(routingkey), 0, 0, &props, amqp_cstring_bytes(sf->sf_data)); } amqp_channel_close(conn, 1, AMQP_REPLY_SUCCESS); amqp_connection_close(conn, AMQP_REPLY_SUCCESS); amqp_destroy_connection(conn); return 0; }
RabbitMQConnection::RabbitMQConnection (const std::string &address, int port) : address (address), port (port) { conn = amqp_new_connection(); socket = amqp_tcp_socket_new (conn); if (!socket) { throw Glib::IOChannelError (Glib::IOChannelError::Code::FAILED, "Cannot create TCP socket"); } if (amqp_socket_open (socket, address.c_str(), port) ) { throw Glib::IOChannelError (Glib::IOChannelError::Code::FAILED, "Cannot open TCP socket"); } makeSocketLinger (amqp_socket_get_sockfd (socket) ); exception_on_error (amqp_login (conn, "/", 0, 131072, 0, AMQP_SASL_METHOD_PLAIN, "guest", "guest"), "Loging in"); amqp_channel_open (conn, 1); exception_on_error (amqp_get_rpc_reply (conn), "Opening channel"); }
static int init_amqp_connection(amqp_connection_state_t *conn) { int status; amqp_socket_t *socket = NULL; struct amqp_connection_info ci; amqp_rpc_reply_t ret; ci.user = "******"; ci.password = "******"; ci.host = "localhost"; ci.vhost = "/"; ci.port = 20001; ci.ssl = 0; *conn = amqp_new_connection(); if (ci.ssl) { #ifdef WITH_SSL socket = amqp_ssl_socket_new(*conn); if (!socket) { LB_SYSLOG(LOG_ERR, "creating SSL/TLS socket\n"); return LC_BUS_ERR; } 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 LB_SYSLOG(LOG_ERR, "librabbitmq was not built with SSL/TLS support\n"); return LC_BUS_ERR; #endif } else { socket = amqp_tcp_socket_new(*conn); if (!socket) { LB_SYSLOG(LOG_ERR, "creating TCP socket (out of memory)\n"); return LC_BUS_ERR; } } status = amqp_socket_open(socket, ci.host, ci.port); if (status) { LB_SYSLOG(LOG_ERR, "opening socket to %s:%d, status=%d, errno=%s.\n", ci.host, ci.port, status, strerror(errno)); return LC_BUS_ERR; } ret = amqp_login( *conn, ci.vhost, LC_BUS_MAX_CHANNEL, /* channel max */ LC_BUS_MAX_FRAME_SIZE, 0, /* heartbeat */ AMQP_SASL_METHOD_PLAIN, ci.user, ci.password ); if (ret.reply_type != AMQP_RESPONSE_NORMAL) { LB_SYSLOG(LOG_ERR, "error in logging AMQP server, %s\n", amqp_rpc_reply_string(&ret)); return LC_BUS_ERR; } return LC_BUS_OK; }
int main(int argc, char const *const *argv) { struct sockaddr_in si_me, si_other; int s, i, slen = sizeof(si_other) , recv_len; char buf[BUFLEN]; if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) { died("socket"); } memset((char *) &si_me, 0, sizeof(si_me)); si_me.sin_family = AF_INET; si_me.sin_port = htons(PORT); si_me.sin_addr.s_addr = htonl(INADDR_ANY); //bind socket to port if( bind(s , (struct sockaddr*)&si_me, sizeof(si_me) ) == -1) { died("bind"); } if (argc < 2) { fprintf(stderr, "Usage: udp_producer host port\n"); return 1; } char const *hostname; int port; hostname = argv[1]; port = atoi(argv[2]); // amqp // char const *hostname = "localhost"; int port = 5674; int status; amqp_socket_t *socket = NULL; amqp_connection_state_t conn; conn = amqp_new_connection(); socket = amqp_tcp_socket_new(conn); if (!socket) { died("creating TCP socket"); } status = amqp_socket_open(socket, hostname, port); if (status) { died("opening TCP 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"); while(1) { if ((recv_len = recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *) &si_other, &slen)) == -1) { died("recvfrom()"); } amqp_bytes_t message_bytes; message_bytes.len = sizeof(buf); message_bytes.bytes = buf; die_on_error(amqp_basic_publish(conn, 1, amqp_cstring_bytes("udp"), amqp_cstring_bytes("udp"), 0, 0,NULL, message_bytes),"Publishing"); } close(s); return 0; }
/** * 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; }
static int camqp_connect (camqp_config_t *conf) /* {{{ */ { static time_t last_connect_time = 0; amqp_rpc_reply_t reply; int status; #ifdef HAVE_AMQP_TCP_SOCKET amqp_socket_t *socket; #else int sockfd; #endif if (conf->connection != NULL) return (0); time_t now = time(NULL); if (now < (last_connect_time + conf->connection_retry_delay)) { DEBUG("amqp plugin: skipping connection retry, " "ConnectionRetryDelay: %d", conf->connection_retry_delay); return(1); } else { DEBUG ("amqp plugin: retrying connection"); last_connect_time = now; } conf->connection = amqp_new_connection (); if (conf->connection == NULL) { ERROR ("amqp plugin: amqp_new_connection failed."); return (ENOMEM); } #ifdef HAVE_AMQP_TCP_SOCKET # define CLOSE_SOCKET() /* amqp_destroy_connection() closes the socket for us */ /* TODO: add support for SSL using amqp_ssl_socket_new * and related functions */ socket = amqp_tcp_socket_new (conf->connection); if (! socket) { ERROR ("amqp plugin: amqp_tcp_socket_new failed."); amqp_destroy_connection (conf->connection); conf->connection = NULL; return (ENOMEM); } status = amqp_socket_open (socket, CONF(conf, host), conf->port); if (status < 0) { char errbuf[1024]; status *= -1; ERROR ("amqp plugin: amqp_socket_open failed: %s", sstrerror (status, errbuf, sizeof (errbuf))); amqp_destroy_connection (conf->connection); conf->connection = NULL; return (status); } #else /* HAVE_AMQP_TCP_SOCKET */ # define CLOSE_SOCKET() close(sockfd) /* this interface is deprecated as of rabbitmq-c 0.4 */ sockfd = amqp_open_socket (CONF(conf, host), conf->port); if (sockfd < 0) { char errbuf[1024]; status = (-1) * sockfd; ERROR ("amqp plugin: amqp_open_socket failed: %s", sstrerror (status, errbuf, sizeof (errbuf))); amqp_destroy_connection (conf->connection); conf->connection = NULL; return (status); } amqp_set_sockfd (conf->connection, sockfd); #endif reply = amqp_login (conf->connection, CONF(conf, vhost), /* channel max = */ 0, /* frame max = */ 131072, /* heartbeat = */ 0, /* authentication = */ AMQP_SASL_METHOD_PLAIN, CONF(conf, user), CONF(conf, password)); if (reply.reply_type != AMQP_RESPONSE_NORMAL) { ERROR ("amqp plugin: amqp_login (vhost = %s, user = %s) failed.", CONF(conf, vhost), CONF(conf, user)); amqp_destroy_connection (conf->connection); CLOSE_SOCKET (); conf->connection = NULL; return (1); } amqp_channel_open (conf->connection, /* channel = */ 1); /* FIXME: Is checking "reply.reply_type" really correct here? How does * it get set? --octo */ if (reply.reply_type != AMQP_RESPONSE_NORMAL) { ERROR ("amqp plugin: amqp_channel_open failed."); amqp_connection_close (conf->connection, AMQP_REPLY_SUCCESS); amqp_destroy_connection (conf->connection); CLOSE_SOCKET (); conf->connection = NULL; return (1); } INFO ("amqp plugin: Successfully opened connection to vhost \"%s\" " "on %s:%i.", CONF(conf, vhost), CONF(conf, host), conf->port); status = camqp_create_exchange (conf); if (status != 0) return (status); if (!conf->publish) return (camqp_setup_queue (conf)); return (0); } /* }}} int camqp_connect */
int main(){ int status; amqp_rpc_reply_t reply; printf("RabbitMQ Library Init...\n"); // init new connection mq_conn = amqp_new_connection(); if((mq_socket = amqp_tcp_socket_new(mq_conn)) == NULL) { printf("RabbitMQ amqp_tcp_socket_new FAILED...\n"); } if((status = amqp_socket_open(mq_socket, (rabbitmq_hostname == NULL)?"127.0.0.1":rabbitmq_hostname, (rabbitmq_port == 0)?5672:rabbitmq_port)) != AMQP_STATUS_OK) { printf("RabbitMQ amqp_socket_open FAILED...\n"); } reply = amqp_login(mq_conn, (rabbitmq_virtualhost==NULL)?"/":rabbitmq_virtualhost, 0, 131072, 0, AMQP_SASL_METHOD_PLAIN, (rabbitmq_username==NULL)?"guest":rabbitmq_username, (rabbitmq_password==NULL)?"guest":rabbitmq_password); switch(reply.reply_type) { case AMQP_RESPONSE_NORMAL: { printf("RabbitMQ Init Success.\n"); break; } case AMQP_RESPONSE_LIBRARY_EXCEPTION: { printf("RabbitMQ Init Failed: AMQP_RESPONSE_LIBRARY_EXCEPTION.\n"); break; } case AMQP_RESPONSE_SERVER_EXCEPTION: { printf("RabbitMQ Init Failed: AMQP_RESPONSE_SERVER_EXCEPTION.\n"); break; } case AMQP_RESPONSE_NONE: { printf("RabbitMQ Init Failed: AMQP_RESPONSE_NONE.\n"); break; } } amqp_channel_open(mq_conn, 1); amqp_get_rpc_reply(mq_conn); printf("Message will send to RabbitMQ: [%s:%d] [virtualhost:%s exchange:%s routingkey:%s]\n", rabbitmq_hostname, rabbitmq_port, rabbitmq_virtualhost, rabbitmq_exchange, rabbitmq_routingkey ); char *msg = "{\"ds\": \"1\"}"; send_msg_to_rabbitmq(msg); amqp_channel_close(mq_conn, 1, AMQP_REPLY_SUCCESS); amqp_connection_close(mq_conn, AMQP_REPLY_SUCCESS); amqp_destroy_connection(mq_conn); }
static int rmq_reconnect(evi_reply_sock *sock) { rmq_params_t * rmqp = (rmq_params_t *)sock->params; #if defined AMQP_VERSION_v04 amqp_socket_t *amqp_sock; #endif int socket; if (!rmqp || !(rmqp->flags & RMQ_PARAM_RKEY)) { LM_ERR("not enough socket info\n"); return -1; } if (!(rmqp->flags & RMQ_PARAM_CONN) || !rmqp->conn) { /* init new connection */ if (!(rmqp->conn = amqp_new_connection())) { LM_ERR("cannot create new connection\n"); return -1; } rmqp->flags |= RMQ_PARAM_CONN; #if defined AMQP_VERSION_v04 amqp_sock = amqp_tcp_socket_new(rmqp->conn); if (!amqp_sock) { LM_ERR("cannot create AMQP socket\n"); goto destroy_rmqp; } socket = amqp_socket_open(amqp_sock, sock->address.s, sock->port); if (socket < 0) { LM_ERR("cannot open AMQP socket\n"); goto destroy_rmqp; } #else socket = amqp_open_socket(sock->address.s, sock->port); if (socket < 0) { LM_ERR("cannot open AMQP socket\n"); goto destroy_rmqp; } amqp_set_sockfd(rmqp->conn, socket); #endif if (rmq_error("Logging in", amqp_login( rmqp->conn, RMQ_DEFAULT_VHOST, 0, RMQ_DEFAULT_MAX, rmqp->heartbeat, AMQP_SASL_METHOD_PLAIN, rmqp->flags & RMQ_PARAM_USER ? rmqp->user.s : RMQ_DEFAULT_UP, rmqp->flags & RMQ_PARAM_PASS ? rmqp->pass.s : RMQ_DEFAULT_UP))) goto destroy_rmqp; } if (!(rmqp->flags & RMQ_PARAM_CHAN)) { rmqp->channel = 1; amqp_channel_open(rmqp->conn, rmqp->channel); rmqp->flags |= RMQ_PARAM_CHAN; if (rmq_error("Opening channel", amqp_get_rpc_reply(rmqp->conn))) goto destroy_rmqp; } return 0; destroy_rmqp: rmq_destroy_param(rmqp); return -1; }
int main(int argc, char const *const *argv) { char const *hostname; int port, status; char const *exchange; char const *bindingkey; amqp_socket_t *socket = NULL; amqp_connection_state_t conn; amqp_bytes_t queuename; if (argc < 5) { fprintf(stderr, "Usage: amqp_listen host port exchange bindingkey\n"); return 1; } hostname = argv[1]; port = atoi(argv[2]); exchange = argv[3]; bindingkey = argv[4]; conn = amqp_new_connection(); socket = amqp_tcp_socket_new(conn); if (!socket) { die("creating TCP socket"); } status = amqp_socket_open(socket, hostname, port); if (status) { die("opening TCP 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_queue_declare_ok_t *r = amqp_queue_declare(conn, 1, amqp_empty_bytes, 0, 0, 0, 1, amqp_empty_table); die_on_amqp_error(amqp_get_rpc_reply(conn), "Declaring queue"); queuename = amqp_bytes_malloc_dup(r->queue); if (queuename.bytes == NULL) { fprintf(stderr, "Out of memory while copying queue name"); return 1; } } amqp_queue_bind(conn, 1, queuename, amqp_cstring_bytes(exchange), amqp_cstring_bytes(bindingkey), amqp_empty_table); die_on_amqp_error(amqp_get_rpc_reply(conn), "Binding queue"); amqp_basic_consume(conn, 1, queuename, amqp_empty_bytes, 0, 1, 0, amqp_empty_table); die_on_amqp_error(amqp_get_rpc_reply(conn), "Consuming"); { while (1) { amqp_rpc_reply_t res; amqp_envelope_t envelope; amqp_maybe_release_buffers(conn); res = amqp_consume_message(conn, &envelope, NULL, 0); if (AMQP_RESPONSE_NORMAL != res.reply_type) { break; } printf("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) { printf("Content-type: %.*s\n", (int) envelope.message.properties.content_type.len, (char *) envelope.message.properties.content_type.bytes); } amqp_destroy_envelope(&envelope); } } 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; }
amqp_connection_resource *connection_resource_constructor(amqp_connection_params *params, zend_bool persistent TSRMLS_DC) { struct timeval tv = {0}; struct timeval *tv_ptr = &tv; char *std_datetime; amqp_table_entry_t client_properties_entries[5]; amqp_table_t client_properties_table; amqp_table_entry_t custom_properties_entries[1]; amqp_table_t custom_properties_table; amqp_connection_resource *resource; /* Allocate space for the connection resource */ resource = (amqp_connection_resource *)pecalloc(1, sizeof(amqp_connection_resource), persistent); /* Create the connection */ resource->connection_state = amqp_new_connection(); /* Create socket object */ resource->socket = amqp_tcp_socket_new(resource->connection_state); if (params->connect_timeout > 0) { tv.tv_sec = (long int) params->connect_timeout; tv.tv_usec = (long int) ((params->connect_timeout - tv.tv_sec) * 1000000); } else { tv_ptr = NULL; } /* Try to connect and verify that no error occurred */ if (amqp_socket_open_noblock(resource->socket, params->host, params->port, tv_ptr)) { zend_throw_exception(amqp_connection_exception_class_entry, "Socket error: could not connect to host.", 0 TSRMLS_CC); connection_resource_destructor(resource, persistent TSRMLS_CC); return NULL; } if (!php_amqp_set_resource_read_timeout(resource, params->read_timeout TSRMLS_CC)) { connection_resource_destructor(resource, persistent TSRMLS_CC); return NULL; } if (!php_amqp_set_resource_write_timeout(resource, params->write_timeout TSRMLS_CC)) { connection_resource_destructor(resource, persistent TSRMLS_CC); return NULL; } std_datetime = php_std_date(time(NULL) TSRMLS_CC); client_properties_entries[0].key = amqp_cstring_bytes("type"); client_properties_entries[0].value.kind = AMQP_FIELD_KIND_UTF8; client_properties_entries[0].value.value.bytes = amqp_cstring_bytes("php-amqp extension"); client_properties_entries[1].key = amqp_cstring_bytes("version"); client_properties_entries[1].value.kind = AMQP_FIELD_KIND_UTF8; client_properties_entries[1].value.value.bytes = amqp_cstring_bytes(PHP_AMQP_VERSION); client_properties_entries[2].key = amqp_cstring_bytes("revision"); client_properties_entries[2].value.kind = AMQP_FIELD_KIND_UTF8; client_properties_entries[2].value.value.bytes = amqp_cstring_bytes(PHP_AMQP_REVISION); client_properties_entries[3].key = amqp_cstring_bytes("connection type"); client_properties_entries[3].value.kind = AMQP_FIELD_KIND_UTF8; client_properties_entries[3].value.value.bytes = amqp_cstring_bytes(persistent ? "persistent" : "transient"); client_properties_entries[4].key = amqp_cstring_bytes("connection started"); client_properties_entries[4].value.kind = AMQP_FIELD_KIND_UTF8; client_properties_entries[4].value.value.bytes = amqp_cstring_bytes(std_datetime); client_properties_table.entries = client_properties_entries; client_properties_table.num_entries = sizeof(client_properties_entries) / sizeof(amqp_table_entry_t); custom_properties_entries[0].key = amqp_cstring_bytes("client"); custom_properties_entries[0].value.kind = AMQP_FIELD_KIND_TABLE; custom_properties_entries[0].value.value.table = client_properties_table; custom_properties_table.entries = custom_properties_entries; custom_properties_table.num_entries = sizeof(custom_properties_entries) / sizeof(amqp_table_entry_t); /* We can assume that connection established here but it is not true, real handshake goes during login */ assert(params->frame_max > 0); amqp_rpc_reply_t res = amqp_login_with_properties( resource->connection_state, params->vhost, params->channel_max, params->frame_max, params->heartbeat, &custom_properties_table, AMQP_SASL_METHOD_PLAIN, params->login, params->password ); efree(std_datetime); if (res.reply_type != AMQP_RESPONSE_NORMAL) { char *message, *long_message; php_amqp_connection_resource_error(res, &message, resource, 0 TSRMLS_CC); spprintf(&long_message, 0, "%s - Potential login failure.", message); zend_throw_exception(amqp_connection_exception_class_entry, long_message, 0 TSRMLS_CC); efree(message); efree(long_message); /* https://www.rabbitmq.com/resources/specs/amqp0-9-1.pdf * * 2.2.4 The Connection Class: * ... a peer that detects an error MUST close the socket without sending any further data. * * 4.10.2 Denial of Service Attacks: * ... The general response to any exceptional condition in the connection negotiation is to pause that connection * (presumably a thread) for a period of several seconds and then to close the network connection. This * includes syntax errors, over-sized data, and failed attempts to authenticate. */ connection_resource_destructor(resource, persistent TSRMLS_CC); return NULL; } /* Allocate space for the channel slots in the ring buffer */ resource->max_slots = (amqp_channel_t) amqp_get_channel_max(resource->connection_state); assert(resource->max_slots > 0); resource->slots = (amqp_channel_resource **)pecalloc(resource->max_slots + 1, sizeof(amqp_channel_object*), persistent); resource->is_connected = '\1'; return resource; }
int main(int argc, char const *const *argv) { char const *hostname; int port, status; char const *exchange; char const *bindingkey; amqp_socket_t *socket = NULL; amqp_connection_state_t conn; amqp_bytes_t queuename; if (argc < 3) { fprintf(stderr, "Usage: amqp_consumer host port\n"); return 1; } hostname = argv[1]; port = atoi(argv[2]); exchange = "amq.direct"; /* argv[3]; */ bindingkey = "test queue"; /* argv[4]; */ conn = amqp_new_connection(); socket = amqp_tcp_socket_new(conn); if (!socket) { die("creating TCP socket"); } status = amqp_socket_open(socket, hostname, port); if (status) { die("opening TCP 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_queue_declare_ok_t *r = amqp_queue_declare(conn, 1, amqp_empty_bytes, 0, 0, 0, 1, amqp_empty_table); die_on_amqp_error(amqp_get_rpc_reply(conn), "Declaring queue"); queuename = amqp_bytes_malloc_dup(r->queue); if (queuename.bytes == NULL) { fprintf(stderr, "Out of memory while copying queue name"); return 1; } } amqp_queue_bind(conn, 1, queuename, amqp_cstring_bytes(exchange), amqp_cstring_bytes(bindingkey), amqp_empty_table); die_on_amqp_error(amqp_get_rpc_reply(conn), "Binding queue"); amqp_basic_consume(conn, 1, queuename, amqp_empty_bytes, 0, 1, 0, amqp_empty_table); die_on_amqp_error(amqp_get_rpc_reply(conn), "Consuming"); run(conn); 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, char *argv[]) { char const *hostname; int port, status; char const *exchange; char const *routingkey; char const *messagebody; amqp_socket_t *socket = NULL; amqp_connection_state_t conn; amqp_bytes_t reply_to_queue; if (argc < 6) { /* minimum number of mandatory arguments */ fprintf(stderr, "usage:\namqp_rpc_sendstring_client host port exchange routingkey messagebody\n"); return 1; } hostname = argv[1]; port = atoi(argv[2]); exchange = argv[3]; routingkey = argv[4]; messagebody = argv[5]; /* establish a channel that is used to connect RabbitMQ server */ conn = amqp_new_connection(); socket = amqp_tcp_socket_new(conn); if (!socket) { printf("creating TCP socket"); } status = amqp_socket_open(socket, hostname, port); if (status) { printf("opening TCP socket"); } printf("established connection!\n"); amqp_login(conn, "/", 0, 131072, 0, AMQP_SASL_METHOD_PLAIN, "guest", "guest");// "Logging in"); amqp_channel_open(conn, 1); amqp_get_rpc_reply(conn);//, "Opening channel"); printf("open channel!\n"); /* create private reply_to queue */ { amqp_queue_declare_ok_t *r = amqp_queue_declare(conn, 1, amqp_empty_bytes, 0, 0, 0, 1, amqp_empty_table); amqp_get_rpc_reply(conn);//, "Declaring queue"); printf("declare queue!\n"); reply_to_queue = amqp_bytes_malloc_dup(r->queue); if (reply_to_queue.bytes == NULL) { fprintf(stderr, "Out of memory while copying queue name"); return 1; } } /* send the message */ { /* set properties */ amqp_basic_properties_t props; props._flags = AMQP_BASIC_CONTENT_TYPE_FLAG | AMQP_BASIC_DELIVERY_MODE_FLAG | AMQP_BASIC_REPLY_TO_FLAG | AMQP_BASIC_CORRELATION_ID_FLAG; props.content_type = amqp_cstring_bytes("text/plain"); props.delivery_mode = 2; /* persistent delivery mode */ props.reply_to = amqp_bytes_malloc_dup(reply_to_queue); if (props.reply_to.bytes == NULL) { fprintf(stderr, "Out of memory while copying queue name"); return 1; } props.correlation_id = amqp_cstring_bytes("1"); /* publish */ amqp_basic_publish(conn, 1, amqp_cstring_bytes(exchange), amqp_cstring_bytes(routingkey), 0, 0, &props, amqp_cstring_bytes(messagebody)); // "Publishing"); amqp_bytes_free(props.reply_to); } /* wait an answer */ printf("waiting answer!\n"); { amqp_basic_consume(conn, 1, reply_to_queue, amqp_empty_bytes, 0, 1, 0, amqp_empty_table); amqp_get_rpc_reply(conn);//, "Consuming"); amqp_bytes_free(reply_to_queue); { 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); } printf("----\n"); body_target = frame.payload.properties.body_size; body_received = 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; printf("len -> %d \n",frame.payload.body_fragment.len); assert(body_received <= body_target); amqp_dump(frame.payload.body_fragment.bytes, frame.payload.body_fragment.len); } if (body_received != body_target) { /* Can only happen when amqp_simple_wait_frame returns <= 0 */ /* We break here to close the connection */ break; } /* everything was fine, we can quit now because we received the reply */ break; } } } /* closing */ amqp_channel_close(conn, 1, AMQP_REPLY_SUCCESS);//, "Closing channel"); amqp_connection_close(conn, AMQP_REPLY_SUCCESS);//, "Closing connection"); amqp_destroy_connection(conn);//, "Ending connection"); return 0; }
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; }