Пример #1
0
static gboolean
afamqp_dd_connect(AMQPDestDriver *self, gboolean reconnect)
{
    int sockfd;
    amqp_rpc_reply_t ret;

    if (reconnect && self->conn)
    {
        ret = amqp_get_rpc_reply(self->conn);
        if (ret.reply_type == AMQP_RESPONSE_NORMAL)
            return TRUE;
    }

    self->conn = amqp_new_connection();
    sockfd = amqp_open_socket(self->host, self->port);
    if (sockfd < 0)
    {
        gchar *errstr = amqp_error_string(-sockfd);
        msg_error("Error connecting to AMQP server",
                  evt_tag_str("driver", self->super.super.super.id),
                  evt_tag_str("error", errstr),
                  evt_tag_int("time_reopen", self->super.time_reopen),
                  NULL);
        g_free(errstr);
        return FALSE;
    }
    amqp_set_sockfd(self->conn, sockfd);

    ret = amqp_login(self->conn, self->vhost, 0, 131072, 0,
                     AMQP_SASL_METHOD_PLAIN, self->user, self->password);
    if (!afamqp_is_ok(self, "Error during AMQP login", ret))
        return FALSE;

    amqp_channel_open(self->conn, 1);
    ret = amqp_get_rpc_reply(self->conn);
    if (!afamqp_is_ok(self, "Error during AMQP channel open", ret))
        return FALSE;

    if (self->declare)
    {
        amqp_exchange_declare(self->conn, 1, amqp_cstring_bytes(self->exchange),
                              amqp_cstring_bytes(self->exchange_type), 0, 0,
                              amqp_empty_table);
        ret = amqp_get_rpc_reply(self->conn);
        if (!afamqp_is_ok(self, "Error during AMQP exchange declaration", ret))
            return FALSE;
    }

    msg_debug ("Connecting to AMQP succeeded",
               evt_tag_str("driver", self->super.super.super.id),
               NULL);

    return TRUE;
}
Пример #2
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;
}