Skip to content

Commit

Permalink
Merge pull request #42 from clue-labs/default-loop
Browse files Browse the repository at this point in the history
Simplify usage by supporting new default loop
  • Loading branch information
clue authored Jul 11, 2021
2 parents 0410d08 + c85436d commit 70c5dc0
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 22 deletions.
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ Once [installed](#install), you can use the following code to connect to an UDP
`localhost:1234` and send and receive UDP datagrams:

```php
$loop = React\EventLoop\Factory::create();
$factory = new React\Datagram\Factory($loop);
$factory = new React\Datagram\Factory();

$factory->createClient('localhost:1234')->then(function (React\Datagram\Socket $client) {
$client->send('first');
Expand All @@ -20,8 +19,6 @@ $factory->createClient('localhost:1234')->then(function (React\Datagram\Socket $
echo 'received "' . $message . '" from ' . $serverAddress. PHP_EOL;
});
});

$loop->run();
```

See also the [examples](examples).
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"require": {
"php": ">=5.3",
"evenement/evenement": "^3.0 || ^2.0 || ^1.0",
"react/event-loop": "^1.0 || ^0.5 || ^0.4 || ^0.3",
"react/event-loop": "^1.2",
"react/dns": "^1.7",
"react/promise": "~2.1|~1.2"
},
Expand Down
17 changes: 8 additions & 9 deletions examples/client.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<?php

use React\EventLoop\Loop;

require_once __DIR__.'/../vendor/autoload.php';

$loop = React\EventLoop\Factory::create();
$factory = new React\Datagram\Factory($loop);
$factory = new React\Datagram\Factory();

$factory->createClient('localhost:1234')->then(function (React\Datagram\Socket $client) use ($loop) {
$factory->createClient('localhost:1234')->then(function (React\Datagram\Socket $client) {
$client->send('first');

$client->on('message', function($message, $serverAddress, $client) {
Expand All @@ -17,24 +18,22 @@
});

$n = 0;
$tid = $loop->addPeriodicTimer(2.0, function() use ($client, &$n) {
$tid = Loop::addPeriodicTimer(2.0, function() use ($client, &$n) {
$client->send('tick' . ++$n);
});

// read input from STDIN and forward everything to server
$loop->addReadStream(STDIN, function () use ($client, $loop, $tid) {
Loop::addReadStream(STDIN, function () use ($client, $tid) {
$msg = fgets(STDIN, 2000);
if ($msg === false) {
// EOF => flush client and stop perodic sending and waiting for input
$client->end();
$loop->cancelTimer($tid);
$loop->removeReadStream(STDIN);
Loop::cancelTimer($tid);
Loop::removeReadStream(STDIN);
} else {
$client->send(trim($msg));
}
});
}, function($error) {
echo 'ERROR: ' . $error->getMessage() . PHP_EOL;
});

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

require_once __DIR__.'/../vendor/autoload.php';

$loop = React\EventLoop\Factory::create();
$factory = new React\Datagram\Factory($loop);
$factory = new React\Datagram\Factory();

$factory->createServer('localhost:1234')->then(function (React\Datagram\Socket $server) {
$server->on('message', function($message, $address, $server) {
Expand All @@ -12,5 +11,3 @@
echo 'client ' . $address . ': ' . $message . PHP_EOL;
});
});

$loop->run();
12 changes: 10 additions & 2 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use React\Dns\Config\Config as DnsConfig;
use React\Dns\Resolver\Factory as DnsFactory;
use React\Dns\Resolver\ResolverInterface;
use React\EventLoop\Loop;
use React\EventLoop\LoopInterface;
use React\Promise;
use React\Promise\CancellablePromiseInterface;
Expand All @@ -18,13 +19,20 @@ class Factory

/**
*
* @param LoopInterface $loop
* 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.
*
* @param ?LoopInterface $loop
* @param ?ResolverInterface $resolver Resolver instance to use. Will otherwise
* try to load the system default DNS config or fall back to using
* Google's public DNS 8.8.8.8
*/
public function __construct(LoopInterface $loop, ResolverInterface $resolver = null)
public function __construct(LoopInterface $loop = null, ResolverInterface $resolver = null)
{
$loop = $loop ?: Loop::get();
if ($resolver === null) {
// try to load nameservers from system config or default to Google's public DNS
$config = DnsConfig::loadSystemConfigBlocking();
Expand Down
15 changes: 13 additions & 2 deletions tests/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace React\Tests\Datagram;

use React\Datagram\Socket;
use React\Datagram\Factory;
use Clue\React\Block;
use React\Datagram\Factory;
use React\Datagram\Socket;
use React\Promise;

class FactoryTest extends TestCase
Expand All @@ -23,6 +23,17 @@ public function setUpFactory()
$this->factory = new Factory($this->loop, $this->resolver);
}

public function testConstructWithoutLoopAssignsLoopAutomatically()
{
$factory = new Factory();

$ref = new \ReflectionProperty($factory, 'loop');
$ref->setAccessible(true);
$loop = $ref->getValue($factory);

$this->assertInstanceOf('React\EventLoop\LoopInterface', $loop);
}

public function testCreateClient()
{
$this->resolver->expects($this->never())->method('resolve');
Expand Down

0 comments on commit 70c5dc0

Please sign in to comment.