~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/rabbitmq/rabbitmq_handler.h

  • Committer: Brian Aker
  • Date: 2009-02-21 00:18:15 UTC
  • Revision ID: brian@tangent.org-20090221001815-x20e8h71e984lvs1
Completion (?) of uint conversion.

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
 
#ifndef PLUGIN_RABBITMQ_RABBITMQ_HANDLER_H
26
 
#define PLUGIN_RABBITMQ_RABBITMQ_HANDLER_H
27
 
 
28
 
#include <exception>
29
 
#include <string>
30
 
#include <amqp.h>
31
 
#include <amqp_framing.h>
32
 
#include <netinet/in.h>
33
 
 
34
 
namespace drizzle_plugin
35
 
{
36
 
 
37
 
/**
38
 
 * exception thrown by the rabbitmq handler
39
 
 *
40
 
 */
41
 
class rabbitmq_handler_exception : public std::exception
42
 
{
43
 
private:
44
 
  const char* message;
45
 
public:
46
 
  rabbitmq_handler_exception(const char* m):message(m) {};
47
 
  rabbitmq_handler_exception(std::string m):message(m.c_str()) {};
48
 
  virtual const char* what() const throw()
49
 
  {
50
 
    return message;
51
 
  }
52
 
};
53
 
 
54
 
 
55
 
/**
56
 
 * @brief wrapper around librabbitmq, hides error handling and reconnections etc
57
 
 * TODO: add reconnection handling
58
 
 */
59
 
class RabbitMQHandler
60
 
{
61
 
private:
62
 
  amqp_connection_state_t rabbitmqConnection; 
63
 
  int sockfd; ///< the socket file desc to the rabbitmq server, 
64
 
              ///< need this to be able to close() it.
65
 
  const std::string &hostname;
66
 
  const in_port_t port;
67
 
  const std::string &username;
68
 
  const std::string &password;
69
 
  const std::string &virtualhost;
70
 
public:
71
 
  /**
72
 
   * @brief
73
 
   *   Constructs a new RabbitMQHandler, purpose is to 
74
 
   *   hide away the error handling, reconnections etc.
75
 
   *
76
 
   * @details
77
 
   *   Connects to the given rabbitmq server on the virtualhost
78
 
   *   with the given username/password. 
79
 
   *
80
 
   * @param[in] hostname the host to connect to.
81
 
   * @param[in] port the port.
82
 
   * @param[in] username the username to use when logging in.
83
 
   * @param[in] password the password to use.
84
 
   * @param[in] virtualhost the rabbitmq virtual host.
85
 
   * @throw exception if we cannot connect to rabbitmq server
86
 
   */
87
 
  RabbitMQHandler(const std::string &hostname, 
88
 
                  const in_port_t port, 
89
 
                  const std::string &username, 
90
 
                  const std::string &password, 
91
 
                  const std::string &virtualhost)
92
 
    throw(rabbitmq_handler_exception);
93
 
 
94
 
  ~RabbitMQHandler();
95
 
 
96
 
  /**
97
 
   * @brief
98
 
   *   Publishes the message to the server
99
 
   *
100
 
   * @details
101
 
   *   publishes the given message
102
 
   *
103
 
   * @param[in] message the message to send
104
 
   * @param[in] length the length of the message
105
 
   * @param[in] exchangeName name of the exchange to publish to
106
 
   * @param[in] routingKey the routing key to use
107
 
   * @throw exception if there is a problem publishing
108
 
   */
109
 
  void publish(void *message, 
110
 
               const int length, 
111
 
               const std::string &exchangeName, 
112
 
               const std::string &routingKey)
113
 
    throw(rabbitmq_handler_exception);
114
 
 
115
 
 
116
 
private:
117
 
  /**
118
 
   * @brief
119
 
   *   Handles errors produced by librabbitmq
120
 
   *
121
 
   * @details
122
 
   *   If an error occurs, an error string is thrown.
123
 
   *
124
 
   * @param[in] x the response from librabbitmq
125
 
   * @param[in] context the context the call occured, simply appended to the error message.
126
 
   *
127
 
   * @throw exception with the message unless the command was successful
128
 
   */
129
 
  void handleAMQPError(amqp_rpc_reply_t x, std::string context) throw(rabbitmq_handler_exception);
130
 
};
131
 
 
132
 
} /* namespace drizzle_plugin */
133
 
 
134
 
#endif /* PLUGIN_RABBITMQ_RABBITMQ_HANDLER_H */