cvs: pear /IO_Async Async.php package.xml
| From: | Stig Bakken | Date: | Sun, 26 Aug 2001 16:13:09 +0000 |
| Subject: | cvs: pear /IO_Async Async.php package.xml | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-664@lists.php.net to get a copy of this message | ||
ssb Sun Aug 26 12:13:09 2001 EDT
Added files:
/pear/IO_Async Async.php package.xml
Log:
* initial commit of IO_Async
Index: pear/IO_Async/Async.php +++ pear/IO_Async/Async.php <?php /* vim: set expandtab tabstop=4 shiftwidth=4: */ // +----------------------------------------------------------------------+ // | PHP version 4.0 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997, 1998, 1999, 2000, 2001 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 2.0 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/2_02.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Authors: Stig Sæther Bakken <ssb@fast.no> | // | | // +----------------------------------------------------------------------+ // // $Id: Async.php,v 1.1 2001/08/26 16:13:08 ssb Exp $ // TODO: // - it's not exactly IO until we can O, today we only I :-) // require_once "PEAR.php"; // These are set to strings instead of integers in case we ever // need to open up for user-defined modes. define('IO_ASYNC_BUFFER_ALL', 'BufferAll'); define('IO_ASYNC_CALLBACK_BLOCK', 'CallbackBlock'); define('IO_ASYNC_CALLBACK_LINE', 'CallbackLine'); define('IO_ASYNC_ERROR_BAD_PARAM', 1); define('IO_ASYNC_ERROR_NO_SOCKET', 2); define('IO_ASYNC_ERROR_NYI', 3); // Not Yet Implemented /** * Backgrounded asynchronous socket IO implemented using ticks. * * This class implements functions for doing backgrounded socket IO. * There are three modes of operation: * * IO_ASYNC_BUFFER_ALL: this mode simply reads all the results into * memory and waits for you to retrieve it via the fetchBuffer() * method. * * IO_ASYNC_CALLBACK_BLOCK: this mode reads a block at a time and * invokes a callback function after each. * * IO_ASYNC_CALLBACK_LINE: this mode reads a line at a time and * invokes a callback function after each. * * Callback functions are given two parameters: the IO_Async object * and the data just read (string). * */ class IO_Async extends PEAR { /** * Socket file descriptor. * @var resource */ var $_fp; /** * Buffer for data read from socket. * @var string */ var $_buffer; /** * Maximum size of _buffer. If set to -1, there are no buffer * size constraints. * @var int */ var $_max_buffer_size; /** * Size of buffer to use for individual read/write operations. * @var int */ var $_buffer_size; /** * This object's mode of operation. * @var string */ var $_mode; /** * Callback function or (object,method) tuple for line/block * callbacks. * @var mixed */ var $_callback; /** * TCP connect timeout in number of seconds * @var double */ var $_connect_timeout; /** * IO_Async constructor. * * @param array $options Associative array of configuration * options. Recognized keys are: * * 'mode' (int) Mode of operation, determines what IO_Async does * after reading one block or line of data. Legal * values are IO_ASYNC_BUFFER_ALL (default), * IO_ASYNC_CALLBACK_BLOCK and IO_ASYNC_CALLBACK_LINE. * * 'callback' (mixed) For IO_ASYNC_CALLBACK_* mode objects, this is * the function name or (object,method name) tuple * used for processing incoming data. Callbacks * are called with two parameters: the IO_Async * object and a string with the just-read data. * * 'bufsize' (int) Size of buffer used for individual reads/writes. * Defaults to 2048 bytes. * * 'maxbuffer' (int) For IO_ASYNC_BUFFER_ALL, this is the maximum * number of bytes that will be stored, or -1 for no * limit. Defaults to -1. * * 'timeout' (double) TCP connect timeout in number of * seconds. The openFile method does not support * connect timeouts. * * @access public */ function IO_Async($options = array()) { extract($options); $this->PEAR(); if (!isset($mode)) { $mode = IO_ASYNC_BUFFER_ALL; } if (!isset($buffer_size)) { if ($mode == IO_ASYNC_BUFFER_ALL) { $buffer_size = -1; } else { $buffer_size = 2048; } } $this->_fp = null; $this->_buffer = ''; $this->_mode = $mode; } /** * Register an already opened socket for backgrounded operation * and put it in non-blocking mode. Also sets up a tick handler, * data will be read from the socket as soon as some PHP code is * declared with ticks. * * @param resource $socket a socket resource * * @return mixed True on success, or a PEAR error on failure. The * possible error codes returned by this method are: * * IO_ASYNC_ERROR_BAD_PARAM : bad parameter, either the object * already has a socket registered, or the parameter was not a * resource. * * @access public * */ function registerSocket($socket) { if (is_resource($this->_fp)) { return $this->raiseError('IO_Async::registerSocket: already have a file descriptor', IO_ASYNC_ERROR_BAD_PARAM); } if (!is_resource($socket)) { return $this->raiseError('IO_Async::registerSocket: parameter not a resource', IO_ASYNC_ERROR_BAD_PARAM); } $this->_fp = $socket; $this->_buffer = ''; socket_set_blocking($this->_fp, false); register_tick_function('_IO_Async_tick_handler', &$this); return true; } /** * Open a file (or URL if fopen url wrappers are enabled). If you * use this method to open an HTTP URL, the headers will be read * during this call. If the HTTP request takes some time to * process before you get any headers, use the openHttp*() methods * instead. * * @param string $file file name or URL to open * * @return mixed True on success, a PEAR error on failure. The * error codes that may be returned by this method are: * IO_ASYNC_ERROR_BAD_PARAM. * * @access public */ function openFile($file) { $fp = @fopen($file, "r"); if (!is_resource($fp)) { return $this->raiseError("IO_Async::openFile: $php_errormsg", IO_ASYNC_ERROR_BAD_PARAM); } return $this->registerSocket($socket); } /** * Open a socket to an HTTP server and send an HTTP GET or POST * request. This method does not read headers, the returned * output will contain the HTTP response header as well as the * contents. * * @param string $host host name or IP number of host to contact * * @param int $port port number (default 80) * * @param string $path what path/uri to request (default '/') * * @param string $method which HTTP method to use, 'GET' or 'POST' * * @param mixed $post_vars if $method is 'POST', this parameter * is for giving post data, for example url-encoded form data for * default post requests, or XML payload for XML-RPC requests. * If this parameter is an array, it is treated as a list of * variables that will be url-encoded and sent as the post body. * Ignored for GET requests. * * @param string $mime_type what Content-type: header to send in * POST requests (default 'application/x-www-form-urlencoded'). * Ignored for GET requests. * * @return mixed True on success, a PEAR error on failure. The * error codes that may be returned are: * IO_ASYNC_ERROR_BAD_PARAM. * * @access public */ function openHttp($host, $port = 80, $path = '/', $method = 'GET', $post_data = '', $mime_type = 'application/x-www-form-urlencoded') { $en = $es = ''; $fp = fsockopen($host, $port, $en, $es); if (!is_resource($fp)) { return $this->raiseError("IO_Async::openHttp: $es (code $en)", IO_ASYNC_ERROR_BAD_PARAM); } switch (strtoupper($method)) { case 'GET': { fwrite($fp, "GET $path HTTP/1.0\r\nHost: $host:$port\r\n\r\n"); break; } case 'POST': { if (is_array($post_data) && $mime_type == 'application/x-www-form-urlencoded') { $post_data = $this->_uriEncodeArray($post_data); } fwrite($fp, "POST $path HTTP/1.0\r\nHost: $host:$port\r\n". "Content-type: $mime_type\r\n\r\n{$post_data}"); break; } default: { return $this->raiseError('IO_Async::openHttp: invalid method', IO_ASYNC_ERROR_BAD_PARAM); } } return $this->registerSocket($socket); } function readEvent() { if (!is_resource($this->_fp)) { return $this->raiseError("IO_Async::readLine: no socket opened", IO_ASYNC_ERROR_NO_SOCKET); } switch ($this->_mode) { case IO_ASYNC_CALLBACK_LINE: { $line = ''; do { $line .= fgets($this->_fp, $this->_buffer_size); } while (substr($line, -1) != "\n"); $this->_runCallback(&$line); break; } case IO_ASYNC_CALLBACK_BLOCK: { $data = fread($this->_fp, $this->_buffer_size); $this->_runCallback(&$data); break; } case IO_ASYNC_BUFFER_ALL: { $this->_buffer .= fread($this->_fp, $this->_buffer_size); break; } } /** * Set the socket read buffer size. You can only increase the buffer * size. * * @param int $new_size the new buffer size * * @return bool true if the buffer size was changed, false if you * specified a smaller buffer * * @access public */ function setBufferSize($new_size) { if ($new_size > $this->_buffer_size) { $this->_buffer_size = $new_size; return true; } return false; } function _runCallback(&$data) { if (function_exists($this->_callback)) { call_user_func($this->_callback, &$this, $data); } } function readEvent() { switch ($this->_mode) { case IO_ASYNC_BUFFER_ALL: { break; } case IO_ASYNC_CALLBACK_BLOCK: { break; } case IO_ASYNC_CALLBACK_LINE: { break; } } } } /** * This is the tick handling function. Internal use only. * * @param object $tobj an IO_Async object * * @return void * * @access private */ function _IO_Async_tick_handler(&$obj) { $obj->readEvent(); } // Local Variables: // indent-tabs-mode: nil // c-basic-offset: 4 // tab-width: 4 // End: ?> Index: pear/IO_Async/package.xml +++ pear/IO_Async/package.xml <?xml version="1.0" encoding="ISO-8859-1" ?> <Package Version="1.0"> <Name>IO_Async</Name> <Summary> Backgrounded asynchronous socket IO implemented with PHP ticks </Summary> <Description> This package provides a class for doing backgrounded asynchronous socket operations. The backgrounding is implemented using ticks. You can use IO_Async to download data from multiple sources while your script is processing other code. </Description> <Maintainer> <Initials>SSB</Initials> <Name>Stig Sæther Bakken</Name> <Email>stig@php.net</Email> </Maintainer> <FileList> <Dir Name="/" BaseInstallDir="IO"> <File Role="php">Async.php</File> </Dir> </FileList> </Package>
Index: pear/IO_Async/Async.php +++ pear/IO_Async/Async.php <?php /* vim: set expandtab tabstop=4 shiftwidth=4: */ // +----------------------------------------------------------------------+ // | PHP version 4.0 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997, 1998, 1999, 2000, 2001 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 2.0 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/2_02.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Authors: Stig Sæther Bakken <ssb@fast.no> | // | | // +----------------------------------------------------------------------+ // // $Id: Async.php,v 1.1 2001/08/26 16:13:08 ssb Exp $ // TODO: // - it's not exactly IO until we can O, today we only I :-) // require_once "PEAR.php"; // These are set to strings instead of integers in case we ever // need to open up for user-defined modes. define('IO_ASYNC_BUFFER_ALL', 'BufferAll'); define('IO_ASYNC_CALLBACK_BLOCK', 'CallbackBlock'); define('IO_ASYNC_CALLBACK_LINE', 'CallbackLine'); define('IO_ASYNC_ERROR_BAD_PARAM', 1); define('IO_ASYNC_ERROR_NO_SOCKET', 2); define('IO_ASYNC_ERROR_NYI', 3); // Not Yet Implemented /** * Backgrounded asynchronous socket IO implemented using ticks. * * This class implements functions for doing backgrounded socket IO. * There are three modes of operation: * * IO_ASYNC_BUFFER_ALL: this mode simply reads all the results into * memory and waits for you to retrieve it via the fetchBuffer() * method. * * IO_ASYNC_CALLBACK_BLOCK: this mode reads a block at a time and * invokes a callback function after each. * * IO_ASYNC_CALLBACK_LINE: this mode reads a line at a time and * invokes a callback function after each. * * Callback functions are given two parameters: the IO_Async object * and the data just read (string). * */ class IO_Async extends PEAR { /** * Socket file descriptor. * @var resource */ var $_fp; /** * Buffer for data read from socket. * @var string */ var $_buffer; /** * Maximum size of _buffer. If set to -1, there are no buffer * size constraints. * @var int */ var $_max_buffer_size; /** * Size of buffer to use for individual read/write operations. * @var int */ var $_buffer_size; /** * This object's mode of operation. * @var string */ var $_mode; /** * Callback function or (object,method) tuple for line/block * callbacks. * @var mixed */ var $_callback; /** * TCP connect timeout in number of seconds * @var double */ var $_connect_timeout; /** * IO_Async constructor. * * @param array $options Associative array of configuration * options. Recognized keys are: * * 'mode' (int) Mode of operation, determines what IO_Async does * after reading one block or line of data. Legal * values are IO_ASYNC_BUFFER_ALL (default), * IO_ASYNC_CALLBACK_BLOCK and IO_ASYNC_CALLBACK_LINE. * * 'callback' (mixed) For IO_ASYNC_CALLBACK_* mode objects, this is * the function name or (object,method name) tuple * used for processing incoming data. Callbacks * are called with two parameters: the IO_Async * object and a string with the just-read data. * * 'bufsize' (int) Size of buffer used for individual reads/writes. * Defaults to 2048 bytes. * * 'maxbuffer' (int) For IO_ASYNC_BUFFER_ALL, this is the maximum * number of bytes that will be stored, or -1 for no * limit. Defaults to -1. * * 'timeout' (double) TCP connect timeout in number of * seconds. The openFile method does not support * connect timeouts. * * @access public */ function IO_Async($options = array()) { extract($options); $this->PEAR(); if (!isset($mode)) { $mode = IO_ASYNC_BUFFER_ALL; } if (!isset($buffer_size)) { if ($mode == IO_ASYNC_BUFFER_ALL) { $buffer_size = -1; } else { $buffer_size = 2048; } } $this->_fp = null; $this->_buffer = ''; $this->_mode = $mode; } /** * Register an already opened socket for backgrounded operation * and put it in non-blocking mode. Also sets up a tick handler, * data will be read from the socket as soon as some PHP code is * declared with ticks. * * @param resource $socket a socket resource * * @return mixed True on success, or a PEAR error on failure. The * possible error codes returned by this method are: * * IO_ASYNC_ERROR_BAD_PARAM : bad parameter, either the object * already has a socket registered, or the parameter was not a * resource. * * @access public * */ function registerSocket($socket) { if (is_resource($this->_fp)) { return $this->raiseError('IO_Async::registerSocket: already have a file descriptor', IO_ASYNC_ERROR_BAD_PARAM); } if (!is_resource($socket)) { return $this->raiseError('IO_Async::registerSocket: parameter not a resource', IO_ASYNC_ERROR_BAD_PARAM); } $this->_fp = $socket; $this->_buffer = ''; socket_set_blocking($this->_fp, false); register_tick_function('_IO_Async_tick_handler', &$this); return true; } /** * Open a file (or URL if fopen url wrappers are enabled). If you * use this method to open an HTTP URL, the headers will be read * during this call. If the HTTP request takes some time to * process before you get any headers, use the openHttp*() methods * instead. * * @param string $file file name or URL to open * * @return mixed True on success, a PEAR error on failure. The * error codes that may be returned by this method are: * IO_ASYNC_ERROR_BAD_PARAM. * * @access public */ function openFile($file) { $fp = @fopen($file, "r"); if (!is_resource($fp)) { return $this->raiseError("IO_Async::openFile: $php_errormsg", IO_ASYNC_ERROR_BAD_PARAM); } return $this->registerSocket($socket); } /** * Open a socket to an HTTP server and send an HTTP GET or POST * request. This method does not read headers, the returned * output will contain the HTTP response header as well as the * contents. * * @param string $host host name or IP number of host to contact * * @param int $port port number (default 80) * * @param string $path what path/uri to request (default '/') * * @param string $method which HTTP method to use, 'GET' or 'POST' * * @param mixed $post_vars if $method is 'POST', this parameter * is for giving post data, for example url-encoded form data for * default post requests, or XML payload for XML-RPC requests. * If this parameter is an array, it is treated as a list of * variables that will be url-encoded and sent as the post body. * Ignored for GET requests. * * @param string $mime_type what Content-type: header to send in * POST requests (default 'application/x-www-form-urlencoded'). * Ignored for GET requests. * * @return mixed True on success, a PEAR error on failure. The * error codes that may be returned are: * IO_ASYNC_ERROR_BAD_PARAM. * * @access public */ function openHttp($host, $port = 80, $path = '/', $method = 'GET', $post_data = '', $mime_type = 'application/x-www-form-urlencoded') { $en = $es = ''; $fp = fsockopen($host, $port, $en, $es); if (!is_resource($fp)) { return $this->raiseError("IO_Async::openHttp: $es (code $en)", IO_ASYNC_ERROR_BAD_PARAM); } switch (strtoupper($method)) { case 'GET': { fwrite($fp, "GET $path HTTP/1.0\r\nHost: $host:$port\r\n\r\n"); break; } case 'POST': { if (is_array($post_data) && $mime_type == 'application/x-www-form-urlencoded') { $post_data = $this->_uriEncodeArray($post_data); } fwrite($fp, "POST $path HTTP/1.0\r\nHost: $host:$port\r\n". "Content-type: $mime_type\r\n\r\n{$post_data}"); break; } default: { return $this->raiseError('IO_Async::openHttp: invalid method', IO_ASYNC_ERROR_BAD_PARAM); } } return $this->registerSocket($socket); } function readEvent() { if (!is_resource($this->_fp)) { return $this->raiseError("IO_Async::readLine: no socket opened", IO_ASYNC_ERROR_NO_SOCKET); } switch ($this->_mode) { case IO_ASYNC_CALLBACK_LINE: { $line = ''; do { $line .= fgets($this->_fp, $this->_buffer_size); } while (substr($line, -1) != "\n"); $this->_runCallback(&$line); break; } case IO_ASYNC_CALLBACK_BLOCK: { $data = fread($this->_fp, $this->_buffer_size); $this->_runCallback(&$data); break; } case IO_ASYNC_BUFFER_ALL: { $this->_buffer .= fread($this->_fp, $this->_buffer_size); break; } } /** * Set the socket read buffer size. You can only increase the buffer * size. * * @param int $new_size the new buffer size * * @return bool true if the buffer size was changed, false if you * specified a smaller buffer * * @access public */ function setBufferSize($new_size) { if ($new_size > $this->_buffer_size) { $this->_buffer_size = $new_size; return true; } return false; } function _runCallback(&$data) { if (function_exists($this->_callback)) { call_user_func($this->_callback, &$this, $data); } } function readEvent() { switch ($this->_mode) { case IO_ASYNC_BUFFER_ALL: { break; } case IO_ASYNC_CALLBACK_BLOCK: { break; } case IO_ASYNC_CALLBACK_LINE: { break; } } } } /** * This is the tick handling function. Internal use only. * * @param object $tobj an IO_Async object * * @return void * * @access private */ function _IO_Async_tick_handler(&$obj) { $obj->readEvent(); } // Local Variables: // indent-tabs-mode: nil // c-basic-offset: 4 // tab-width: 4 // End: ?> Index: pear/IO_Async/package.xml +++ pear/IO_Async/package.xml <?xml version="1.0" encoding="ISO-8859-1" ?> <Package Version="1.0"> <Name>IO_Async</Name> <Summary> Backgrounded asynchronous socket IO implemented with PHP ticks </Summary> <Description> This package provides a class for doing backgrounded asynchronous socket operations. The backgrounding is implemented using ticks. You can use IO_Async to download data from multiple sources while your script is processing other code. </Description> <Maintainer> <Initials>SSB</Initials> <Name>Stig Sæther Bakken</Name> <Email>stig@php.net</Email> </Maintainer> <FileList> <Dir Name="/" BaseInstallDir="IO"> <File Role="php">Async.php</File> </Dir> </FileList> </Package>