-
Notifications
You must be signed in to change notification settings - Fork 135
/
rabbitMQLib.inc
272 lines (244 loc) · 7.62 KB
/
rabbitMQLib.inc
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
<?php
require_once('get_host_info.inc');
class rabbitMQServer
{
private $machine = "";
public $BROKER_HOST;
private $BROKER_PORT;
private $USER;
private $PASSWORD;
private $VHOST;
private $exchange;
private $queue;
private $routing_key = '*';
private $exchange_type = "topic";
private $auto_delete = false;
function __construct($machine, $server = "rabbitMQ")
{
$this->machine = getHostInfo(array($machine));
$this->BROKER_HOST = $this->machine[$server]["BROKER_HOST"];
$this->BROKER_PORT = $this->machine[$server]["BROKER_PORT"];
$this->USER = $this->machine[$server]["USER"];
$this->PASSWORD = $this->machine[$server]["PASSWORD"];
$this->VHOST = $this->machine[$server]["VHOST"];
if (isset( $this->machine[$server]["EXCHANGE_TYPE"]))
{
$this->exchange_type = $this->machine[$server]["EXCHANGE_TYPE"];
}
if (isset( $this->machine[$server]["AUTO_DELETE"]))
{
$this->auto_delete = $this->machine[$server]["AUTO_DELETE"];
}
$this->exchange = $this->machine[$server]["EXCHANGE"];
$this->queue = $this->machine[$server]["QUEUE"];
}
function process_message($msg)
{
// send the ack to clear the item from the queue
if ($msg->getRoutingKey() !== "*")
{
return;
}
$this->conn_queue->ack($msg->getDeliveryTag());
try
{
if ($msg->getReplyTo())
{
// message wants a response
// process request
$body = $msg->getBody();
$payload = json_decode($body, true);
$response;
if (isset($this->callback))
{
$response = call_user_func($this->callback,$payload);
}
$params = array();
$params['host'] = $this->BROKER_HOST;
$params['port'] = $this->BROKER_PORT;
$params['login'] = $this->USER;
$params['password'] = $this->PASSWORD;
$params['vhost'] = $this->VHOST;
$conn = new AMQPConnection($params);
$conn->connect();
$channel = new AMQPChannel($conn);
$exchange = new AMQPExchange($channel);
$exchange->setName($this->exchange);
$exchange->setType($this->exchange_type);
$conn_queue = new AMQPQueue($channel);
$conn_queue->setName($msg->getReplyTo());
$replykey = $this->routing_key.".response";
$conn_queue->bind($exchange->getName(),$replykey);
$exchange->publish(json_encode($response),$replykey,AMQP_NOPARAM,array('correlation_id'=>$msg->getCorrelationId()));
return;
}
}
catch(Exception $e)
{
// ampq throws exception if get fails...
echo "error: rabbitMQServer: process_message: exception caught: ".$e;
}
// message does not require a response, send ack immediately
$body = $msg->getBody();
$payload = json_decode($body, true);
if (isset($this->callback))
{
call_user_func($this->callback,$payload);
}
echo "processed one-way message\n";
}
function process_requests($callback)
{
try
{
$this->callback = $callback;
$params = array();
$params['host'] = $this->BROKER_HOST;
$params['port'] = $this->BROKER_PORT;
$params['login'] = $this->USER;
$params['password'] = $this->PASSWORD;
$params['vhost'] = $this->VHOST;
$conn = new AMQPConnection($params);
$conn->connect();
$channel = new AMQPChannel($conn);
$exchange = new AMQPExchange($channel);
$exchange->setName($this->exchange);
$exchange->setType($this->exchange_type);
$this->conn_queue = new AMQPQueue($channel);
$this->conn_queue->setName($this->queue);
$this->conn_queue->bind($exchange->getName(),$this->routing_key);
$this->conn_queue->consume(array($this,'process_message'));
// Loop as long as the channel has callbacks registered
while (count($channel->callbacks))
{
$channel->wait();
}
}
catch (Exception $e)
{
trigger_error("Failed to start request processor: ".$e,E_USER_ERROR);
}
}
}
class rabbitMQClient
{
private $machine = "";
public $BROKER_HOST;
private $BROKER_PORT;
private $USER;
private $PASSWORD;
private $VHOST;
private $exchange;
private $queue;
private $routing_key = '*';
private $response_queue = array();
private $exchange_type = "topic";
function __construct($machine, $server = "rabbitMQ")
{
$this->machine = getHostInfo(array($machine));
$this->BROKER_HOST = $this->machine[$server]["BROKER_HOST"];
$this->BROKER_PORT = $this->machine[$server]["BROKER_PORT"];
$this->USER = $this->machine[$server]["USER"];
$this->PASSWORD = $this->machine[$server]["PASSWORD"];
$this->VHOST = $this->machine[$server]["VHOST"];
if (isset( $this->machine[$server]["EXCHANGE_TYPE"]))
{
$this->exchange_type = $this->machine[$server]["EXCHANGE_TYPE"];
}
if (isset( $this->machine[$server]["AUTO_DELETE"]))
{
$this->auto_delete = $this->machine[$server]["AUTO_DELETE"];
}
$this->exchange = $this->machine[$server]["EXCHANGE"];
$this->queue = $this->machine[$server]["QUEUE"];
}
function process_response($response)
{
$uid = $response->getCorrelationId();
if (!isset($this->response_queue[$uid]))
{
echo "unknown uid\n";
return true;
}
$this->conn_queue->ack($response->getDeliveryTag());
$body = $response->getBody();
$payload = json_decode($body, true);
if (!(isset($payload)))
{
$payload = "[empty response]";
}
$this->response_queue[$uid] = $payload;
return false;
}
function send_request($message)
{
$uid = uniqid();
$json_message = json_encode($message);
try
{
$params = array();
$params['host'] = $this->BROKER_HOST;
$params['port'] = $this->BROKER_PORT;
$params['login'] = $this->USER;
$params['password'] = $this->PASSWORD;
$params['vhost'] = $this->VHOST;
$conn = new AMQPConnection($params);
$conn->connect();
$channel = new AMQPChannel($conn);
$exchange = new AMQPExchange($channel);
$exchange->setName($this->exchange);
$exchange->setType($this->exchange_type);
$callback_queue = new AMQPQueue($channel);
$callback_queue->setName($this->queue."_response");
$callback_queue->declare();
$callback_queue->bind($exchange->getName(),$this->routing_key.".response");
$this->conn_queue = new AMQPQueue($channel);
$this->conn_queue->setName($this->queue);
$this->conn_queue->bind($exchange->getName(),$this->routing_key);
$exchange->publish($json_message,$this->routing_key,AMQP_NOPARAM,array('reply_to'=>$callback_queue->getName(),'correlation_id'=>$uid));
$this->response_queue[$uid] = "waiting";
$callback_queue->consume(array($this,'process_response'));
$response = $this->response_queue[$uid];
unset($this->response_queue[$uid]);
return $response;
}
catch(Exception $e)
{
die("failed to send message to exchange: ". $e->getMessage()."\n");
}
}
/**
@brief send a one-way message to the server. These are
auto-acknowledged and give no response.
@param message the body of the request. This must make sense to the
server
*/
function publish($message)
{
$json_message = json_encode($message);
try
{
$params = array();
$params['host'] = $this->BROKER_HOST;
$params['port'] = $this->BROKER_PORT;
$params['login'] = $this->USER;
$params['password'] = $this->PASSWORD;
$params['vhost'] = $this->VHOST;
$conn = new AMQPConnection($params);
$conn->connect();
$channel = new AMQPChannel($conn);
$exchange = new AMQPExchange($channel);
$exchange->setName($this->exchange);
$exchange->setType($this->exchange_type);
$this->conn_queue = new AMQPQueue($channel);
$this->conn_queue->setName($this->queue);
$this->conn_queue->bind($exchange->getName(),$this->routing_key);
return $exchange->publish($json_message,$this->routing_key);
}
catch(Exception $e)
{
die("failed to send message to exchange: ". $e->getMessage()."\n");
}
}
}
?>