33namespace Clue \React \Ami ;
44
55use 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 ;
97use Clue \React \Ami \Protocol \Event ;
8+ use Clue \React \Ami \Protocol \Response ;
9+ use React \Promise \Deferred ;
1010
1111class 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}
0 commit comments