Skip to content

Commit bddf584

Browse files
committed
Merge branch 'actionid' into collector
2 parents ac9d1f9 + c4256f6 commit bddf584

10 files changed

Lines changed: 138 additions & 34 deletions

File tree

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# CHANGELOG
2+
3+
This file is a manually maintained list of changes for each release. Feel free
4+
to add your changes here when sending pull requests. Also send corrections if
5+
you spot any mistakes.
6+
7+
## 0.0.0 (2014-06-25)
8+
9+
* Initial concept

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2014 Christian Lück
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is furnished
10+
to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# clue/asterisk-ami-react [![Build Status](https://travis-ci.org/clue/reactphp-asterisk-ami.png?branch=master)](https://travis-ci.org/clue/reactphp-asterisk-ami)
2+
3+
Simple async, event-driven access to the Asterisk Manager Interface (AMI)
4+
5+
> Note: This project is in eary alpha stage! Feel free to report any issues you encounter.
6+
7+
## Quickstart example
8+
9+
Once [installed](#install), you can use the following code to access your local
10+
Asterisk Telephony instance and issue some simple commands via AMI:
11+
12+
```php
13+
$factory = new Factory($loop);
14+
15+
$factory->createClient('username:secret@localhost')->then(function (Client $client) {
16+
echo 'Client connected' . PHP_EOL;
17+
18+
$api = new Api($client);
19+
$api->listCommands()->then(function (Response $response) {
20+
echo 'Available commands:' . PHP_EOL;
21+
var_dump($response);
22+
});
23+
});
24+
25+
$loop->run();
26+
```
27+
28+
See also the [examples](example).
29+
30+
## Install
31+
32+
The recommended way to install this library is [through composer](http://getcomposer.org). [New to composer?](http://getcomposer.org/doc/00-intro.md)
33+
34+
```JSON
35+
{
36+
"require": {
37+
"clue/asterisk-ami-react": "dev-master"
38+
}
39+
}
40+
```
41+
42+
> Note: This project is currently not listed on packagist.
43+
44+
## License
45+
46+
MIT

composer.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
2-
"name": "clue/redis-react",
3-
"description": "Async redis client implementation",
4-
"keywords": ["redis", "client", "async", "react"],
5-
"homepage": "https://github.com/clue/reactphp-redis",
2+
"name": "clue/asterisk-ami-react",
3+
"description": "Async, event-driven access to the Asterisk Manager Interface (AMI)",
4+
"keywords": ["Asterisk Manager Interface", "AMI", "async", "react"],
5+
"homepage": "https://github.com/clue/reactphp-asterisk-ami",
66
"license": "MIT",
77
"authors": [
88
{

composer.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Api.php

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,33 @@ public function __construct(Client $client)
2020
public function login($username, $secret, $events = null)
2121
{
2222
$events = $this->boolParam($events);
23-
return $this->client->request(new Action('Login', array('UserName' => $username, 'Secret' => $secret, 'Events' => $events)));
23+
return $this->request('Login', array('UserName' => $username, 'Secret' => $secret, 'Events' => $events));
2424
}
2525

2626
public function logout()
2727
{
28-
return $this->client->request(new Action('Logout'));
28+
return $this->request('Logout');
2929
}
3030

3131
public function agentLogoff($agentId, $soft = false)
3232
{
3333
$bool = $soft ? 'true' : 'false';
34-
return $this->client->request(new Action('AgentLogoff', array('Agent' => $agentId, 'Soft' => $bool)));
34+
return $this->request('AgentLogoff', array('Agent' => $agentId, 'Soft' => $bool));
3535
}
3636

3737
public function ping()
3838
{
39-
return $this->client->request(new Action('Ping'));
39+
return $this->request('Ping');
4040
}
4141

4242
public function coreShowChannels()
4343
{
44-
return $this->client->request(new Action('CoreShowChannels'));
44+
return $this->request('CoreShowChannels');
4545
}
4646

4747
public function command($command)
4848
{
49-
return $this->client->request(new Action('Command', array('Command' => $command)));
49+
return $this->request('Command', array('Command' => $command));
5050
}
5151

5252
public function events($eventMask)
@@ -59,42 +59,42 @@ public function events($eventMask)
5959
$eventMask = implode(',', $eventMask);
6060
}
6161

62-
return $this->client->request(new Action('Events', array('EventMask' => $eventMask)));
62+
return $this->request('Events', array('EventMask' => $eventMask));
6363
}
6464

6565
public function sipPeers()
6666
{
67-
return $this->client->request(new Action('SIPPeers'));
67+
return $this->request('SIPPeers');
6868
}
6969

7070
public function sipShowPeer($peerName)
7171
{
72-
return $this->client->request(new Action('SIPshowpeer', array('Peer' => $peerName)));
72+
return $this->request('SIPshowpeer', array('Peer' => $peerName));
7373
}
7474

7575
public function listCommands()
7676
{
77-
return $this->client->request(new Action('ListCommands'));
77+
return $this->request('ListCommands');
7878
}
7979

8080
public function sendText($channel, $message)
8181
{
82-
return $this->client->request(new Action('Sendtext', array('Channel' => $channel, 'Message' => $message)));
82+
return $this->request('Sendtext', array('Channel' => $channel, 'Message' => $message));
8383
}
8484

8585
public function hangup($channel, $cause)
8686
{
87-
return $this->client->request(new Action('Hangup', array('Channel' => $channel, 'Cause' => $cause)));
87+
return $this->request('Hangup', array('Channel' => $channel, 'Cause' => $cause));
8888
}
8989

9090
public function challenge($authType = 'MD5')
9191
{
92-
return $this->client->request(new Action('Challenge', array('AuthType' => $authType)));
92+
return $this->request('Challenge', array('AuthType' => $authType));
9393
}
9494

9595
public function getConfig($filename, $category = null)
9696
{
97-
return $this->client->request(new Action('GetConfig', array('Filename' => $filename, 'Category' => $category)));
97+
return $this->request('GetConfig', array('Filename' => $filename, 'Category' => $category));
9898
}
9999

100100
private function boolParam($value)
@@ -107,4 +107,9 @@ private function boolParam($value)
107107
}
108108
return null;
109109
}
110+
111+
private function request($name, array $args = array())
112+
{
113+
return $this->client->request($this->client->createAction($name, $args));
114+
}
110115
}

src/Client.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Clue\React\Ami\Protocol\Event;
66
use Clue\React\Ami\Protocol\Action;
77
use Evenement\EventEmitter;
8-
use React\Stream\StreamInterface;
8+
use React\Stream\Stream;
99
use Clue\React\Ami\Protocol\Parser;
1010
use React\Promise\Deferred;
1111
use Exception;
@@ -20,7 +20,9 @@ class Client extends EventEmitter
2020
private $pending = array();
2121
private $ending = false;
2222

23-
public function __construct(StreamInterface $stream, Parser $parser = null)
23+
private $actionId = 0;
24+
25+
public function __construct(Stream $stream, Parser $parser = null)
2426
{
2527
if ($parser === null) {
2628
$parser = new Parser();
@@ -124,4 +126,11 @@ public function isBusy()
124126
{
125127
return !!$this->pending;
126128
}
129+
130+
public function createAction($name, array $args = array())
131+
{
132+
$args = array('ActionID' => (string)++$this->actionId) + $args;
133+
134+
return new Action($name, $args);
135+
}
127136
}

src/Protocol/Action.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ public function __construct($action, array $parts = array())
1010
{
1111
$this->action = $action;
1212
$this->parts = $parts;
13-
14-
$this->parts['ActionID'] = (string)mt_rand();
1513
}
1614

1715
public function getMessageSerialized()

tests/ClientTest.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
use React\Stream\Stream;
44
use Clue\React\Ami\Protocol\Parser;
55
use Clue\React\Ami\Client;
6-
use React\Stream\ThroughStream;
6+
use React\EventLoop\Factory;
77

88
class ClientTest extends TestCase
99
{
1010
public function testClosingStreamClosesClient()
1111
{
12-
$stream = new ThroughStream();
12+
$stream = $this->createStreamMock();
1313

1414
$client = new Client($stream);
1515

@@ -21,7 +21,7 @@ public function testClosingStreamClosesClient()
2121

2222
public function testParserExceptionForwardsErrorAndClosesClient()
2323
{
24-
$stream = new ThroughStream();
24+
$stream = $this->createStreamMock();
2525
$parser = new Parser();
2626

2727
$client = new Client($stream, $parser);
@@ -31,4 +31,9 @@ public function testParserExceptionForwardsErrorAndClosesClient()
3131

3232
$stream->emit('data', array("invalid chunk\r\n\r\ninvalid chunk\r\n\r\n"));
3333
}
34+
35+
private function createStreamMock()
36+
{
37+
return new Stream(fopen('php://memory', 'r+'), $this->getMock('React\EventLoop\LoopInterface'));
38+
}
3439
}

tests/Protocol/ActionTest.php

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,48 @@
11
<?php
22

33
use Clue\React\Ami\Protocol\Action;
4+
45
class ActionTest extends TestCase
56
{
7+
public function testIdDefaultsToNull()
8+
{
9+
$action = new Action('name');
10+
11+
$this->assertNull($action->getActionId());
12+
}
13+
14+
public function testIdCanBeSet()
15+
{
16+
$action = new Action('name', array('ActionID' => '123'));
17+
18+
$this->assertEquals('123', $action->getActionId());
19+
}
20+
621
public function testSerializeSimple()
722
{
823
$action = new Action('name');
9-
$id = $action->getActionId();
1024

11-
$this->assertEquals("Action: name\r\nActionID: $id\r\n\r\n", $action->getMessageSerialized());
25+
$this->assertEquals("Action: name\r\n\r\n", $action->getMessageSerialized());
1226
}
1327

1428
public function testSerializeKeySingle()
1529
{
1630
$action = new Action('name', array('Key' => 'Value'));
17-
$id = $action->getActionId();
1831

19-
$this->assertEquals("Action: name\r\nKey: Value\r\nActionID: $id\r\n\r\n", $action->getMessageSerialized());
32+
$this->assertEquals("Action: name\r\nKey: Value\r\n\r\n", $action->getMessageSerialized());
2033
}
2134

2235
public function testSerializeKeyMultipleValues()
2336
{
2437
$action = new Action('name', array('Key' => array('Value1', 'Value2')));
25-
$id = $action->getActionId();
2638

27-
$this->assertEquals("Action: name\r\nKey: Value1\r\nKey: Value2\r\nActionID: $id\r\n\r\n", $action->getMessageSerialized());
39+
$this->assertEquals("Action: name\r\nKey: Value1\r\nKey: Value2\r\n\r\n", $action->getMessageSerialized());
2840
}
2941

3042
public function testSerializeKeyMultipleKeyValues()
3143
{
3244
$action = new Action('name', array('Variables' => array('first' => 'on', 'second' => 'off')));
33-
$id = $action->getActionId();
3445

35-
$this->assertEquals("Action: name\r\nVariables: first=on\r\nVariables: second=off\r\nActionID: $id\r\n\r\n", $action->getMessageSerialized());
46+
$this->assertEquals("Action: name\r\nVariables: first=on\r\nVariables: second=off\r\n\r\n", $action->getMessageSerialized());
3647
}
3748
}

0 commit comments

Comments
 (0)