Skip to content

Commit 88fc34d

Browse files
committed
Merge pull request clue#4 from clue/parser
Use new request parser, support inline protocol. Fixes clue#3.
2 parents e0b60f0 + 864a889 commit 88fc34d

File tree

6 files changed

+30
-32
lines changed

6 files changed

+30
-32
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"react/promise": "~1",
1515
"react/socket-client": "0.3.*",
1616
"react/event-loop": "0.3.*",
17-
"clue/redis-protocol": "0.2.*"
17+
"clue/redis-protocol": "0.3.*"
1818
},
1919
"autoload": {
2020
"psr-0": { "Clue\\Redis\\Server": "src" }

composer.lock

+6-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Clue/Redis/Server/Client.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ public function end()
2929
$this->connection->end();
3030
}
3131

32-
public function write(ModelInterface $response)
32+
public function write($data)
3333
{
34-
$this->connection->write($response->getMessageSerialized());
34+
$this->connection->write($data);
3535
}
3636

3737
public function getRequestDebug(ModelInterface $request)

src/Clue/Redis/Server/Invoker.php

+8-6
Original file line numberDiff line numberDiff line change
@@ -33,26 +33,28 @@ private function getNumberOfArguments(ReflectionMethod $method)
3333

3434
public function invoke($command, array $args)
3535
{
36+
$command = strtolower($command);
37+
3638
if (!isset($this->commands[$command])) {
37-
return new ErrorReply('ERR Unknown or disabled command \'' . $command . '\'');
39+
return $this->serializer->getErrorMessage('ERR Unknown or disabled command \'' . $command . '\'');
3840
}
3941

4042
$n = count($args);
4143
if ($n < $this->commands[$command]) {
42-
return new ErrorReply('ERR wrong number of arguments for \'' . $command . '\' command');
44+
return $this->serializer->getErrorMessage('ERR wrong number of arguments for \'' . $command . '\' command');
4345
}
4446

4547
try {
4648
$ret = call_user_func_array(array($this->business, $command), $args);
4749
}
4850
catch (Exception $e) {
49-
return $this->serializer->createReplyModel($e);
51+
return $this->serializer->getErrorMessage($e);
5052
}
5153

52-
if (!($ret instanceof ModelInterface)) {
53-
$ret = $this->serializer->createReplyModel($ret);
54+
if ($ret instanceof ModelInterface) {
55+
return $ret->getMessageSerialized($this->serializer);
5456
}
5557

56-
return $ret;
58+
return $this->serializer->getReplyMessage($ret);
5759
}
5860
}

src/Clue/Redis/Server/Server.php

+9-17
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@
1313
use Clue\Redis\Protocol\Parser\ParserException;
1414
use SplObjectStorage;
1515
use Exception;
16-
use Clue\Redis\React\Client\Request;
16+
use Clue\Redis\Protocol\Model\Request;
1717
use ReflectionMethod;
1818
use ReflectionException;
19+
use Clue\Redis\Protocol\Parser\RequestParser;
1920

2021
/**
2122
* Dummy redis server implementation
@@ -60,22 +61,23 @@ public function __construct(ServerSocket $socket, LoopInterface $loop, ProtocolF
6061

6162
public function handleConnection(Connection $connection)
6263
{
63-
$parser = $this->protocol->createParser();
64+
$parser = $this->protocol->createResponseParser();
65+
$parser = new RequestParser();
6466
$that = $this;
6567

6668
$client = new Client($connection);
6769
$this->clients->attach($client);
6870

6971
$connection->on('data', function ($data) use ($parser, $that, $client) {
7072
try {
71-
$parser->pushIncoming($data);
73+
$messages = $parser->pushIncoming($data);
7274
}
7375
catch (ParserException $e) {
7476
$that->emit('error', array($e, $client));
7577
return;
7678
}
77-
while ($parser->hasIncomingModel()) {
78-
$that->handleRequest($parser->popIncomingModel(), $client);
79+
foreach ($messages as $message) {
80+
$that->handleRequest($message, $client);
7981
}
8082
});
8183

@@ -93,21 +95,11 @@ public function handleDisconnection(Client $client)
9395
$this->emit('disconnection', array($client, $this));
9496
}
9597

96-
public function handleRequest(ModelInterface $request, Client $client)
98+
public function handleRequest(Request $request, Client $client)
9799
{
98100
$this->emit('request', array($request, $client));
99101

100-
if (!($request instanceof MultiBulkReply) || !$request->isRequest()) {
101-
$model = new ErrorReply('ERR Malformed request. Bye!');
102-
$client->write($model);
103-
$client->end();
104-
return;
105-
}
106-
107-
$args = $request->getValueNative();
108-
$method = strtolower(array_shift($args));
109-
110-
$ret = $this->business->invoke($method, $args);
102+
$ret = $this->business->invoke($request->getCommand(), $request->getArgs());
111103
if ($ret !== null) {
112104
$client->write($ret);
113105
}

tests/FactoryTest.php

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ public function setUp()
1313

1414
public function testPairAuthRejectDisconnects()
1515
{
16+
if (defined('HPHP_VERSION')) {
17+
$this->markTestSkipped();
18+
}
19+
1620
$server = null;
1721

1822
// bind to a random port on the local interface

0 commit comments

Comments
 (0)