1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2010 Marcus Eriksson
8
* Marcus Eriksson <krummas@gmail.com>
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.
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.
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
26
#include "rabbitmq_log.h"
27
#include <drizzled/message/transaction.pb.h>
28
#include <google/protobuf/io/coded_stream.h>
30
#include <drizzled/plugin/registry.h>
31
#include <drizzled/plugin.h>
33
#include "rabbitmq_handler.h"
36
using namespace drizzled;
37
using namespace google;
40
* The hostname to connect to
42
static char* sysvar_rabbitmq_host= NULL;
47
static int sysvar_rabbitmq_port= 0;
52
static char* sysvar_rabbitmq_username= NULL;
57
static char* sysvar_rabbitmq_password= NULL;
60
* rabbitmq virtualhost
62
static char* sysvar_rabbitmq_virtualhost= NULL;
65
* rabbitmq exchangename
67
static char* sysvar_rabbitmq_exchange= NULL;
70
* rabbitmq routing key
72
static char* sysvar_rabbitmq_routingkey= NULL;
75
* Is the rabbitmq log enabled?
77
static bool sysvar_rabbitmq_log_enabled= false;
80
RabbitMQLog::RabbitMQLog(const string name_arg,
81
RabbitMQHandler* mqHandler)
82
:plugin::TransactionApplier(name_arg)
84
rabbitMQHandler= mqHandler;
87
RabbitMQLog::~RabbitMQLog()
91
void RabbitMQLog::apply(const message::Transaction &to_apply)
93
size_t message_byte_length= to_apply.ByteSize();
94
uint8_t* buffer= static_cast<uint8_t *>(malloc(message_byte_length));
97
errmsg_printf(ERRMSG_LVL_ERROR, _("Failed to allocate enough memory to transaction message\n"));
102
to_apply.SerializeWithCachedSizesToArray(buffer);
105
rabbitMQHandler->publish(buffer,
106
int(message_byte_length),
107
sysvar_rabbitmq_exchange,
108
sysvar_rabbitmq_routingkey);
112
errmsg_printf(ERRMSG_LVL_ERROR, _(e.what()));
118
RabbitMQLog *rabbitmqLogger; ///< the actual plugin
119
RabbitMQHandler* rabbitmqHandler; ///< the rabbitmq handler
122
* Initialize the rabbitmq logger - instanciates the dependencies (the handler)
123
* and creates the log handler with the dependency - makes it easier to swap out
124
* handler implementation
126
static int init(plugin::Registry ®istry)
128
if(sysvar_rabbitmq_log_enabled)
132
rabbitmqHandler= new RabbitMQHandler(sysvar_rabbitmq_host,
133
sysvar_rabbitmq_port,
134
sysvar_rabbitmq_username,
135
sysvar_rabbitmq_password,
136
sysvar_rabbitmq_virtualhost);
140
errmsg_printf(ERRMSG_LVL_ERROR, _("Failed to allocate the RabbitMQHandler. Got error: %s\n"),
146
rabbitmqLogger= new RabbitMQLog("rabbit-log", rabbitmqHandler);
150
errmsg_printf(ERRMSG_LVL_ERROR, _("Failed to allocate the RabbitMQLog instance. Got error: %s\n"),
155
registry.add(rabbitmqLogger);
161
static int deinit(plugin::Registry ®istry)
163
/* Cleanup the logger itself - delete the logger first, then handler, to avoid NPEs */
166
registry.remove(rabbitmqLogger);
167
delete rabbitmqLogger;
168
delete rabbitmqHandler;
176
static DRIZZLE_SYSVAR_BOOL(enable,
177
sysvar_rabbitmq_log_enabled,
179
N_("Enable rabbitmq log"),
180
NULL, /* check func */
181
NULL, /* update func */
182
false /* default */);
185
static DRIZZLE_SYSVAR_STR(hostname,
186
sysvar_rabbitmq_host,
188
N_("Host name to connect to"),
189
NULL, /* check func */
190
NULL, /* update func*/
191
"localhost" /* default */);
194
static DRIZZLE_SYSVAR_INT(port,
195
sysvar_rabbitmq_port,
198
NULL, /* check func */
199
NULL, /* update func */
205
static DRIZZLE_SYSVAR_STR(username,
206
sysvar_rabbitmq_username,
208
N_("RabbitMQ username"),
209
NULL, /* check func */
210
NULL, /* update func*/
211
"guest" /* default */);
213
static DRIZZLE_SYSVAR_STR(password,
214
sysvar_rabbitmq_password,
216
N_("RabbitMQ password"),
217
NULL, /* check func */
218
NULL, /* update func*/
219
"guest" /* default */);
221
static DRIZZLE_SYSVAR_STR(virtualhost,
222
sysvar_rabbitmq_virtualhost,
224
N_("RabbitMQ virtualhost"),
225
NULL, /* check func */
226
NULL, /* update func*/
229
static DRIZZLE_SYSVAR_STR(exchange,
230
sysvar_rabbitmq_exchange,
232
N_("Name of RabbitMQ exchange to publish to"),
233
NULL, /* check func */
234
NULL, /* update func*/
235
"ReplicationExchange" /* default */);
237
static DRIZZLE_SYSVAR_STR(routingkey,
238
sysvar_rabbitmq_routingkey,
240
N_("Name of RabbitMQ routing key to use"),
241
NULL, /* check func */
242
NULL, /* update func*/
243
"ReplicationRoutingKey" /* default */);
246
static drizzle_sys_var* system_variables[]= {
247
DRIZZLE_SYSVAR(enable),
248
DRIZZLE_SYSVAR(hostname),
249
DRIZZLE_SYSVAR(port),
250
DRIZZLE_SYSVAR(username),
251
DRIZZLE_SYSVAR(password),
252
DRIZZLE_SYSVAR(virtualhost),
253
DRIZZLE_SYSVAR(exchange),
254
DRIZZLE_SYSVAR(routingkey),
258
DRIZZLE_PLUGIN(init, deinit, NULL, system_variables);