1
0
mirror of https://github.com/danog/amp.git synced 2024-11-27 04:24:42 +01:00

Merge branch 'psr' of https://github.com/lyrixx/amp into lyrixx-psr

This commit is contained in:
Daniel Lowrey 2015-02-05 23:54:12 -05:00
commit 24acba4486
11 changed files with 26 additions and 31 deletions

0
LICENSE Executable file → Normal file
View File

View File

@ -2,7 +2,6 @@
require __DIR__ . '/../vendor/autoload.php'; require __DIR__ . '/../vendor/autoload.php';
use Amp\ReactorFactory;
use Amp\Reactor; use Amp\Reactor;
/** /**

View File

@ -14,7 +14,7 @@ Amp\run(function() {
// garbage collect resources associated with one-time events after they finish executing. // garbage collect resources associated with one-time events after they finish executing.
Amp\immediately($ticker); Amp\immediately($ticker);
// Execute every $msInterval milliseconds until the resulting $watcherId is cancelled. // 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. // At some point in the future we need to cancel this watcher or our program will never end.
$repeatingWatcherId = Amp\repeat($ticker, $msInterval = 1000); $repeatingWatcherId = Amp\repeat($ticker, $msInterval = 1000);

View File

@ -1,7 +1,6 @@
<?php <?php
use Amp\Reactor; use Amp\Reactor;
use Amp\ReactorFactory;
require __DIR__ . '/../vendor/autoload.php'; require __DIR__ . '/../vendor/autoload.php';
@ -28,7 +27,7 @@ Amp\run(function(Reactor $reactor) {
}, $msDelay = 5000); }, $msDelay = 5000);
// After about five seconds the program will exit on its own. Why? This happens because in // 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() // that time frame we will have canceled the repeating watcher we registered using repeat()
// and the two one-off events (immediately() + once()) are automatically garbage collected // and the two one-off events (immediately() + once()) are automatically garbage collected
// by the Reactor after they execute. // by the Reactor after they execute.
}); });

View File

@ -19,12 +19,11 @@ class Client {
/** /**
* A simple TCP server that broadcasts the current time once per second to all connected clients * A simple TCP server that broadcasts the current time once per 3 seconds to all connected clients
*/ */
class Server { class Server {
private $reactor; private $reactor;
private $clients = []; private $clients = [];
private $timeBroadcastWatcher;
private $ioGranularity = 8192; private $ioGranularity = 8192;
public function __construct(Amp\Reactor $reactor = null) { public function __construct(Amp\Reactor $reactor = null) {
@ -47,7 +46,7 @@ class Server {
}); });
// Let's schedule a broadcast of the current time to all connected sockets every three seconds // Let's schedule a broadcast of the current time to all connected sockets every three seconds
$this->timeBroadcastWatcher = $this->reactor->repeat(function() { $this->reactor->repeat(function() {
$this->broadcastTime(); $this->broadcastTime();
}, $msInterval = 3000); }, $msInterval = 3000);
@ -97,7 +96,7 @@ class Server {
if ($data == '' && $this->isSocketDead($client->socket)) { if ($data == '' && $this->isSocketDead($client->socket)) {
$this->unloadClient($client); $this->unloadClient($client);
} else { } else {
printf("Data rcvd from client %d: %s\n", $client->id, $data); printf("Data received from client %d: %s\n", $client->id, $data);
} }
} }

View File

@ -3,7 +3,7 @@
/** /**
* Process signals are "watchable" events just like timers and stream IO * Process signals are "watchable" events just like timers and stream IO
* availability. SignalReactor::onSignal() returns a unique watcher ID that * availability. SignalReactor::onSignal() returns a unique watcher ID that
* may be disabled/enabled/cancelled like any other watcher. * may be disabled/enabled/canceled like any other watcher.
* *
* The available signal number constants vary by operating system, but you * The available signal number constants vary by operating system, but you
* can see the possible signals in your PHP install with the following * can see the possible signals in your PHP install with the following
@ -15,7 +15,7 @@
*/ */
require __DIR__ . '/../vendor/autoload.php'; require __DIR__ . '/../vendor/autoload.php';
(new Amp\UvReactor)->run(function($reactor) { (new Amp\UvReactor)->run(function(Amp\Reactor $reactor) {
// Let's tick off output once per second so we can see activity. // Let's tick off output once per second so we can see activity.
$reactor->repeat(function() { $reactor->repeat(function() {
echo "tick: ", date('c'), "\n"; echo "tick: ", date('c'), "\n";

View File

@ -9,7 +9,7 @@ define('SERVER_ADDRESS', '127.0.0.1:1337');
* echo server example * echo server example
* 1. Connect to 127.0.0.1 at port 1337 from various terminals; * 1. Connect to 127.0.0.1 at port 1337 from various terminals;
* 2. Type in anything and press ENTER; * 2. Type in anything and press ENTER;
* 3. Reactor will asyncronously read from client and broadcast to others. * 3. Reactor will asynchronously read from client and broadcast to others.
*/ */
/** /**
@ -106,7 +106,7 @@ class Server {
if ($data == '' && $this->isSocketDead($client->socket)) { if ($data == '' && $this->isSocketDead($client->socket)) {
$this->unloadClient($client); $this->unloadClient($client);
} else { } else {
printf("Data rcvd from client %d: %s\n", $client->id, $data); printf("Data received from client %d: %s\n", $client->id, $data);
$this->broadcast($client, "{$client->id} said: {$data}\n"); $this->broadcast($client, "{$client->id} said: {$data}\n");
} }
} }

View File

@ -1,15 +1,15 @@
<?php <?php
namespace Amp; namespace Amp;
interface SignalReactor extends Reactor { interface SignalReactor extends Reactor {
/** /**
* React to process control signals * React to process control signals
* *
* @param int $signo The signal number to watch for * @param int $signo The signal number to watch for
* @param callable $onSignal * @param callable $onSignal
* @return int Returns a unique integer watcher ID * @return int Returns a unique integer watcher ID
*/ */
public function onSignal($signo, callable $onSignal); public function onSignal($signo, callable $onSignal);
} }

View File

@ -4,10 +4,10 @@ namespace Amp;
/** /**
* A "safe" Struct class for public property aggregators * A "safe" Struct class for public property aggregators
* *
* This class is intended to make using public properties a little safer by throwing when * This class is intended to make using public properties a little safer by throwing when
* nonexistent property names are read or written. All property aggregation classes in the * nonexistent property names are read or written. All property aggregation classes in the
* Amp library descend from Struct somewhere in their inheritance heirarchies. * Amp library descend from Struct somewhere in their inheritance hierarchies.
*/ */
abstract class Struct { abstract class Struct {
final public function __get($property) { final public function __get($property) {

View File

@ -7,7 +7,6 @@ namespace Amp;
* the Promisor that created it. * the Promisor that created it.
*/ */
class Unresolved implements Promise { class Unresolved implements Promise {
private $isWaiting = false;
private $isResolved = false; private $isResolved = false;
private $watchers = []; private $watchers = [];
private $whens = []; private $whens = [];

View File

@ -246,7 +246,6 @@ function all(array $promises) {
$results = []; $results = [];
$remaining = count($promises); $remaining = count($promises);
$promisor = new Future; $promisor = new Future;
$isResolved = false;
foreach ($promises as $key => $resolvable) { foreach ($promises as $key => $resolvable) {
if (!$resolvable instanceof Promise) { if (!$resolvable instanceof Promise) {