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 / Manager.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  5.9 KB  |  240 lines

  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PEAR :: System :: Socket :: Manager                                  |
  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: Manager.php,v 1.1 2004/04/29 15:55:38 mike Exp $
  15.  
  16. /**
  17. * System::Socket::Manager
  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_SOCKET_ROOT', dirname(__FILE__) . '/..');
  25. }
  26.  
  27. require_once 'PEAR.php';
  28. require_once SYSTEM_SOCKET_ROOT . '/Socket.php';
  29.  
  30.  
  31. /** 
  32. * System_Socket_Manager
  33. * Parent class for System_Socket_Connection and Systrem_Socket_Listener which
  34. * performs some basic tasks, like handling observers, attaching a debugger
  35. * automagically and controlling the base socket resource handle and object.
  36. *
  37. * @author   Michael Wallner <mike@php.net>
  38. * @version  $Revision: 1.1 $
  39. * @access   public
  40. */
  41. class System_Socket_Manager extends PEAR
  42. {
  43.     /**
  44.     * System_Socket object
  45.     * @access protected
  46.     */
  47.     var $Socket = null;
  48.     
  49.     /**
  50.     * Whether we hold a System_Socket object
  51.     * @access proteceted
  52.     */
  53.     var $hasSocket = false;
  54.     
  55.     /**
  56.     * Observer objects
  57.     * @access protected
  58.     */
  59.     var $observers = array();
  60.     
  61.     /**
  62.     * Constructor
  63.     * 
  64.     * @access   protected
  65.     * @return   object  System_Socket_Manager
  66.     * @param    objcet  System_Socket
  67.     */
  68.     function System_Socket_Manager(&$socket)
  69.     {
  70.         System_Socket_Manager::__construct($socket);
  71.     }
  72.     
  73.     /**
  74.     * ZE2 Constructor
  75.     * @ignore
  76.     */
  77.     function __construct(&$socket)
  78.     {
  79.         if ($this->hasSocket = (is_a($socket, 'System_Socket') &&
  80.             $socket->hasResource())) {
  81.             $this->Socket = &$socket;
  82.         }
  83.         
  84.         if (defined('SYSTEM_SOCKET_DEBUG') && SYSTEM_SOCKET_DEBUG) {
  85.             include_once SYSTEM_SOCKET_ROOT . '/Socket/Debugger.php';
  86.             $this->attach(new System_Socket_Debugger(SYSTEM_SOCKET_DEBUG));
  87.         }
  88.     }
  89.     
  90.     /**
  91.     * Destructor
  92.     * @ignore
  93.     */
  94.     function _System_Socket_Manager()
  95.     {
  96.         $this->__destruct();
  97.     }
  98.     
  99.     /**
  100.     * ZE2 Destructor
  101.     * @ignore
  102.     */
  103.     function __destruct()
  104.     {
  105.         $this->close();
  106.         $this->notify('__destruct', true);
  107.     }
  108.     
  109.     /**
  110.     * Close Socket
  111.     * 
  112.     * Close the underlying socket resource
  113.     * 
  114.     * @access   public
  115.     * @return   void
  116.     */
  117.     function close()
  118.     {
  119.         $this->hasSocket = false;
  120.         $this->Socket->close();
  121.         $this->notify('close', true);
  122.     }
  123.     
  124.     /** 
  125.     * Whether we have a socket
  126.     * 
  127.     * @access   public
  128.     * @return   bool
  129.     */
  130.     function hasSocket()
  131.     {
  132.         return $this->hasSocket;
  133.     }
  134.     
  135.     /**
  136.     * Close Socket
  137.     * 
  138.     * Close a socket resource or a System_Socket or System_Socket_Manager object
  139.     * 
  140.     * @access   public
  141.     * @return   void
  142.     * @param    mixed   $socket
  143.     */
  144.     function closeSocket(&$socket)
  145.     {
  146.         if (System_Socket::isResource($socket)) {
  147.             socket_close($socket);
  148.             $this->notify(__FUNCTION__, true, get_resource_type($socket));
  149.         } elseif (is_object($socket) && method_exists($socket, 'close')) {
  150.             $socket->close();
  151.             $this->notify(__FUNCTION__, true, get_class($socket));
  152.         } else {
  153.             $this->notify(__FUNCTION__, false, $socket);
  154.         }
  155.         $socket = null;
  156.     }
  157.  
  158.     /**
  159.     * Attach observer
  160.     * 
  161.     * @access   public
  162.     * @return   bool
  163.     * @param    object  System_Socket_Observer  $observer
  164.     */
  165.     function attach(&$observer)
  166.     {
  167.         if (is_a($observer, 'System_Socket_Observer')) {
  168.             $this->observers[$observer->getHashCode()] = &$observer;
  169.             return true;
  170.         }
  171.         return false;
  172.     }
  173.     
  174.     /**
  175.     * Detach observer
  176.     * 
  177.     * @access   public
  178.     * @return   void
  179.     * @param    object  System_Socket_Observer $observer
  180.     */
  181.     function detach(&$observer)
  182.     {
  183.         unset($this->observers[$observer->getHashCode()]);
  184.     }
  185.     
  186.     /** 
  187.     * Check if a specific observer is already attached
  188.     * 
  189.     * @access   public
  190.     * @return   bool
  191.     * @param    object  System_Socket_Observer  $observer
  192.     */
  193.     function attached(&$observer)
  194.     {
  195.         return isset($this->observers[$observer->getHashCode()]);
  196.     }
  197.     
  198.     /**
  199.     * Notify observers
  200.     * 
  201.     * @access   protected
  202.     * @return   mixed   the supplied result
  203.     * @param    string  $event
  204.     * @param    mixed   $result
  205.     * @param    mixed   $arg [, $arg [, ...]]
  206.     */
  207.     function notify($event, $result)
  208.     {
  209.         static $class;
  210.         
  211.         if (!isset($class)) {
  212.             $class = get_class($this);
  213.         }
  214.         
  215.         if ($c = count($this->observers)) {
  216.             $args = func_get_args();
  217.             $note = array(
  218.                 0       => $event,
  219.                 'event' => $event,
  220.                 1       => $class,
  221.                 'class' => $class,
  222.                 2       => $result,
  223.                 'result'=> $result,
  224.                 3       => $args = array_splice($args, 2),
  225.                 'args'  => $args,
  226.             );
  227.  
  228.             reset($this->observers);
  229.             while (list($hashCode) = each($this->observers)) {
  230.                 $this->observers[$hashCode]->notify($note);
  231.             }
  232.         }
  233.  
  234.         return $result;
  235.     }
  236. }
  237. ?>
  238.