|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* AppserverIo\PersistenceContainerClient\RemoteMethodCallParser |
|
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/rmi |
|
18
|
|
|
* @link http://www.appserver.io |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
namespace AppserverIo\RemoteMethodInvocation; |
|
22
|
|
|
|
|
23
|
|
|
use AppserverIo\Psr\Socket\SocketInterface; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* This is a parser for a native persistence container remote method call. |
|
27
|
|
|
* |
|
28
|
|
|
* A Remote method call must have the following format: |
|
29
|
|
|
* |
|
30
|
|
|
* <METHOD> <CONTENT-LENGTH> <PROTOCOL>/<VERSION>\r\n |
|
31
|
|
|
* <CONTENT> |
|
32
|
|
|
* |
|
33
|
|
|
* for example: |
|
34
|
|
|
* |
|
35
|
|
|
* INVOKE 12 RMC/1.0 |
|
36
|
|
|
* czoxOiIxIjs= |
|
37
|
|
|
* |
|
38
|
|
|
* @author Tim Wagner <[email protected]> |
|
39
|
|
|
* @copyright 2015 TechDivision GmbH <[email protected]> |
|
40
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
41
|
|
|
* @link https://github.com/appserver-io/rmi |
|
42
|
|
|
* @link http://www.appserver.io |
|
43
|
|
|
*/ |
|
44
|
|
|
class RemoteMethodCallParser |
|
45
|
|
|
{ |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Parses the header of a remote method call. |
|
49
|
|
|
* |
|
50
|
|
|
* @param string $line The header line to parse |
|
51
|
|
|
* |
|
52
|
|
|
* @return integer The content length to parse |
|
53
|
|
|
* @throws \AppserverIo\RemoteMethodInvocation\RemoteMethodCallException |
|
54
|
|
|
*/ |
|
55
|
|
|
public function parseHeader($line) |
|
56
|
|
|
{ |
|
57
|
|
|
|
|
58
|
|
|
// parse the header line with |
|
59
|
|
|
list ($remoteMethod, $contentLength, $protocolVersion) = explode(' ', trim($line)); |
|
60
|
|
|
|
|
61
|
|
|
// parse protocol and version |
|
62
|
|
|
list ($protocol, $version) = explode('/', $protocolVersion); |
|
63
|
|
|
|
|
64
|
|
|
// check if protocol and version are valid |
|
65
|
|
|
if ($protocol !== RemoteMethodProtocol::PROTOCOL && $version !== RemoteMethodProtocol::VERSION) { |
|
66
|
|
|
throw new RemoteMethodCallException(sprintf('Protocol %s not supported', $protocolVersion)); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
// check the request method |
|
70
|
|
|
switch ($remoteMethod) { |
|
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
case RemoteMethodProtocol::REMOTE_METHOD_INVOKE: |
|
73
|
|
|
return (integer) $contentLength; |
|
74
|
|
|
break; |
|
|
|
|
|
|
75
|
|
|
|
|
76
|
|
|
case RemoteMethodProtocol::REMOTE_METHOD_RESULT: |
|
77
|
|
|
return (integer) $contentLength; |
|
78
|
|
|
break; |
|
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
default: |
|
81
|
|
|
throw new RemoteMethodCallException(sprintf('Found invalid remote method %s', $remoteMethod)); |
|
82
|
|
|
break; |
|
|
|
|
|
|
83
|
|
|
|
|
|
|
|
|
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Parses the request body and tries to unpack the remote method |
|
89
|
|
|
* instance from it. |
|
90
|
|
|
* |
|
91
|
|
|
* @param \AppserverIo\Psr\Socket\SocketInterface $connection The package remote method instance |
|
92
|
|
|
* @param integer $contentLength The content length to read |
|
93
|
|
|
* |
|
94
|
|
|
* @return object The unpacked remote method object/result |
|
95
|
|
|
*/ |
|
96
|
|
|
public function parseBody(SocketInterface $connection, $contentLength) |
|
97
|
|
|
{ |
|
98
|
|
|
$rawResponse = stream_get_contents($connection->getConnectionResource(), $contentLength); |
|
99
|
|
|
return RemoteMethodProtocol::unpack($rawResponse); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|