Skip to content
This repository was archived by the owner on Oct 30, 2024. It is now read-only.

Commit c283b98

Browse files
committed
Implement request response
1 parent 5ecbe8c commit c283b98

6 files changed

Lines changed: 283 additions & 2 deletions

File tree

examples/reqres/req.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
require_once __DIR__ . "/../../vendor/autoload.php";
3+
4+
$connectionOptions = new \Nats\ConnectionOptions();
5+
$connectionOptions->setHost('localhost')->setPort(4222);
6+
7+
$c = new Nats\Connection($connectionOptions);
8+
$c->connect();
9+
10+
$c->request(
11+
'sayhello',
12+
'Marty McFly',
13+
function ($response) {
14+
echo $response->getBody();
15+
}
16+
);

examples/reqres/res.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
require_once __DIR__ . "/../../vendor/autoload.php";
3+
4+
$connectionOptions = new \Nats\ConnectionOptions();
5+
$connectionOptions
6+
->setHost('localhost')
7+
->setPort(4222);
8+
$c = new Nats\Connection($connectionOptions);
9+
$c->connect();
10+
11+
$sid = $c->subscribe(
12+
"sayhello",
13+
function ($res) {
14+
$res->reply("Hello, " . $res->getBody() . " !!!");
15+
}
16+
);
17+
18+
$c->wait(2);
19+
20+
$c->unsubscribe($sid);

src/Connection.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,26 @@ public function ping()
199199
$this->pings += 1;
200200
}
201201

202+
/**
203+
* Request does a request and executes a callback with the response.
204+
*
205+
* @param string $subject Message topic.
206+
* @param string $payload Message data.
207+
* @param resource $callback Closure to be executed as callback.
208+
* @param integer $quantity Number of messages to wait for.
209+
*/
210+
public function request($subject, $payload, $callback, $wait = 1)
211+
{
212+
$inbox = uniqid('_INBOX.');
213+
$this->subscribe($inbox, $callback);
214+
215+
$msg = 'PUB '.$subject.' '.$inbox.' '.strlen($payload);
216+
$this->send($msg . "\r\n" . $payload);
217+
$this->pubs += 1;
218+
219+
$this->wait($wait);
220+
}
221+
202222
/**
203223
* Publish publishes the data argument to the given subject.
204224
*
@@ -264,18 +284,25 @@ private function handlePING()
264284
private function handleMSG($line)
265285
{
266286
$parts = explode(' ', $line);
287+
$subject = null;
267288
$length = $parts[3];
268289
$sid = $parts[2];
269290

291+
if (count($parts) == 5) {
292+
$length = $parts[5];
293+
$subject = $parts[3];
294+
}
295+
270296
$payload = $this->receive($length);
297+
$msg = new Message($subject, $payload, $sid, $this);
271298

272299
$func = $this->subscriptions[$sid];
273300
if (is_callable($func)) {
274-
$func($payload);
301+
$func($msg);
275302
} else {
276303
return new \Exception('not callable');
277304
}
278-
305+
279306
return;
280307
}
281308

src/Message.php

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
<?php
2+
namespace Nats;
3+
4+
/**
5+
* Message Class.
6+
*/
7+
class Message
8+
{
9+
/**
10+
* Message Subject
11+
*
12+
* @var string
13+
*/
14+
private $subject;
15+
16+
/**
17+
* Message Body
18+
*
19+
* @var string
20+
*/
21+
private $body;
22+
23+
/**
24+
* Message Ssid
25+
*
26+
* @var string
27+
*/
28+
private $sid;
29+
30+
/**
31+
* Message related connection
32+
*
33+
* @var string
34+
*/
35+
private $conn;
36+
37+
/**
38+
* Message constructor
39+
* @param string $subject
40+
* @param string $body
41+
* @param string $sid
42+
* @param Connection $conn
43+
*/
44+
public function __construct($subject, $body, $sid, $conn)
45+
{
46+
$this->setSubject($subject);
47+
$this->setBody($body);
48+
$this->setSid($sid);
49+
$this->setConn($conn);
50+
}
51+
52+
/**
53+
* Set subject
54+
*
55+
* @param string $subject Subject
56+
* @return $this
57+
*/
58+
public function setSubject($subject)
59+
{
60+
$this->subject = $subject;
61+
62+
return $this;
63+
}
64+
65+
/**
66+
* Get subject
67+
*
68+
* @return string
69+
*/
70+
public function getSubject()
71+
{
72+
return $this->subject;
73+
}
74+
75+
/**
76+
* Set body
77+
*
78+
* @param string $body Body
79+
* @return $this
80+
*/
81+
public function setBody($body)
82+
{
83+
$this->body = $body;
84+
return $this;
85+
}
86+
87+
/**
88+
* Get body
89+
*
90+
* @return string
91+
*/
92+
public function getBody()
93+
{
94+
return $this->body;
95+
}
96+
97+
/**
98+
* Set Ssid
99+
*
100+
* @param string $sid Ssid
101+
* @return $this
102+
*/
103+
public function setSid($sid)
104+
{
105+
$this->sid = $sid;
106+
return $this;
107+
}
108+
109+
/**
110+
* Get Ssid
111+
*
112+
* @return string
113+
*/
114+
public function getSid()
115+
{
116+
return $this->sid;
117+
}
118+
119+
/**
120+
* String representation of a message
121+
*
122+
* @return string
123+
*/
124+
public function __toString()
125+
{
126+
return $this->getBody();
127+
}
128+
129+
/**
130+
* Set Conn
131+
*
132+
* @param Connection $conn
133+
* @return $this
134+
*/
135+
public function setConn($conn)
136+
{
137+
$this->conn = $conn;
138+
return $this;
139+
}
140+
141+
/**
142+
* Get Conn
143+
*
144+
* @return string
145+
*/
146+
public function getConn()
147+
{
148+
return $this->conn;
149+
}
150+
151+
/**
152+
* Allows you reply the message with a specific body
153+
*
154+
* @param string $body
155+
*/
156+
public function reply($body)
157+
{
158+
$this->getConn()->publish(
159+
$this->getSubject(),
160+
$body
161+
);
162+
}
163+
}

tests/Unit/ConnectionTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,4 +153,21 @@ public function testSubscription()
153153
// time_nanosleep(1, 0);
154154
$this->c->wait(1);
155155
}
156+
157+
/**
158+
* Test Request command
159+
*
160+
* @return void
161+
*/
162+
public function testRequest()
163+
{
164+
$this->c->subscribe("sayhello", function ($res) {
165+
$res->reply("Hello, ".$res->getBody(). " !!!");
166+
});
167+
168+
$this->c->request('sayhello', 'McFly', function ($message) {
169+
$this->assertNotNull($message);
170+
$this->assertEquals($message, 'Hello, McFly !!!');
171+
});
172+
}
156173
}

tests/Unit/MessageTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
namespace Nats\tests\Unit;
3+
4+
use Nats\Connection;
5+
use Nats\Message;
6+
7+
/**
8+
* Class MessageTest
9+
*/
10+
class MessageTest extends \PHPUnit_Framework_TestCase
11+
{
12+
/**
13+
* Tests Message getters and setters. Only necessary for code coverage.
14+
*
15+
* @return void
16+
*/
17+
public function testSettersAndGetters()
18+
{
19+
$conn = new Connection();
20+
21+
$msg = new Message('subject', 'body', 'sid', $conn);
22+
23+
$this->assertEquals('subject', $msg->getSubject());
24+
$this->assertEquals('body', $msg->getBody());
25+
$this->assertEquals('sid', $msg->getSid());
26+
$this->assertEquals($conn, $msg->getConn());
27+
28+
$msg
29+
->setSubject('subject2')
30+
->setBody('body2')
31+
->setSid('sid2');
32+
33+
$this->assertEquals('subject2', $msg->getSubject());
34+
$this->assertEquals('body2', $msg->getBody());
35+
$this->assertEquals('sid2', $msg->getSid());
36+
37+
}
38+
}

0 commit comments

Comments
 (0)