~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/rabbitmq/rabbitmq_handler.cc

  • Committer: Monty Taylor
  • Date: 2011-02-13 17:26:39 UTC
  • mfrom: (2157.2.2 give-in-to-pkg-config)
  • mto: This revision was merged to the branch mainline in revision 2166.
  • Revision ID: mordred@inaugust.com-20110213172639-nhy7i72sfhoq13ms
Merged in pkg-config fixes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
 */
24
24
 
25
25
#include "config.h"
 
26
 
 
27
#include <drizzled/gettext.h>
 
28
 
26
29
#include "rabbitmq_handler.h"
27
30
 
28
31
using namespace std;
29
32
 
30
 
RabbitMQHandler::RabbitMQHandler(const char* rabbitMQHost, 
31
 
                                 const int rabbitMQPort, 
32
 
                                 const char* rabbitMQUsername, 
33
 
                                 const char* rabbitMQPassword, 
34
 
                                 const char* rabbitMQVirtualhost) 
35
 
  throw(rabbitmq_handler_exception):
36
 
  hostname(rabbitMQHost),
37
 
  port(rabbitMQPort),
38
 
  username(rabbitMQUsername),
39
 
  password(rabbitMQPassword),
40
 
  virtualhost(rabbitMQVirtualhost)
41
 
{
42
 
  rabbitmqConnection = amqp_new_connection();
 
33
namespace drizzle_plugin
 
34
{
 
35
 
 
36
RabbitMQHandler::RabbitMQHandler(const std::string &rabbitMQHost, 
 
37
                                 const in_port_t rabbitMQPort, 
 
38
                                 const std::string &rabbitMQUsername, 
 
39
                                 const std::string &rabbitMQPassword, 
 
40
                                 const std::string &rabbitMQVirtualhost) 
 
41
  throw(rabbitmq_handler_exception) :
 
42
    rabbitmqConnection(amqp_new_connection()),
 
43
    sockfd(amqp_open_socket(rabbitMQHost.c_str(), rabbitMQPort)),
 
44
    hostname(rabbitMQHost),
 
45
    port(rabbitMQPort),
 
46
    username(rabbitMQUsername),
 
47
    password(rabbitMQPassword),
 
48
    virtualhost(rabbitMQVirtualhost)
 
49
{
43
50
  /* open the socket to the rabbitmq server */
44
 
  sockfd = amqp_open_socket(hostname, port);
45
51
  if(sockfd < 0) 
46
52
  {
47
 
    throw rabbitmq_handler_exception("Could not open socket, is rabbitmq running?");
 
53
    throw rabbitmq_handler_exception(_("Could not open socket, is rabbitmq running?"));
48
54
  }
49
55
  amqp_set_sockfd(rabbitmqConnection, sockfd);
50
56
  /* login to rabbitmq, handleAMQPError throws exception if there is a problem */
51
57
  handleAMQPError(amqp_login(rabbitmqConnection, 
52
 
                             virtualhost, 
53
 
                             0, 
54
 
                             131072, 
55
 
                             0, 
56
 
                             AMQP_SASL_METHOD_PLAIN, 
57
 
                             username, 
58
 
                             password), 
59
 
                  "rabbitmq login");
 
58
                             virtualhost.c_str(), 
 
59
                             0, 
 
60
                             131072, 
 
61
                             0, 
 
62
                             AMQP_SASL_METHOD_PLAIN, 
 
63
                             username.c_str(), 
 
64
                             password.c_str()), 
 
65
                  "rabbitmq login");
60
66
  /* open the channel */
61
67
  amqp_channel_open(rabbitmqConnection, 1);
62
68
}
79
85
  close(sockfd);
80
86
}
81
87
 
82
 
void RabbitMQHandler::publish(const uint8_t *message, 
83
 
                              const int length, 
84
 
                              const char* exchangeName, 
85
 
                              const char* routingKey) throw(rabbitmq_handler_exception)
 
88
void RabbitMQHandler::publish(void *message, 
 
89
                              const int length, 
 
90
                              const std::string &exchangeName, 
 
91
                              const std::string &routingKey)
 
92
throw(rabbitmq_handler_exception)
86
93
{
87
94
  amqp_bytes_t b;
88
 
  b.bytes= (void*)message;
 
95
  b.bytes= message;
89
96
  b.len= length;
90
97
  
91
 
  if(amqp_basic_publish(rabbitmqConnection,
92
 
                        1,
93
 
                        amqp_cstring_bytes(exchangeName),
94
 
                        amqp_cstring_bytes(routingKey),
95
 
                        0,
96
 
                        0,
97
 
                        NULL,
98
 
                        b) < 0)
 
98
  if (amqp_basic_publish(rabbitmqConnection,
 
99
                         1,
 
100
                         amqp_cstring_bytes(exchangeName.c_str()),
 
101
                         amqp_cstring_bytes(routingKey.c_str()),
 
102
                         0,
 
103
                         0,
 
104
                         NULL,
 
105
                         b) < 0)
99
106
  {
100
107
    throw rabbitmq_handler_exception("Could not publish message");
101
108
  }
130
137
    }
131
138
  }
132
139
}
 
140
 
 
141
} /* namespace drizzle_plugin */