<?php require 'MIO/Selector.php'; require 'MIO/SelectionKey.php'; require 'MIO/Stream.php'; require 'MIO/StreamFactory.php'; require 'MIO/Exception.php'; $urls = array('http://uk.php.net/get/php-5.2.1.tar.bz2/from/this/mirror' => 'php.tar.bz2', 'http://dev.mysql.com/get/Downloads/MySQL-5.1/mysql-5.1.15-beta-linux-i686-glibc23.tar.gz/from/http://mirrors.dedipower.com/www.mysql.com/' => 'mysql.tar.gz', 'http://www.mirrorservice.org/sites/ftp.apache.org/httpd/httpd-2.2.4.tar.bz2' => 'apache.tar.bz2'); $selector = new MioSelector(); $streams = new MioStreamFactory(); foreach ($urls as $url => $file) { // I suppose I should really have a buffer in between them // but I think I can be pretty sure the network is going to // be slower than the filesystem. $selector->register($streams->createFileStream($url, 'r'), MioSelectionKey::OP_READ, $streams->createFileStream($file, 'w+')); } echo "\n"; for ($i = 1;; $i++) { while (!($count = $selector->select(100000))) { if ($count === false) { $selector->close(); break 2; } } foreach ($selector->selected_keys as $key) { if ($key->isReadable()) { $key->attachment->write($key->stream->read(16384)); } else { echo "Que!\n"; } }
/** * Echo class for responding to data */ class EchoGen { private $message = ''; public function put($message) { $this->message = $message; } public function get() { return "The message is: " . $this->message; } } $selector = new MioSelector(); $streams = new MioStreamFactory(); echo "Creating server..."; $selector->register($streams->createServerStream('127.0.0.1:8888'), MioSelectionKey::OP_ACCEPT); echo "DONE\n"; $i = 0; while (true) { /** * Add socket streams five at a time otherwise the server * buffer gets filled up and fsockopen blocks */ for ($j = 0; $j < 5 && $i < 100; $i++, $j++) { echo "Creating client " . ($i + 1) . "..."; $selector->register($streams->createSocketStream('127.0.0.1', 8888), MioSelectionKey::OP_WRITE, new MessageGen()); echo "DONE\n"; }