Ejemplo n.º 1
0
static int noit_rabbimq_connect(iep_thread_driver_t *dr) {
  struct amqp_driver *driver = (struct amqp_driver *)dr;

  if(!driver->connection) {
    int sidx = driver->nconnects++ % driver->nhosts;
    struct timeval timeout;
    amqp_rpc_reply_t r, *rptr;

    mtevL(mtev_error, "AMQP connect: %s:%d\n",
          driver->hostname[sidx], driver->port);
    BUMPSTAT(connects);
    driver->hostidx = sidx;
    timeout.tv_sec = driver->heartbeat;
    timeout.tv_usec = 0;
    driver->sockfd = amqp_open_socket(driver->hostname[sidx], driver->port, &timeout);
    if(driver->sockfd < 0) {
      mtevL(mtev_error, "AMQP connect failed: %s:%d\n",
            driver->hostname[sidx], driver->port);
      return -1;
    }
    if(setsockopt(driver->sockfd, SOL_SOCKET, SO_SNDBUF, &desired_sndbuf, sizeof(desired_sndbuf)) < 0)
      mtevL(mtev_debug, "rabbitmq: setsockopt(SO_SNDBUF, %ld) -> %s\n", (long int)desired_sndbuf, strerror(errno));
    if(setsockopt(driver->sockfd, SOL_SOCKET, SO_RCVBUF, &desired_rcvbuf, sizeof(desired_rcvbuf)) < 0)
      mtevL(mtev_debug, "rabbitmq: setsockopt(SO_RCVBUF, %ld) -> %s\n", (long int)desired_rcvbuf, strerror(errno));
    driver->has_error = 0;
    driver->connection = amqp_new_connection();
    amqp_set_basic_return_cb(driver->connection, noit_rabbitmq_brcb, driver);
    amqp_set_sockfd(driver->connection, driver->sockfd);
    r = amqp_login(driver->connection,
                   driver->vhost, 0, 131072, driver->heartbeat,
                   AMQP_SASL_METHOD_PLAIN,
                   driver->username, driver->password);
    if(r.reply_type != AMQP_RESPONSE_NORMAL) {
      mtevL(mtev_error, "AMQP login failed\n");
      amqp_connection_close(driver->connection, AMQP_REPLY_SUCCESS);
      amqp_destroy_connection(driver->connection);
      if(driver->sockfd >= 0) close(driver->sockfd);
      driver->sockfd = -1;
      driver->connection = NULL;
      return -1;
    }

    amqp_channel_open(driver->connection, 1);
    rptr = amqp_get_rpc_reply();
    if(rptr->reply_type != AMQP_RESPONSE_NORMAL) {
      mtevL(mtev_error, "AMQP channe_open failed\n");
      amqp_connection_close(driver->connection, AMQP_REPLY_SUCCESS);
      amqp_destroy_connection(driver->connection);
      if(driver->sockfd >= 0) close(driver->sockfd);
      driver->sockfd = -1;
      driver->connection = NULL;
      return -1;
    }
    mtev_gettimeofday(&driver->last_hb, NULL);
    return 0;
  }
  /* 1 means already connected */
  return 1;
}
Ejemplo n.º 2
0
static int
noit_rabbimq_submit(iep_thread_driver_t *dr,
                    const char *payload, size_t payloadlen) {
  int rv;
  amqp_bytes_t body;
  struct amqp_driver *driver = (struct amqp_driver *)dr;
  const char *routingkey = driver->routingkey;

  body.len = payloadlen;
  body.bytes = (char *)payload;
  if(*payload == 'M' ||
     *payload == 'S' ||
     *payload == 'C' ||
     (*payload == 'H' && payload[1] == '1') ||
     (*payload == 'F' && payload[1] == '1') ||
     (*payload == 'B' && (payload[1] == '1' || payload[1] == '2'))) {
    char uuid_str[32 * 2 + 1];
    int account_id, check_id;
    if(extract_uuid_from_jlog(payload, payloadlen,
                              &account_id, &check_id, uuid_str)) {
      if(*routingkey) {
        char *replace;
        int newlen = strlen(driver->routingkey) + 1 + sizeof(uuid_str) + 2 * 32;
        replace = alloca(newlen);
        snprintf(replace, newlen, "%s.%x.%x.%d.%d%s", driver->routingkey,
                 account_id%16, (account_id/16)%16, account_id,
                 check_id, uuid_str);
        routingkey = replace;
      }
    }
  }
  rv = amqp_basic_publish(driver->connection, 1,
                          amqp_cstring_bytes(driver->exchange),
                          amqp_cstring_bytes(routingkey),
                          1, 0, NULL, body);
  if(rv < 0) {
    mtevL(mtev_error, "AMQP publish failed, disconnecting\n");
    amqp_connection_close(driver->connection, AMQP_REPLY_SUCCESS);
    amqp_destroy_connection(driver->connection);
    if(driver->sockfd >= 0) close(driver->sockfd);
    driver->sockfd = -1;
    driver->connection = NULL;
    return -1;
  }
  BUMPSTAT(publications);
  noit_rabbitmq_heartbeat(driver);
  noit_rabbitmq_read_frame(driver);
  amqp_maybe_release_buffers(driver->connection);
  if(driver->has_error) {
    amqp_connection_close(driver->connection, AMQP_REPLY_SUCCESS);
    amqp_destroy_connection(driver->connection);
    if(driver->sockfd >= 0) close(driver->sockfd);
    driver->sockfd = -1;
    driver->connection = NULL;
    return -1;
  }
  return 0;
}
Ejemplo n.º 3
0
static int noit_rabbimq_connect(iep_thread_driver_t *dr) {
  struct amqp_driver *driver = (struct amqp_driver *)dr;

  if(!driver->connection) {
    int sidx = driver->nconnects++ % driver->nhosts;
    struct timeval timeout;
    amqp_rpc_reply_t r, *rptr;

    noitL(noit_error, "AMQP connect: %s:%d\n",
          driver->hostname[sidx], driver->port);
    BUMPSTAT(connects);
    driver->hostidx = sidx;
    timeout.tv_sec = driver->heartbeat;
    timeout.tv_usec = 0;
    driver->sockfd = amqp_open_socket(driver->hostname[sidx], driver->port, &timeout);
    if(driver->sockfd < 0) {
      noitL(noit_error, "AMQP connect failed: %s:%d\n",
            driver->hostname[sidx], driver->port);
      return -1;
    }
    driver->has_error = 0;
    driver->connection = amqp_new_connection();
    amqp_set_basic_return_cb(driver->connection, noit_rabbitmq_brcb, driver);
    amqp_set_sockfd(driver->connection, driver->sockfd);
    r = amqp_login(driver->connection,
                   driver->vhost, 0, 131072, driver->heartbeat,
                   AMQP_SASL_METHOD_PLAIN,
                   driver->username, driver->password);
    if(r.reply_type != AMQP_RESPONSE_NORMAL) {
      noitL(noit_error, "AMQP login failed\n");
      amqp_connection_close(driver->connection, AMQP_REPLY_SUCCESS);
      amqp_destroy_connection(driver->connection);
      driver->connection = NULL;
      return -1;
    }

    amqp_channel_open(driver->connection, 1);
    rptr = amqp_get_rpc_reply();
    if(rptr->reply_type != AMQP_RESPONSE_NORMAL) {
      noitL(noit_error, "AMQP channe_open failed\n");
      amqp_connection_close(driver->connection, AMQP_REPLY_SUCCESS);
      amqp_destroy_connection(driver->connection);
      driver->connection = NULL;
      return -1;
    }
    gettimeofday(&driver->last_hb, NULL);
    return 0;
  }
  /* 1 means already connected */
  return 1;
}
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;
}
Ejemplo n.º 5
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");
}
Ejemplo n.º 6
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;

}
Ejemplo n.º 7
0
void close_and_destroy_connection(amqp_connection_state_t connection_state_) {
  amqp_rpc_reply_t rpc_reply = amqp_connection_close(connection_state_, AMQP_REPLY_SUCCESS);
  assert(rpc_reply.reply_type == AMQP_RESPONSE_NORMAL);

  int rc = amqp_destroy_connection(connection_state_);
  assert(rc == AMQP_STATUS_OK);
}
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;
}
Ejemplo n.º 9
0
void AMQP::login() {
	amqp_rpc_reply_t res = amqp_login(cnn, vhost.c_str(), 0, FRAME_MAX, 0, AMQP_SASL_METHOD_PLAIN, user.c_str(), password.c_str());
	if ( res.reply_type != AMQP_RESPONSE_NORMAL) {
		amqp_destroy_connection(cnn);
		throw AMQPException(&res);
	}
}
Ejemplo n.º 10
0
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
    {
      int sock = amqp_open_socket(host.c_str(), port);
      m_impl->CheckForError(sock);

      amqp_set_sockfd(m_impl->m_connection, sock);

      m_impl->CheckRpcReply(0, amqp_login(m_impl->m_connection, vhost.c_str(), 0,
            frame_max, BROKER_HEARTBEAT, AMQP_SASL_METHOD_PLAIN,
            username.c_str(), password.c_str()));
    }
    catch (...)
    {
      amqp_destroy_connection(m_impl->m_connection);
      throw;
    }

    m_impl->SetIsConnected(true);
}
Ejemplo n.º 11
0
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);
}
Ejemplo n.º 12
0
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;
}
Ejemplo n.º 13
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);
}
Ejemplo n.º 14
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;
}
Ejemplo n.º 15
0
int amqp_tune_connection(amqp_connection_state_t state,
			 int channel_max,
			 int frame_max,
			 int heartbeat)
{
  void *newbuf;

  ENFORCE_STATE(state, CONNECTION_STATE_IDLE);

  state->channel_max = channel_max;
  state->frame_max = frame_max;
  state->heartbeat = heartbeat;

  empty_amqp_pool(&state->frame_pool);
  init_amqp_pool(&state->frame_pool, frame_max);

  state->inbound_buffer.len = frame_max;
  state->outbound_buffer.len = frame_max;
  newbuf = realloc(state->outbound_buffer.bytes, frame_max);
  if (newbuf == NULL) {
    amqp_destroy_connection(state);
    return -ENOMEM;
  }
  state->outbound_buffer.bytes = newbuf;

  return 0;
}
Ejemplo n.º 16
0
Channel::Channel(const std::string &host,
                 int port,
                 const std::string &username,
                 const std::string &password,
                 const std::string &vhost,
                 int frame_max,
                 const std::string &path_to_ca_cert,
                 const std::string &path_to_client_key,
                 const std::string &path_to_client_cert) :
    m_impl(new Detail::ChannelImpl)
{
    m_impl->m_connection = amqp_new_connection();
    if (NULL == m_impl->m_connection)
    {
        throw std::bad_alloc();
    }

    amqp_socket_t *socket = amqp_ssl_socket_new(m_impl->m_connection);
    if (NULL == socket)
    {
        throw std::bad_alloc();
    }

    try
    {
        int status = amqp_ssl_socket_set_cacert(socket, path_to_ca_cert.c_str());
        if (status)
        {
            throw std::runtime_error("Error in setting CA certificate for socket");
        }

        if (path_to_client_key != ""
                && path_to_client_cert != "")
        {
            status = amqp_ssl_socket_set_key(socket,
                                             path_to_client_cert.c_str(),
                                             path_to_client_key.c_str());
            if (status)
            {
                throw std::runtime_error("Error in setting client certificate for socket");
            }
        }

        status = amqp_socket_open(socket, host.c_str(), port);
        if (status)
        {
            throw std::runtime_error("Error in opening SSL/TLS connection for socket");
        }

        m_impl->DoLogin(username, password, vhost, frame_max);
    }
    catch (...)
    {
        amqp_destroy_connection(m_impl->m_connection);
        throw;
    }

    m_impl->SetIsConnected(true);
}
Ejemplo n.º 17
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;
}
Ejemplo n.º 18
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;
}
Ejemplo n.º 19
0
eSquere *   eSquere::DestroyConnection(){
    int x=amqp_destroy_connection(this->connect);
    if(x<0){
        this->ErrorStatus=true;
        this->ErrorMessage=amqp_error_string2(x);
    }
    return this;
}
Ejemplo n.º 20
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);
}
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;
}
Ejemplo n.º 22
0
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");
	}
}
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;
}
Ejemplo n.º 24
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;
}
Ejemplo n.º 25
0
static int noit_rabbimq_disconnect(iep_thread_driver_t *d) {
  struct amqp_driver *dr = (struct amqp_driver *)d;
  if(dr->connection) {
    amqp_destroy_connection(dr->connection);
    dr->sockfd = -1;
    dr->connection = NULL;
    return 0;
  }
  return -1;
}
Ejemplo n.º 26
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;
	}
}
Ejemplo n.º 27
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");
}
Ejemplo n.º 28
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");
}
Ejemplo n.º 29
0
AMQP::~AMQP() {
	if (channels.size()) {
		vector<AMQPBase*>::iterator i;
		for (i=channels.begin(); i!=channels.end(); i++) {
			delete *i;
		}
	}

	amqp_destroy_connection(cnn);
	close(sockfd);
};
Ejemplo n.º 30
0
static void
local_amqp_disconnect_bs(struct brokerstate *bs) {
  if(bs && bs->conn) {
    int errorstate = bs->inerror;
    amqp_connection_close(bs->conn, AMQP_REPLY_SUCCESS);
    if(bs->sockfd >= 0) close(bs->sockfd);
    amqp_destroy_connection(bs->conn);
    memset(bs, 0, sizeof(*bs));
    bs->inerror = errorstate;
  }
}