From c3298f104a2063be7223d77f0d03a83efbd5294a Mon Sep 17 00:00:00 2001 From: Daniel Lowrey Date: Wed, 22 Jul 2015 09:09:17 -0400 Subject: [PATCH] remove examples --- examples/001_basic_run.php | 43 -------- examples/002_scheduling.php | 31 ------ examples/003_tcp_server.php | 141 ------------------------- examples/004_libuv_signal_handling.php | 29 ----- 4 files changed, 244 deletions(-) delete mode 100644 examples/001_basic_run.php delete mode 100644 examples/002_scheduling.php delete mode 100644 examples/003_tcp_server.php delete mode 100644 examples/004_libuv_signal_handling.php diff --git a/examples/001_basic_run.php b/examples/001_basic_run.php deleted file mode 100644 index c88cbae..0000000 --- a/examples/001_basic_run.php +++ /dev/null @@ -1,43 +0,0 @@ -onReadable(STDIN, function() { - if ($line = fgets(STDIN)) { - echo "INPUT> ", $line, "\n"; - } - }); - - // Countdown RUN_TIME seconds then end the event loop - $secondsRemaining = RUN_TIME; - $reactor->repeat(function() use (&$secondsRemaining) { - if (--$secondsRemaining > 0) { - echo "$secondsRemaining seconds to shutdown\n"; - } else { - $reactor->stop(); // <-- explicitly stop the loop - } - }, $msInterval = 1000); -}); diff --git a/examples/002_scheduling.php b/examples/002_scheduling.php deleted file mode 100644 index 83a8a7a..0000000 --- a/examples/002_scheduling.php +++ /dev/null @@ -1,31 +0,0 @@ -immediately($ticker); - - // Execute every $msInterval milliseconds until the resulting $watcherId is canceled. - // At some point in the future we need to cancel this watcher or our program will never end. - $repeatingWatcherId = Amp\repeat($ticker, $msInterval = 1000); - - // Five seconds from now let's cancel the repeating ticker we just registered - $reactor->once(function() use ($repeatingWatcherId) { - $reactor->cancel($repeatingWatcherId); - echo "Cancelled repeating ticker\n"; - }, $msDelay = 5000); - - // After about five seconds the program will exit on its own. Why? This happens because in - // that time frame we will have cancelled the repeating watcher we registered using repeat() - // and the two one-off events (immediately() + once()) are automatically garbage collected - // by the Reactor after they execute. -}); diff --git a/examples/003_tcp_server.php b/examples/003_tcp_server.php deleted file mode 100644 index 853e90b..0000000 --- a/examples/003_tcp_server.php +++ /dev/null @@ -1,141 +0,0 @@ -reactor = $reactor ?: Amp\reactor(); - } - - public function start($address) { - if (!$server = @stream_socket_server($address, $errno, $errstr)) { - throw new RuntimeException( - sprintf('Failed binding server on %s; [%d] %s', $address, $errno, $errstr) - ); - } - - printf("Server socket listening on %s\n", $address); - - stream_set_blocking($server, false); - - $this->reactor->onReadable($server, function() use ($server) { - $this->acceptClients($server); - }); - - // Let's schedule a broadcast of the current time to all connected sockets every three seconds - $this->reactor->repeat(function() { - $this->broadcastTime(); - }, $msInterval = 3000); - - // Release the hounds! - $this->reactor->run(); - } - - private function acceptClients($server) { - while ($socket = @stream_socket_accept($server, $timeout=0, $name)) { - $client = new Client; - $client->id = (int) $socket; - $client->socket = $socket; - - // What to do when the client socket is readable - $client->readWatcher = $this->reactor->onReadable($socket, function() use ($client) { - $this->readFromClient($client); - }); - - // What to do when the socket is writable - $client->writeWatcher = $this->reactor->onWritable($socket, function() use ($client) { - $this->writeToClient($client); - }); - - // Buffer something to send to the client. The writability watcher we just enabled - // above will take care of sending this data automatically. - $client->outputBuffer = "--- Welcome to the example server! ---\n\n"; - - printf("Client socket accepted: %s\n", $name); - - // Store the client using its integer ID - $this->clients[$client->id] = $client; - } - } - - private function broadcastTime() { - $data = date('r') . "\n"; - foreach ($this->clients as $client) { - $client->outputBuffer .= $data; - $this->reactor->enable($client->writeWatcher); - } - } - - private function readFromClient(Client $client) { - $data = @fread($client->socket, $this->ioGranularity); - - // This only happens on EOF. If the socket has died we need to unload it. - if ($data == '' && $this->isSocketDead($client->socket)) { - $this->unloadClient($client); - } else { - printf("Data received from client %d: %s\n", $client->id, $data); - } - } - - private function isSocketDead($socket) { - return (!is_resource($socket) || feof($socket)); - } - - private function writeToClient(Client $client) { - $bytesWritten = @fwrite($client->socket, $client->outputBuffer); - - if ($bytesWritten === strlen($client->outputBuffer)) { - // All data written. Disable the writability watcher. Sockets are essentially "always" - // writable, so it's important to disable write watchers when you don't have any data - // remaining to write. Otherwise you'll just hammer your CPU. - $client->outputBuffer = ''; - $this->reactor->disable($client->writeWatcher); - } elseif ($bytesWritten > 0) { - // Data was partially written -- truncate the buffer - $client->outputBuffer = substr($client->outputBuffer, $bytesWritten); - } elseif ($this->isSocketDead($client->socket)) { - // Otherwise the client is dead and we just unload it - $this->unloadClient($client); - } - } - - /** - * We have to clean up after ourselves or we'll create memory leaks. Always be sure to cancel - * any stream IO watchers or repeating timer events once they're no longer needed! - */ - private function unloadClient(Client $client) { - $this->reactor->cancel($client->readWatcher); - $this->reactor->cancel($client->writeWatcher); - if (is_resource($client->socket)) { - @fclose($client->socket); - } - unset($this->clients[$client->id]); - - printf("Client %d disconnected\n", $client->id); - } -} - -(new Server)->start(SERVER_ADDRESS); diff --git a/examples/004_libuv_signal_handling.php b/examples/004_libuv_signal_handling.php deleted file mode 100644 index bbead56..0000000 --- a/examples/004_libuv_signal_handling.php +++ /dev/null @@ -1,29 +0,0 @@ -getConstants()); - */ -require __DIR__ . '/../vendor/autoload.php'; - -(new Amp\UvReactor)->run(function(Amp\Reactor $reactor) { - // Let's tick off output once per second so we can see activity. - $reactor->repeat(function() { - echo "tick: ", date('c'), "\n"; - }, $msInterval = 1000); - - // What to do when a SIGINT signal is received - $reactor->onSignal(UV::SIGINT, function() { - echo "Caught SIGINT! exiting ...\n"; - exit; - }); -});