10000 Merge pull request #21 from PaulRotmann/noloop · clue/reactphp-multicast@9d5adfd · GitHub
[go: up one dir, main page]

Skip to content

Commit 9d5adfd

Browse files
authored
Merge pull request #21 from PaulRotmann/noloop
Simplify usage by supporting new default loop
2 parents dd1c1d7 + fb1335f commit 9d5adfd

File tree

9 files changed

+38
-43
lines changed

9 files changed

+38
-43
lines changed

README.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,14 @@ Once [installed](#install), you can use the following code to create a simple
4141
echo server that listens for incoming multicast messages:
4242

4343
```php
44-
$loop = React\EventLoop\Factory::create();
45-
$factory = new Factory($loop);
44+
$factory = new Clue\React\Multicast\Factory();
4645
$socket = $factory->createReceiver('224.10.20.30:4050');
4746

4847
$socket->on('message', function ($data, $remote) use ($socket) {
4948
echo 'Sending back ' . strlen($data) . ' bytes to ' . $remote . PHP_EOL;
5049
$socket->send($data, $remote);
5150
});
5251

53-
$loop->run();
5452
```
5553

5654
See also the [examples](examples).
@@ -60,13 +58,17 @@ See also the [examples](examples).
6058
### Factory
6159

6260
The `Factory` is responsible for creating your [`SocketInterface`](#socketinterface) instances.
63-
It also registers everything with the main [`EventLoop`](https://github.com/reactphp/event-loop#usage).
6461

6562
```php
66-
$loop = React\EventLoop\Factory::create();
67-
$factory = new Factory($loop);
63+
$factory = new Clue\React\Multicast\Factory();
6864
```
6965

66+
This class takes an optional `LoopInterface|null $loop` parameter that can be used to
67+
pass the event loop instance to use for this object. You can use a `null` value
68+
here in order to use the [default loop](https://github.com/reactphp/event-loop#loop).
69+
This value SHOULD NOT be given unless you're sure you want to explicitly use a
70+
given event loop instance.
71+
7072
#### createSender()
7173

7274
The `createSender(): SocketInterface` method can be used to

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
},
1919
"require": {
2020
"php": ">=5.3",
21-
"react/event-loop": "^1.0 || ^0.5 || ^0.4 || ^0.3",
21+
"react/event-loop": "^1.2",
2222
"react/datagram": "~1.0"
2323
},
2424
"require-dev": {

examples/dump-received.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55
* Accepts a single argument socket address (defaults to 224.10.20.30:12345)
66
*/
77

8-
use Clue\React\Multicast\Factory;
9-
use Clue\Hexdump\Hexdump;
10-
118
require __DIR__ . '/../vendor/autoload.php';
129

1310
$address = '224.10.20.30:12345'; // random test address
@@ -18,14 +15,12 @@
1815
$address = $argv[1];
1916
}
2017

21-
$loop = React\EventLoop\Factory::create();
22-
$factory = new Factory($loop);
18+
$factory = new Clue\React\Multicast\Factory();
2319
$socket = $factory->createReceiver($address);
24-
$hex = new Hexdump();
20+
$hex = new Clue\Hexdump\Hexdump();
2521

2622
$socket->on('message', function ($data, $remote) use ($hex) {
2723
echo 'Received from ' . $remote . PHP_EOL;
2824
echo $hex->dump($data) . PHP_EOL;
2925
});
3026

31-
$loop->run();

examples/echo-received.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
* Accepts a single argument socket address (defaults to 224.10.20.30:12345)
66
*/
77

8-
use Clue\React\Multicast\Factory;
9-
108
require __DIR__ . '/../vendor/autoload.php';
119

1210
$address = '224.10.20.30:12345'; // random test address
@@ -17,13 +15,10 @@
1715
$address = $argv[1];
1816
}
1917

20-
$loop = React\EventLoop\Factory::create();
21-
$factory = new Factory($loop);
18+
$factory = new Clue\React\Multicast\Factory();
2219
$socket = $factory->createReceiver($address);
2320

2421
$socket->on('message', function ($data, $remote) use ($socket) {
2522
echo 'Sending back ' . strlen($data) . ' bytes to ' . $remote . PHP_EOL;
2623
$socket->send($data, $remote);
2724
});
28-
29-
$loop->run();

examples/send-once.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,11 @@
55
* Accepts a single argument socket address (defaults to 224.10.20.30:12345)
66
*/
77

8-
use Clue\React\Multicast\Factory;
9-
108
require __DIR__ . '/../vendor/autoload.php';
119

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

14-
$loop = React\EventLoop\Factory::create();
15-
$factory = new Factory($loop);
12+
$factory = new Clue\React\Multicast\Factory();
1613
$sender = $factory->createSender();
1714

1815
// do not wait for incoming messages
@@ -21,5 +18,3 @@
2118
// send a simple message
2219
$message = 'ping 123';
2320
$sender->send($message, $address);
24-
25-
$loop->run();

examples/send-wait.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,13 @@
55
* Accepts a single argument socket address (defaults to 224.10.20.30:12345)
66
*/
77

8-
use Clue\React\Multicast\Factory;
9-
use Clue\Hexdump\Hexdump;
10-
118
require __DIR__ . '/../vendor/autoload.php';
129

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

15-
$loop = React\EventLoop\Factory::create();
16-
$factory = new Factory($loop);
12+
$factory = new Clue\React\Multicast\Factory();
1713
$sender = $factory->createSender();
18-
$hex = new Hexdump();
14+
$hex = new Clue\Hexdump\Hexdump();
1915

2016
// print a hexdump of every message received
2117
$sender->on('message', function ($data, $remote) use ($hex) {
@@ -26,5 +22,3 @@
2622
// send a simple message
2723
$message = 'ping 123';
2824
$sender->send($message, $address);
29-
30-
$loop->run();

examples/ssdp.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@
33
* UPnP simple service discovery protocol (SSDP)
44
*/
55

6-
use Clue\React\Multicast\Factory;
76

87
require __DIR__ . '/../vendor/autoload.php';
98

109
$address = '239.255.255.250:1900';
1110

12-
$loop = React\EventLoop\Factory::create();
13-
$factory = new Factory($loop);
11+
$factory = new Clue\React\Multicast\Factory();
1412
$sender = $factory->createSender();
1513

1614
// dump all incoming messages
@@ -20,7 +18,7 @@
2018
});
2119

2220
// stop waiting for incoming messages after 3.0s (MX is 2s)
23-
$loop->addTimer(3.0, function () use ($sender) {
21+
Loop::addTimer(3.0, function () use ($sender) {
2422
$sender->pause();
2523
});
2624

@@ -32,5 +30,3 @@
3230
$data .= "ST: ssdp:all\r\n";
3331
$data .= "\r\n";
3432
$sender->send($data, $address);
35-
36-
$loop->run();

src/Factory.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,25 @@
22

33
namespace Clue\React\Multicast;
44

5+
use React\EventLoop\Loop;
56
use React\EventLoop\LoopInterface;
67
use React\Datagram\Socket as DatagramSocket;
78
use BadMethodCallException;
89
use RuntimeException;
910

1011
class Factory
1112
{
13+
/** @var LoopInterface */
1214
private $loop;
1315

1416
/**
1517
* The `Factory` is responsible for creating your [`SocketInterface`](#socketinterface) instances.
16-
* It also registers everything with the main [`EventLoop`](https://github.com/reactphp/event-loop#usage).
18+
*
19+
* This class takes an optional `LoopInterface|null $loop` parameter that can be used to
20+
* pass the event loop instance to use for this object. You can use a `null` value
21+
* here in order to use the [default loop](https://github.com/reactphp/event-loop#loop).
22+
* This value SHOULD NOT be given unless you're sure you want to explicitly use a
23+
* given event loop instance.
1724
*
1825
* ```php
1926
* $loop = React\EventLoop\Factory::create();
@@ -22,9 +29,9 @@ class Factory
2229
*
2330
* @param LoopInterface $loop
2431
*/
25-
public function __construct(LoopInterface $loop)
32+
public function __construct(LoopInterface $loop = null)
2633
{
27-
$this->loop = $loop;
34+
$this->loop = $loop ?: Loop::get();
2835
}
2936

3037
/**

tests/FunctionalTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,15 @@ public function testMultipleReceivers()
5757

5858
$this->loop->run();
5959
}
60+
61+
public function testConstructWithoutLoopAssignsLoopAutomatically()
62+
{
63+
$factory = new Factory();
64+
65+
$ref = new \ReflectionProperty($factory, 'loop');
66+
$ref->setAccessible(true);
67+
$loop = $ref->getValue($factory);
68+
69+
$this->assertInstanceOf('React\EventLoop\LoopInterface', $loop);
70+
}
6071
}

0 commit comments

Comments
 (0)
0