void Channel::listen(Callable_envelope& callback){
	amqp_connection_state_t conn = this->conn_ptr->_getconn_n();
		//open channel
		//amqp_channel_open(conn, channel);
		//die_on_amqp_error(amqp_get_rpc_reply(conn), "Opening channel");
		//set consumer
	amqp_basic_consume(conn,
			this->channel_n,
			amqp_cstring_bytes(this->queue_name.c_str()),
			amqp_empty_bytes, 0, 1, 0, amqp_empty_table);//auto ack consumer
	die_on_amqp_error(amqp_get_rpc_reply(this->conn_ptr->_getconn_n()), "rabbitmq_listen 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) {
			        //this solution is not really work when network changed.

			    	  label_listen_reconnet:
			    	  Log::log.warning("Channel::listen, AMQP response abnormal\n");
			    	  Log::log.warning("Channel::listen, reconnecting...\n");
			    	  //first close previous
			    	  amqp_channel_close(this->conn_ptr->_getconn_n(), this->channel_n, AMQP_REPLY_SUCCESS);
			    	  amqp_connection_close(this->conn_ptr->_getconn_n(), AMQP_REPLY_SUCCESS);
			    	  amqp_destroy_connection(this->conn_ptr->_getconn_n());

			    	  //reconnect to rabbitMQ server!
			    	  this->conn_ptr->reconnnect();
			    	  amqp_channel_open(this->conn_ptr->_getconn_n(), channel_n);
			    	  if(amqp_get_rpc_reply(this->conn_ptr->_getconn_n()).reply_type !=AMQP_RESPONSE_NORMAL){
			    	  	Log::log.warning("Channel::publish:Opening channel_n error\n");
			    	  	sleep(2);
			    	  	goto label_listen_reconnet;
			    	  }else{
			    		  continue;
			    	  }
			      }
			      callback.callback(envelope);
			      amqp_destroy_envelope(&envelope);
			 }
		}
		die_on_amqp_error(amqp_channel_close(conn, this->channel_n, AMQP_REPLY_SUCCESS), "Closing channel");
}
int main(int argc, char const * const *argv) {
  char const *hostname;
  int port;
  int rate_limit;
  int message_count;

  int sockfd;
  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();

  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, 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;
}
RabbitMQConnection::~RabbitMQConnection()
{
  int fd;

  if (conn == NULL) {
    GST_DEBUG ("service already stopped");
    return;
  }

  fd = amqp_socket_get_sockfd (socket);

  /* Errors are ignored during close */
  if (!closeOnRelease) {
    /* close socket */
    close (fd);
  }

  amqp_channel_close (conn, 1, AMQP_REPLY_SUCCESS);
  amqp_connection_close (conn, AMQP_REPLY_SUCCESS);
  amqp_destroy_connection (conn);

  if (closeOnRelease) {
    /* inform remote side that we are done */
    shutdown (fd, SHUT_WR);
  }

  conn = NULL;
}
Beispiel #4
0
void rpc_close(struct status *status)
{
    int ret = 0;

    DEBUG(2, ("[*] Closing RPC\n"));

    /* Adquire lock */
    ret = pthread_spin_lock(&status->lock);
    if (ret) {
        fprintf(stderr, "[!] pthread_spin_lock: %s\n", strerror(ret));
        return;
    }

    if (status->conn) {
        if (amqp_channel_close(status->conn, 1, AMQP_REPLY_SUCCESS).reply_type != AMQP_RESPONSE_NORMAL) {
            fprintf(stderr, "[!] Error closing AMQP channel\n");
        }
        if (amqp_connection_close(status->conn, AMQP_REPLY_SUCCESS).reply_type != AMQP_RESPONSE_NORMAL) {
            fprintf(stderr, "[!] Error closing AMQP connection\n");
        }
        if (amqp_destroy_connection(status->conn) < 0) {
            fprintf(stderr, "[!] Error destroying AMQP connection\n");
        }
    }
    status->conn = NULL;

    /* Release lock */
    pthread_spin_unlock(&status->lock);
}
Beispiel #5
0
static int exit_amqp_connection_atomic()
{
    int res = LC_BUS_OK;
    amqp_rpc_reply_t ret;
    int i;

    for (i = 0; i < LC_BUS_MAX_CONNECTION; ++i) {
        if (connection_tid[i]) {
            ret = amqp_channel_close(lc_conn[i],
                    LC_BUS_CHANNEL, AMQP_REPLY_SUCCESS);
            if (ret.reply_type != AMQP_RESPONSE_NORMAL) {
                LB_SYSLOG(LOG_ERR, "error in closing channel: %s\n",
                        amqp_rpc_reply_string(&ret));
            }

            ret = amqp_connection_close(lc_conn[i], AMQP_REPLY_SUCCESS);
            if (ret.reply_type != AMQP_RESPONSE_NORMAL) {
                LB_SYSLOG(LOG_ERR, "error in closing connection, %s\n",
                        amqp_rpc_reply_string(&ret));
            }

            res = amqp_destroy_connection(lc_conn[i]);
            if (res) {
                LB_SYSLOG(LOG_ERR, "error in destroy connection ret=%d\n", res);
            }

            connection_tid[i] = 0;
        }
    }

    return LC_BUS_OK;
}
Beispiel #6
0
void destroy()
{
    amqp_channel_close(conn, channel_id, AMQP_REPLY_SUCCESS);
    amqp_connection_close(conn, AMQP_REPLY_SUCCESS);
    amqp_destroy_connection(conn);
    printf("amqp close and destroy connection.\n");
}
Beispiel #7
0
/* 	php_amqp_disconnect
	handles disconnecting from amqp
	called by disconnect(), reconnect(), and d_tor
 */
void php_amqp_disconnect(amqp_connection_object *amqp_connection)
{
	void * old_handler;
	/*
	If we are trying to close the connection and the connection already closed, it will throw
	SIGPIPE, which is fine, so ignore all SIGPIPES
	*/

	/* Start ignoring SIGPIPE */
	old_handler = signal(SIGPIPE, SIG_IGN);

	if (amqp_connection->is_channel_connected == '\1') {
		amqp_channel_close(amqp_connection->conn, AMQP_CHANNEL, AMQP_REPLY_SUCCESS);
	}
        
	amqp_connection->is_channel_connected = '\0';

	if (amqp_connection->conn && amqp_connection->is_connected == '\1') {
                amqp_connection_close(amqp_connection->conn, AMQP_REPLY_SUCCESS);
		amqp_destroy_connection(amqp_connection->conn);
	}
	amqp_connection->is_connected = '\0';
	if (amqp_connection->fd) {
		close(amqp_connection->fd);
	}

	/* End ignoring of SIGPIPEs */
	signal(SIGPIPE, old_handler);
	
	return;

}
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;
}
Beispiel #9
0
static void
_amqp_connection_disconnect(AMQPDestDriver* self)
{
  amqp_channel_close(self->conn, 1, AMQP_REPLY_SUCCESS);
  amqp_connection_close(self->conn, AMQP_REPLY_SUCCESS);
  _amqp_connection_deinit(self);
}
Beispiel #10
0
/**
 * Close a session with the filter, this is the mechanism
 * by which a filter may cleanup data structure etc.
 * In the case of the MQ filter we simply close the connection to the server.
 *
 * @param instance	The filter instance data
 * @param session	The session being closed
 */
static	void 	
closeSession(FILTER *instance, void *session)
{
  MQ_SESSION	*my_session = (MQ_SESSION *)session;
  amqp_channel_close(my_session->conn,my_session->channel,AMQP_REPLY_SUCCESS);
  amqp_connection_close(my_session->conn,AMQP_REPLY_SUCCESS);
  amqp_destroy_connection(my_session->conn);
}
Beispiel #11
0
static void
afamqp_dd_disconnect(AMQPDestDriver *self)
{
  amqp_channel_close(self->conn, 1, AMQP_REPLY_SUCCESS);
  amqp_connection_close(self->conn, AMQP_REPLY_SUCCESS);
  amqp_destroy_connection(self->conn);
  self->conn = NULL;
}
int main(int argc, char const *const *argv)
{
  char const *hostname;
  int port, status;
  int rate_limit;
  int message_count;
  amqp_socket_t *socket;
  amqp_connection_state_t conn;

  if (argc < 5) {
    fprintf(stderr, "Usage: amqps_producer host port rate_limit message_count "
            "[cacert.pem [key.pem cert.pem]]\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_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 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");

  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;
}
Beispiel #13
0
void AMQPBase::closeChannel()
{
    if (opened) {
        amqp_channel_close(*cnn, channelNum, AMQP_REPLY_SUCCESS);
        amqp_rpc_reply_t res = amqp_get_rpc_reply(*cnn);
        THROW_AMQP_EXC_IF_FAILED(res, "close channel");
        opened = 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;
}
Beispiel #15
0
int main(int argc, char const * const *argv) {
  char const *hostname;
  int port;
  char const *exchange;
  char const *bindingkey;

  int sockfd;
  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();

  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, 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;
}
Beispiel #16
0
static
void shutdown_amqp_connection(amqp_connection_state_t connection, int amqp_rc, uint num_channels)
{
    // close amqp connection, including all open channels
    if (amqp_rc >= 0) {
        for (int i=1; i <= num_channels; i++)
            log_amqp_error(amqp_channel_close(connection, i, AMQP_REPLY_SUCCESS), "closing AMQP channel");
        log_amqp_error(amqp_connection_close(connection, AMQP_REPLY_SUCCESS), "closing AMQP connection");
    }
    log_error(amqp_destroy_connection(connection), "closing AMQP connection");
}
Beispiel #17
0
static void
closeAMQPConnection(instanceData *pData)
{
	if (pData->conn != NULL) {
		die_on_amqp_error(amqp_channel_close(pData->conn, 1, AMQP_REPLY_SUCCESS), "amqp_channel_close");
		die_on_amqp_error(amqp_connection_close(pData->conn, AMQP_REPLY_SUCCESS), "amqp_connection_close");
		die_on_error(amqp_destroy_connection(pData->conn), "amqp_destroy_connection");

		pData->conn = NULL;
	}
}
Beispiel #18
0
void close_connection(amqp_connection_state_t conn)
{
  int res;
  die_rpc(amqp_channel_close(conn, 1, AMQP_REPLY_SUCCESS),
          "closing channel");
  die_rpc(amqp_connection_close(conn, AMQP_REPLY_SUCCESS),
          "closing connection");

  res = amqp_destroy_connection(conn);
  die_amqp_error(res, "closing connection");
}
Beispiel #19
0
/*
 * Close a channel and wait for 5 seconds to receive a channel close OK event
 */
static wiced_result_t amqp_ch_close( wiced_amqp_connection_t *conn, uint16_t channel )
{
    if ( amqp_channel_close( channel, conn ) != WICED_SUCCESS )
    {
        return WICED_ERROR;
    }
    if ( amqp_wait_for( AMQP_EVENT_CHANNEL_RECV_CLOSE_OK, 5000 ) != WICED_SUCCESS )
    {
        return WICED_ERROR;
    }
    return WICED_SUCCESS;
}
Beispiel #20
0
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;
}
Beispiel #21
0
AMQP::~AMQP() {
	if (channels.size()) {
		vector<AMQPBase*>::iterator i;
		for (i=channels.begin(); i!=channels.end(); i++) {
			delete *i;
		}
	}

	amqp_channel_close(cnn, 1, AMQP_REPLY_SUCCESS);
	amqp_connection_close(cnn, AMQP_REPLY_SUCCESS);
	amqp_destroy_connection(cnn);
};
Beispiel #22
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;
}
Beispiel #23
0
/*
 * Functions
 */
static void camqp_close_connection (camqp_config_t *conf) /* {{{ */
{
    int sockfd;

    if ((conf == NULL) || (conf->connection == NULL))
        return;

    sockfd = amqp_get_sockfd (conf->connection);
    amqp_channel_close (conf->connection, CAMQP_CHANNEL, AMQP_REPLY_SUCCESS);
    amqp_connection_close (conf->connection, AMQP_REPLY_SUCCESS);
    amqp_destroy_connection (conf->connection);
    close (sockfd);
    conf->connection = NULL;
} /* }}} void camqp_close_connection */
void Channel::publish(const std::string& exchange,const std::string& routing_key,
		const std::string& mesg ){
	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 = 1; /* transient delivery mode */
	int x = amqp_basic_publish(this->conn_ptr->_getconn_n(),
		   								this->channel_n,
		                                amqp_cstring_bytes(exchange.c_str()),
		                                amqp_cstring_bytes(routing_key.c_str()),
		                                0,
		                                0,
										&props,
			                            amqp_cstring_bytes(mesg.c_str()));
	if(x<0){
		label_reconnet:
		//meaning error occur
		Log::log.warning("%s: %s\n", "publishing", amqp_error_string2(x));
		Log::log.warning("Channel::publish: Reconnecting to RabbitMQ\n");

		//first close all the things
		amqp_channel_close(this->conn_ptr->_getconn_n(), this->channel_n, AMQP_REPLY_SUCCESS);
		amqp_connection_close(this->conn_ptr->_getconn_n(), AMQP_REPLY_SUCCESS);
		amqp_destroy_connection(this->conn_ptr->_getconn_n());

		//reconnect to rabbitMQ server!
		this->conn_ptr->reconnnect();
		amqp_channel_open(this->conn_ptr->_getconn_n(), channel_n);
		if(amqp_get_rpc_reply(this->conn_ptr->_getconn_n()).reply_type !=AMQP_RESPONSE_NORMAL){
			Log::log.warning("Channel::publish:Opening channel_n error\n");
		}else{
			x = amqp_basic_publish(this->conn_ptr->_getconn_n(),
													this->channel_n,
													amqp_cstring_bytes(exchange.c_str()),
													amqp_cstring_bytes(routing_key.c_str()),
													0,
													0,
													&props,
													amqp_cstring_bytes(mesg.c_str()));
		}
		if(x<0){
			sleep(2);
			goto label_reconnet;
		}

	}

	//die_on_error(x, "Publishing");
}
Beispiel #25
0
void rmq_close(int destroy)
{
  if (ctx->channel) {
    amqp_channel_close(ctx->conn, ctx->channel, AMQP_REPLY_SUCCESS);
    ctx->channel = 0;
  }
  if (ctx->socket) {
    amqp_connection_close(ctx->conn, AMQP_REPLY_SUCCESS);
    ctx->socket = 0;
  }
  if (destroy) {
    amqp_destroy_connection(ctx->conn);
    ctx->conn = 0;
  }
}
Beispiel #26
0
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;
}
Beispiel #27
0
void rmq_destroy_param(rmq_params_t *rmqp)
{
	if (!rmqp)
		return;
	if (rmqp->conn && rmqp->flags & RMQ_PARAM_CONN) {
		if (rmqp->flags & RMQ_PARAM_CHAN) {
			rmq_error("closing channel",
					amqp_channel_close(rmqp->conn, rmqp->channel,
						AMQP_REPLY_SUCCESS));
		}
		rmq_error("closing connection",
				amqp_connection_close(rmqp->conn, AMQP_REPLY_SUCCESS));
		if (amqp_destroy_connection(rmqp->conn) < 0)
			LM_ERR("cannot destroy connection\n");
	}

	rmqp->flags &= ~(RMQ_PARAM_CONN|RMQ_PARAM_CHAN);
}
Beispiel #28
0
void janus_rabbitmq_destroy(void) {
	if(!g_atomic_int_get(&initialized))
		return;
	g_atomic_int_set(&stopping, 1);

	if(rmq_client) {
		rmq_client->destroy = 1;
		g_async_queue_push(rmq_client->messages, &exit_message);
		if(rmq_client->in_thread)
			g_thread_join(rmq_client->in_thread);
		if(rmq_client->out_thread)
			g_thread_join(rmq_client->out_thread);
		if(rmq_client->rmq_conn && rmq_client->rmq_channel) {
			amqp_channel_close(rmq_client->rmq_conn, rmq_client->rmq_channel, AMQP_REPLY_SUCCESS);
			amqp_connection_close(rmq_client->rmq_conn, AMQP_REPLY_SUCCESS);
			amqp_destroy_connection(rmq_client->rmq_conn);
		}
	}
	g_free(rmq_client);
	janus_transport_session_destroy(rmq_session);

	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);

	g_atomic_int_set(&initialized, 0);
	g_atomic_int_set(&stopping, 0);
	JANUS_LOG(LOG_INFO, "%s destroyed!\n", JANUS_RABBITMQ_NAME);
}
Beispiel #29
0
int releaseMsgClient(MsgReceiver_t *receiver) {
    if(receiver == NULL) {
        LOG4CXX_ERROR(logger, "releaseMsgClient: receiver is null");
        return EINVAL;
    }
    
    if(receiver->thread_run) {
        // stop thread
        LOG4CXX_DEBUG(logger, "releaseMsgClient: canceling an event receiver thread");
        receiver->thread_run = false;
        
        pthread_cancel(receiver->thread);
        pthread_join(receiver->thread, NULL);
    }
    
    amqp_channel_close(receiver->conn_state, receiver->channel, AMQP_REPLY_SUCCESS);
    amqp_connection_close(receiver->conn_state, AMQP_REPLY_SUCCESS);
    amqp_destroy_connection(receiver->conn_state);
    
    free(receiver);
    LOG4CXX_DEBUG(logger, "releaseMsgClient: closed connection");
    return 0;
}
Beispiel #30
0
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;
}