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/module/registry.h>
31
#include <drizzled/plugin.h>
33
#include "rabbitmq_handler.h"
34
#include <boost/program_options.hpp>
35
#include <drizzled/module/option_map.h>
37
namespace po= boost::program_options;
40
using namespace drizzled;
41
using namespace google;
43
namespace drizzle_plugin
49
static port_constraint sysvar_rabbitmq_port;
52
RabbitMQLog::RabbitMQLog(const string &name,
53
const std::string &exchange,
54
const std::string &routingkey,
55
RabbitMQHandler* mqHandler) :
56
plugin::TransactionApplier(name),
57
_rabbitMQHandler(mqHandler),
59
_routingkey(routingkey)
62
RabbitMQLog::~RabbitMQLog()
65
plugin::ReplicationReturnCode
66
RabbitMQLog::apply(Session &, const message::Transaction &to_apply)
68
size_t message_byte_length= to_apply.ByteSize();
69
uint8_t* buffer= new uint8_t[message_byte_length];
72
errmsg_printf(error::ERROR, _("Failed to allocate enough memory to transaction message\n"));
74
return plugin::UNKNOWN_ERROR;
77
to_apply.SerializeWithCachedSizesToArray(buffer);
80
_rabbitMQHandler->publish(buffer,
81
int(message_byte_length),
87
errmsg_printf(error::ERROR, _(e.what()));
89
return plugin::UNKNOWN_ERROR;
92
return plugin::SUCCESS;
95
static RabbitMQLog *rabbitmqLogger; ///< the actual plugin
96
static RabbitMQHandler* rabbitmqHandler; ///< the rabbitmq handler
99
* Initialize the rabbitmq logger - instanciates the dependencies (the handler)
100
* and creates the log handler with the dependency - makes it easier to swap out
101
* handler implementation
103
static int init(drizzled::module::Context &context)
105
const module::option_map &vm= context.getOptions();
109
rabbitmqHandler= new RabbitMQHandler(vm["host"].as<string>(),
110
sysvar_rabbitmq_port,
111
vm["username"].as<string>(),
112
vm["password"].as<string>(),
113
vm["virtualhost"].as<string>());
117
errmsg_printf(error::ERROR, _("Failed to allocate the RabbitMQHandler. Got error: %s\n"),
123
rabbitmqLogger= new RabbitMQLog("rabbit_log_applier",
124
vm["exchange"].as<string>(),
125
vm["routingkey"].as<string>(),
130
errmsg_printf(error::ERROR, _("Failed to allocate the RabbitMQLog instance. Got error: %s\n"),
135
context.add(rabbitmqLogger);
136
ReplicationServices &replication_services= ReplicationServices::singleton();
137
replication_services.attachApplier(rabbitmqLogger, vm["use-replicator"].as<string>());
139
context.registerVariable(new sys_var_const_string_val("host", vm["host"].as<string>()));
140
context.registerVariable(new sys_var_constrained_value_readonly<in_port_t>("port", sysvar_rabbitmq_port));
141
context.registerVariable(new sys_var_const_string_val("username", vm["username"].as<string>()));
142
context.registerVariable(new sys_var_const_string_val("password", vm["password"].as<string>()));
143
context.registerVariable(new sys_var_const_string_val("virtualhost", vm["virtualhost"].as<string>()));
144
context.registerVariable(new sys_var_const_string_val("exchange", vm["exchange"].as<string>()));
145
context.registerVariable(new sys_var_const_string_val("routingkey", vm["routingkey"].as<string>()));
151
static void init_options(drizzled::module::option_context &context)
154
po::value<string>()->default_value("localhost"),
155
_("Host name to connect to"));
157
po::value<port_constraint>(&sysvar_rabbitmq_port)->default_value(5672),
158
_("Port to connect to"));
159
context("virtualhost",
160
po::value<string>()->default_value("/"),
161
_("RabbitMQ virtualhost"));
163
po::value<string>()->default_value("guest"),
164
_("RabbitMQ username"));
166
po::value<string>()->default_value("guest"),
167
_("RabbitMQ password"));
168
context("use-replicator",
169
po::value<string>()->default_value("default_replicator"),
170
_("Name of the replicator plugin to use (default='default_replicator')"));
172
po::value<string>()->default_value("ReplicationExchange"),
173
_("Name of RabbitMQ exchange to publish to"));
174
context("routingkey",
175
po::value<string>()->default_value("ReplicationRoutingKey"),
176
_("Name of RabbitMQ routing key to use"));
179
} /* namespace drizzle_plugin */
181
DRIZZLE_PLUGIN(drizzle_plugin::init, NULL, drizzle_plugin::init_options);