|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* AppserverIo\Messaging\MessageQueueParser |
|
5
|
|
|
* |
|
6
|
|
|
* NOTICE OF LICENSE |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
|
9
|
|
|
* that is available through the world-wide-web at this URL: |
|
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
|
11
|
|
|
* |
|
12
|
|
|
* PHP version 5 |
|
13
|
|
|
* |
|
14
|
|
|
* @author Tim Wagner <[email protected]> |
|
15
|
|
|
* @copyright 2015 TechDivision GmbH <[email protected]> |
|
16
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
17
|
|
|
* @link https://github.com/appserver-io/messaging |
|
18
|
|
|
* @link http://www.appserver.io |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
namespace AppserverIo\Messaging; |
|
22
|
|
|
|
|
23
|
|
|
use AppserverIo\Psr\Socket\SocketInterface; |
|
24
|
|
|
use AppserverIo\Psr\Pms\MessageQueueException; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* This is a parser for a native message invocation. |
|
28
|
|
|
* |
|
29
|
|
|
* A method invokation must have the following format: |
|
30
|
|
|
* |
|
31
|
|
|
* <METHOD> <CONTENT-LENGTH> <PROTOCOL>/<VERSION>\r\n |
|
32
|
|
|
* <CONTENT> |
|
33
|
|
|
* |
|
34
|
|
|
* for example: |
|
35
|
|
|
* |
|
36
|
|
|
* MSG 12 MQ/1.0 |
|
37
|
|
|
* czoxOiIxIjs= |
|
38
|
|
|
* |
|
39
|
|
|
* @author Tim Wagner <[email protected]> |
|
40
|
|
|
* @copyright 2015 TechDivision GmbH <[email protected]> |
|
41
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
42
|
|
|
* @link https://github.com/appserver-io/messaging |
|
43
|
|
|
* @link http://www.appserver.io |
|
44
|
|
|
*/ |
|
45
|
|
|
class MessageQueueParser |
|
46
|
|
|
{ |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Parses the header of a message call call. |
|
50
|
|
|
* |
|
51
|
|
|
* @param string $line The header line to parse |
|
52
|
|
|
* |
|
53
|
|
|
* @return integer The content length to parse |
|
54
|
|
|
* @throws \AppserverIo\Psr\Pms\MessageQueueException |
|
55
|
|
|
*/ |
|
56
|
|
|
public function parseHeader($line) |
|
57
|
|
|
{ |
|
58
|
|
|
|
|
59
|
|
|
// parse the header line with |
|
60
|
|
|
list ($messageType, $contentLength, $protocolVersion) = explode(' ', trim($line)); |
|
61
|
|
|
|
|
62
|
|
|
// check protocol and version |
|
63
|
|
|
$this->checkProtocolAndVersion($protocolVersion); |
|
64
|
|
|
|
|
65
|
|
|
// check the message type |
|
66
|
|
|
switch ($messageType) { |
|
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
case MessageQueueProtocol::MESSAGE_TYPE_MSG: |
|
69
|
|
|
return (integer) $contentLength; |
|
70
|
|
|
|
|
71
|
|
|
default: |
|
72
|
|
|
throw new MessageQueueException(sprintf('Found invalid message type %s', $messageType)); |
|
73
|
|
|
|
|
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Parses the request body and tries to unpack the remote method |
|
79
|
|
|
* instance from it. |
|
80
|
|
|
* |
|
81
|
|
|
* @param \AppserverIo\Psr\Socket\SocketInterface $connection The package remote method instance |
|
82
|
|
|
* @param integer $contentLength The content length to read |
|
83
|
|
|
* |
|
84
|
|
|
* @return object The unpacked message object |
|
85
|
|
|
*/ |
|
86
|
|
|
public function parseBody(SocketInterface $connection, $contentLength) |
|
87
|
|
|
{ |
|
88
|
|
|
$rawResponse = stream_get_contents($connection->getConnectionResource(), $contentLength); |
|
89
|
|
|
return MessageQueueProtocol::unpack($rawResponse); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Parses the message queue response and returns a response instance. |
|
94
|
|
|
* |
|
95
|
|
|
* @param string $line The response string to parse |
|
96
|
|
|
* |
|
97
|
|
|
* @return \AppserverIo\Messaging\QueueResponse The queue response instance |
|
98
|
|
|
* @throws \AppserverIo\Psr\Pms\MessageQueueException Is thrown if we found an invalid status code |
|
99
|
|
|
*/ |
|
100
|
|
|
public function parseResult($line) |
|
101
|
|
|
{ |
|
102
|
|
|
// parse the header line with |
|
103
|
|
|
list ($protocolVersion, $statusCode, $message, ) = explode(' ', trim($line)); |
|
104
|
|
|
|
|
105
|
|
|
// check protocol and version |
|
106
|
|
|
$this->checkProtocolAndVersion($protocolVersion); |
|
107
|
|
|
|
|
108
|
|
|
// prepare the queue response |
|
109
|
|
|
$responseMessages = MessageQueueProtocol::getResponseMessages(); |
|
110
|
|
|
if (isset($responseMessages[$statusCode])) { |
|
111
|
|
|
return new QueueResponse($statusCode, $message); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
// we can't prepare the queue response because of an unknown status code |
|
115
|
|
|
throw new MessageQueueException(sprintf('Found unknown status code %d', $statusCode)); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Checks if the protocol version specified in the request is valid. |
|
120
|
|
|
* |
|
121
|
|
|
* @param string $protocolVersion The protocol version specified in the request header |
|
122
|
|
|
* |
|
123
|
|
|
* @return void |
|
124
|
|
|
* @throws \AppserverIo\Psr\Pms\MessageQueueException Is thrown if the protocol version is not supported |
|
125
|
|
|
*/ |
|
126
|
|
|
protected function checkProtocolAndVersion($protocolVersion) |
|
127
|
|
|
{ |
|
128
|
|
|
|
|
129
|
|
|
// parse protocol and version |
|
130
|
|
|
list ($protocol, $version) = explode('/', $protocolVersion); |
|
131
|
|
|
|
|
132
|
|
|
// check if protocol and version are valid |
|
133
|
|
|
if ($protocol !== MessageQueueProtocol::PROTOCOL && $version !== MessageQueueProtocol::VERSION) { |
|
134
|
|
|
throw new MessageQueueException(sprintf('Protocol %s not supported', $protocolVersion)); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|