Skip to content

Commit 350f252

Browse files
Merge pull request #58 from transistive/route-message
Route message
2 parents f35f549 + 3f52752 commit 350f252

File tree

6 files changed

+73
-2
lines changed

6 files changed

+73
-2
lines changed

src/Bolt.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ final class Bolt
4545
/**
4646
* @var array
4747
*/
48-
private $versions = [4.1, 4, 3];
48+
private $versions = [4.3, 4.1, 4, 3];
4949

5050
/**
5151
* @var float
@@ -388,4 +388,20 @@ public function __destruct()
388388
$this->connection->disconnect();
389389
}
390390

391+
/**
392+
* fetch the current routing table, if the message specification allows it.
393+
*
394+
* @param array|null $routing
395+
*
396+
* @return array{rt: array{servers: list<array{addresses: list<string>, role: 'WRITE'|'READ'|'ROUTE'}>, ttl: int}}|null
397+
*/
398+
public function route(?array $routing = null): ?array
399+
{
400+
if (!method_exists($this->protocol, 'route')) {
401+
return null;
402+
}
403+
404+
$routing = $routing ?? ['address' => $this->connection->getIp() . ':' . $this->connection->getPort()];
405+
return $this->protocol->route($routing);
406+
}
391407
}

src/connection/AConnection.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,19 @@ protected function printHex(string $str, bool $write = true)
5656
}
5757
echo '</pre>';
5858
}
59+
60+
public function getIp(): string
61+
{
62+
return $this->ip;
63+
}
64+
65+
public function getPort(): int
66+
{
67+
return $this->port;
68+
}
69+
70+
public function getTimeout(): float
71+
{
72+
return $this->timeout;
73+
}
5974
}

src/connection/IConnection.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,10 @@ public function write(string $buffer);
2525
public function read(int $length = 2048): string;
2626

2727
public function disconnect();
28+
29+
public function getIp(): string;
30+
31+
public function getPort(): int;
32+
33+
public function getTimeout(): float;
2834
}

src/connection/Socket.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,5 +125,4 @@ public function disconnect()
125125
@socket_close($this->socket);
126126
}
127127
}
128-
129128
}

src/protocol/V4_3.php

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

33
namespace Bolt\protocol;
44

5+
use Bolt\error\MessageException;
6+
use Bolt\error\PackException;
7+
58
/**
69
* Class Protocol version 4.3
710
*
@@ -11,5 +14,21 @@
1114
*/
1215
class V4_3 extends V4_1
1316
{
17+
public function route(...$args): array
18+
{
19+
if (count($args) < 1) {
20+
throw new PackException('Wrong arguments count');
21+
}
22+
23+
$this->write($this->packer->pack(0x66, (object) $args[0], $args[1] ?? [], $args[2] ?? null));
24+
25+
$signature = 0;
26+
$output = $this->read($signature);
27+
28+
if ($signature === self::FAILURE) {
29+
throw new MessageException($output['message'] . ' (' . $output['code'] . ')');
30+
}
1431

32+
return $signature === self::SUCCESS ? $output : [];
33+
}
1534
}

tests/BoltTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,4 +165,20 @@ private function formatParameter(Bolt $bolt, string $name): string
165165
return self::$parameterType ? ('{' . $name . '}') : ('$' . $name);
166166
}
167167

168+
169+
170+
/**
171+
* @depends testHello
172+
* @param Bolt $bolt
173+
*/
174+
public function testRoute(Bolt $bolt): void
175+
{
176+
$version = $bolt->getProtocolVersion();
177+
if ($version >= 4.3) {
178+
$route = $bolt->route();
179+
self::assertNotEmpty($route);
180+
} else {
181+
self::assertNull($bolt->route());
182+
}
183+
}
168184
}

0 commit comments

Comments
 (0)