~drizzle-trunk/drizzle/development

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 *
 *  Copyright (C) 2010 Marcus Eriksson
 *
 *  Authors:
 *
 *  Marcus Eriksson <krummas@gmail.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <config.h>

#include <drizzled/gettext.h>

#include "rabbitmq_handler.h"

using namespace std;

namespace drizzle_plugin
{

RabbitMQHandler::RabbitMQHandler(const std::string &rabbitMQHost, 
                                 const in_port_t rabbitMQPort, 
                                 const std::string &rabbitMQUsername, 
                                 const std::string &rabbitMQPassword, 
                                 const std::string &rabbitMQVirtualhost, 
				 const std::string &rabbitMQExchange, 
				 const std::string &rabbitMQRoutingKey) 
  throw(rabbitmq_handler_exception) :
    rabbitmqConnection(amqp_new_connection()),
    hostname(rabbitMQHost),
    port(rabbitMQPort),
    username(rabbitMQUsername),
    password(rabbitMQPassword),
    virtualhost(rabbitMQVirtualhost),
    exchange(rabbitMQExchange), 
    routingKey(rabbitMQRoutingKey)
{
  pthread_mutex_init(&publishLock, NULL);
  connect();
}

RabbitMQHandler::~RabbitMQHandler()
{
  pthread_mutex_destroy(&publishLock);
  disconnect();
}

void RabbitMQHandler::publish(void *message, 
                              const int length)
throw(rabbitmq_handler_exception)
{
  pthread_mutex_lock(&publishLock);
  amqp_bytes_t b;
  b.bytes= message;
  b.len= length;
  
  if (amqp_basic_publish(rabbitmqConnection,
                         1,
                         amqp_cstring_bytes(exchange.c_str()),
                         amqp_cstring_bytes(routingKey.c_str()),
                         0,
                         0,
                         NULL,
                         b) < 0)
  {
    pthread_mutex_unlock(&publishLock);
    throw rabbitmq_handler_exception("Could not publish message");
  }
  pthread_mutex_unlock(&publishLock);

}

void RabbitMQHandler::reconnect() throw(rabbitmq_handler_exception)
{
  disconnect();
  connect();
}

void RabbitMQHandler::disconnect() throw(rabbitmq_handler_exception) 
{
  try
  {
    handleAMQPError(amqp_channel_close(rabbitmqConnection, 
				       1, 
				       AMQP_REPLY_SUCCESS),
		    "close channel");
    handleAMQPError(amqp_connection_close(rabbitmqConnection, 
					  AMQP_REPLY_SUCCESS),
		    "close connection");
    amqp_destroy_connection(rabbitmqConnection);
  }
  catch(exception& e) {} // do not throw in destructorn 
  close(sockfd);
}

void RabbitMQHandler::connect() throw(rabbitmq_handler_exception) {
  sockfd = amqp_open_socket(hostname.c_str(), port);
  if(sockfd < 0) 
  {
    throw rabbitmq_handler_exception(_("Could not open socket, is rabbitmq running?"));
  }
  amqp_set_sockfd(rabbitmqConnection, sockfd);
  /* login to rabbitmq, handleAMQPError throws exception if there is a problem */
  handleAMQPError(amqp_login(rabbitmqConnection, 
                             virtualhost.c_str(), 
                             0, 
                             131072, 
                             0, 
                             AMQP_SASL_METHOD_PLAIN, 
                             username.c_str(), 
                             password.c_str()), 
                  "rabbitmq login");
  /* open the channel */
  amqp_channel_open(rabbitmqConnection, 1);
  handleAMQPError(amqp_get_rpc_reply(rabbitmqConnection), "RPC Reply");
  amqp_table_t empty_table = { 0, NULL }; // for users of old librabbitmq users - amqp_empty_table did not exist
  amqp_exchange_declare(rabbitmqConnection, 1, amqp_cstring_bytes(exchange.c_str()), amqp_cstring_bytes("fanout"), 0, 0, empty_table);
  handleAMQPError(amqp_get_rpc_reply(rabbitmqConnection), "RPC Reply");
}

void RabbitMQHandler::handleAMQPError(amqp_rpc_reply_t x, string context) throw(rabbitmq_handler_exception)
{
  string errorMessage("");
  switch (x.reply_type) {
  case AMQP_RESPONSE_NORMAL:
    break;
  case AMQP_RESPONSE_NONE:
    errorMessage.assign("No response in ");
    errorMessage.append(context);
    throw rabbitmq_handler_exception(errorMessage);
  case AMQP_RESPONSE_LIBRARY_EXCEPTION:
  case AMQP_RESPONSE_SERVER_EXCEPTION:
    switch (x.reply.id) {      
    case AMQP_CONNECTION_CLOSE_METHOD:
      errorMessage.assign("Connection closed in ");
      errorMessage.append(context);
      throw rabbitmq_handler_exception(errorMessage);
    case AMQP_CHANNEL_CLOSE_METHOD:
      errorMessage.assign("Channel closed in ");
      errorMessage.append(context);
      throw rabbitmq_handler_exception(errorMessage);
    default:
      errorMessage.assign("Unknown error in ");
      errorMessage.append(context);
      throw rabbitmq_handler_exception(errorMessage);
    }
  }
}

} /* namespace drizzle_plugin */