Skip to content

Commit

Permalink
Simplify usage by supporting new default loop
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulRotmann committed Jul 14, 2021
1 parent dd1c1d7 commit 39ba3e9
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 30 deletions.
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 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 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
4 changes: 1 addition & 3 deletions examples/dump-received.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
$address = $argv[1];
}

$loop = React\EventLoop\Factory::create();
$factory = new Factory($loop);
$factory = new Factory();
$socket = $factory->createReceiver($address);
$hex = new Hexdump();

Expand All @@ -28,4 +27,3 @@
echo $hex->dump($data) . PHP_EOL;
});

$loop->run();
5 changes: 1 addition & 4 deletions examples/echo-received.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,10 @@
$address = $argv[1];
}

$loop = React\EventLoop\Factory::create();
$factory = new Factory($loop);
$factory = new 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();
5 changes: 1 addition & 4 deletions examples/send-once.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@

$address = isset($argv[1]) ? $argv[1] : '224.10.20.30:12345';

$loop = React\EventLoop\Factory::create();
$factory = new Factory($loop);
$factory = new Factory();
$sender = $factory->createSender();

// do not wait for incoming messages
Expand All @@ -21,5 +20,3 @@
// send a simple message
$message = 'ping 123';
$sender->send($message, $address);

$loop->run();
5 changes: 1 addition & 4 deletions examples/send-wait.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@

$address = isset($argv[1]) ? $argv[1] : '224.10.20.30:12345';

$loop = React\EventLoop\Factory::create();
$factory = new Factory($loop);
$factory = new Factory();
$sender = $factory->createSender();
$hex = new Hexdump();

Expand All @@ -26,5 +25,3 @@
// send a simple message
$message = 'ping 123';
$sender->send($message, $address);

$loop->run();
7 changes: 2 additions & 5 deletions examples/ssdp.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@

$address = '239.255.255.250:1900';

$loop = React\EventLoop\Factory::create();
$factory = new Factory($loop);
$factory = new Factory();
$sender = $factory->createSender();

// dump all incoming messages
Expand All @@ -20,7 +19,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 +31,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);
}
}

0 comments on commit 39ba3e9

Please sign in to comment.