Skip to content

Commit 61790ce

Browse files
authored
Merge pull request #25 from clue-labs/socket
Replace legacy SocketClient with new Socket component and improve forward compatibility with new components
2 parents 528d7c3 + 55b0eda commit 61790ce

File tree

7 files changed

+49
-38
lines changed

7 files changed

+49
-38
lines changed

README.md

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

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

4545
```php
4646
$factory = new Factory($loop, $connector);

composer.json

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,11 @@
1515
},
1616
"require": {
1717
"php": ">=5.3",
18-
"react/event-loop": "~0.4.0|~0.3.0",
19-
"react/socket-client": "^0.5 || ^0.4 || ^0.3",
20-
"react/dns": "^0.4.1 || ^0.3",
18+
"clue/qdatastream": "^0.6",
19+
"react/event-loop": "^1.0 || ^0.5 || ^0.4 || ^0.3",
2120
"react/promise": "~2.0|~1.1",
22-
"react/stream": "^0.4.2",
23-
"clue/qdatastream": "^0.6"
21+
"react/socket": "^1.0 || ^0.8 || ^0.7",
22+
"react/stream": "^1.0 || ^0.7 || ^0.6 || ^0.5 || ^0.4.6"
2423
},
2524
"require-dev": {
2625
"clue/block-react": "^1.1",

examples/connect.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,9 @@
127127

128128
$client->writeClientInit();
129129

130-
$client->on('close', function () use (&$timer) {
130+
$client->on('close', function () use (&$timer, $loop) {
131131
var_dump('CLOSED');
132-
$timer->cancel();
132+
$loop->cancelTimer($timer);
133133
});
134134

135135
$timer = $loop->addTimer(60.0 * 60, function ($timer) use ($client) {

src/Factory.php

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,22 @@
22

33
namespace Clue\React\Quassel;
44

5-
use React\SocketClient\ConnectorInterface;
6-
use React\Stream\Stream;
7-
use Clue\React\Quassel\Io\Handshaker;
85
use Clue\React\Quassel\Io\Prober;
9-
use React\EventLoop\LoopInterface;
10-
use React\Dns\Resolver\Factory as ResolverFactory;
11-
use React\SocketClient\Connector;
126
use Clue\React\Quassel\Io\Protocol;
7+
use React\EventLoop\LoopInterface;
138
use React\Promise;
9+
use React\Socket\ConnectorInterface;
10+
use React\Socket\Connector;
11+
use React\Stream\DuplexStreamInterface;
1412
use InvalidArgumentException;
1513

1614
class Factory
1715
{
1816
public function __construct(LoopInterface $loop, ConnectorInterface $connector = null, Prober $prober = null)
1917
{
20-
if ($connector === null) {
21-
$resolverFactory = new ResolverFactory();
22-
$resolver = $resolverFactory->create('8.8.8.8', $loop);
23-
$connector = new Connector($loop, $resolver);
24-
}
18+
if ($connector === null) {
19+
$connector = new Connector($loop);
20+
}
2521
if ($prober === null) {
2622
$prober = new Prober();
2723
}
@@ -53,14 +49,14 @@ public function createClient($address)
5349
}
5450
}
5551

56-
$promise = $this->connector->create($parts['host'], $parts['port']);
52+
$promise = $this->connector->connect($parts['host'] . ':' . $parts['port']);
5753

5854
// protocol probe not already set
5955
if ($probe === 0) {
6056
$connector = $this->connector;
6157
$prober = $this->prober;
6258

63-
$promise = $promise->then(function (Stream $stream) use ($prober, &$probe, $connector, $parts) {
59+
$promise = $promise->then(function (DuplexStreamInterface $stream) use ($prober, &$probe, $connector, $parts) {
6460
return $prober->probe($stream)->then(
6561
function ($ret) use (&$probe, $stream) {
6662
// probe returned successfully, create new client for this stream
@@ -73,7 +69,7 @@ function ($e) use ($connector, $parts) {
7369
if ($e->getCode() === Prober::ERROR_CLOSED) {
7470
// legacy servers will terminate connection while probing
7571
// let's just open a new connection and assume default probe
76-
return $connector->create($parts['host'], $parts['port']);
72+
return $connector->connect($parts['host'] . ':' . $parts['port']);
7773
}
7874
throw $e;
7975
}
@@ -82,7 +78,7 @@ function ($e) use ($connector, $parts) {
8278
}
8379

8480
return $promise->then(
85-
function (Stream $stream) use (&$probe) {
81+
function (DuplexStreamInterface $stream) use (&$probe) {
8682
return new Client($stream, Protocol::createFromProbe($probe));
8783
}
8884
);

src/Io/Prober.php

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

33
namespace Clue\React\Quassel\Io;
44

5-
use React\Stream\Stream;
5+
use React\Stream\DuplexStreamInterface;
66
use React\Promise\Deferred;
77

88
class Prober
@@ -20,7 +20,7 @@ public function __construct(Binary $binary = null)
2020
$this->binary = $binary;
2121
}
2222

23-
public function probe(Stream $stream, $compression = false, $encryption = false)
23+
public function probe(DuplexStreamInterface $stream, $compression = false, $encryption = false)
2424
{
2525
$magic = Protocol::MAGIC;
2626
if ($compression) {

tests/ClientTest.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
use Clue\React\Quassel\Io\Protocol;
55
use Clue\QDataStream\QVariant;
66
use Clue\QDataStream\Types;
7+
use React\Stream\ThroughStream;
78

89
class ClientTest extends TestCase
910
{
1011
public function setUp()
1112
{
12-
$this->stream = $this->getMockBuilder('React\Stream\Stream')->disableOriginalConstructor()->setMethods(array('write', 'end', 'close', 'pause', 'resume', 'isReadable', 'isWritable'))->getMock();
13+
$this->stream = $this->getMockBuilder('React\Stream\DuplexStreamInterface')->getMock();
1314
$this->protocol = $this->getMockBuilder('Clue\React\Quassel\Io\Protocol')->disableOriginalConstructor()->getMock();
1415
$this->splitter = $this->getMockBuilder('Clue\React\Quassel\Io\PacketSplitter')->disableOriginalConstructor()->getMock();
1516

@@ -61,34 +62,49 @@ public function testPipeWillReturnDestStream()
6162

6263
public function testCloseEventWillBeForwarded()
6364
{
65+
$this->stream = new ThroughStream();
66+
$this->client = new Client($this->stream, $this->protocol, $this->splitter);
67+
6468
$this->client->on('close', $this->expectCallableOnce());
6569
$this->stream->emit('close');
6670
}
6771

6872
public function testDrainEventWillBeForwarded()
6973
{
74+
$this->stream = new ThroughStream();
75+
$this->client = new Client($this->stream, $this->protocol, $this->splitter);
76+
7077
$this->client->on('drain', $this->expectCallableOnce());
7178
$this->stream->emit('drain');
7279
}
7380

7481
public function testEndEventWillBeForwardedAndClose()
7582
{
83+
$this->stream = new ThroughStream();
84+
$this->client = new Client($this->stream, $this->protocol, $this->splitter);
85+
7686
$this->client->on('end', $this->expectCallableOnce());
77-
$this->stream->expects($this->once())->method('close');
78-
$this->stream->emit('end');
87+
$this->stream->on('close', $this->expectCallableOnce());
88+
$this->stream->end();
7989
}
8090

8191
public function testErrorEventWillBeForwardedAndClose()
8292
{
93+
$this->stream = new ThroughStream();
94+
$this->client = new Client($this->stream, $this->protocol, $this->splitter);
95+
8396
$e = new \RuntimeException();
8497

8598
$this->client->on('error', $this->expectCallableOnceWith($e));
86-
$this->stream->expects($this->once())->method('close');
99+
$this->stream->on('close', $this->expectCallableOnce());
87100
$this->stream->emit('error', array($e));
88101
}
89102

90103
public function testDataEventWillNotBeForwardedIfItIsAnIncompletePacket()
91104
{
105+
$this->stream = new ThroughStream();
106+
$this->client = new Client($this->stream, $this->protocol, $this->splitter);
107+
92108
$this->splitter->expects($this->once())->method('push')->with("hello", array($this->client, 'handlePacket'));
93109
$this->client->on('data', $this->expectCallableNever());
94110

tests/FactoryTest.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class FactoryTest extends TestCase
1010
public function setUp()
1111
{
1212
$this->loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
13-
$this->connector = $this->getMockBuilder('React\SocketClient\ConnectorInterface')->getMock();
13+
$this->connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock();
1414
$this->prober = $this->getMockBuilder('Clue\React\Quassel\Io\Prober')->disableOriginalConstructor()->getMock();
1515

1616
$this->factory = new Factory($this->loop, $this->connector, $this->prober);
@@ -24,46 +24,46 @@ public function testCtorOptionalArgs()
2424
public function testPassHostnameAndDefaultPortToConnector()
2525
{
2626
$deferred = new Deferred();
27-
$this->connector->expects($this->once())->method('create')->with($this->equalTo('example.com', 4242))->will($this->returnValue($deferred->promise()));
27+
$this->connector->expects($this->once())->method('connect')->with($this->equalTo('example.com:4242'))->will($this->returnValue($deferred->promise()));
2828
$this->factory->createClient('example.com');
2929
}
3030

3131
public function testPassHostnameAndPortToConnector()
3232
{
3333
$deferred = new Deferred();
34-
$this->connector->expects($this->once())->method('create')->with($this->equalTo('example.com', 1234))->will($this->returnValue($deferred->promise()));
34+
$this->connector->expects($this->once())->method('connect')->with($this->equalTo('example.com:1234'))->will($this->returnValue($deferred->promise()));
3535
$this->factory->createClient('example.com:1234');
3636
}
3737

3838
public function testInvalidUriWillRejectWithoutConnecting()
3939
{
40-
$this->connector->expects($this->never())->method('create');
40+
$this->connector->expects($this->never())->method('connect');
4141

4242
$this->expectPromiseReject($this->factory->createClient('///'));
4343
}
4444

4545
public function testInvalidSchemeWillRejectWithoutConnecting()
4646
{
47-
$this->connector->expects($this->never())->method('create');
47+
$this->connector->expects($this->never())->method('connect');
4848

4949
$this->expectPromiseReject($this->factory->createClient('https://example.com:1234/'));
5050
}
5151

5252
public function testWillInvokeProberAfterConnecting()
5353
{
54-
$stream = $this->getMockBuilder('React\Stream\Stream')->disableOriginalConstructor()->getMock();
54+
$stream = $this->getMockBuilder('React\Stream\DuplexStreamInterface')->getMock();
5555

56-
$this->connector->expects($this->once())->method('create')->will($this->returnValue(Promise\resolve($stream)));
56+
$this->connector->expects($this->once())->method('connect')->will($this->returnValue(Promise\resolve($stream)));
5757
$this->prober->expects($this->once())->method('probe')->with($this->equalTo($stream))->will($this->returnValue(Promise\resolve(Protocol::TYPE_DATASTREAM)));
5858

5959
$this->expectPromiseResolve($this->factory->createClient('localhost'));
6060
}
6161

6262
public function testWillNotInvokeProberIfSchemeIsProtocol()
6363
{
64-
$stream = $this->getMockBuilder('React\Stream\Stream')->disableOriginalConstructor()->getMock();
64+
$stream = $this->getMockBuilder('React\Stream\DuplexStreamInterface')->getMock();
6565

66-
$this->connector->expects($this->once())->method('create')->will($this->returnValue(Promise\resolve($stream)));
66+
$this->connector->expects($this->once())->method('connect')->will($this->returnValue(Promise\resolve($stream)));
6767
$this->prober->expects($this->never())->method('probe');
6868

6969
$this->expectPromiseResolve($this->factory->createClient('legacy://localhost'));

0 commit comments

Comments
 (0)