Changeset 1817
- Timestamp:
- 09/02/08 02:51:11 (4 months ago)
- Files:
-
- labs/incubator/src/main/php/net/stubbles/remote/protocol/easc/stubEASCException.php (added)
- labs/incubator/src/main/php/net/stubbles/remote/protocol/easc/stubEASCHeader.php (added)
- labs/incubator/src/main/php/net/stubbles/remote/protocol/easc/stubEASCProtocolHandler.php (modified) (8 diffs)
- labs/incubator/src/test/php/net/stubbles/remote/RemoteTestSuite.php (modified) (1 diff)
- labs/incubator/src/test/php/net/stubbles/remote/protocol/easc/stubEASCHeaderTestCase.php (added)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
labs/incubator/src/main/php/net/stubbles/remote/protocol/easc/stubEASCProtocolHandler.php
r1815 r1817 2 2 /** 3 3 * Protocol handler for the EASC protocol. 4 * 4 * 5 5 * @author Frank Kleine <mikey@stubbles.net> 6 6 * @package stubbles … … 11 11 'net::stubbles::remote::protocol::stubProtocolHandler', 12 12 'net::stubbles::remote::protocol::easc::stubEASCException', 13 'net::stubbles::remote::protocol::easc::stubEASCHeader', 13 14 'net::stubbles::peer::stubBSDSocket' 14 15 ); 15 16 /** 16 17 * Protocol handler for the EASC protocol. 17 * 18 * 18 19 * @package stubbles 19 20 * @subpackage remote_protocol_easc … … 38 39 * @var stubBSDSocket 39 40 */ 40 protected $socket; 41 /** 42 * magic easc protocol number 43 */ 44 const MAGIC_NUMBER = 0x3c872747; 41 protected $socket; 45 42 /** 46 43 * request message type: init … … 183 180 * @return mixed 184 181 */ 185 public function invoke($oid, $method, $args)182 public function invoke($oid, $method, array $args) 186 183 { 187 184 return $this->sendPacket(self::MSG_CALL, 188 185 pack('NN', 0, $oid), 189 186 array(new stubByteCountedString($method), 190 new stubByteCountedString($this->serializer->serialize(stubArrayList::with( array($args))))187 new stubByteCountedString($this->serializer->serialize(stubArrayList::with($args))) 191 188 ) 192 189 ); … … 205 202 protected function sendPacket($type, $data = '', array $bytes = array()) 206 203 { 207 $bsize = sizeof($bytes); 208 // calculate packet length 204 $bsize = sizeof($bytes); 209 205 $length = strlen($data); 210 206 for ($i = 0; $i < $bsize; $i++) { … … 212 208 } 213 209 214 // Write packet 215 $packet = pack('Nc4Na*', 216 self::MAGIC_NUMBER, // magic number to ensure correctness of message 217 $this->versionMajor, // version: major 218 $this->versionMinor, // version: minor 219 $type, // message type 220 false, // compression: always false, probably used in later protocol versions 221 $length, // length of data to follow 222 $data // the real data to send 223 ); 224 210 $requestHeader = new stubEASCHeader(stubEASCHeader::DEFAULT_MAGIC_NUMBER, 1, 0, $type, false, $length); 225 211 try { 226 $this->socket->write($packet); 212 $requestHeader->writeToSocket($this->socket, $data); 213 $this->socket->write(pack('a*', $data)); 227 214 for ($i = 0; $i < $bsize; $i++) { 228 215 $bytes[$i]->writeTo($this->socket); 229 216 } 230 217 231 $ header = unpack('Nmagic/cvmajor/cvminor/ctype/ctran/Nlength', $this->readBytes(12));218 $responseHeader = stubEASCHeader::readFromSocket($this->socket); 232 219 } catch (stubConnectionException $ce) { 233 220 throw new stubRemoteException($ce->getMessage(), $ce); 234 221 } 235 222 236 if ( self::MAGIC_NUMBER != $header['magic']) {223 if (null === $responseHeader) { 237 224 $this->socket->close(); 238 throw new stubEASCException('Magic number mismatch (have: ' . $header['magic'] . ' expect: ' . self::MAGIC_NUMBER); 225 throw new stubEASCException('Illegal response from server, can not find any header.'); 226 } elseif (stubEASCHeader::DEFAULT_MAGIC_NUMBER != $responseHeader->getMagicNumber()) { 227 $this->socket->close(); 228 throw new stubEASCException('Magic number mismatch, got "' . $responseHeader->getMagicNumber() . '" but expected "' . stubEASCHeader::MAGIC_NUMBER . '".'); 239 229 } 240 230 … … 242 232 $context = array('handler' => $this); 243 233 try { 244 switch ($ header['type']) {234 switch ($responseHeader->getMessageType()) { 245 235 case self::MSG_VALUE: 246 236 $data = stubByteCountedString::readFrom($this->socket); … … 264 254 265 255 default: 266 $data = $this->readBytes($ header['length']);256 $data = $this->readBytes($responseHeader->getDataLength()); 267 257 $this->socket->close(); 268 258 throw new stubEASCException('Unknown message type: ' . $data); labs/incubator/src/test/php/net/stubbles/remote/RemoteTestSuite.php
r1813 r1817 33 33 34 34 // protocol::easc 35 $suite->addTestFile($dir . '/protocol/easc/stubEASCHeaderTestCase.php'); 35 36 $suite->addTestFile($dir . '/protocol/easc/stubEASCSerializerTestCase.php'); 36 37 $suite->addTestFile($dir . '/protocol/easc/mapping/stubEASCArrayListMappingTestCase.php');
