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 / SharedMemory / Sharedance.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  5.0 KB  |  213 lines

  1. <?php
  2. /**
  3. *
  4. * The Sharedance driver for SharedMemory
  5. *
  6. * PHP versions 4 and 5
  7. *
  8. * LICENSE: This source file is subject to version 3.0 of the PHP license
  9. * that is available through the world-wide-web at the following URI:
  10. * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
  11. * the PHP License and are unable to obtain it through the web, please
  12. * send a note to license@php.net so we can mail you a copy immediately.
  13. *
  14. * @category   System
  15. * @package    System_Sharedmemory
  16. * @author     Evgeny Stepanischev <bolk@lixil.ru>
  17. * @copyright  2005 Evgeny Stepanischev
  18. * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
  19. * @version    CVS: $Id:$
  20. * @link       http://pear.php.net/package/System_SharedMemory
  21. */
  22.  
  23. /**
  24. *
  25. * The methods PEAR SharedMemory uses to interact with Sharedance
  26. * for interacting with Sharedance shared memory
  27. *
  28. * These methods overload the ones declared System_SharedMemory_Common
  29. *
  30. * @category   System
  31. * @package    System_Sharedmemory
  32. * @package    System_Sharedmemory
  33. * @author     Evgeny Stepanischev <bolk@lixil.ru>
  34. * @copyright  2005 Evgeny Stepanischev
  35. * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
  36. * @version    CVS: $Id:$
  37. * @link       http://pear.php.net/package/System_SharedMemory
  38. */
  39.  
  40. require_once 'System/SharedMemory/Common.php';
  41.  
  42. // {{{ class System_SharedMemory_Sharedance
  43.  
  44. class System_SharedMemory_Sharedance extends System_SharedMemory_Common
  45. {
  46.     // {{{ properties
  47.     /**
  48.     * true if plugin was connected to backend
  49.     *
  50.     * @var bool
  51.     *
  52.     * @access private
  53.     */
  54.     var $_connected;
  55.  
  56.     /**
  57.     * connection handler
  58.     *
  59.     * @var string
  60.     *
  61.     * @access private
  62.     */
  63.     var $_h;
  64.  
  65.     /**
  66.     * Contains internal options
  67.     *
  68.     * @var string
  69.     *
  70.     * @access private
  71.     */
  72.     var $_options;
  73.     // }}}
  74.  
  75.     // {{{ constructor
  76.  
  77.     /**
  78.      * Constructor. Init all variables.
  79.      *
  80.      * @param array $options
  81.      *
  82.      * @access public
  83.      */
  84.     function System_SharedMemory_Sharedance($options)
  85.     {
  86.         $this->_options = ($this->_default($options, array
  87.         (
  88.             'host' => '127.0.0.1',
  89.             'port' => 1042,
  90.             'timeout' => 10,
  91.         )));
  92.  
  93.         $this->_h = null;
  94.         $this->_open();
  95.     }
  96.     // }}}
  97.     // {{{ isConnected()
  98.  
  99.     /**
  100.      * returns true if plugin was 
  101.      * successfully connected to backend
  102.      *
  103.      * @return bool true if connected
  104.      * @access public
  105.      */
  106.     function isConnected()
  107.     {
  108.         return $this->_connected;
  109.     }
  110.     // }}}
  111.     // {{{ get()
  112.  
  113.     /**
  114.      * returns value of variable in shared mem
  115.      *
  116.      * @param string $name name of variable
  117.      *
  118.      * @return mixed value of the variable
  119.      * @access public
  120.      */
  121.      function get($name)
  122.      {
  123.          $this->_open();
  124.          $s = 'F' . pack('N', strlen($name)) . $name;
  125.          fwrite($this->_h, $s);
  126.  
  127.          for ($data = ''; !feof($this->_h);) {
  128.              $data .= fread($this->_h, 4096);
  129.          }
  130.  
  131.          $this->_close();
  132.  
  133.          return $data === '' ? null : unserialize($data);
  134.      }
  135.      // }}}
  136.      // {{{ set()
  137.  
  138.     /**
  139.      * set value of variable in shared mem
  140.      *
  141.      * @param string $name  name of the variable
  142.      * @param string $value value of the variable
  143.      * @param int $ttl (optional) time to life of the variable
  144.      *
  145.      * @return bool true on success
  146.      * @access public
  147.      */
  148.     function set($name, $value)
  149.     {
  150.         $this->_open();
  151.         $value = serialize($value);
  152.         $s = 'S' . pack('NN', strlen($name), strlen($value)) . $name . $value;
  153.  
  154.         fwrite($this->_h, $s);
  155.         $ret = fgets($this->_h);
  156.         $this->_close();
  157.  
  158.         return $ret === "OK\n";
  159.     }
  160.     // }}}
  161.     // {{{ rm()
  162.  
  163.     /**
  164.      * remove variable from memory
  165.      *
  166.      * @param string $name  name of the variable
  167.      *
  168.      * @return bool true on success
  169.      * @access public
  170.      */
  171.      function rm($name)
  172.      {
  173.          $this->_open();
  174.          $s = 'D' . pack('N', strlen($name)) . $name;
  175.          fwrite($this->_h, $s);
  176.          $ret = fgets($this->_h);
  177.          $this->_close();
  178.  
  179.          return $ret === "OK\n";
  180.      }
  181.      // }}}
  182.      // {{{ _close()
  183.  
  184.      /**
  185.      * close connection to backend
  186.      * (sharedance isn't support persistent connection)
  187.      *
  188.      * @access private
  189.      */
  190.      function _close()
  191.      {
  192.          fclose($this->_h);
  193.          $this->_h = false;
  194.      }
  195.      // }}}
  196.      // {{{ _open()
  197.  
  198.      /**
  199.      * open connection to backend if it doesn't connected yet
  200.      *
  201.      * @access private
  202.      */
  203.      function _open()
  204.      {
  205.          if (!is_resource($this->_h)) {
  206.              $this->_h = fsockopen($this->_options['host'], $this->_options['port'], $_, $_, $this->_options['timeout']);
  207.              $this->_connected = is_resource($this->_h);         
  208.          }
  209.      }
  210.      // }}}
  211. }
  212. // }}}
  213. ?>