Changeset 1716

Show
Ignore:
Timestamp:
07/20/08 16:10:24 (1 month ago)
Author:
mikey
Message:

finish remote protocol handler and pool

Files:

Legend:

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

    r1695 r1716  
    88 */ 
    99stubClassLoader::load('net::stubbles::remote::protocol::stubProtocolHandler', 
    10                       'net::stubbles::util::net::stubBSDSocket' 
     10                      'net::stubbles::peer::stubBSDSocket' 
    1111); 
    1212/** 
     
    101101     * @throws  stubRemoteException 
    102102     */ 
    103     public function init(stubURL $url) 
     103    public function initialize(stubURL $url) 
    104104    { 
    105105        sscanf($url->getParam('version', '1.0'), 
  • labs/incubator/src/main/php/net/stubbles/remote/protocol/stubProtocolHandler.php

    r1695 r1716  
    66 * @package     stubbles 
    77 * @subpackage  remote_protocol 
     8 * @version     $Id$ 
    89 */ 
    9 stubClassLoader::load('net::stubbles::util::net::stubURL'); 
     10stubClassLoader::load('net::stubbles::peer::stubURL'); 
    1011/** 
    1112 * Interface for a protocol handler. 
     
    2223     * @throws  stubRemoteException 
    2324     */ 
    24     public function init(stubURL $url); 
     25    public function initialize(stubURL $url); 
    2526 
    2627    /** 
  • labs/incubator/src/main/php/net/stubbles/remote/protocol/stubProtocolHandlerFactory.php

    r1695 r1716  
    11<?php 
    22/** 
    3  * Factory for creating protocol handlers
     3 * Factory for creating protocol handlers and managing protocol handler classes
    44 *  
    55 * @author      Frank Kleine <mikey@stubbles.net> 
    66 * @package     stubbles 
    77 * @subpackage  remote_protocol 
     8 * @version     $Id$ 
    89 */ 
    9 stubClassLoader::load('net::stubbles::remote::protocol::stubProtocolHandler', 
     10stubClassLoader::load('net::stubbles::lang::exceptions::stubMethodNotSupportedException', 
     11                      'net::stubbles::remote::protocol::stubProtocolHandler', 
    1012                      'net::stubbles::remote::protocol::stubUnknownProtocolException' 
    1113); 
    1214/** 
    13  * Factory for creating protocol handlers
     15 * Factory for creating protocol handlers and managing protocol handler classes
    1416 *  
    1517 * @package     stubbles 
     
    3436     * static initializing 
    3537     */ 
     38    // @codeCoverageIgnoreStart 
    3639    public static function __static() 
    3740    { 
     
    3942        self::$instance->setHandler('easc', 'net::stubbles::remote::protocol::easc::stubEASCProtocolHandler'); 
    4043    } 
     44    // @codeCoverageIgnoreEnd 
    4145 
    4246    /** 
     
    5054    /** 
    5155     * prevent cloning 
     56     * 
     57     * @throws  stubMethodNotSupportedExceptions 
    5258     */ 
    53     private function __clone() 
     59    public function __clone() 
    5460    { 
    55         // intentionally empty 
     61        throw new stubMethodNotSupportedException('Cloning the protocol handler factory is not supported.'); 
    5662    } 
    5763 
     
    6773 
    6874    /** 
     75     * clears the protocol list 
     76     */ 
     77    public function clear() 
     78    { 
     79        $this->handlers = array(); 
     80        $this->setHandler('easc', 'net::stubbles::remote::protocol::easc::stubEASCProtocolHandler'); 
     81    } 
     82 
     83    /** 
    6984     * sets a protocol handler class for a given protocol 
    7085     * 
    71      * @param  string  $protocol                  the protocol to register the class for 
    72      * @param  string  $protocolHandlerClassName  the name of the handler class 
     86     * @param   string                      $protocol                  the protocol to register the class for 
     87     * @param   string                      $protocolHandlerClassName  the name of the handler class 
     88     * @return  stubProtocolHandlerFactory 
    7389     */ 
    7490    public function setHandler($protocol, $protocolHandlerClassName) 
    7591    { 
    7692        $this->handlers[$protocol] = $protocolHandlerClassName; 
     93        return $this; 
     94    } 
     95 
     96    /** 
     97     * checks if a handler for a certain protocol is registered 
     98     * 
     99     * @param   string  $protocol 
     100     * @return  bool 
     101     */ 
     102    public function hasHandler($protocol) 
     103    { 
     104        return isset($this->handlers[$protocol]); 
    77105    } 
    78106 
     
    82110     * @param  string  $protocol 
    83111     */ 
    84     public function getHandler($protocol) 
     112    public function createHandler($protocol) 
    85113    { 
    86114        if (isset($this->handlers[$protocol]) === false) { 
  • labs/incubator/src/main/php/net/stubbles/remote/protocol/stubProtocolHandlerPool.php

    r1695 r1716  
    11<?php 
    22/** 
    3  * Factory for creating protocol handlers
     3 * Factory for creating protocol handlers and managing protocol handler instances
    44 *  
    55 * @author      Frank Kleine <mikey@stubbles.net> 
     
    88 */ 
    99stubClassLoader::load('net::stubbles::lang::exceptions::stubIllegalArgumentException', 
     10                      'net::stubbles::lang::exceptions::stubMethodNotSupportedException', 
    1011                      'net::stubbles::remote::protocol::stubProtocolHandler', 
    1112                      'net::stubbles::remote::protocol::stubProtocolHandlerFactory', 
    12                       'net::stubbles::util::net::stubURL' 
     13                      'net::stubbles::peer::stubURL' 
    1314); 
    1415/** 
    15  * Factory for creating protocol handlers
     16 * Factory for creating protocol handlers and managing protocol handler instances
    1617 *  
    1718 * @package     stubbles 
     
    2728    protected static $instance; 
    2829    /** 
     30     * protocol handler factory to be used 
     31     * 
     32     * @var  stubProtocolHandlerFactory 
     33     */ 
     34    protected $protocolHandlerFactory; 
     35    /** 
    2936     * the pool of protocol handlers 
    3037     * 
    3138     * @var  array<string,stubProtocolHandler> 
    3239     */ 
    33     protected $pool            = array(); 
     40    protected $handlers               = array(); 
    3441 
    3542    /** 
     
    4350    /** 
    4451     * prevent cloning 
     52     * 
     53     * @throws  stubMethodNotSupportedExceptions 
    4554     */ 
    46     private function __clone() 
     55    public function __clone() 
    4756    { 
    48         // intentionally empty 
     57        throw new stubMethodNotSupportedException('Cloning the protocol handler pool is not supported.'); 
    4958    } 
    5059 
     
    5665    public function getInstance() 
    5766    { 
    58         if (isset(self::$instance) == false) { 
     67        if (null === self::$instance) { 
    5968            self::$instance = new self(); 
     69            self::$instance->protocolHandlerFactory = stubProtocolHandlerFactory::getInstance(); 
    6070        } 
    6171         
     
    6474 
    6575    /** 
    66      * pool a handler 
     76     * sets the protocol handler factory to be used 
    6777     * 
    68      * @param   string               $url 
    69      * @param   stubProtocolHandler  $handler  handler to pool 
     78     * @param  stubProtocolHandlerFactory  $protocolHandlerFactory 
     79     */ 
     80    public function setProtocolHandlerFactory(stubProtocolHandlerFactory $protocolHandlerFactory) 
     81    { 
     82        $this->protocolHandlerFactory = $protocolHandlerFactory; 
     83    } 
     84 
     85    /** 
     86     * clears all handlers from the pool 
     87     */ 
     88    public function clear() 
     89    { 
     90        $this->handlers = array(); 
     91    } 
     92 
     93    /** 
     94     * adds a handler to the pool 
     95     * 
     96     * @param   string               $url      url to add handler for, this should be protocol and hostname 
     97     * @param   stubProtocolHandler  $handler  handler to add to the pool 
    7098     * @return  stubProtocolHandler  the pooled handler 
    7199     */ 
    72     public function pool($url, stubProtocolHandler $handler) 
     100    public function addHandler($url, stubProtocolHandler $handler) 
    73101    { 
    74         $this->pool[$url] = $handler; 
     102        $this->handlers[$url] = $handler; 
    75103        return $handler; 
     104    } 
     105 
     106    /** 
     107     * checks whether a handler is available for given url 
     108     * 
     109     * @param   string  $url 
     110     * @return  bool 
     111     * @throws  stubIllegalArgumentException 
     112     */ 
     113    public function hasHandler($url) 
     114    { 
     115        if (is_string($url) === true) { 
     116            $url = stubURL::fromString($url); 
     117        } elseif (($url instanceof stubURL) === false) { 
     118            throw new stubIllegalArgumentException('Param url must be an instance of net::stubbles::peer::stubURL or a string denoting an URL.'); 
     119        } 
     120         
     121        $key = $url->getScheme() . '://' . $url->getHost(); 
     122        if (isset($this->handlers[$key]) === true) { 
     123            return true; 
     124        } 
     125         
     126        return $this->protocolHandlerFactory->hasHandler($url->getScheme()); 
    76127    } 
    77128 
     
    79130     * acquire a handler 
    80131     * 
    81      * @param   string  key 
     132     * @param   string                        $url         url to aquire a handler for          
     133     * @param   bool                          $initialize  optional  if handler should be initialized 
    82134     * @return  stubProtocolHandler 
    83      * @throws  stubMalformedURLException 
     135     * @throws  stubIllegalArgumentException 
    84136     * @throws  stubUnknownProtocolException 
    85137     */ 
    86     public function acquire($key, $initialize = false) 
     138    public function getHandler($url, $initialize = false) 
    87139    { 
    88         $url = stubURL::fromString($key); 
    89         $key = $url->getScheme() .'://' . $url->getHost(); 
    90         if (isset($this->pool[$key]) === true) { 
    91             $handler = $this->pool[$key]; 
     140        if (is_string($url) === true) { 
     141            $url = stubURL::fromString($url); 
     142        } elseif (($url instanceof stubURL) === false) { 
     143            throw new stubIllegalArgumentException('Param url must be an instance of net::stubbles::peer::stubURL or a string denoting an URL.'); 
     144        } 
     145         
     146        $key = $url->getScheme() . '://' . $url->getHost(); 
     147        if (isset($this->handlers[$key]) === true) { 
     148            $handler = $this->handlers[$key]; 
    92149        } else { 
    93             $handler = $this->pool($key, stubProtocolHandlerFactory::getInstance()->getHandler($url->getScheme())); 
     150            $handler = $this->addHandler($key, $this->protocolHandlerFactory->createHandler($url->getScheme())); 
    94151        } 
    95152         
  • labs/incubator/src/test/AllTests.php

    r1688 r1716  
    2323require_once TEST_SRC_PATH . '/php/net/stubbles/auth/AuthTestSuite.php'; 
    2424require_once TEST_SRC_PATH . '/php/net/stubbles/events/EventTestSuite.php'; 
     25require_once TEST_SRC_PATH . '/php/net/stubbles/remote/RemoteTestSuite.php'; 
    2526/** 
    2627 * Class to organize all tests. 
     
    4950        $suite->addTestSuite('AuthTestSuite'); 
    5051        $suite->addTestSuite('EventTestSuite'); 
     52        $suite->addTestSuite('RemoteTestSuite'); 
    5153        return $suite; 
    5254    }