home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / System / Socket / ConnectionPool.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  5.6 KB  |  214 lines

  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PEAR :: System :: Socket :: ConnectionPool                           |
  4. // +----------------------------------------------------------------------+
  5. // | This source file is subject to version 3.0 of the PHP license,       |
  6. // | that is available at http://www.php.net/license/3_0.txt              |
  7. // | If you did not receive a copy of the PHP license and are unable      |
  8. // | to obtain it through the world-wide-web, please send a note to       |
  9. // | license@php.net so we can mail you a copy immediately.               |
  10. // +----------------------------------------------------------------------+
  11. // | Copyright (c) 2004 Michael Wallner <mike@iworks.at>                  |
  12. // +----------------------------------------------------------------------+
  13. //
  14. // $Id: ConnectionPool.php,v 1.1 2004/04/29 15:55:38 mike Exp $
  15.  
  16. /**
  17. * System::Socket::ConnectionPool
  18. * @author       Michael Wallner <mike@php.net>
  19. * @package      System_Socket
  20. * @category     System
  21. */
  22.  
  23. if (!defined('SYSTEM_SOCKET_ROOT')) {
  24.     define('SYSTEM_SCKET_ROOT', dirname(__FILE__) . '/..');
  25. }
  26. /**
  27. * System_Socket_ConnectionPool
  28. *
  29. * @author   Michael Wallner <mike@php.net>
  30. * @version  $Revision: 1.1 $
  31. * @access   public
  32. */
  33. class System_Socket_ConnectionPool
  34. {
  35.     /**
  36.     * Socket pool
  37.     * 
  38.     * @access   protected
  39.     * @var      array
  40.     */
  41.     var $pool = array();
  42.     
  43.     /**
  44.     * Constructor
  45.     * 
  46.     * Instantiate a new System_Socket_ConnectionPool object and add specified
  47.     * array of socket connections to the pool.  Actually the method 
  48.     * System_Socket_ConnectionPool::push() gets called for each array element.
  49.     *
  50.     * @access   public
  51.     * @return   object  System_Socket_ConnectionPool
  52.     * @param    array   $sockets    reference to an array of socket connections
  53.     */
  54.     function System_Socket_ConnectionPool(&$sockets)
  55.     {
  56.         System_Socket_ConnectionPool::__construct($sockets);
  57.     }
  58.     
  59.     /**
  60.     * ZE2 Constructor
  61.     * @ignore
  62.     */
  63.     function __construct(&$sockets)
  64.     {
  65.         foreach (array_keys($sockets) as $key){
  66.             $this->push($sockets[$key]);
  67.         }
  68.     }
  69.     
  70.     /**
  71.     * Push a socket connection
  72.     *
  73.     * Add a System_Socket_Connection object to the pool.
  74.     * You can push one of the following values:
  75.     *   * a valid socket resource handle
  76.     *   * a System_Socket_Connection object
  77.     *   * a System_Socket object
  78.     * 
  79.     * 
  80.     * @access   public
  81.     * @return   bool
  82.     * @param    mixed   $s  resource handle or System_Socket[_Connection] object
  83.     */
  84.     function push(&$s)
  85.     {
  86.         if (is_object($s)) {
  87.             if (is_a($s, 'System_Socket_Connection')) {
  88.                 $this->pool[$s->getID()] = &$s;
  89.             } elseif (is_a($s, 'System_Socket')) {
  90.                 $conn = &new System_Socket_Connection($s);
  91.                 $this->pool[$conn->getID()] = &$conn;
  92.             } else {
  93.                 return false;
  94.             }
  95.         } elseif (System_Socket::isResource($s)) {
  96.             $conn =  &new System_Socket_Connection(new System_Socket($s));
  97.             $this->pool[$conn->getID()] = &$conn;
  98.         } else {
  99.             return false;
  100.         }
  101.         return true;
  102.     }
  103.     
  104.     /**
  105.     * Shift a socket connection
  106.     * 
  107.     * Retrieve a System_Socket_Connection object from the pool.
  108.     * Note that the System_Socket_ConnecionPool object will no 
  109.     * longer contain the shifted connection.
  110.     *
  111.     * @access   public
  112.     * @return   object  System_Socket_Connection
  113.     */
  114.     function &shift()
  115.     {
  116.         return count($this->pool) ? array_shift($this->pool) : null;
  117.     }
  118.     
  119.     /** 
  120.     * Get all connections
  121.     * 
  122.     * This method returns a refernce to the connection pool array.
  123.     * 
  124.     * @access   public
  125.     * @return   array   connection pool
  126.     */
  127.     function &getConnections()
  128.     {
  129.         return $this->pool;
  130.     }
  131.     
  132.     /**
  133.     * Close all connections of this pool
  134.     * 
  135.     * Each socket resource handle will be closed and all 
  136.     * System_Socket_Connection objects of this pool
  137.     * will be unset.
  138.     * 
  139.     * @access   public
  140.     * @return   void
  141.     */
  142.     function close()
  143.     {
  144.         while ($conn = &$this->shift()) {
  145.             $conn->close();
  146.             unset($conn);
  147.         }
  148.     }
  149.     
  150.     /** 
  151.     * Count connections
  152.     * 
  153.     * @access   public
  154.     * @return   int
  155.     */
  156.     function count()
  157.     {
  158.         return count($this->pool);
  159.     }
  160.     
  161.     /** 
  162.     * Write to all connections
  163.     * 
  164.     * @access   public
  165.     * @return   array
  166.     * @param    string  $data
  167.     */
  168.     function write($data)
  169.     {
  170.         $result = array();
  171.         foreach (array_keys($this->pool) as $ID){
  172.             $result[$ID] = $this->pool[$ID]->write($data);
  173.         }
  174.         return $result;
  175.     }
  176.     
  177.     /** 
  178.     * Read from all connections
  179.     * 
  180.     * @access   public
  181.     * @return   array
  182.     * @param    int     $bytes
  183.     */
  184.     function read($bytes = 4096)
  185.     {
  186.         $result = array();
  187.         foreach (array_keys($this->pool) as $ID){
  188.             $result[$ID] = $this->pool[$ID]->read($bytes);
  189.         }
  190.         return $result;
  191.     }
  192.     
  193.     /** 
  194.     * Call a method by name of all connections
  195.     * 
  196.     * @access   public
  197.     * @return   array
  198.     * @param    string  $method
  199.     * @param    mixed   [$arg[, $arg[, ...]]]
  200.     */
  201.     function call($method)
  202.     {
  203.         $params = array_splice(func_get_args(), 1);
  204.         $result = array();
  205.         foreach (array_keys($this->pool) as $ID){
  206.             $result[$ID] = call_user_method_array(
  207.                 $method, $this->pool[$ID], $params);
  208.         }
  209.         return $result;
  210.     }
  211.     
  212. }
  213. ?>