drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
rabbitmq_handler.cc
1/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3 *
4 * Copyright (C) 2010 Marcus Eriksson
5 *
6 * Authors:
7 *
8 * Marcus Eriksson <[email protected]>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25
26#include <config.h>
27
28#include <pthread.h>
29#include <unistd.h>
30
31#include <drizzled/gettext.h>
32
33#include "rabbitmq_handler.h"
34
35using namespace std;
36
37namespace drizzle_plugin {
38namespace rabbitmq {
39
40extern bool sysvar_logging_enable;
41
42RabbitMQHandler::RabbitMQHandler(const std::string &rabbitMQHost,
43 const in_port_t rabbitMQPort,
44 const std::string &rabbitMQUsername,
45 const std::string &rabbitMQPassword,
46 const std::string &rabbitMQVirtualhost,
47 const std::string &rabbitMQExchange,
48 const std::string &rabbitMQRoutingKey) :
49 rabbitmqConnection(amqp_new_connection()),
50 hostname(rabbitMQHost),
51 port(rabbitMQPort),
52 username(rabbitMQUsername),
53 password(rabbitMQPassword),
54 virtualhost(rabbitMQVirtualhost),
55 exchange(rabbitMQExchange),
56 routingKey(rabbitMQRoutingKey),
57 rabbitmq_connection_established(false)
58{
59 pthread_mutex_init(&publishLock, NULL);
60 connect();
61}
62
63RabbitMQHandler::~RabbitMQHandler()
64{
65 pthread_mutex_destroy(&publishLock);
66 disconnect();
67}
68
69void RabbitMQHandler::publish(void *message,
70 const int length)
71{
72 // return if query logging is not enabled
73 //if (sysvar_logging_enable == false)
74 // return;
75
76 pthread_mutex_lock(&publishLock);
77 amqp_bytes_t b;
78 b.bytes= message;
79 b.len= length;
80
81 if (amqp_basic_publish(rabbitmqConnection,
82 1,
83 amqp_cstring_bytes(exchange.c_str()),
84 amqp_cstring_bytes(routingKey.c_str()),
85 0,
86 0,
87 NULL,
88 b) < 0)
89 {
90 pthread_mutex_unlock(&publishLock);
91 throw rabbitmq_handler_exception("Could not publish message");
92 }
93 pthread_mutex_unlock(&publishLock);
94
95}
96
97void RabbitMQHandler::reconnect()
98{
99 disconnect();
100 connect();
101}
102
103void RabbitMQHandler::disconnect()
104{
105 try
106 {
107 handleAMQPError(amqp_channel_close(rabbitmqConnection,
108 1,
109 AMQP_REPLY_SUCCESS),
110 "close channel");
111 handleAMQPError(amqp_connection_close(rabbitmqConnection,
112 AMQP_REPLY_SUCCESS),
113 "close connection");
114 amqp_destroy_connection(rabbitmqConnection);
115 }
116 catch(exception& e) {} // do not throw in destructorn
117}
118
119void RabbitMQHandler::connect() {
120 amqp_socket_t *socket= amqp_tcp_socket_new(rabbitmqConnection);
121 if (socket == NULL)
122 {
123 rabbitmq_connection_established= false;
124 return;
125 }
126 if (amqp_socket_open(socket, hostname.c_str(), port) != AMQP_STATUS_OK)
127 {
128 rabbitmq_connection_established= false;
129 return;
130 }
131 try
132 {
133 /* login to rabbitmq, handleAMQPError throws exception if there is a problem */
134 handleAMQPError(amqp_login(rabbitmqConnection,
135 virtualhost.c_str(),
136 0,
137 131072,
138 0,
139 AMQP_SASL_METHOD_PLAIN,
140 username.c_str(),
141 password.c_str()),
142 "rabbitmq login");
143 /* open the channel */
144 amqp_channel_open(rabbitmqConnection, 1);
145 handleAMQPError(amqp_get_rpc_reply(rabbitmqConnection), "RPC Reply");
146 amqp_table_t empty_table = { 0, NULL }; // for users of old librabbitmq users - amqp_empty_table did not exist
147 amqp_exchange_declare(rabbitmqConnection, 1, amqp_cstring_bytes(exchange.c_str()), amqp_cstring_bytes("fanout"), 0, 0, 0, 0, empty_table);
148 handleAMQPError(amqp_get_rpc_reply(rabbitmqConnection), "RPC Reply");
149 rabbitmq_connection_established= true;
150 }
151 catch(exception& e)
152 {
153 rabbitmq_connection_established= false;
154 }
155}
156
157void RabbitMQHandler::handleAMQPError(amqp_rpc_reply_t x, string context)
158{
159 string errorMessage("");
160 switch (x.reply_type) {
161 case AMQP_RESPONSE_NORMAL:
162 break;
163 case AMQP_RESPONSE_NONE:
164 errorMessage.assign("No response in ");
165 errorMessage.append(context);
166 throw rabbitmq_handler_exception(errorMessage);
167 case AMQP_RESPONSE_LIBRARY_EXCEPTION:
168 case AMQP_RESPONSE_SERVER_EXCEPTION:
169 switch (x.reply.id) {
170 case AMQP_CONNECTION_CLOSE_METHOD:
171 errorMessage.assign("Connection closed in ");
172 errorMessage.append(context);
173 throw rabbitmq_handler_exception(errorMessage);
174 case AMQP_CHANNEL_CLOSE_METHOD:
175 errorMessage.assign("Channel closed in ");
176 errorMessage.append(context);
177 throw rabbitmq_handler_exception(errorMessage);
178 default:
179 errorMessage.assign("Unknown error in ");
180 errorMessage.append(context);
181 throw rabbitmq_handler_exception(errorMessage);
182 }
183 }
184}
185
186} /* namespace rabbitmq */
187} /* namespace drizzle_plugin */
RabbitMQHandler(const std::string &hostname, const in_port_t port, const std::string &username, const std::string &password, const std::string &virtualhost, const std::string &exchange, const std::string &routingKey)
Constructs a new RabbitMQHandler, purpose is to hide away the error handling, reconnections etc.
void handleAMQPError(amqp_rpc_reply_t x, std::string context)
Handles errors produced by librabbitmq.
void publish(void *message, const int length)
Publishes the message to the server.
STL namespace.