~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/rabbitmq/rabbitmq_handler.h

  • Committer: Daniel Nichter
  • Date: 2011-10-23 16:01:37 UTC
  • mto: This revision was merged to the branch mainline in revision 2448.
  • Revision ID: daniel@percona.com-20111023160137-7ac3blgz8z4tf8za
Add Administration Getting Started and Logging.  Capitalize SQL clause keywords.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 <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
 
23
 */
 
24
 
 
25
#pragma once
 
26
 
 
27
#include <exception>
 
28
#include <string>
 
29
#include <amqp.h>
 
30
#include <amqp_framing.h>
 
31
#include <netinet/in.h>
 
32
 
 
33
namespace drizzle_plugin
 
34
{
 
35
 
 
36
/**
 
37
 * exception thrown by the rabbitmq handler
 
38
 *
 
39
 */
 
40
class rabbitmq_handler_exception : public std::exception
 
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.
 
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;
 
69
  const std::string &exchange;
 
70
  const std::string &routingKey;
 
71
  pthread_mutex_t publishLock;
 
72
public:
 
73
  /**
 
74
   * @brief
 
75
   *   Constructs a new RabbitMQHandler, purpose is to 
 
76
   *   hide away the error handling, reconnections etc.
 
77
   *
 
78
   * @details
 
79
   *   Connects to the given rabbitmq server on the virtualhost
 
80
   *   with the given username/password. 
 
81
   *
 
82
   * @param[in] hostname the host to connect to.
 
83
   * @param[in] port the port.
 
84
   * @param[in] username the username to use when logging in.
 
85
   * @param[in] password the password to use.
 
86
   * @param[in] virtualhost the rabbitmq virtual host.
 
87
   * @throw exception if we cannot connect to rabbitmq server
 
88
   */
 
89
  RabbitMQHandler(const std::string &hostname, 
 
90
                  const in_port_t port, 
 
91
                  const std::string &username, 
 
92
                  const std::string &password, 
 
93
                  const std::string &virtualhost,
 
94
                  const std::string &exchange,
 
95
                  const std::string &routingKey)
 
96
    throw(rabbitmq_handler_exception);
 
97
 
 
98
  ~RabbitMQHandler();
 
99
 
 
100
  /**
 
101
   * @brief
 
102
   *   Publishes the message to the server
 
103
   *
 
104
   * @details
 
105
   *   publishes the given message
 
106
   *
 
107
   * @param[in] message the message to send
 
108
   * @param[in] length the length of the message
 
109
   * @throw exception if there is a problem publishing
 
110
   */
 
111
  void publish(void *message, 
 
112
               const int length)
 
113
    throw(rabbitmq_handler_exception);
 
114
 
 
115
  void reconnect() throw(rabbitmq_handler_exception);
 
116
  void disconnect() throw(rabbitmq_handler_exception);
 
117
 
 
118
private:
 
119
  /**
 
120
   * @brief
 
121
   *   Handles errors produced by librabbitmq
 
122
   *
 
123
   * @details
 
124
   *   If an error occurs, an error string is thrown.
 
125
   *
 
126
   * @param[in] x the response from librabbitmq
 
127
   * @param[in] context the context the call occured, simply appended to the error message.
 
128
   *
 
129
   * @throw exception with the message unless the command was successful
 
130
   */
 
131
  void handleAMQPError(amqp_rpc_reply_t x, std::string context) throw(rabbitmq_handler_exception);
 
132
 
 
133
  void connect() throw(rabbitmq_handler_exception);
 
134
 
 
135
};
 
136
 
 
137
} /* namespace drizzle_plugin */
 
138