forked from clue/reactphp-ami
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.php
More file actions
210 lines (182 loc) · 6.26 KB
/
Client.php
File metadata and controls
210 lines (182 loc) · 6.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php
namespace Clue\React\Ami;
use Clue\React\Ami\Protocol\Action;
use Clue\React\Ami\Protocol\ErrorException;
use Clue\React\Ami\Protocol\Event;
use Clue\React\Ami\Protocol\Message;
use Clue\React\Ami\Protocol\Parser;
use Clue\React\Ami\Protocol\Response;
use Clue\React\Ami\Protocol\UnexpectedMessageException;
use Evenement\EventEmitter;
use React\Promise\Deferred;
use React\Socket\ConnectionInterface;
use Exception;
use UnexpectedValueException;
/**
* The `Client` is responsible for exchanging messages with the Asterisk Manager Interface
* and keeps track of pending actions.
*
* If you want to send outgoing actions, see below for the [`ActionSender`](#actionsender) class.
*
* Besides defining a few methods, this interface also implements the
* `EventEmitterInterface` which allows you to react to certain events as documented below.
*/
class Client extends EventEmitter
{
private $stream;
private $pending = array();
private $ending = false;
private $actionId = 0;
/**
* @param ConnectionInterface $stream
* @param ?Parser $parser
*/
public function __construct(ConnectionInterface $stream, $parser = null)
{
if ($parser !== null && !$parser instanceof Parser) { // manual type check to support legacy PHP < 7.1
throw new \InvalidArgumentException('Argument #2 ($parser) expected null|Clue\React\Ami\Protocol\Parser');
}
if ($parser === null) {
$parser = new Parser();
}
$this->stream = $stream;
$that = $this;
$this->stream->on('data', function ($chunk) use ($parser, $that) {
try {
$messages = $parser->push($chunk);
} catch (UnexpectedValueException $e) {
$that->emit('error', array($e, $that));
return;
}
foreach ($messages as $message) {
$that->handleMessage($message);
}
});
$this->on('error', array($that, 'close'));
$this->stream->on('close', array ($that, 'close'));
}
/**
* Queue the given messages to be sent via AMI
* and wait for a [`Response`](#response) object that matches the value of its "ActionID" field.
*
* This method is considered advanced usage and mostly used internally only.
* Creating [`Action`](#action) objects, sending them via AMI and waiting
* for incoming [`Response`](#response) objects is usually hidden behind the
* [`ActionSender`](#actionsender) interface.
*
* If you happen to need a custom or otherwise unsupported action, you can
* also do so manually as follows. Consider filing a PR to add new actions
* to the [`ActionSender`](#actionsender).
*
* ```php
* $action = $client->createAction('Originate', array('Channel' => …));
* $promise = $client->request($action);
* ```
*
* @param Action $message
* @return \React\Promise\PromiseInterface<Response,\Exception>
*/
public function request(Action $message)
{
$deferred = new Deferred();
if ($this->ending) {
$deferred->reject(new Exception('Already ending'));
} else {
$out = $message->getMessageSerialized();
//var_dump('out', $out);
$this->stream->write($out);
$this->pending[$message->getActionId()] = $deferred;
}
return $deferred->promise();
}
/** @internal */
public function handleMessage(Message $message)
{
if ($message instanceof Event) {
$this->emit('event', array($message));
return;
}
assert($message instanceof Response);
$id = $message->getActionId();
if (!isset($this->pending[$id])) {
$this->emit('error', array(new UnexpectedMessageException($message), $this));
return;
}
if ($message->getFieldValue('Response') === 'Error') {
$this->pending[$id]->reject(new ErrorException($message));
} else {
$this->pending[$id]->resolve($message);
}
unset($this->pending[$id]);
// last pending messages received => close client
if ($this->ending && !$this->pending) {
$this->close();
}
}
/**
* Force-close the AMI connection and reject all pending actions.
*
* @return void
*/
public function close()
{
if ($this->stream === null) {
return;
}
$this->ending = true;
$stream = $this->stream;
$this->stream = null;
$stream->close();
$this->emit('close', array($this));
// reject all remaining/pending requests
foreach ($this->pending as $deferred) {
$deferred->reject(new Exception('Client closing'));
}
$this->pending = array();
}
/**
* Soft-close the AMI connection once all pending actions are completed.
*
* @return void
*/
public function end()
{
$this->ending = true;
if (!$this->isBusy()) {
$this->close();
}
}
public function isBusy()
{
return !!$this->pending;
}
/**
* Construct a custom AMI action.
*
* This method is considered advanced usage and mostly used internally only.
* Creating [`Action`](#action) objects, sending them via AMI and waiting
* for incoming [`Response`](#response) objects is usually hidden behind the
* [`ActionSender`](#actionsender) interface.
*
* If you happen to need a custom or otherwise unsupported action, you can
* also do so manually as follows. Consider filing a PR to add new actions
* to the [`ActionSender`](#actionsender).
*
* A unique value will be added to "ActionID" field automatically (needed to
* match the incoming responses).
*
* ```php
* $action = $client->createAction('Originate', array('Channel' => …));
* $promise = $client->request($action);
* ```
*
* @param string $name
* @param array<string,string|string[]|null> $args
* @return Action
*/
public function createAction($name, array $args = array())
{
$args = array('Action' => $name, 'ActionID' => (string)++$this->actionId) + $args;
return new Action($args);
}
}