1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Interactive commands |
4
|
|
|
* User: moyo |
5
|
|
|
* Date: 26/02/2018 |
6
|
|
|
* Time: 5:18 PM |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Carno\NSQ\Chips; |
10
|
|
|
|
11
|
|
|
use Carno\Promise\Promised; |
12
|
|
|
|
13
|
|
|
trait ITCommands |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @param array $options |
17
|
|
|
* @return Promised |
18
|
|
|
*/ |
19
|
|
|
public function identify(array $options) : Promised |
20
|
|
|
{ |
21
|
|
|
$j = json_encode($options); |
22
|
|
|
$w = $this->waiting(); |
|
|
|
|
23
|
|
|
$this->sending("IDENTIFY\n" . pack('N', strlen($j)) . $j); |
|
|
|
|
24
|
|
|
return $w; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param string $topic |
29
|
|
|
* @param string ...$messages |
30
|
|
|
* @return Promised |
31
|
|
|
*/ |
32
|
|
|
public function pub(string $topic, string ...$messages) : Promised |
33
|
|
|
{ |
34
|
|
|
$w = $this->waiting(); |
35
|
|
|
if (count($messages) > 1) { |
36
|
|
|
// multi pub |
37
|
|
|
$mn = pack('N', count($messages)); |
38
|
|
|
$ml = 0; |
39
|
|
|
$ms = ''; |
40
|
|
|
foreach ($messages as $message) { |
41
|
|
|
$ml += strlen($message) + 4; |
42
|
|
|
$ms .= pack('N', strlen($message)) . $message; |
43
|
|
|
} |
44
|
|
|
$this->sending("MPUB {$topic}\n" . pack('N', $ml + 4) . $mn . $ms); |
45
|
|
|
} else { |
46
|
|
|
// single pub |
47
|
|
|
$this->sending("PUB {$topic}\n" . pack('N', strlen($messages[0])) . $messages[0]); |
48
|
|
|
} |
49
|
|
|
return $w; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string $topic |
54
|
|
|
* @param string $message |
55
|
|
|
* @param int $defer |
56
|
|
|
* @return Promised |
57
|
|
|
*/ |
58
|
|
|
public function dpub(string $topic, string $message, int $defer) : Promised |
59
|
|
|
{ |
60
|
|
|
$w = $this->waiting(); |
61
|
|
|
$this->sending("DPUB {$topic} {$defer}\n" . pack('N', strlen($message)) . $message); |
62
|
|
|
return $w; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param string $topic |
67
|
|
|
* @param string $channel |
68
|
|
|
* @return Promised |
69
|
|
|
*/ |
70
|
|
|
public function sub(string $topic, string $channel) : Promised |
71
|
|
|
{ |
72
|
|
|
$w = $this->waiting(); |
73
|
|
|
$this->sending("SUB {$topic} {$channel}\n"); |
74
|
|
|
return $w; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param int $count |
79
|
|
|
*/ |
80
|
|
|
public function rdy(int $count) : void |
81
|
|
|
{ |
82
|
|
|
$this->sending("RDY {$count}\n"); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return Promised |
87
|
|
|
*/ |
88
|
|
|
public function cls() : Promised |
89
|
|
|
{ |
90
|
|
|
$w = $this->waiting(); |
91
|
|
|
$this->sending("CLS\n"); |
92
|
|
|
return $w; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param string $id |
97
|
|
|
*/ |
98
|
|
|
public function fin(string $id) : void |
99
|
|
|
{ |
100
|
|
|
$this->sending("FIN {$id}\n"); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param string $id |
105
|
|
|
* @param int $defer |
106
|
|
|
*/ |
107
|
|
|
public function req(string $id, int $defer = 0) : void |
108
|
|
|
{ |
109
|
|
|
$this->sending("REQ {$id} {$defer}\n"); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param string $id |
114
|
|
|
*/ |
115
|
|
|
public function touch(string $id) : void |
116
|
|
|
{ |
117
|
|
|
$this->sending("TOUCH {$id}\n"); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
*/ |
122
|
|
|
public function nop() : void |
123
|
|
|
{ |
124
|
|
|
$this->sending("NOP\n"); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|