~drizzle-trunk/drizzle/development

1283.2.2 by Marcus Eriksson
add copyright headers, change load_by_default to no, include config.h, change string initialization to () style
1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
1283.2.1 by Marcus Eriksson
rabbitmq replication applier
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
1283.2.2 by Marcus Eriksson
add copyright headers, change load_by_default to no, include config.h, change string initialization to () style
3
 *
4
 *  Copyright (C) 2010 Marcus Eriksson
5
 *
6
 *  Authors:
7
 *
8
 *  Marcus Eriksson <krummas@gmail.com>
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
1283.2.1 by Marcus Eriksson
rabbitmq replication applier
23
 */
24
2234 by Brian Aker
Mass removal of ifdef/endif in favor of pragma once.
25
#pragma once
1441.1.2 by Jay Pipes
Two little problems were preventing the plugin from being loaded:
26
27
#include <exception>
1283.2.1 by Marcus Eriksson
rabbitmq replication applier
28
#include <string>
29
#include <amqp.h>
30
#include <amqp_framing.h>
1964.2.14 by Monty Taylor
RabbitMQ.
31
#include <netinet/in.h>
32
33
namespace drizzle_plugin
34
{
1283.2.1 by Marcus Eriksson
rabbitmq replication applier
35
36
/**
37
 * exception thrown by the rabbitmq handler
38
 *
39
 */
1964.2.14 by Monty Taylor
RabbitMQ.
40
class rabbitmq_handler_exception : public std::exception
1283.2.1 by Marcus Eriksson
rabbitmq replication applier
41
{
42
private:
43
  const char* message;
44
public:
45
  rabbitmq_handler_exception(const char* m):message(m) {};
46
  rabbitmq_handler_exception(std::string m):message(m.c_str()) {};
47
  virtual const char* what() const throw()
48
  {
49
    return message;
50
  }
51
};
52
53
54
/**
55
 * @brief wrapper around librabbitmq, hides error handling and reconnections etc
56
 * TODO: add reconnection handling
57
 */
58
class RabbitMQHandler
59
{
60
private:
61
  amqp_connection_state_t rabbitmqConnection; 
62
  int sockfd; ///< the socket file desc to the rabbitmq server, 
63
              ///< need this to be able to close() it.
1964.2.14 by Monty Taylor
RabbitMQ.
64
  const std::string &hostname;
65
  const in_port_t port;
66
  const std::string &username;
67
  const std::string &password;
68
  const std::string &virtualhost;
1283.2.1 by Marcus Eriksson
rabbitmq replication applier
69
public:
70
  /**
71
   * @brief
72
   *   Constructs a new RabbitMQHandler, purpose is to 
73
   *   hide away the error handling, reconnections etc.
74
   *
75
   * @details
76
   *   Connects to the given rabbitmq server on the virtualhost
77
   *   with the given username/password. 
78
   *
79
   * @param[in] hostname the host to connect to.
80
   * @param[in] port the port.
81
   * @param[in] username the username to use when logging in.
82
   * @param[in] password the password to use.
83
   * @param[in] virtualhost the rabbitmq virtual host.
84
   * @throw exception if we cannot connect to rabbitmq server
85
   */
1964.2.14 by Monty Taylor
RabbitMQ.
86
  RabbitMQHandler(const std::string &hostname, 
87
                  const in_port_t port, 
88
                  const std::string &username, 
89
                  const std::string &password, 
90
                  const std::string &virtualhost)
91
    throw(rabbitmq_handler_exception);
92
1283.2.1 by Marcus Eriksson
rabbitmq replication applier
93
  ~RabbitMQHandler();
94
95
  /**
96
   * @brief
97
   *   Publishes the message to the server
98
   *
99
   * @details
100
   *   publishes the given message
101
   *
102
   * @param[in] message the message to send
103
   * @param[in] length the length of the message
104
   * @param[in] exchangeName name of the exchange to publish to
105
   * @param[in] routingKey the routing key to use
106
   * @throw exception if there is a problem publishing
107
   */
1964.2.14 by Monty Taylor
RabbitMQ.
108
  void publish(void *message, 
109
               const int length, 
110
               const std::string &exchangeName, 
111
               const std::string &routingKey)
112
    throw(rabbitmq_handler_exception);
113
114
115
private:
1283.2.1 by Marcus Eriksson
rabbitmq replication applier
116
  /**
117
   * @brief
118
   *   Handles errors produced by librabbitmq
119
   *
120
   * @details
121
   *   If an error occurs, an error string is thrown.
122
   *
123
   * @param[in] x the response from librabbitmq
124
   * @param[in] context the context the call occured, simply appended to the error message.
125
   *
126
   * @throw exception with the message unless the command was successful
127
   */
128
  void handleAMQPError(amqp_rpc_reply_t x, std::string context) throw(rabbitmq_handler_exception);
129
};
130
1964.2.14 by Monty Taylor
RabbitMQ.
131
} /* namespace drizzle_plugin */
132