Skip to content

Commit 8cda514

Browse files
authored
Merge pull request #39 from clue-labs/socket
Replace deprecated SocketClient with new Socket component and improve forward compatibility with upcoming ReactPHP components
2 parents 4d4c45d + 488e54b commit 8cda514

8 files changed

Lines changed: 37 additions & 68 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ $factory = new Factory($loop);
9191
```
9292

9393
If you need custom DNS or proxy settings, you can explicitly pass a
94-
custom instance of the [`ConnectorInterface`](https://github.com/reactphp/socket-client#connectorinterface):
94+
custom instance of the [`ConnectorInterface`](https://github.com/reactphp/socket#connectorinterface):
9595

9696
```php
9797
$factory = new Factory($loop, $connector);

composer.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@
1212
],
1313
"require": {
1414
"php": ">=5.3",
15-
"evenement/evenement": "~1.0|~2.0",
16-
"react/promise": "~1.0|~2.0",
17-
"react/socket-client": "^0.5 || ^0.4 || ^0.3",
18-
"react/event-loop": "0.3.*|0.4.*"
15+
"evenement/evenement": "^3.0 || ^2.0 || ^1.0",
16+
"react/event-loop": "^1.0 || ^0.5 || ^0.4 || ^0.3",
17+
"react/promise": "^2.0 || ^1.0",
18+
"react/socket": "^1.0 || ^0.8.2"
1919
},
2020
"require-dev": {
21+
"clue/block-react": "^1.2",
2122
"phpunit/phpunit": "^5.0 || ^4.8"
2223
},
2324
"autoload": {

src/Client.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22

33
namespace Clue\React\Ami;
44

5-
use Clue\React\Ami\Protocol\Event;
65
use Clue\React\Ami\Protocol\Action;
7-
use Evenement\EventEmitter;
8-
use React\Stream\Stream;
6+
use Clue\React\Ami\Protocol\ErrorException;
7+
use Clue\React\Ami\Protocol\Event;
8+
use Clue\React\Ami\Protocol\Message;
99
use Clue\React\Ami\Protocol\Parser;
10+
use Clue\React\Ami\Protocol\UnexpectedMessageException;
11+
use Evenement\EventEmitter;
1012
use React\Promise\Deferred;
13+
use React\Socket\ConnectionInterface;
1114
use Exception;
1215
use UnexpectedValueException;
13-
use Clue\React\Ami\Protocol\Message;
14-
use Clue\React\Ami\Protocol\ErrorException;
15-
use Clue\React\Ami\Protocol\UnexpectedMessageException;
1616

1717
class Client extends EventEmitter
1818
{
@@ -23,7 +23,7 @@ class Client extends EventEmitter
2323

2424
private $actionId = 0;
2525

26-
public function __construct(Stream $stream, Parser $parser = null)
26+
public function __construct(ConnectionInterface $stream, Parser $parser = null)
2727
{
2828
if ($parser === null) {
2929
$parser = new Parser();
@@ -47,8 +47,6 @@ public function __construct(Stream $stream, Parser $parser = null)
4747
$this->on('error', array($that, 'close'));
4848

4949
$this->stream->on('close', array ($that, 'close'));
50-
51-
$this->stream->resume();
5250
}
5351

5452
public function request(Action $message)

src/Factory.php

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,47 +3,40 @@
33
namespace Clue\React\Ami;
44

55
use React\EventLoop\LoopInterface;
6-
use React\SocketClient\ConnectorInterface;
7-
use React\SocketClient\Connector;
8-
use React\SocketClient\SecureConnector;
9-
use React\Dns\Resolver\Factory as ResolverFactory;
10-
use React\Stream\Stream;
6+
use React\Socket\ConnectionInterface;
7+
use React\Socket\Connector;
8+
use React\Socket\ConnectorInterface;
119
use InvalidArgumentException;
1210

1311
class Factory
1412
{
1513
private $loop;
1614
private $connector;
17-
private $secureConnector;
1815

19-
public function __construct(LoopInterface $loop, ConnectorInterface $connector = null, ConnectorInterface $secureConnector = null)
16+
public function __construct(LoopInterface $loop, ConnectorInterface $connector = null)
2017
{
2118
if ($connector === null) {
22-
$resolverFactory = new ResolverFactory();
23-
$connector = new Connector($loop, $resolverFactory->create('8.8.8.8', $loop));
24-
}
25-
if ($secureConnector === null) {
26-
$secureConnector = new SecureConnector($connector, $loop);
19+
$connector = new Connector($loop);
2720
}
2821

2922
$this->loop = $loop;
3023
$this->connector = $connector;
31-
$this->secureConnector = $secureConnector;
3224
}
3325

3426
public function createClient($address = null)
3527
{
3628
$parts = $this->parseUrl($address);
3729

38-
$secure = (isset($parts['scheme']) && $parts['scheme'] !== 'tcp');
39-
$connector = $secure ? $this->secureConnector : $this->connector;
30+
if (isset($parts['scheme']) && $parts['scheme'] !== 'tcp') {
31+
$parts['host'] = 'tls://' . $parts['host'];
32+
}
4033

41-
$promise = $connector->create($parts['host'], $parts['port'])->then(function (Stream $stream) {
34+
$promise = $this->connector->connect($parts['host'] . ':' . $parts['port'])->then(function (ConnectionInterface $stream) {
4235
return new Client($stream);
4336
});
4437

4538
if (isset($parts['user'])) {
46-
$promise = $promise->then(function (Client $client) use ($parts, $secure) {
39+
$promise = $promise->then(function (Client $client) use ($parts) {
4740
$sender = new ActionSender($client);
4841

4942
return $sender->login($parts['user'], $parts['pass'])->then(
@@ -79,10 +72,6 @@ private function parseUrl($target)
7972
$parts['port'] = '5038';
8073
}
8174

82-
if ($parts['host'] === 'localhost') {
83-
$parts['host'] = '127.0.0.1';
84-
}
85-
8675
return $parts;
8776
}
8877
}

tests/ClientTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,19 @@ public function testClosingStreamClosesClient()
1616

1717
$client->on('close', $this->expectCallableOnce());
1818

19-
$stream->close();
20-
//$stream->emit('close', array($this));
19+
$stream->emit('close');
2120
}
2221

2322
public function testParserExceptionForwardsErrorAndClosesClient()
2423
{
2524
$stream = $this->createStreamMock();
25+
$stream->expects($this->once())->method('close');
26+
2627
$parser = new Parser();
2728

2829
$client = new Client($stream, $parser);
2930

3031
$client->on('error', $this->expectCallableOnce());
31-
$client->on('close', $this->expectCallableOnce());
3232

3333
$stream->emit('data', array("invalid chunk\r\n\r\ninvalid chunk\r\n\r\n"));
3434
}
@@ -45,6 +45,6 @@ public function testUnexpectedResponseEmitsErrorAndClosesClient()
4545

4646
private function createStreamMock()
4747
{
48-
return new Stream(fopen('php://memory', 'r+'), $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock());
48+
return $this->getMockBuilder('React\Socket\Connection')->disableOriginalConstructor()->setMethods(array('write', 'close'))->getMock();
4949
}
5050
}

tests/CollectorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function testCollectingSIPEvents()
5151

5252
private function createClientMock()
5353
{
54-
$stream = $this->getMockBuilder('React\Stream\Stream')->disableOriginalConstructor()->getMock();
54+
$stream = $this->getMockBuilder('React\Socket\Connection')->disableOriginalConstructor()->getMock();
5555

5656
$client = $this->getMockBuilder('Clue\React\Ami\Client')->setMethods(array('createAction'))->setConstructorArgs(array($stream))->getMock();
5757

tests/FactoryTest.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,14 @@ class FactoryTest extends TestCase
66
{
77
private $loop;
88
private $tcp;
9-
private $tls;
109
private $factory;
1110

1211
public function setUp()
1312
{
1413
$this->loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
15-
$this->tcp = $this->getMockBuilder('React\SocketClient\ConnectorInterface')->getMock();
16-
$this->tls = $this->getMockBuilder('React\SocketClient\ConnectorInterface')->getMock();
14+
$this->tcp = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock();
1715

18-
$this->factory = new Factory($this->loop, $this->tcp, $this->tls);
16+
$this->factory = new Factory($this->loop, $this->tcp);
1917
}
2018

2119
public function testDefaultCtor()
@@ -26,23 +24,23 @@ public function testDefaultCtor()
2624
public function testCreateClientUsesTcpConnectorWithDefaultLocation()
2725
{
2826
$promise = new Promise(function () { });
29-
$this->tcp->expects($this->once())->method('create')->with('127.0.0.1', 5038)->willReturn($promise);
27+
$this->tcp->expects($this->once())->method('connect')->with('127.0.0.1:5038')->willReturn($promise);
3028

3129
$this->factory->createClient();
3230
}
3331

34-
public function testCreateClientUsesTcpConnectorWithLocalhostLocation()
32+
public function testCreateClientUsesDefaultPortForTcpConnection()
3533
{
3634
$promise = new Promise(function () { });
37-
$this->tcp->expects($this->once())->method('create')->with('127.0.0.1', 5038)->willReturn($promise);
35+
$this->tcp->expects($this->once())->method('connect')->with('localhost:5038')->willReturn($promise);
3836

3937
$this->factory->createClient('localhost');
4038
}
4139

4240
public function testCreateClientUsesTlsConnectorWithTlsLocation()
4341
{
4442
$promise = new Promise(function () { });
45-
$this->tls->expects($this->once())->method('create')->with('ami.local', 1234)->willReturn($promise);
43+
$this->tcp->expects($this->once())->method('connect')->with('tls://ami.local:1234')->willReturn($promise);
4644

4745
$this->factory->createClient('tls://ami.local:1234');
4846
}

tests/FunctionalTest.php

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<?php
22

33
use Clue\React\Ami\Factory;
4-
use React\Promise\PromiseInterface;
54
use Clue\React\Ami\Client;
65
use Clue\React\Ami\ActionSender;
7-
use Clue\React\Ami\Protocol\Response;
6+
use Clue\React\Block;
7+
use React\Promise\PromiseInterface;
88

99
class FunctionalTest extends TestCase
1010
{
@@ -90,23 +90,6 @@ public function testSendRejectedAfterClose(Client $client)
9090

9191
private function waitFor(PromiseInterface $promise)
9292
{
93-
$resolved = null;
94-
$exception = null;
95-
96-
$promise->then(function ($c) use (&$resolved) {
97-
$resolved = $c;
98-
}, function($error) use (&$exception) {
99-
$exception = $error;
100-
});
101-
102-
while ($resolved === null && $exception === null) {
103-
self::$loop->tick();
104-
}
105-
106-
if ($exception !== null) {
107-
throw $exception;
108-
}
109-
110-
return $resolved;
93+
return Block\await($promise, self::$loop, 5.0);
11194
}
11295
}

0 commit comments

Comments
 (0)