Re: [Asterisk-java-users] EVENT LISTENER PROBLEM
Brought to you by:
srt
From: Daniele R. <dan...@gm...> - 2009-07-23 10:03:00
|
Hi Martin, I've seen Live Api but it does not usefull for my case. Infact I need to get a very detailed lists of all events that are raised on the channel (SIP). I've wrote a small test case in order to explain better my problem: public class AsteriskManagerTest extends Thread { private SipEventListenerTest managerEventListener; private ManagerConnection managerConnection; private Logger log = Logger.getLogger(AsteriskManagerTest.class); private OriginateAction asteriskAction = null; public AsteriskManagerTest(String asteriskHost, int asteriskManagerPort, String asteriskManagerUser, String asteriskManagerPsw) { ManagerConnectionFactory factory = new ManagerConnectionFactory(asteriskHost, asteriskManagerPort, asteriskManagerUser, asteriskManagerPsw); this.managerConnection = factory.createManagerConnection(); log.debug("Creata connessione al server Asterisk " + managerConnection.getState()); } @Override public void run() { try { managerConnection.addEventListener(this.managerEventListener); managerConnection.login(); //CONTROLLO LA VALIDITA' DELL'AZIONE log.debug("getAsteriskAction().getChannel() " + getAsteriskAction().getChannel()); log.debug("getAsteriskAction().getContext() " + getAsteriskAction().getContext()); log.debug("getAsteriskAction().getExten() " + getAsteriskAction().getExten()); ResponseEvents risposte = managerConnection.sendEventGeneratingAction(getAsteriskAction(), 3000000); managerConnection.logoff(); this.managerEventListener = null; try { Thread.sleep(10000); } catch (InterruptedException ex) { } } catch (IllegalStateException ex) { log.error("Illegal State Exception", ex); } catch (IOException ex) { log.error("IO Exception", ex); } catch (AuthenticationFailedException ex) { log.error("AuthenticationFailedException", ex); } catch (TimeoutException ex) { log.info("TimeoutException" + ex.getMessage()); } log.info("**************************** FINE DEL RUN() ASTERISK_MANAGER ***************************************************"); } public static void main(String[] args) throws Exception { AsteriskManagerTest manager = null; for (int i = 0; i < 2; i++) { manager = new AsteriskManagerTest("host", 5038, "manager", "psw"); OriginateAction action = new OriginateAction(); if (i == 0) { action.setChannel("SIP/trunk/number1"); } else { action.setChannel("SIP/ehiweb/number2"); } action.setContext("outgoing"); action.setExten("s"); action.setPriority(new Integer(1)); action.setTimeout(new Long(30000)); action.setVariable("data", "12/01/18/30"); action.setActionId("PROVA"); manager.setAsteriskAction(action); //LISTENER manager.setManagerEventListener(new SipEventListenerTest()); System.out.println("**************************** START THREAD N." + i + " ASTERISK_MANAGER ***************************************************"); manager.start(); } } /** * @return the managerEventListener */ public ManagerEventListener getManagerEventListener() { return managerEventListener; } /** * @param managerEventListener the managerEventListener to set */ public void setManagerEventListener(SipEventListenerTest managerEventListener) { this.managerEventListener = managerEventListener; } /** * @return the asteriskAction */ public OriginateAction getAsteriskAction() { return asteriskAction; } /** * @param asteriskAction the asteriskAction to set */ public void setAsteriskAction(OriginateAction asteriskAction) { this.asteriskAction = asteriskAction; } } public class SipEventListenerTest extends CallManagerEvent implements ManagerEventListener { @Override public void onManagerEvent(ManagerEvent event) { log.debug("Event " + ((AbstractChannelEvent) event).getUniqueId() + " - " + event); } } And these are the log of the 2 simultaneous calls. 23/07/2009 11:22:22 DEBUG AsteriskManagerTest:34 - Creata connessione al server Asterisk INITIAL **************************** START THREAD N.0 ASTERISK_MANAGER *************************************************** 23/07/2009 11:22:22 DEBUG AsteriskManagerTest:34 - Creata connessione al server Asterisk INITIAL **************************** START THREAD N.1 ASTERISK_MANAGER *************************************************** 23/07/2009 11:22:23 DEBUG AsteriskManagerTest:44 - getAsteriskAction().getChannel() SIP/trunk/3470841943 23/07/2009 11:22:23 DEBUG AsteriskManagerTest:45 - getAsteriskAction().getContext() outgoing 23/07/2009 11:22:23 DEBUG AsteriskManagerTest:46 - getAsteriskAction().getExten() s 23/07/2009 11:22:23 DEBUG AsteriskManagerTest:44 - getAsteriskAction().getChannel() SIP/trunk/3400917567 23/07/2009 11:22:23 DEBUG AsteriskManagerTest:45 - getAsteriskAction().getContext() outgoing 23/07/2009 11:22:23 DEBUG AsteriskManagerTest:46 - getAsteriskAction().getExten() s 23/07/2009 11:22:23 DEBUG CallManagerEvent:19 - Event 1248340950.745 - org.asteriskjava.manager.event.NewChannelEvent[dateReceived=Thu Jul 23 11:22:23 CEST 2009,privilege='call,all',callerid='null',state='Down',channelstate='0',timestamp='null',calleridname='null',uniqueid='1248340950.745',exten='null',context='from_cheapnet',accountcode='null',server='null',calleridnum='null',channel='SIP/ehiweb-00b7cdd8',channelstatedesc='Down',systemHashcode=18019860] 23/07/2009 11:22:23 DEBUG CallManagerEvent:19 - Event 1248340950.745 - org.asteriskjava.manager.event.NewCallerIdEvent[dateReceived=Thu Jul 23 11:22:23 CEST 2009,privilege='call,all',callerid='null',calleridname='null',timestamp='null',uniqueid='1248340950.745',cidcallingpres='0',cidcallingprestxt='Presentation Allowed, Not Screened',server='null',calleridnum='null',channel='SIP/ehiweb-00b7cdd8',systemHashcode=17933228] 23/07/2009 11:22:32 DEBUG CallManagerEvent:19 - Event 1248340950.746 - org.asteriskjava.manager.event.NewChannelEvent[dateReceived=Thu Jul 23 11:22:32 CEST 2009,privilege='call,all',callerid='null',state='Down',channelstate='0',timestamp='null',calleridname='null',uniqueid='1248340950.746',exten='null',context='from_cheapnet',accountcode='null',server='null',calleridnum='null',channel='SIP/ehiweb-00b7b1a8',channelstatedesc='Down',systemHashcode=7486844] 23/07/2009 11:22:32 DEBUG CallManagerEvent:19 - Event 1248340950.746 - org.asteriskjava.manager.event.NewCallerIdEvent[dateReceived=Thu Jul 23 11:22:32 CEST 2009,privilege='call,all',callerid='null',calleridname='null',timestamp='null',uniqueid='1248340950.746',cidcallingpres='0',cidcallingprestxt='Presentation Allowed, Not Screened',server='null',calleridnum='null',channel='SIP/ehiweb-00b7b1a8',systemHashcode=1377187] 23/07/2009 11:22:32 DEBUG CallManagerEvent:19 - Event 1248340950.746 - org.asteriskjava.manager.event.HangupEvent[dateReceived=Thu Jul 23 11:22:32 CEST 2009,privilege='call,all',causetxt='User busy',callerid='null',cause='17',calleridname='null',timestamp='null',uniqueid='1248340950.746',server='null',calleridnum='null',channel='SIP/ehiweb-00b7b1a8',systemHashcode=9004539] 23/07/2009 11:22:33 DEBUG CallManagerEvent:19 - Event 1248340950.745 - org.asteriskjava.manager.event.NewChannelEvent[dateReceived=Thu Jul 23 11:22:33 CEST 2009,privilege='call,all',callerid='null',state='Down',channelstate='0',timestamp='null',calleridname='null',uniqueid='1248340950.745',exten='null',context='from_cheapnet',accountcode='null',server='null',calleridnum='null',channel='SIP/ehiweb-00b7cdd8',channelstatedesc='Down',systemHashcode=381299] 23/07/2009 11:22:33 DEBUG CallManagerEvent:19 - Event 1248340950.745 - org.asteriskjava.manager.event.NewCallerIdEvent[dateReceived=Thu Jul 23 11:22:33 CEST 2009,privilege='call,all',callerid='null',calleridname='null',timestamp='null',uniqueid='1248340950.745',cidcallingpres='0',cidcallingprestxt='Presentation Allowed, Not Screened',server='null',calleridnum='null',channel='SIP/ehiweb-00b7cdd8',systemHashcode=25377109] 23/07/2009 11:22:33 DEBUG CallManagerEvent:19 - Event 1248340950.746 - org.asteriskjava.manager.event.NewChannelEvent[dateReceived=Thu Jul 23 11:22:33 CEST 2009,privilege='call,all',callerid='null',state='Down',channelstate='0',timestamp='null',calleridname='null',uniqueid='1248340950.746',exten='null',context='from_cheapnet',accountcode='null',server='null',calleridnum='null',channel='SIP/ehiweb-00b7b1a8',channelstatedesc='Down',systemHashcode=2693887] 23/07/2009 11:22:33 DEBUG CallManagerEvent:19 - Event 1248340950.746 - org.asteriskjava.manager.event.NewCallerIdEvent[dateReceived=Thu Jul 23 11:22:33 CEST 2009,privilege='call,all',callerid='null',calleridname='null',timestamp='null',uniqueid='1248340950.746',cidcallingpres='0',cidcallingprestxt='Presentation Allowed, Not Screened',server='null',calleridnum='null',channel='SIP/ehiweb-00b7b1a8',systemHashcode=22837328] 23/07/2009 11:22:33 DEBUG CallManagerEvent:19 - Event 1248340950.746 - org.asteriskjava.manager.event.HangupEvent[dateReceived=Thu Jul 23 11:22:33 CEST 2009,privilege='call,all',causetxt='User busy',callerid='null',cause='17',calleridname='null',timestamp='null',uniqueid='1248340950.746',server='null',calleridnum='null',channel='SIP/ehiweb-00b7b1a8',systemHashcode=11626165] 23/07/2009 11:22:33 DEBUG CallManagerEvent:19 - Event 1248340950.745 - org.asteriskjava.manager.event.HangupEvent[dateReceived=Thu Jul 23 11:22:33 CEST 2009,privilege='call,all',causetxt='User busy',callerid='null',cause='17',calleridname='null',timestamp='null',uniqueid='1248340950.745',server='null',calleridnum='null',channel='SIP/ehiweb-00b7cdd8',systemHashcode=31378924] 23/07/2009 11:22:42 INFO AsteriskManagerTest:65 - **************************** FINE DEL RUN() ASTERISK_MANAGER *************************************************** 23/07/2009 11:22:43 INFO AsteriskManagerTest:65 - **************************** FINE DEL RUN() ASTERISK_MANAGER *************************************************** As you can see, there are events of the 2 calls with 2 uniqueId different: 1248340950.746 and 1248340950.745 but I can't update the status of the call because I can't associate the unique id with my action. So I'm not be able to understand if 1248340950.745 is referred to call1 or call2. This is the question!! Thanks for any suggestion!! Best regards! 2009/7/23 Martin B. Smith <ma...@mb...>: > Daniele, > > Have you considered using the Live API -- either using it directly or > looking to it as a model for what you want to do? It tracks originates > exactly like you're asking for, out of the box (and it's free and full > of open source goodness!!!). > > (I'm a little short on time so I won't actually go and design/implement > an example for your problem, so unless you have a minimal one that > demonstrates the problem, I'm not going to be much help -- sorry!) > > Martin > > > Daniele Renda wrote: >> Hi Martin, >> yes the action is an OriginateAction. >> I've a problem with your example. Infact I'm using the listener >> beacuse of I'd like to catch all events of a call (che >> NewChannelEvent,NewStateEvent,DtmfEvent,HangupEvent,ecc ecc). >> Instead with the : >> >> managerConnection.sendAction(getAsteriskAction(), new SendActionCallback() { >> >> @Override >> public void onResponse(ManagerResponse response) { >> log.debug("received message "+response); >> } >> }); >> >> I'll received only ONE response to my action (probably: action queued >> successfuly). So this is the cause of the fact that I'm using a >> listener to the manager in order to catch every event. >> >> THanks >> >> Best regards >> >> 2009/7/21 Martin B. Smith <ma...@mb...>: >>> What class is action? Is it OriginateAction? >>> >>> For OriginateActions, Asterisk-Java has the ability to give you all of >>> the events associated with the OriginateAction, through the >>> OriginateResponseEvent. >>> >>> In your case, you're asking Asterisk-Java to send an action, wait >>> 300000000 ms for a resultant event, and then do nothing with it. >>> >>> You're also looking at ((AbstractChannelEvent)event).getUniqueId(), >>> which is the unique name of the channel. It won't contain your actionId. >>> >>> Since you do *not* want to wait for the result of the originate (you >>> want to do more originates), you may want to use the callback version: >>> >>> void sendAction(ManagerAction action, SendActionCallback callback) >>> >>> "Sends a ManagerAction to the Asterisk server and registers a callback >>> handler to be called when the corresponding ManagerResponse is received." >>> >>> And then ideally, the response should be an OriginateResponse: >>> void onResponse(ManagerResponse response) >>> >>> Finally, have you considered using a proxy so you aren't sending actions >>> and handling events in the same thread? >>> http://asterisk-java.org/development/apidocs/org/asteriskjava/manager/ManagerEventListenerProxy.html >>> >>> Good luck, >>> >>> Martin >>> >>> >>> >>> Daniele Renda wrote: >>>> Hi Martin, >>>> the code is quite structurated but I try to paste the relevant points: >>>> *CREATE THE ACTION (in a thread. This action then is send to the >>>> Manager thread that send the action to Asterisk) >>>> action.setContext(sContext); >>>> action.setExten(sExten); >>>> action.setPriority(new Integer(iPriority)); >>>> action.setTimeout(new Long(iTimeout)); >>>> action.setActionId(""+telefonata.getId()); >>>> action.setAsync(true); >>>> >>>> *MANAGER (is a thread that send the action to Asterisk and waint ultil >>>> the HangupEvent is throw or untile timeout) >>>> ManagerConnectionFactory factory = new >>>> ManagerConnectionFactory(asteriskHost, asteriskManagerPort, >>>> asteriskManagerUser, asteriskManagerPsw); >>>> this.managerConnection = factory.createManagerConnection(); >>>> log.debug("Creata connessione al server Asterisk " + >>>> managerConnection.getState()); >>>> managerConnection.addEventListener(this.managerEventListener); >>>> managerConnection.sendAction(getAsteriskAction(), 300000000); >>>> try { >>>> Thread.sleep(180000); >>>> } catch (InterruptedException ex) { >>>> } >>>> } >>>> log.debug("Sto effettuato la disconnessione dal Manager di Asterisk >>>> sull'host " + managerConnection.getHostname()); >>>> managerConnection.logoff(); >>>> >>>> *EVENT LISTENER (the listener to asterisk Event) >>>> @Override >>>> public void onManagerEvent(ManagerEvent event) { >>>> log.debug("Evento "+((AbstractChannelEvent)event).getUniqueId()+" - " + event); >>>> >>>> The listener is very long and intercept all the events to asterisk. >>>> >>>> *DIALPLAN >>>> The action that run from Java code is somethig as this: >>>> Dial(trunk/telephoneNumber), extension s, priority 1, context >>>> >>>> [context] >>>> exten => s,1,Answer() >>>> ;I play a message >>>> exten => s,2,Background(custom/message/intro) >>>> exten => s,3,Hangup() >>>> >>>> In few words, this is an automatic call system that calls many users >>>> and play them a simple vocal message. >>>> My problem is that in the events that asterisk send to me (througt >>>> Asterisk Java) there isn't something that tell to me which call the >>>> refers. Infact all calls go to [context] and all make same things. >>>> >>>> Thanks >>>> >>>> Best regards >>>> >>>> >>>> >>>> 2009/7/21 Martin B. Smith <ma...@mb...>: >>>>> Hi Daniel, >>>>> >>>>> That snippet doesn't help much; you don't even provide the declaration >>>>> of the action variable or how you send it. Any number of things could be >>>>> going on here, and we don't really have enough information to diagnose >>>>> the problem. >>>>> >>>>> Furthermore, depending on how your dialplan is setup, your ability to >>>>> determine which events correspond to which calls could be impaired. >>>>> >>>>> We'd really need a complete example of how you create and send the >>>>> action, and how you handle the response; we'd also need to see any >>>>> relevant dialplan contexts that the call may go into. >>>>> >>>>> Good luck, >>>>> >>>>> Martin >>>>> >>>>> >>>>> >>>>> Daniele Renda wrote: >>>>>> Hi, >>>>>> I'm using Asterisk Java with my Asterisk 1.6 and all works fine. I've only a >>>>>> small problem with ManagerEventListener. I'm using the listener to >>>>>> catch the event about a call that I've scheduled from Java. I create a >>>>>> automatic call in this mode: >>>>>> >>>>>> >>>>>> action.setContext(sContext); >>>>>> action.setExten(sExten); >>>>>> action.setPriority(new Integer(iPriority)); >>>>>> action.setTimeout(new Long(iTimeout)); >>>>>> action.setActionId(""+telefonata.getId()); >>>>>> action.setAsync(true); >>>>>> >>>>>> All works fine if I make only ONE call at same time, but if I try to make >>>>>> two or more calls I am not be able to detect wich events are of the >>>>>> first call and which are of the second call. >>>>>> The unique id of the events are not linked to the action that I've >>>>>> submitted and the actionId that I can set is not included in the >>>>>> response event. So how I can detect exactly the events of each call >>>>>> separatly? >>>>>> >>>>>> >>>>>> Thanks very much for your help! >>>>>> Best regards >>>>> ------------------------------------------------------------------------------ >>>>> _______________________________________________ >>>>> Asterisk-java-users mailing list >>>>> Ast...@li... >>>>> https://lists.sourceforge.net/lists/listinfo/asterisk-java-users >>>>> >>>> >>>> >>> >>> ------------------------------------------------------------------------------ >>> _______________________________________________ >>> Asterisk-java-users mailing list >>> Ast...@li... >>> https://lists.sourceforge.net/lists/listinfo/asterisk-java-users >>> >> >> >> > > > ------------------------------------------------------------------------------ > _______________________________________________ > Asterisk-java-users mailing list > Ast...@li... > https://lists.sourceforge.net/lists/listinfo/asterisk-java-users > -- Daniele Renda |