Skip to content

Commit a3f392d

Browse files
committed
Merge pull request clue#14 from clue/revert-12-responseapi
Revert "Split off ResponseApi from Client"
2 parents 7099196 + fc11892 commit a3f392d

File tree

9 files changed

+93
-329
lines changed

9 files changed

+93
-329
lines changed

README.md

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,19 @@ local redis server and send some requests:
1313

1414
$factory = new Factory($connector);
1515
$factory->createClient()->then(function (Client $client) use ($loop) {
16-
$api = new RequestApi($client);
16+
$client->SET('greeting', 'Hello world');
17+
$client->APPEND('greeting', '!');
1718

18-
$api->set('greeting', 'Hello world');
19-
$api->append('greeting', '!');
20-
21-
$api->get('greeting')->then(function ($greeting) {
19+
$client->GET('greeting')->then(function ($greeting) {
2220
echo $greeting . PHP_EOL;
2321
});
2422

25-
$api->incr('invocation')->then(function ($n) {
23+
$client->INCR('invocation')->then(function ($n) {
2624
echo 'count: ' . $n . PHP_EOL;
2725
});
2826

2927
// end connection once all pending requests have been resolved
30-
$api->end();
28+
$client->end();
3129
});
3230

3331
$loop->run();

example/cli.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
$line = fgets(STDIN);
3131
if ($line === false || $line === '') {
3232
echo '# CTRL-D -> Ending connection...' . PHP_EOL;
33-
$client->close();
33+
$client->end();
3434
} else {
3535
$line = rtrim($line);
3636

@@ -39,7 +39,7 @@
3939
} else {
4040
$params = explode(' ', $line);
4141
$method = array_shift($params);
42-
$client->send($method, $params);
42+
call_user_func_array(array($client, $method), $params);
4343
}
4444
}
4545
});

example/incr.php

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

33
use Clue\React\Redis\Client;
44
use Clue\React\Redis\Factory;
5-
use Clue\React\Redis\RequestApi;
65

76
require __DIR__ . '/../vendor/autoload.php';
87

@@ -13,15 +12,13 @@
1312
$factory = new Factory($connector);
1413

1514
$factory->createClient()->then(function (Client $client) {
16-
$api = new RequestApi($client);
15+
$client->incr('test');
1716

18-
$api->incr('test');
19-
20-
$api->get('test')->then(function ($result) {
17+
$client->get('test')->then(function ($result) {
2118
var_dump($result);
2219
});
2320

24-
$api->end();
21+
$client->end();
2522
});
2623

2724
$loop->run();

src/Client.php

Lines changed: 59 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ class Client extends EventEmitter
2020
private $stream;
2121
private $parser;
2222
private $serializer;
23+
private $requests = array();
24+
private $ending = false;
2325

2426
public function __construct(Stream $stream, ParserInterface $parser = null, SerializerInterface $serializer = null)
2527
{
@@ -46,7 +48,7 @@ public function __construct(Stream $stream, ParserInterface $parser = null, Seri
4648

4749
foreach ($models as $data) {
4850
try {
49-
$that->emit('message', array($data, $that));
51+
$that->handleMessage($data);
5052
}
5153
catch (UnderflowException $error) {
5254
$that->emit('error', array($error));
@@ -65,32 +67,73 @@ public function __construct(Stream $stream, ParserInterface $parser = null, Seri
6567
$this->serializer = $serializer;
6668
}
6769

68-
/**
69-
* Sends command with given $name and additial $args
70-
*
71-
* @param name $name
72-
* @param array $args
73-
*/
74-
public function sendRequest($name, array $args = array())
70+
public function __call($name, $args)
71+
{
72+
$request = new Deferred();
73+
74+
if ($this->ending) {
75+
$request->reject(new RuntimeException('Connection closed'));
76+
} else {
77+
$this->stream->write($this->serializer->getRequestMessage($name, $args));
78+
$this->requests []= $request;
79+
}
80+
81+
return $request->promise();
82+
}
83+
84+
public function handleMessage(ModelInterface $message)
85+
{
86+
$this->emit('message', array($message, $this));
87+
88+
if (!$this->requests) {
89+
throw new UnderflowException('Unexpected reply received, no matching request found');
90+
}
91+
92+
$request = array_shift($this->requests);
93+
/* @var $request Deferred */
94+
95+
if ($message instanceof ErrorReply) {
96+
$request->reject($message);
97+
} else {
98+
$request->resolve($message->getValueNative());
99+
}
100+
101+
if ($this->ending && !$this->isBusy()) {
102+
$this->close();
103+
}
104+
}
105+
106+
public function isBusy()
75107
{
76-
$this->stream->write($this->serializer->getRequestMessage($name, $args));
108+
return !!$this->requests;
77109
}
78110

79111
/**
80-
* Sends given message model (request message)
112+
* end connection once all pending requests have been replied to
81113
*
82-
* @param ModelInterface $message
114+
* @uses self::close() once all replies have been received
115+
* @see self::close() for closing the connection immediately
83116
*/
84-
public function sendMessage(ModelInterface $message)
117+
public function end()
85118
{
86-
$this->stream->write($message->getMessageSerialized($this->serializer));
119+
$this->ending = true;
120+
121+
if (!$this->isBusy()) {
122+
$this->close();
123+
}
87124
}
88125

89-
/**
90-
* Immediately terminate the connection and discard incoming and outgoing buffers
91-
*/
92126
public function close()
93127
{
128+
$this->ending = true;
129+
94130
$this->stream->close();
131+
132+
// reject all remaining requests in the queue
133+
while($this->requests) {
134+
$request = array_shift($this->requests);
135+
/* @var $request Request */
136+
$request->reject(new RuntimeException('Connection closing'));
137+
}
95138
}
96139
}

src/Factory.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ public function createClient($target = null)
4343

4444
if ($auth !== null) {
4545
$promise = $promise->then(function (Client $client) use ($auth) {
46-
$api = new RequestApi($client);
47-
return $api->auth($auth)->then(
46+
return $client->auth($auth)->then(
4847
function () use ($client) {
4948
return $client;
5049
},
@@ -58,8 +57,7 @@ function ($error) use ($client) {
5857

5958
if ($db !== null) {
6059
$promise = $promise->then(function (Client $client) use ($db) {
61-
$api = new RequestApi($client);
62-
return $api->select($db)->then(
60+
return $client->select($db)->then(
6361
function () use ($client) {
6462
return $client;
6563
},

src/RequestApi.php

Lines changed: 0 additions & 98 deletions
This file was deleted.

tests/ClientTest.php

Lines changed: 0 additions & 79 deletions
This file was deleted.

0 commit comments

Comments
 (0)