Skip to content

Simplify usage by supporting new default loop #17

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

Merged
merged 1 commit into from
Sep 3, 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
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,12 @@ as defined in [RFC 6763](http://tools.ietf.org/html/rfc6763).
Once [installed](#install), you can use the following code to look up the address of a local domain name:

```php
$loop = React\EventLoop\Factory::create();
$factory = new Factory($loop);
$factory = new Factory();
$resolver = $factory->createResolver();

$resolver->lookup('hostname.local')->then(function ($ip) {
echo 'Found: ' . $ip . PHP_EOL;
});

$loop->run();
```

See also the [examples](examples).
Expand All @@ -52,13 +49,17 @@ See also the [examples](examples).
### Factory

The `Factory` is responsible for creating your [`Resolver`](#resolver) instance.
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.

#### createResolver()

The `createResolver()` method can be used to create a mDNS resolver instance that sends multicast DNS queries and waits for incoming unicast DNS responses. It returns a [`Resolver`](#resolver) instance.
Expand Down Expand Up @@ -106,8 +107,7 @@ The resulting blocking code could look something like this:
```php
use Clue\React\Block;

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

$promise = $resolver->lookup('me.local');
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"php": ">=5.3",
"clue/multicast-react": "^1.0 || ^0.2",
"react/dns": "~0.4.0|~0.3.0",
"react/event-loop": "^1.0 || ^0.5 || ^0.4 || ^0.3",
"react/event-loop": "^1.2",
"react/promise": "^2.1 || ^1.2.1"
},
"require-dev": {
Expand Down
5 changes: 1 addition & 4 deletions examples/lookup.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@

$name = isset($argv[1]) ? $argv[1] : 'me.local';

$loop = React\EventLoop\Factory::create();
$factory = new Clue\React\Mdns\Factory($loop);
$factory = new Clue\React\Mdns\Factory();
$mdns = $factory->createResolver();

$mdns->resolve($name)->then('e', 'e');

function e($v) {
echo $v . PHP_EOL;
}

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

namespace Clue\React\Mdns;

use React\EventLoop\Loop;
use React\EventLoop\LoopInterface;
use React\Dns\Query\ExecutorInterface;
use React\Dns\Resolver\Resolver;
Expand All @@ -10,17 +11,20 @@ class Factory
{
const DNS = '224.0.0.251:5353';

/** @var LoopInterface */
private $loop;

/** @var ExecutorInterface */
private $executor;

public function __construct(LoopInterface $loop, ExecutorInterface $executor = null)
/**
* @param ?LoopInterface $loop
* @param ?ExecutorInterface $executor
*/
public function __construct(LoopInterface $loop = null, ExecutorInterface $executor = null)
{
if ($executor === null) {
$executor = new MulticastExecutor($loop);
}

$this->loop = $loop;
$this->executor = $executor;
$this->loop = $loop ?: Loop::get();
$this->executor = $executor ?: new MulticastExecutor($loop);
}

public function createResolver()
Expand Down
37 changes: 22 additions & 15 deletions src/MulticastExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use React\Dns\Model\Message;
use React\Dns\Protocol\Parser;
use React\Dns\Protocol\BinaryDumper;
use React\EventLoop\Loop;
use React\EventLoop\LoopInterface;
use React\Promise\Deferred;
use Clue\React\Multicast\Factory as DatagramFactory;
Expand All @@ -21,29 +22,35 @@
*/
class MulticastExecutor implements ExecutorInterface
{
/** @var LoopInterface */
private $loop;

/** @var Parser */
private $parser;

/** @var BinaryDumper */
private $dumper;

/** @var number */
private $timeout;

/** @var DatagramFactory */
private $factory;

public function __construct(LoopInterface $loop, Parser $parser = null, BinaryDumper $dumper = null, $timeout = 5, DatagramFactory $factory = null)
/**
* @param ?LoopInterface $loop
* @param ?Parser $parser
* @param ?BinaryDumper $dumper
* @param number $timeout
* @param ?DatagramFactory $factory
*/
public function __construct(LoopInterface $loop = null, Parser $parser = null, BinaryDumper $dumper = null, $timeout = 5, DatagramFactory $factory = null)
{
if ($parser === null) {
$parser = new Parser();
}
if ($dumper === null) {
$dumper = new BinaryDumper();
}
if ($factory === null) {
$factory = new DatagramFactory($loop);
}

$this->loop = $loop;
$this->parser = $parser;
$this->dumper = $dumper;
$this->loop = $loop ?: Loop::get();
$this->parser = $parser ?: new Parser();
$this->dumper = $dumper ?: new BinaryDumper();
$this->timeout = $timeout;
$this->factory = $factory;
$this->factory = $factory ?: new DatagramFactory($this->loop);
}

public function query($nameserver, Query $query)
Expand Down
11 changes: 11 additions & 0 deletions tests/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,15 @@ public function testCreateResolver()

$this->assertInstanceOf('React\Dns\Resolver\Resolver', $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);
}
}
11 changes: 11 additions & 0 deletions tests/MulticastExecutorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ public function testQueryWillReturnPromise()
$this->assertInstanceOf('React\Promise\PromiseInterface', $ret);
}

public function testConstructWithoutLoopAssignsLoopAutomatically()
{
$executor = new MulticastExecutor();

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

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

public function testCancellingPromiseWillCloseSocketAndReject()
{
$nameserver = Factory::DNS;
Expand Down