cvs: pear /IO_Async Async.php test.php /IO_Async/tests 01-encodearray.phpt

From: Date: Tue, 28 Aug 2001 14:39:04 +0000
Subject: cvs: pear /IO_Async Async.php test.php /IO_Async/tests 01-encodearray.phpt
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-683@lists.php.net to get a copy of this message
ssb Tue Aug 28 10:39:04 2001 EDT Added files: /pear/IO_Async test.php /pear/IO_Async/tests 01-encodearray.phpt Modified files: /pear/IO_Async Async.php Log: * more IO_Async work # test.php makes current CVS crash

Index: pear/IO_Async/Async.php diff -u pear/IO_Async/Async.php:1.2 pear/IO_Async/Async.php:1.3 --- pear/IO_Async/Async.php:1.2 Sun Aug 26 12:41:29 2001 +++ pear/IO_Async/Async.php Tue Aug 28 10:39:04 2001 @@ -17,7 +17,7 @@ // | | // +----------------------------------------------------------------------+ // -// $Id: Async.php,v 1.2 2001/08/26 16:41:29 ssb Exp $ +// $Id: Async.php,v 1.3 2001/08/28 14:39:04 ssb Exp $ // TODO: // - it's not exactly IO until we can O, today we only I :-) @@ -51,8 +51,8 @@ * 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). + * Callback functions are given two parameters: the data just read + * (string) and the IO_Async object. * */ class IO_Async extends PEAR @@ -102,6 +102,19 @@ var $_connect_timeout; /** + * User-defined read mode handlers. + * @var array + */ + var $_read_mode_handlers = array(); + + /** + * Public variable used to signal when the socket is closed + * and done. + * @var bool + */ + var $done; + + /** * IO_Async constructor. * * @param array $options Associative array of configuration @@ -115,8 +128,8 @@ * '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. + * are called with two parameters: a string with the data + * just read and the IO_Async object. * * 'bufsize' (int) Size of buffer used for individual reads/writes. * Defaults to 2048 bytes. @@ -126,28 +139,42 @@ * limit. Defaults to -1. * * 'timeout' (double) TCP connect timeout in number of - * seconds. The openFile method does not support - * connect timeouts. + * seconds. * * @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; - } + extract($options); + if (isset($mode)) { + $this->_mode = $mode; + } else { + $this->_mode = IO_ASYNC_BUFFER_ALL; + } + if (isset($callback)) { + $this->_callback = $callback; + } else { + $this->_callback = ''; + } + if (isset($bufsize)) { + $this->_buffer_size = $bufsize; + } else { + $this->_buffer_size = 2048; + } + if (isset($maxbuffer)) { + $this->_max_buffer_size = $maxbuffer; + } else { + $this->_max_buffer_size = -1; + } + if (isset($timeout)) { + $this->_connect_timeout = $timeout; + } else { + $this->_connect_timeout = 5.0; } - $this->_fp = null; $this->_buffer = ''; - $this->_mode = $mode; + $this->_fp = null; + $this->done = false; } /** @@ -180,37 +207,13 @@ } $this->_fp = $socket; $this->_buffer = ''; + $this->done = false; 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 @@ -272,7 +275,7 @@ IO_ASYNC_ERROR_BAD_PARAM); } } - return $this->registerSocket($socket); + return $this->registerSocket($fp); } /** @@ -294,63 +297,66 @@ do { $line .= fgets($this->_fp, $this->_buffer_size); } while (substr($line, -1) != "\n"); - $this->_runCallback(&$line); + if (function_exists($this->_callback)) { + call_user_func($this->_callback, $line, &$this); + } + break; } case IO_ASYNC_CALLBACK_BLOCK: { $data = fread($this->_fp, $this->_buffer_size); - $this->_runCallback(&$data); + if (function_exists($this->_callback)) { + call_user_func($this->_callback, $data, &$this); + } break; } case IO_ASYNC_BUFFER_ALL: { if ($this->_max_buffer_size != -1 && - strlen($this->_buffer) + $this->_buffer_size > $this->_max_buffer_size) { + strlen($this->_buffer) + $this->_buffer_size > $this->_max_buffer_size) + { $trunclen = $this->_max_buffer_size - strlen($this->_buffer); $tmp = fread($this->_fp, $this->_buffer_size); $this->_buffer .= substr($tmp, 0, $trunclen); + $this->done = true; return false; // signal that we're done } else { $this->_buffer .= fread($this->_fp, $this->_buffer_size); } break; } + default: { + if (isset($this->_read_mode_handlers[$this->_mode])) { + call_user_func($this->_read_mode_handlers[$this->_mode], $this->_fp, &$this); + } + } } - return !feof($this->_fp); - } - - /** - * 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; + $eof = @feof($this->_fp); + if ($eof === true) { + fclose($this->_fp); + } + if ($eof === true || $eof === null) { + print "done! eof=";var_dump($eof); + $this->done = true; + $this->_fp = null; + return false; } - return false; + return true; } - /** - * Run a user-defined callback. - * - * @param string $data read socket data that is passed to the callback - * - * @return void - * - * @access private - */ - function _runCallback(&$data) + function finish() { - if (function_exists($this->_callback)) { - call_user_func($this->_callback, &$this, $data); + if (!is_resource($this->_fp)) { + return $this->raiseError("IO_Async::finish: no socket opened", + IO_ASYNC_ERROR_NO_SOCKET); + } + socket_set_blocking($this->_fp, true); + do { + $err = $this->readEvent(); + } while ($err === true); + // return PEAR error object if $err is one + if ($err === false) { + return true; } + return $err; } /** @@ -414,7 +420,9 @@ */ function _IO_Async_tick_handler(&$obj) { - $obj->readEvent(); + print "<tick>\n"; + var_dump($obj->readEvent()); + print "</tick>\n"; } Index: pear/IO_Async/test.php +++ pear/IO_Async/test.php <?php require "./Async.php"; error_reporting(E_ALL); set_error_handler("test_error_handler"); $obj = new IO_Async(array("mode" => IO_ASYNC_CALLBACK_LINE, "callback" => "line_reader")); $obj->openHttp("www.trd.fast.no", 80, "/~ssb/100ms.php3"); declare (ticks = 1) { for ($i = 0; $i < 100; $i++) { print "LOOP i=$i\n"; if ( // $obj->readEvent() == false || $obj->done) { print "done! breaking out\n"; break; } } } print "out of the ticked scope\n"; function line_reader($data, &$obj) { static $http_body; $data = trim($data); if (empty($data)) { $http_body = true; return; } if (!$http_body) { return; } printf("line_reader data=\"%s\"\n", $data); if ($data == "code") { fclose($obj->_fp); $obj->_fp = null; } } function test_error_handler($errno, $errmsg, $file, $line, $vars) { $errortype = array ( 1 => "Error", 2 => "Warning", 4 => "Parsing Error", 8 => "Notice", 16 => "Core Error", 32 => "Core Warning", 64 => "Compile Error", 128 => "Compile Warning", 256 => "User Error", 512 => "User Warning", 1024=> "User Notice" ); $prefix = $errortype[$errno]; $file = basename($file); print "\n$prefix: $errmsg in $file:$line\n"; } ?> Index: pear/IO_Async/tests/01-encodearray.phpt +++ pear/IO_Async/tests/01-encodearray.phpt --TEST-- IO_Async::_urlEncodeArray --FILE-- <?php require "../Async.php"; $arr1 = array("zero", "one", "two", "three"); $arr2 = array( array("tinsy", "winsy", "spider"), array("lille", "petter", "edderkopp") ); $arr3 = array( "foo1" => "bar1", "foo2" => 42, "foo3" => array( "key1" => $arr1, "key2" => array($arr1) ) ); printf("%s\n", rawurldecode(IO_Async::_uriEncodeArray($arr1, "foo"))); printf("%s\n", rawurldecode(IO_Async::_uriEncodeArray($arr2, "bar"))); printf("%s\n", rawurldecode(IO_Async::_uriEncodeArray($arr3))); ?> --EXPECT-- foo[0]=zero&foo[1]=one&foo[2]=two&foo[3]=three bar[0][0]=tinsy&bar[0][1]=winsy&bar[0][2]=spider&bar[1][0]=lille&bar[1][1]=petter&bar[1][2]=edderkopp foo1=bar1&foo2=42&foo3[key1][0]=zero&foo3[key1][1]=one&foo3[key1][2]=two&foo3[key1][3]=three&foo3[key2][0][0]=zero&foo3[key2][0][1]=one&foo3[key2][0][2]=two&foo3[key2][0][3]=three
« previous php.pear.cvs (#683) next »