1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* The MIT License |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2010 Alvaro Videla |
6
|
|
|
* |
7
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
8
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
9
|
|
|
* in the Software without restriction, including without limitation the rights |
10
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
11
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
12
|
|
|
* furnished to do so, subject to the following conditions: |
13
|
|
|
* |
14
|
|
|
* The above copyright notice and this permission notice shall be included in |
15
|
|
|
* all copies or substantial portions of the Software. |
16
|
|
|
* |
17
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
18
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
19
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
20
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
21
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
22
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
23
|
|
|
* SOFTWARE. |
24
|
|
|
* |
25
|
|
|
* PHP version 5.3 |
26
|
|
|
* |
27
|
|
|
* @category Thumper |
28
|
|
|
* @package Thumper |
29
|
|
|
* @author Alvaro Videla |
30
|
|
|
* @copyright 2010 Alvaro Videla. All rights reserved. |
31
|
|
|
* @license MIT http://opensource.org/licenses/MIT |
32
|
|
|
* @link https://github.com/videlalvaro/Thumper |
33
|
|
|
*/ |
34
|
|
|
namespace Thumper; |
35
|
|
|
|
36
|
|
|
use PhpAmqpLib\Exception\AMQPInvalidArgumentException; |
37
|
|
|
use PhpAmqpLib\Exception\AMQPRuntimeException; |
38
|
|
|
use PhpAmqpLib\Message\AMQPMessage; |
39
|
|
|
|
40
|
|
|
class RpcServer extends BaseConsumer |
41
|
|
|
{ |
42
|
|
|
/** |
43
|
|
|
* Initialize Server. |
44
|
|
|
* |
45
|
|
|
* @param string $name Server name |
46
|
|
|
*/ |
47
|
30 |
|
public function initServer($name) |
48
|
|
|
{ |
49
|
30 |
|
$this->setExchangeOptions( |
50
|
30 |
|
array('name' => $name . '-exchange', 'type' => 'direct') |
51
|
24 |
|
); |
52
|
30 |
|
$this->setQueueOptions(array('name' => $name . '-queue')); |
53
|
30 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Start server. |
57
|
|
|
*/ |
58
|
25 |
|
public function start() |
59
|
|
|
{ |
60
|
25 |
|
$this->setUpConsumer(); |
61
|
|
|
|
62
|
25 |
|
while (count($this->channel->callbacks)) { |
63
|
10 |
|
$this->channel->wait(); |
64
|
8 |
|
} |
65
|
25 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Process message. |
69
|
|
|
* |
70
|
|
|
* @param AMQPMessage $message |
71
|
|
|
* @throws \OutOfBoundsException |
72
|
|
|
* @throws \PhpAmqpLib\Exception\AMQPInvalidArgumentException |
73
|
|
|
*/ |
74
|
15 |
|
public function processMessage(AMQPMessage $message) |
75
|
|
|
{ |
76
|
|
|
try { |
77
|
15 |
|
$message->delivery_info['channel'] |
78
|
15 |
|
->basic_ack($message->delivery_info['delivery_tag']); |
|
|
|
|
79
|
15 |
|
$result = call_user_func($this->callback, $message->body); |
80
|
15 |
|
$this->sendReply($result, $message->get('reply_to'), $message->get('correlation_id')); |
81
|
14 |
|
} catch (AMQPRuntimeException $exception) { |
82
|
5 |
|
$this->sendReply( |
83
|
5 |
|
'error: ' . $exception->getMessage(), |
84
|
5 |
|
$message->get('reply_to'), |
85
|
5 |
|
$message->get('correlation_id') |
86
|
4 |
|
); |
87
|
9 |
|
} catch (AMQPInvalidArgumentException $exception) { |
88
|
5 |
|
$this->sendReply( |
89
|
5 |
|
'error: ' . $exception->getMessage(), |
90
|
5 |
|
$message->get('reply_to'), |
91
|
5 |
|
$message->get('correlation_id') |
92
|
4 |
|
); |
93
|
|
|
} |
94
|
15 |
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Send reply. |
98
|
|
|
* |
99
|
|
|
* @param string $result |
100
|
|
|
* @param string $client |
101
|
|
|
* @param string $correlationId |
102
|
|
|
* @throws \PhpAmqpLib\Exception\AMQPInvalidArgumentException |
103
|
|
|
*/ |
104
|
15 |
|
protected function sendReply($result, $client, $correlationId) |
105
|
|
|
{ |
106
|
15 |
|
$this->setParameter('correlation_id', $correlationId); |
107
|
15 |
|
$reply = new AMQPMessage( |
108
|
12 |
|
$result, |
109
|
15 |
|
$this->getParameters() |
110
|
12 |
|
); |
111
|
15 |
|
$this->channel |
112
|
15 |
|
->basic_publish($reply, '', $client); |
113
|
15 |
|
} |
114
|
|
|
} |
115
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: