diff --git a/README.md b/README.md index 8a242d1..16a6818 100644 --- a/README.md +++ b/README.md @@ -41,8 +41,7 @@ Once [installed](#install), you can use the following code to create a simple echo server that listens for incoming multicast messages: ```php -$loop = React\EventLoop\Factory::create(); -$factory = new Factory($loop); +$factory = new Clue\React\Multicast\Factory(); $socket = $factory->createReceiver('224.10.20.30:4050'); $socket->on('message', function ($data, $remote) use ($socket) { @@ -50,7 +49,6 @@ $socket->on('message', function ($data, $remote) use ($socket) { $socket->send($data, $remote); }); -$loop->run(); ``` See also the [examples](examples). @@ -60,13 +58,17 @@ See also the [examples](examples). ### Factory The `Factory` is responsible for creating your [`SocketInterface`](#socketinterface) instances. -It also registers everything with the main [`EventLoop`](https://github.com/reactphp/event-loop#usage). ```php -$loop = React\EventLoop\Factory::create(); -$factory = new Factory($loop); +$factory = new Clue\React\Multicast\Factory(); ``` +This class takes an optional `LoopInterface|null $loop` parameter that can be used to +pass the event loop instance to use for this object. You can use a `null` value +here in order to use the [default loop](https://github.com/reactphp/event-loop#loop). +This value SHOULD NOT be given unless you're sure you want to explicitly use a +given event loop instance. + #### createSender() The `createSender(): SocketInterface` method can be used to diff --git a/composer.json b/composer.json index 08c1408..826c044 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,7 @@ }, "require": { "php": ">=5.3", - "react/event-loop": "^1.0 || ^0.5 || ^0.4 || ^0.3", + "react/event-loop": "^1.2", "react/datagram": "~1.0" }, "require-dev": { diff --git a/examples/dump-received.php b/examples/dump-received.php index 57d3f13..efa2448 100644 --- a/examples/dump-received.php +++ b/examples/dump-received.php @@ -5,9 +5,6 @@ * Accepts a single argument socket address (defaults to 224.10.20.30:12345) */ -use Clue\React\Multicast\Factory; -use Clue\Hexdump\Hexdump; - require __DIR__ . '/../vendor/autoload.php'; $address = '224.10.20.30:12345'; // random test address @@ -18,14 +15,12 @@ $address = $argv[1]; } -$loop = React\EventLoop\Factory::create(); -$factory = new Factory($loop); +$factory = new Clue\React\Multicast\Factory(); $socket = $factory->createReceiver($address); -$hex = new Hexdump(); +$hex = new Clue\Hexdump\Hexdump(); $socket->on('message', function ($data, $remote) use ($hex) { echo 'Received from ' . $remote . PHP_EOL; echo $hex->dump($data) . PHP_EOL; }); -$loop->run(); diff --git a/examples/echo-received.php b/examples/echo-received.php index 6a32aa0..c13030c 100644 --- a/examples/echo-received.php +++ b/examples/echo-received.php @@ -5,8 +5,6 @@ * Accepts a single argument socket address (defaults to 224.10.20.30:12345) */ -use Clue\React\Multicast\Factory; - require __DIR__ . '/../vendor/autoload.php'; $address = '224.10.20.30:12345'; // random test address @@ -17,13 +15,10 @@ $address = $argv[1]; } -$loop = React\EventLoop\Factory::create(); -$factory = new Factory($loop); +$factory = new Clue\React\Multicast\Factory(); $socket = $factory->createReceiver($address); $socket->on('message', function ($data, $remote) use ($socket) { echo 'Sending back ' . strlen($data) . ' bytes to ' . $remote . PHP_EOL; $socket->send($data, $remote); }); - -$loop->run(); diff --git a/examples/send-once.php b/examples/send-once.php index 795212e..e399201 100644 --- a/examples/send-once.php +++ b/examples/send-once.php @@ -5,14 +5,11 @@ * Accepts a single argument socket address (defaults to 224.10.20.30:12345) */ -use Clue\React\Multicast\Factory; - require __DIR__ . '/../vendor/autoload.php'; $address = isset($argv[1]) ? $argv[1] : '224.10.20.30:12345'; -$loop = React\EventLoop\Factory::create(); -$factory = new Factory($loop); +$factory = new Clue\React\Multicast\Factory(); $sender = $factory->createSender(); // do not wait for incoming messages @@ -21,5 +18,3 @@ // send a simple message $message = 'ping 123'; $sender->send($message, $address); - -$loop->run(); diff --git a/examples/send-wait.php b/examples/send-wait.php index 791183b..5d28e04 100644 --- a/examples/send-wait.php +++ b/examples/send-wait.php @@ -5,17 +5,13 @@ * Accepts a single argument socket address (defaults to 224.10.20.30:12345) */ -use Clue\React\Multicast\Factory; -use Clue\Hexdump\Hexdump; - require __DIR__ . '/../vendor/autoload.php'; $address = isset($argv[1]) ? $argv[1] : '224.10.20.30:12345'; -$loop = React\EventLoop\Factory::create(); -$factory = new Factory($loop); +$factory = new Clue\React\Multicast\Factory(); $sender = $factory->createSender(); -$hex = new Hexdump(); +$hex = new Clue\Hexdump\Hexdump(); // print a hexdump of every message received $sender->on('message', function ($data, $remote) use ($hex) { @@ -26,5 +22,3 @@ // send a simple message $message = 'ping 123'; $sender->send($message, $address); - -$loop->run(); diff --git a/examples/ssdp.php b/examples/ssdp.php index c92a7d5..b4d60b2 100644 --- a/examples/ssdp.php +++ b/examples/ssdp.php @@ -3,14 +3,12 @@ * UPnP simple service discovery protocol (SSDP) */ -use Clue\React\Multicast\Factory; require __DIR__ . '/../vendor/autoload.php'; $address = '239.255.255.250:1900'; -$loop = React\EventLoop\Factory::create(); -$factory = new Factory($loop); +$factory = new Clue\React\Multicast\Factory(); $sender = $factory->createSender(); // dump all incoming messages @@ -20,7 +18,7 @@ }); // stop waiting for incoming messages after 3.0s (MX is 2s) -$loop->addTimer(3.0, function () use ($sender) { +Loop::addTimer(3.0, function () use ($sender) { $sender->pause(); }); @@ -32,5 +30,3 @@ $data .= "ST: ssdp:all\r\n"; $data .= "\r\n"; $sender->send($data, $address); - -$loop->run(); diff --git a/src/Factory.php b/src/Factory.php index 333e790..7189877 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -2,6 +2,7 @@ namespace Clue\React\Multicast; +use React\EventLoop\Loop; use React\EventLoop\LoopInterface; use React\Datagram\Socket as DatagramSocket; use BadMethodCallException; @@ -9,11 +10,17 @@ class Factory { + /** @var LoopInterface */ private $loop; /** * The `Factory` is responsible for creating your [`SocketInterface`](#socketinterface) instances. - * It also registers everything with the main [`EventLoop`](https://github.com/reactphp/event-loop#usage). + * + * This class takes an optional `LoopInterface|null $loop` parameter that can be used to + * pass the event loop instance to use for this object. You can use a `null` value + * here in order to use the [default loop](https://github.com/reactphp/event-loop#loop). + * This value SHOULD NOT be given unless you're sure you want to explicitly use a + * given event loop instance. * * ```php * $loop = React\EventLoop\Factory::create(); @@ -22,9 +29,9 @@ class Factory * * @param LoopInterface $loop */ - public function __construct(LoopInterface $loop) + public function __construct(LoopInterface $loop = null) { - $this->loop = $loop; + $this->loop = $loop ?: Loop::get(); } /** diff --git a/tests/FunctionalTest.php b/tests/FunctionalTest.php index 6229505..e232c23 100644 --- a/tests/FunctionalTest.php +++ b/tests/FunctionalTest.php @@ -57,4 +57,15 @@ public function testMultipleReceivers() $this->loop->run(); } + + public function testConstructWithoutLoopAssignsLoopAutomatically() + { + $factory = new Factory(); + + $ref = new \ReflectionProperty($factory, 'loop'); + $ref->setAccessible(true); + $loop = $ref->getValue($factory); + + $this->assertInstanceOf('React\EventLoop\LoopInterface', $loop); + } }