Changeset 1817

Show
Ignore:
Timestamp:
09/02/08 02:51:11 (4 months ago)
Author:
mikey
Message:

add net::stubbles::remote::protocol::easc::stubEASCHeader

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • labs/incubator/src/main/php/net/stubbles/remote/protocol/easc/stubEASCProtocolHandler.php

    r1815 r1817  
    22/** 
    33 * Protocol handler for the EASC protocol. 
    4  *  
     4 * 
    55 * @author      Frank Kleine <mikey@stubbles.net> 
    66 * @package     stubbles 
     
    1111                      'net::stubbles::remote::protocol::stubProtocolHandler', 
    1212                      'net::stubbles::remote::protocol::easc::stubEASCException', 
     13                      'net::stubbles::remote::protocol::easc::stubEASCHeader', 
    1314                      'net::stubbles::peer::stubBSDSocket' 
    1415); 
    1516/** 
    1617 * Protocol handler for the EASC protocol. 
    17  *  
     18 * 
    1819 * @package     stubbles 
    1920 * @subpackage  remote_protocol_easc 
     
    3839     * @var  stubBSDSocket 
    3940     */ 
    40     protected $socket; 
    41     /** 
    42      * magic easc protocol number 
    43      */ 
    44     const MAGIC_NUMBER    = 0x3c872747; 
     41    protected $socket;     
    4542    /** 
    4643     * request message type: init 
     
    183180     * @return  mixed 
    184181     */ 
    185     public function invoke($oid, $method, $args) 
     182    public function invoke($oid, $method, array $args) 
    186183    { 
    187184        return $this->sendPacket(self::MSG_CALL, 
    188185                                 pack('NN', 0, $oid), 
    189186                                 array(new stubByteCountedString($method), 
    190                                        new stubByteCountedString($this->serializer->serialize(stubArrayList::with(array($args)))) 
     187                                       new stubByteCountedString($this->serializer->serialize(stubArrayList::with($args))) 
    191188                                 ) 
    192189               ); 
     
    205202    protected function sendPacket($type, $data = '', array $bytes = array()) 
    206203    { 
    207         $bsize = sizeof($bytes); 
    208         // calculate packet length 
     204        $bsize  = sizeof($bytes); 
    209205        $length = strlen($data); 
    210206        for ($i = 0; $i < $bsize; $i++) { 
     
    212208        } 
    213209 
    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); 
    225211        try { 
    226             $this->socket->write($packet); 
     212            $requestHeader->writeToSocket($this->socket, $data); 
     213            $this->socket->write(pack('a*', $data)); 
    227214            for ($i = 0; $i < $bsize; $i++) { 
    228215                $bytes[$i]->writeTo($this->socket); 
    229216            } 
    230217 
    231             $header = unpack('Nmagic/cvmajor/cvminor/ctype/ctran/Nlength', $this->readBytes(12)); 
     218            $responseHeader = stubEASCHeader::readFromSocket($this->socket); 
    232219        } catch (stubConnectionException $ce) { 
    233220            throw new stubRemoteException($ce->getMessage(), $ce); 
    234221        } 
    235222 
    236         if (self::MAGIC_NUMBER != $header['magic']) { 
     223        if (null === $responseHeader) { 
    237224            $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 . '".'); 
    239229        } 
    240230 
     
    242232        $context = array('handler' => $this); 
    243233        try { 
    244             switch ($header['type']) { 
     234            switch ($responseHeader->getMessageType()) { 
    245235                case self::MSG_VALUE: 
    246236                    $data = stubByteCountedString::readFrom($this->socket); 
     
    264254 
    265255                default: 
    266                     $data = $this->readBytes($header['length']); 
     256                    $data = $this->readBytes($responseHeader->getDataLength()); 
    267257                    $this->socket->close(); 
    268258                    throw new stubEASCException('Unknown message type: ' . $data); 
  • labs/incubator/src/test/php/net/stubbles/remote/RemoteTestSuite.php

    r1813 r1817  
    3333         
    3434        // protocol::easc 
     35        $suite->addTestFile($dir . '/protocol/easc/stubEASCHeaderTestCase.php'); 
    3536        $suite->addTestFile($dir . '/protocol/easc/stubEASCSerializerTestCase.php'); 
    3637        $suite->addTestFile($dir . '/protocol/easc/mapping/stubEASCArrayListMappingTestCase.php');