Skip to content

Commit 78378be

Browse files
committed
Merge pull request #1 from clue/socket-client
Update to use react/socket-client
2 parents 2568dc9 + 7535b5f commit 78378be

21 files changed

+491
-65
lines changed

.travis.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
language: php
2+
php:
3+
- 5.4
4+
- 5.3
5+
before_script:
6+
- composer install --dev --prefer-source --no-interaction
7+
script:
8+
- phpunit --coverage-text

ConnectionManager/Extra/ConnectionManagerDelay.php

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

33
namespace ConnectionManager\Extra;
44

5-
use ConnectionManager\ConnectionManagerInterface;
5+
use React\SocketClient\ConnectorInterface;
66
use React\EventLoop\LoopInterface;
77
use React\Promise\Deferred;
88

9-
class ConnectionManagerDelay implements ConnectionManagerInterface
9+
class ConnectionManagerDelay implements ConnectorInterface
1010
{
1111
private $connectionManager;
1212
private $loop;
1313
private $delay;
1414

15-
public function __construct(ConnectionManagerInterface $connectionManager, LoopInterface $loop, $delay)
15+
public function __construct(ConnectorInterface $connectionManager, LoopInterface $loop, $delay)
1616
{
1717
$this->connectionManager = $connectionManager;
1818
$this->loop = $loop;
1919
$this->delay = $delay;
2020
}
2121

22-
public function getConnection($host, $port)
22+
public function create($host, $port)
2323
{
2424
$deferred = new Deferred();
2525

2626
$connectionManager = $this->connectionManager;
2727
$this->loop->addTimer($this->delay, function() use ($deferred, $connectionManager, $host, $port) {
28-
$connectionManager->getConnect($host, $port)->then(
28+
$connectionManager->create($host, $port)->then(
2929
array($deferred, 'resolve'),
3030
array($deferred, 'reject')
3131
);

ConnectionManager/Extra/ConnectionManagerReject.php

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

33
namespace ConnectionManager\Extra;
44

5-
use ConnectionManager\ConnectionManagerInterface;
5+
use React\SocketClient\ConnectorInterface;
66
use React\Promise\When;
77
use \Exception;
88

99
// a simple connection manager that rejects every single connection attempt
10-
class ConnectionManagerReject implements ConnectionManagerInterface
10+
class ConnectionManagerReject implements ConnectorInterface
1111
{
12-
public function getConnection($host, $port)
12+
public function create($host, $port)
1313
{
1414
return When::reject(new Exception('Connection rejected'));
1515
}

ConnectionManager/Extra/ConnectionManagerRepeat.php

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

33
namespace ConnectionManager\Extra;
44

5-
use ConnectionManager\ConnectionManagerInterface;
5+
use React\SocketClient\ConnectorInterface;
66
use \InvalidArgumentException;
77
use React\Promise\Deferred;
88
use \Exception;
99

10-
class ConnectionManagerRepeat implements ConnectionManagerInterface
10+
class ConnectionManagerRepeat implements ConnectorInterface
1111
{
1212
protected $connectionManager;
1313
protected $maximumRepetitions;
1414

15-
public function __construct(ConnectionManagerInterface $connectionManager, $maximumRepetitons)
15+
public function __construct(ConnectorInterface $connectionManager, $maximumRepetitons)
1616
{
1717
if ($maximumRepetitons < 1) {
1818
throw new InvalidArgumentException('Maximum number of repetitions must be >= 1');
@@ -21,15 +21,15 @@ public function __construct(ConnectionManagerInterface $connectionManager, $maxi
2121
$this->maximumRepetitions = $maximumRepetitons;
2222
}
2323

24-
public function getConnection($host, $port)
24+
public function create($host, $port)
2525
{
2626
return $this->tryConnection($this->maximumRepetitions, $host, $port);
2727
}
2828

2929
public function tryConnection($repeat, $host, $port)
3030
{
3131
$that = $this;
32-
return $this->connectionManager->getConnection($host, $port)->then(
32+
return $this->connectionManager->create($host, $port)->then(
3333
null,
3434
function ($error) use ($repeat, $that) {
3535
if ($repeat > 0) {

ConnectionManager/Extra/ConnectionManagerSwappable.php

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

33
namespace ConnectionManager\Extra;
44

5-
use ConnectionManager\ConnectionManagerInterface;
5+
use React\SocketClient\ConnectorInterface;
66

77
// connection manager decorator which simplifies exchanging the actual connection manager during runtime
8-
class ConnectionManagerSwappable implements ConnectionManagerInterface
8+
class ConnectionManagerSwappable implements ConnectorInterface
99
{
1010
protected $connectionManager;
1111

12-
public function __construct(ConnectionManagerInterface $connectionManager)
12+
public function __construct(ConnectorInterface $connectionManager)
1313
{
1414
$this->connectionManager = $connectionManager;
1515
}
1616

17-
public function getConnection($host, $port)
17+
public function create($host, $port)
1818
{
19-
return $this->connectionManager->getConnection($host, $port);
19+
return $this->connectionManager->create($host, $port);
2020
}
2121

22-
public function setConnectionManager(ConnectionManagerInterface $connectionManager)
22+
public function setConnectionManager(ConnectorInterface $connectionManager)
2323
{
2424
$this->connectionManager = $connectionManager;
2525
}

ConnectionManager/Extra/ConnectionManagerTimeout.php

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,47 @@
22

33
namespace ConnectionManager\Extra;
44

5-
use ConnectionManager\ConnectionManagerInterface;
5+
use React\SocketClient\ConnectorInterface;
66
use React\EventLoop\LoopInterface;
77
use React\Promise\Deferred;
8+
use Exception;
89

9-
class ConnectionManagerTimeout implements ConnectionManagerInterface
10+
class ConnectionManagerTimeout implements ConnectorInterface
1011
{
1112
private $connectionManager;
1213
private $loop;
1314
private $timeout;
14-
15-
public function __construct(ConnectionManagerInterface $connectionManager, LoopInterface $loop, $timeout)
15+
16+
public function __construct(ConnectorInterface $connectionManager, LoopInterface $loop, $timeout)
1617
{
1718
$this->connectionManager = $connectionManager;
1819
$this->loop = $loop;
1920
$this->timeout = $timeout;
2021
}
21-
22-
public function getConnection($host, $port)
22+
23+
public function create($host, $port)
2324
{
2425
$deferred = new Deferred();
2526
$timedout = false;
26-
27+
2728
$tid = $this->loop->addTimer($this->timeout, function() use ($deferred, &$timedout) {
2829
$deferred->reject(new Exception('Connection attempt timed out'));
2930
$timedout = true;
3031
// TODO: find a proper way to actually cancel the connection
3132
});
32-
33+
3334
$loop = $this->loop;
34-
$this->connectionManager->getConnection($host, $port)->then(function ($connection) use ($tid, $loop, &$timedout, $deferred) {
35+
$this->connectionManager->create($host, $port)->then(function ($connection) use ($tid, $loop, &$timedout, $deferred) {
3536
if ($timedout) {
3637
// connection successfully established but timeout already expired => close successful connection
3738
$connection->end();
3839
} else {
39-
$loop->removeTimeout($tid);
40+
$loop->cancelTimer($tid);
4041
$deferred->resolve($connection);
4142
}
42-
}, function ($error) use ($loop, $tid) {
43-
$loop->removeTimeout($tid);
44-
throw $error;
43+
}, function ($error) use ($loop, $tid, $deferred) {
44+
$loop->cancelTimer($tid);
45+
$deferred->reject($error);
4546
});
4647
return $deferred->promise();
4748
}

ConnectionManager/Extra/Multiple/ConnectionManagerConsecutive.php

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

33
namespace ConnectionManager\Extra\Multiple;
44

5-
use ConnectionManager\ConnectionManagerInterface;
5+
use React\SocketClient\ConnectorInterface;
66
use React\Promise\When;
77
use \UnderflowException;
88

9-
class ConnectionManagerConsecutive implements ConnectionManagerInterface
9+
class ConnectionManagerConsecutive implements ConnectorInterface
1010
{
1111
protected $managers = array();
1212

13-
public function addConnectionManager(ConnectionManagerInterface $connectionManager)
13+
public function addConnectionManager(ConnectorInterface $connectionManager)
1414
{
1515
$this->managers []= $connectionManager;
1616
}
1717

18-
public function getConnection($host, $port)
18+
public function create($host, $port)
1919
{
2020
return $this->tryConnection($this->managers, $host, $port);
2121
}
2222

2323
/**
2424
*
25-
* @param ConnectionManagerInterface[] $managers
25+
* @param ConnectorInterface[] $managers
2626
* @param string $host
2727
* @param int $port
2828
* @return Promise
@@ -35,7 +35,7 @@ public function tryConnection(array $managers, $host, $port)
3535
}
3636
$manager = array_shift($managers);
3737
$that = $this;
38-
return $manager->getConnection($host,$port)->then(null, function() use ($that, $managers, $host, $port) {
38+
return $manager->create($host,$port)->then(null, function() use ($that, $managers, $host, $port) {
3939
// connection failed, re-try with remaining connection managers
4040
return $that->tryConnection($managers, $host, $port);
4141
});

ConnectionManager/Extra/Multiple/ConnectionManagerRandom.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
class ConnectionManagerRandom extends ConnectionManagerConsecutive
66
{
7-
public function getConnection($host, $port)
7+
public function create($host, $port)
88
{
99
$managers = $this->managers;
1010
shuffle($managers);

ConnectionManager/Extra/Multiple/ConnectionManagerSelective.php

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

33
namespace ConnectionManager\Extra\Multiple;
44

5-
use ConnectionManager\ConnectionManagerInterface;
5+
use React\SocketClient\ConnectorInterface;
66
use React\Promise\When;
77
use \UnderflowException;
88
use \InvalidArgumentException;
99

10-
class ConnectionManagerSelective implements ConnectionManagerInterface
10+
class ConnectionManagerSelective implements ConnectorInterface
1111
{
1212
const MATCH_ALL = '*';
13-
13+
1414
private $targets = array();
1515

16-
public function getConnection($host, $port)
16+
public function create($host, $port)
1717
{
1818
try {
1919
$cm = $this->getConnectionManagerFor($host, $port);
2020
}
21-
catch (Exception $e) {
21+
catch (UnderflowException $e) {
2222
return When::reject($e);
2323
}
24-
return $cm->getConnection($host, $port);
24+
return $cm->create($host, $port);
2525
}
2626

2727
public function addConnectionManagerFor($connectionManager, $targetHost=self::MATCH_ALL, $targetPort=self::MATCH_ALL, $priority=0)
@@ -34,27 +34,27 @@ public function addConnectionManagerFor($connectionManager, $targetHost=self::MA
3434
'port' => $targetPort,
3535
'priority' => $priority
3636
);
37-
37+
3838
// return the key as new entry ID
3939
end($this->targets);
4040
$id = key($this->targets);
41-
41+
4242
// sort array by priority
4343
$targets =& $this->targets;
4444
uksort($this->targets, function ($a, $b) use ($targets) {
4545
$pa = $targets[$a]['priority'];
4646
$pb = $targets[$b]['priority'];
4747
return ($pa < $pb ? -1 : ($pa > $pb ? 1 : ($a - $b)));
4848
});
49-
49+
5050
return $id;
5151
}
52-
52+
5353
public function getConnectionManagerEntries()
5454
{
5555
return $this->targets;
5656
}
57-
57+
5858
public function removeConnectionManagerEntry($id)
5959
{
6060
unset($this->targets[$id]);

README.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
# connection-manager-extra
22

33
This project provides _extra_ (in terms of "additional", "extraordinary", "special" and "unusual") decorators
4-
built upon [connection-manager](https://github.com/clue/connection-manager).
4+
built upon [react/socket-client](https://github.com/reactphp/socket-client).
55

66
## Introduction
77

8-
If you're not already familar with [connection-manager](https://github.com/clue/connection-manager),
8+
If you're not already familar with [react/socket-client](https://github.com/reactphp/socket-client),
99
think of it as an async (non-blocking) version of [`fsockopen()`](http://php.net/manual/en/function.fsockopen.php)
1010
or [`stream_socket_client()`](http://php.net/manual/en/function.stream-socket-client.php).
1111
I.e. before you can send and receive data to/from a remote server, you first have to establish a connection - which
1212
takes its time because it involves several steps.
13-
In order to be able to establish several connections at the same time, [connection-manager](https://github.com/clue/connection-manager) provides a simple
13+
In order to be able to establish several connections at the same time, [react/socket-client](https://github.com/reactphp/socket-client) provides a simple
1414
API to establish simple connections in an async (non-blocking) way.
1515

16-
This project includes several classes that extend this base functionality by implementing the same simple `ConnectionManagerInterface`.
17-
This interface provides a single promise-based method `getConnection($host, $ip)` which can be used to easily notify
18-
when the connection is successfully established or the `ConnectionManager` gives up and the connection fails.
16+
This project includes several classes that extend this base functionality by implementing the same simple `ConnectorInterface`.
17+
This interface provides a single promise-based method `create($host, $ip)` which can be used to easily notify
18+
when the connection is successfully established or the `Connector` gives up and the connection fails.
1919

2020
```php
21-
$connectionManager->getConnection('www.google.com', 80)->then(function ($stream) {
21+
$connectionManager->create('www.google.com', 80)->then(function ($stream) {
2222
echo 'connection successfully established';
2323
$stream->write("GET / HTTP/1.0\r\nHost: www.google.com\r\n\r\n");
2424
$stream->end();
@@ -28,17 +28,17 @@ $connectionManager->getConnection('www.google.com', 80)->then(function ($stream)
2828

2929
```
3030

31-
Because everything uses the same simple API, the resulting `ConnectionManager` classes can be easily interchanged
32-
and be used in places that expect the normal `ConnectionManagerInterface`. This can be used to stack them into each other,
31+
Because everything uses the same simple API, the resulting `Connector` classes can be easily interchanged
32+
and be used in places that expect the normal `ConnectorInterface`. This can be used to stack them into each other,
3333
like using [timeouts](#timeout) for TCP connections, [delaying](#delay) SSL/TLS connections,
34-
[retrying](#repeating--retrying) failed connection attemps, [randomly](#random) picking a `ConnectionManager` or
34+
[retrying](#repeating--retrying) failed connection attemps, [randomly](#random) picking a `Connector` or
3535
any combination thereof.
3636

3737
## Usage
3838

3939
This section lists all this libraries' features along with some examples.
4040
The examples assume you've [installed](#install) this library and
41-
already [set up a `ConnectionManager` instance `$connectionManager`](https://github.com/clue/connection-manager#async-tcpip-connections).
41+
already [set up a `SocketClient/Connector` instance `$connectionManager`](https://github.com/reactphp/socket-client#async-tcpip-connections).
4242

4343
All classes are located in the `ConnectionManager\Extra` namespace.
4444

@@ -49,7 +49,7 @@ of `$repeat` times when the connection fails.
4949

5050
```php
5151
$connectionManagerRepeater = new \ConnectionManager\Extra\ConnectionManagerRepeat($connectionManager, 3);
52-
$connectionManagerRepeater->getConnection('www.google.com', 80)->then(function ($stream) {
52+
$connectionManagerRepeater->create('www.google.com', 80)->then(function ($stream) {
5353
echo 'connection successfully established';
5454
$stream->close();
5555
});
@@ -88,7 +88,7 @@ of using a fixed order, it always uses a randomly shuffled order.
8888

8989
### Selective
9090

91-
The `ConnectionManagerSelective()` manages several `ConnectionManager`s and forwards connection through either of
91+
The `ConnectionManagerSelective()` manages several `Connector`s and forwards connection through either of
9292
those besed on lists similar to to firewall or networking access control lists (ACLs).
9393

9494
This allows fine-grained control on how to handle outgoing connections, like rejecting advertisements,

0 commit comments

Comments
 (0)