Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify usage by supporting new default loop #21

Merged
merged 2 commits into from
Jul 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,14 @@ 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) {
echo 'Sending back ' . strlen($data) . ' bytes to ' . $remote . PHP_EOL;
$socket->send($data, $remote);
});

$loop->run();
```

See also the [examples](examples).
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
9 changes: 2 additions & 7 deletions examples/dump-received.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
7 changes: 1 addition & 6 deletions examples/echo-received.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
7 changes: 1 addition & 6 deletions examples/send-once.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -21,5 +18,3 @@
// send a simple message
$message = 'ping 123';
$sender->send($message, $address);

$loop->run();
10 changes: 2 additions & 8 deletions examples/send-wait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -26,5 +22,3 @@
// send a simple message
$message = 'ping 123';
$sender->send($message, $address);

$loop->run();
8 changes: 2 additions & 6 deletions examples/ssdp.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
});

Expand All @@ -32,5 +30,3 @@
$data .= "ST: ssdp:all\r\n";
$data .= "\r\n";
$sender->send($data, $address);

$loop->run();
13 changes: 10 additions & 3 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,25 @@

namespace Clue\React\Multicast;

use React\EventLoop\Loop;
use React\EventLoop\LoopInterface;
use React\Datagram\Socket as DatagramSocket;
use BadMethodCallException;
use RuntimeException;

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();
Expand All @@ -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();
}

/**
Expand Down
11 changes: 11 additions & 0 deletions tests/FunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}