~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/rabbitmq/rabbitmq_handler.h

  • Committer: Brian Aker
  • Date: 2010-05-27 01:25:56 UTC
  • mfrom: (1567.1.4 new-staging)
  • Revision ID: brian@gaz-20100527012556-5zgkirkl7swbigd6
Merge of Brian, Paul. PBXT compile issue, and test framework cleanup. 

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