~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/rabbitmq/rabbitmq_handler.h

  • Committer: Marcus Eriksson
  • Date: 2010-02-06 20:18:39 UTC
  • mto: This revision was merged to the branch mainline in revision 1438.
  • Revision ID: marcuse@localhost.localdomain-20100206201839-7fh52959ox940hud
rabbitmq replication applier

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