|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* AppserverIo\Messaging\MessageQueueProtocol |
|
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
|
|
|
/** |
|
24
|
|
|
* This is a parser for a native persistence container remote method call. |
|
25
|
|
|
* |
|
26
|
|
|
* A Remote method call must have the following format: |
|
27
|
|
|
* |
|
28
|
|
|
* <METHOD> <CONTENT-LENGTH> <PROTOCOL>/<VERSION>\r\n |
|
29
|
|
|
* <CONTENT> |
|
30
|
|
|
* |
|
31
|
|
|
* for example: |
|
32
|
|
|
* |
|
33
|
|
|
* MSG 12 MQ/1.0\r\n |
|
34
|
|
|
* czoxOiIxIjs= |
|
35
|
|
|
* |
|
36
|
|
|
* @author Tim Wagner <[email protected]> |
|
37
|
|
|
* @copyright 2015 TechDivision GmbH <[email protected]> |
|
38
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
39
|
|
|
* @link https://github.com/appserver-io/messaging |
|
40
|
|
|
* @link http://www.appserver.io |
|
41
|
|
|
*/ |
|
42
|
|
|
class MessageQueueProtocol |
|
43
|
|
|
{ |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* This is the line ending we use. |
|
47
|
|
|
* |
|
48
|
|
|
* @var string |
|
49
|
|
|
*/ |
|
50
|
|
|
const EOL = "\r\n"; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Protocol identifier. |
|
54
|
|
|
* |
|
55
|
|
|
* @var string |
|
56
|
|
|
*/ |
|
57
|
|
|
const PROTOCOL = 'MQ'; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Protocol version. |
|
61
|
|
|
* |
|
62
|
|
|
* @var string |
|
63
|
|
|
*/ |
|
64
|
|
|
const VERSION = '1.0'; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Default message type. |
|
68
|
|
|
* |
|
69
|
|
|
* @var string |
|
70
|
|
|
*/ |
|
71
|
|
|
const MESSAGE_TYPE_MSG = 'MSG'; |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Response code for a successfully accepted message. |
|
75
|
|
|
* |
|
76
|
|
|
* @var integer |
|
77
|
|
|
*/ |
|
78
|
|
|
const STATUS_CODE_OK = 200; |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Response code for a unknow message queue state. |
|
82
|
|
|
* |
|
83
|
|
|
* @var integer |
|
84
|
|
|
*/ |
|
85
|
|
|
const STATUS_CODE_UNKNOWN = 100; |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Response code if we can't accept a message. |
|
89
|
|
|
* |
|
90
|
|
|
* @var string |
|
91
|
|
|
*/ |
|
92
|
|
|
const STATUS_CODE_INTERNAL_SERVER_ERROR = 500; |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Array with the available response messages. |
|
96
|
|
|
* |
|
97
|
|
|
* @var array |
|
98
|
|
|
*/ |
|
99
|
|
|
protected static $responseMessages = array( |
|
100
|
|
|
MessageQueueProtocol::STATUS_CODE_OK => "OK", |
|
101
|
|
|
MessageQueueProtocol::STATUS_CODE_UNKNOWN => "Unknown", |
|
102
|
|
|
MessageQueueProtocol::STATUS_CODE_INTERNAL_SERVER_ERROR => "Internal Server Error" |
|
103
|
|
|
); |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Prepares the header line for a remote method invocation request. |
|
107
|
|
|
* |
|
108
|
|
|
* @param string $string The packed remote method instance |
|
109
|
|
|
* |
|
110
|
|
|
* @return string The remote method invocation header for the passed remote method instance |
|
111
|
|
|
*/ |
|
112
|
|
|
public static function prepareMessageHeader($string) |
|
113
|
|
|
{ |
|
114
|
|
|
return MessageQueueProtocol::prepareHeader(MessageQueueProtocol::MESSAGE_TYPE_MSG, $string); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Prepares the result string that will be sent back to the client. |
|
119
|
|
|
* |
|
120
|
|
|
* @param integer $statusCode The status code we want to send back |
|
121
|
|
|
* |
|
122
|
|
|
* @return string The prepared result, read to send back |
|
123
|
|
|
*/ |
|
124
|
|
|
public static function prepareResult($statusCode) |
|
125
|
|
|
{ |
|
126
|
|
|
// check if we have a valid status code |
|
127
|
|
|
if (!isset(MessageQueueProtocol::$responseMessages[$statusCode])) { |
|
128
|
|
|
$statusCode = MessageQueueProtocol::STATUS_CODE_UNKNOWN; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
// prepare the header elements |
|
132
|
|
|
$protocol = MessageQueueProtocol::PROTOCOL; |
|
133
|
|
|
$version = MessageQueueProtocol::VERSION; |
|
134
|
|
|
|
|
135
|
|
|
// prepare the message result ready to be send back to the client |
|
136
|
|
|
return "$protocol/$version $statusCode " . MessageQueueProtocol::$responseMessages[$statusCode] . MessageQueueProtocol::EOL; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Prepares the header line for the passed remote method. |
|
141
|
|
|
* |
|
142
|
|
|
* @param string $method The remote method to prepare the head for |
|
143
|
|
|
* @param string $string The packed remote method instance |
|
144
|
|
|
* |
|
145
|
|
|
* @return string The remote method header for the passed method |
|
146
|
|
|
*/ |
|
147
|
|
|
protected static function prepareHeader($method, $string) |
|
148
|
|
|
{ |
|
149
|
|
|
// prepare the header elements |
|
150
|
|
|
$protocol = MessageQueueProtocol::PROTOCOL; |
|
151
|
|
|
$version = MessageQueueProtocol::VERSION; |
|
152
|
|
|
$contentLength = strlen($string); |
|
153
|
|
|
|
|
154
|
|
|
// concatenate the header string |
|
155
|
|
|
return "$method $contentLength $protocol/$version" . MessageQueueProtocol::EOL; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Returns an array with the available response messages. |
|
160
|
|
|
* |
|
161
|
|
|
* @return array The array with the available response messages |
|
162
|
|
|
*/ |
|
163
|
|
|
public static function getResponseMessages() |
|
164
|
|
|
{ |
|
165
|
|
|
return MessageQueueProtocol::$responseMessages; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Packs the passed instance. |
|
170
|
|
|
* |
|
171
|
|
|
* @param object $instance The instance to be packed |
|
172
|
|
|
* |
|
173
|
|
|
* @return string The packed instance |
|
174
|
|
|
*/ |
|
175
|
|
|
public static function pack($instance) |
|
176
|
|
|
{ |
|
177
|
|
|
return base64_encode(serialize($instance)); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Unpacks the passed instance. |
|
182
|
|
|
* |
|
183
|
|
|
* @param string $string The packed object instance. |
|
184
|
|
|
* |
|
185
|
|
|
* @return object The un-packed object instance |
|
186
|
|
|
*/ |
|
187
|
|
|
public static function unpack($string) |
|
188
|
|
|
{ |
|
189
|
|
|
return unserialize(base64_decode($string)); |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
|