~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/rabbitmq/rabbitmq_handler.h

  • Committer: Brian Aker
  • Date: 2010-04-04 07:54:04 UTC
  • mfrom: (1283.2.2 rabbitmq)
  • Revision ID: brian@gaz-20100404075404-ai3htaz923xgvo61
Merge

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