Skip to content

Commit 9345baa

Browse files
committed
Merge Collector into ActionSender
1 parent 63f042d commit 9345baa

5 files changed

Lines changed: 91 additions & 97 deletions

File tree

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,16 @@ $sender = new ActionSender($client);
208208
All public methods resemble their respective AMI actions.
209209

210210
```php
211+
$sender->login($name, $pass);
212+
$sender->logoff();
211213
$sender->ping();
214+
$sender->command($command);
215+
$sender->events($eventMask);
216+
217+
$sender->coreShowChannels();
218+
$sender->sipPeers();
219+
$sender->agents();
220+
212221
// many more…
213222
```
214223

@@ -239,6 +248,10 @@ $sender->ping()->then(
239248
});
240249
```
241250

251+
All actions resolve with a [`Response`](#response) object on success,
252+
some actions are documented to return the specialized [`Collection`](#collection)
253+
object to contain a list of entries.
254+
242255
#### Custom actions
243256

244257
Using the `ActionSender` is not strictly necessary, but is the recommended way to execute common actions.
@@ -305,6 +318,7 @@ to access the list entries and completion event.
305318

306319
```
307320
Action: CoreShowChannels
321+
308322
Response: Success
309323
EventList: start
310324
Message: Channels will follow

examples/peers.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
<?php
22

3-
use Clue\React\Ami\Factory;
4-
use Clue\React\Ami\Client;
53
use Clue\React\Ami\ActionSender;
6-
use Clue\React\Ami\Collector;
4+
use Clue\React\Ami\Client;
5+
use Clue\React\Ami\Factory;
76
use Clue\React\Ami\Protocol\Collection;
87

98
require __DIR__ . '/../vendor/autoload.php';
@@ -16,7 +15,7 @@
1615
$factory->createClient($target)->then(function (Client $client) use ($loop) {
1716
echo 'Successfully connected' . PHP_EOL;
1817

19-
$collector = new Collector($client);
18+
$collector = new ActionSender($client);
2019

2120
$collector->sipPeers()->then(function (Collection $collection) {
2221
var_dump('result', $collection);

src/ActionSender.php

Lines changed: 69 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
namespace Clue\React\Ami;
44

55
use Clue\React\Ami\Client;
6-
use Clue\React\Ami\Protocol\Response;
7-
use Clue\React\Ami\Protocol\Action;
8-
use UnexpectedValueException;
6+
use Clue\React\Ami\Protocol\Collection;
97
use Clue\React\Ami\Protocol\Event;
8+
use Clue\React\Ami\Protocol\Response;
9+
use React\Promise\Deferred;
1010

1111
class ActionSender
1212
{
@@ -39,11 +39,6 @@ public function ping()
3939
return $this->request('Ping');
4040
}
4141

42-
public function coreShowChannels()
43-
{
44-
return $this->request('CoreShowChannels');
45-
}
46-
4742
public function command($command)
4843
{
4944
return $this->request('Command', array('Command' => $command));
@@ -62,11 +57,6 @@ public function events($eventMask)
6257
return $this->request('Events', array('EventMask' => $eventMask));
6358
}
6459

65-
public function sipPeers()
66-
{
67-
return $this->request('SIPPeers');
68-
}
69-
7060
public function sipShowPeer($peerName)
7161
{
7262
return $this->request('SIPshowpeer', array('Peer' => $peerName));
@@ -97,6 +87,32 @@ public function getConfig($filename, $category = null)
9787
return $this->request('GetConfig', array('Filename' => $filename, 'Category' => $category));
9888
}
9989

90+
/**
91+
* @return \React\Promise\PromiseInterface Promise<Collection> collection with "Event: CoreShowChannel"
92+
*/
93+
public function coreShowChannels()
94+
{
95+
return $this->collectEvents('CoreShowChannels', 'CoreShowChannelsComplete');
96+
}
97+
98+
/**
99+
*
100+
* @return \React\Promise\PromiseInterface Promise<Collection> collection with "Event: PeerEntry"
101+
*/
102+
public function sipPeers()
103+
{
104+
return $this->collectEvents('SIPPeers', 'PeerlistComplete');
105+
}
106+
107+
/**
108+
*
109+
* @return \React\Promise\PromiseInterface Promise<Collection> collection with "Event: Agents"
110+
*/
111+
public function agents()
112+
{
113+
return $this->collectEvents('Agents', 'AgentsComplete');
114+
}
115+
100116
private function boolParam($value)
101117
{
102118
if ($value === true) {
@@ -112,4 +128,44 @@ private function request($name, array $args = array())
112128
{
113129
return $this->client->request($this->client->createAction($name, $args));
114130
}
131+
132+
private function collectEvents($command, $expectedEndEvent)
133+
{
134+
$req = $this->client->createAction($command);
135+
$ret = $this->client->request($req);
136+
$id = $req->getActionId();
137+
138+
$deferred = new Deferred();
139+
140+
// collect all intermediary channel events with this action ID
141+
$collected = array();
142+
$collector = function (Event $event) use ($id, &$collected, $deferred, $expectedEndEvent) {
143+
if ($event->getActionId() === $id) {
144+
$collected []= $event;
145+
146+
if ($event->getName() === $expectedEndEvent) {
147+
$deferred->resolve($collected);
148+
}
149+
}
150+
};
151+
$this->client->on('event', $collector);
152+
153+
// unregister collector if client fails
154+
$client = $this->client;
155+
$unregister = function () use ($client, $collector) {
156+
$client->removeListener('event', $collector);
157+
};
158+
$ret->then(null, $unregister);
159+
160+
// stop waiting for events
161+
$deferred->promise()->then($unregister);
162+
163+
return $ret->then(function (Response $response) use ($deferred) {
164+
// final result has been received => merge all intermediary channel events
165+
return $deferred->promise()->then(function ($collected) use ($response) {
166+
$last = array_pop($collected);
167+
return new Collection($response, $collected, $last);
168+
});
169+
});
170+
}
115171
}

src/Collector.php

Lines changed: 0 additions & 74 deletions
This file was deleted.
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
<?php
22

3-
use Clue\React\Ami\Collector;
4-
use Clue\React\Ami\Protocol\Collection;
5-
use Clue\React\Ami\Protocol\Response;
3+
use Clue\React\Ami\ActionSender;
64
use Clue\React\Ami\Client;
7-
use Clue\React\Ami\Protocol\Event;
85
use Clue\React\Ami\Protocol\Action;
6+
use Clue\React\Ami\Protocol\Event;
7+
use Clue\React\Ami\Protocol\Response;
98

10-
class CollectorTest extends TestCase
9+
class ActionSenderTest extends TestCase
1110
{
1211
public function testCollectingSIPEvents()
1312
{
@@ -19,7 +18,7 @@ public function testCollectingSIPEvents()
1918
->with($this->equalTo('SIPPeers'), $this->equalTo(array()))
2019
->will($this->returnValue(new Action(array('Action' => 'SIPPeers', 'ActionID' => '123'))));
2120

22-
$collector = new Collector($client);
21+
$collector = new ActionSender($client);
2322

2423
$promise = $collector->sipPeers();
2524

0 commit comments

Comments
 (0)