Thread: [Asterisk-java-users] Timeout when hanging up
Brought to you by:
srt
From: Brett S. <bs...@id...> - 2005-11-08 09:15:33
|
I'm trying something which is probably a little unusual. Basically I want to check if a number is inservice or out of service. I do this by dialing the number and if it rings I instantly drop the connection by hanging up. The code basically works fine however the hangup action is throwing a timeout even though the line is successfully hung up. Even if I set the time to 30 seconds a timeout still occurs, where as the sip phone I'm dialing hangs up immediately. Interestingly as soon as the hangup times out I get a HangupEvent. So I thought it might be worth posting the code here for inspection and comment. /* * Created on 8/11/2005 * * To change the template for this generated file go to * Window - Preferences - Java - Code Generation - Code and Comments */ package Carma.voip.cleaner; import java.io.IOException; import net.sf.asterisk.manager.AuthenticationFailedException; import net.sf.asterisk.manager.ManagerConnection; import net.sf.asterisk.manager.ManagerConnectionFactory; import net.sf.asterisk.manager.ManagerEventHandler; import net.sf.asterisk.manager.ManagerResponseHandler; import net.sf.asterisk.manager.TimeoutException; import net.sf.asterisk.manager.action.HangupAction; import net.sf.asterisk.manager.action.OriginateAction; import net.sf.asterisk.manager.action.StatusAction; import net.sf.asterisk.manager.event.ManagerEvent; import net.sf.asterisk.manager.event.NewChannelEvent; import net.sf.asterisk.manager.response.ManagerResponse; public class Manager implements ManagerEventHandler, ManagerResponseHandler { private ManagerConnection managerConnection; public Manager() throws IOException { ManagerConnectionFactory factory = new ManagerConnectionFactory(); this.managerConnection = factory.getManagerConnection("10.0.5.55", "username", "password"); } public void run() throws IOException, AuthenticationFailedException, TimeoutException { OriginateAction originateAction; ManagerResponse originateResponse; originateAction = new OriginateAction(); originateAction.setChannel("SIP/202"); originateAction.setContext("default"); originateAction.setExten("202"); originateAction.setPriority(new Integer(1)); originateAction.setTimeout(new Integer(30000)); originateAction.setAsync(new Boolean(true)); // register for events managerConnection.addEventHandler(this); // connect to Asterisk and log in managerConnection.login(); // request channel state managerConnection.sendAction(new StatusAction()); managerConnection.sendAction(originateAction, this); try { Thread.sleep(10000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } // and finally log off and disconnect managerConnection.logoff(); } public void handleEvent(ManagerEvent event) { if (event instanceof NewChannelEvent) { NewChannelEvent cevent = (NewChannelEvent) event; System.out.println(cevent.getState()); if (cevent.getState().compareToIgnoreCase("Ringing") == 0) { HangupAction hangup = new HangupAction(); hangup.setChannel(cevent.getChannel()); try { // The problem is here, I've also tried explicit timeouts of upto 30seconds and an timeout exception is still thrown. managerConnection.sendAction(hangup); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (TimeoutException e) { // We ignore the timeout as for some reason hangup always times out. } } } // just print received events System.out.println("Event: " + event); } public void handleResponse(ManagerResponse response) { System.out.println("Response: " + response); } public static void main(String[] args) throws Exception { Manager helloManager; helloManager = new Manager(); helloManager.run(); } } |
From: Stefan R. <sr...@re...> - 2005-11-08 11:24:30
|
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi Brett, your code looks ok. If you receive a TimeoutException this indicates that Astersik does not return a Response to your Action. I just tried your code with Asterisk CVS-HEAD and it I dont get any timeout. What version of Asterisk and Asterisk-Java do you use? It would help if you could send me a trace of the conversation between Asterisk and your application. I usually generate them using ngrep on my Asterisk server (ngrep -s 4000 port 5038 works well). Then we can quickly see the cause of this problem. =Stefan -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.2 (MingW32) Comment: GnuPT 2.7.2 Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org iD8DBQFDcIrSTUZ7XZofpgURAtaEAKCcVYMBzdjYOjJmxgvGW02B0JRi9QCgv+HM OI5pyRbOG6hc0MoeqpyXLPk= =cwpS -----END PGP SIGNATURE----- |
From: Brett S. <bs...@no...> - 2005-11-08 11:45:40
|
Thanks for the prompt reply. I'm using asterisk-java 0.2 rc2 and asterisk @ home 1.0.9. Here is the trace: login as: root root@10.0.5.55's password: Last login: Tue Nov 8 09:32:06 2005 from crystalreportsxi.cwmedia.com.au Welcome to Asterisk@Home ------------------------------------------------- For access to the Asterisk@Home web GUI use this URL http://10.0.5.55 For help on Asterisk@Home commands you can use from this command shell type help-aah. [root@asterisk1 root]# ngrep -s 4000 port 5038 interface: eth0 (10.0.0.0/255.255.0.0) filter: ip and ( port 5038 ) #### T 10.0.5.55:5038 -> 10.0.0.205:5164 [AP] Asterisk Call Manager/1.0.. # T 10.0.0.205:5164 -> 10.0.5.55:5038 [AP] action: Challenge..actionid: 16939420_0#..authtype: MD5.... ## T 10.0.5.55:5038 -> 10.0.0.205:5164 [AP] Response: Success..ActionID: 16939420_0#..Challenge: 680897534.... # T 10.0.0.205:5164 -> 10.0.5.55:5038 [AP] key: 99fa3b5002176ef1355028ca360e5c0d..action: Login..actionid: 16939420_1# ..authtype: MD5..username: admin.... # T 10.0.5.55:5038 -> 10.0.0.205:5164 [AP] Response: Success.. # T 10.0.5.55:5038 -> 10.0.0.205:5164 [AP] ActionID: 16939420_1#.. ## T 10.0.5.55:5038 -> 10.0.0.205:5164 [AP] Message: Authentication accepted.... # T 10.0.0.205:5164 -> 10.0.5.55:5038 [AP] command: show version files..action: Command..actionid: 16939420_2#.... # T 10.0.5.55:5038 -> 10.0.0.205:5164 [AP] Response: Follows.. # T 10.0.5.55:5038 -> 10.0.0.205:5164 [AP] ActionID: 16939420_2#.. # T 10.0.5.55:5038 -> 10.0.0.205:5164 [AP] Usage: show version. Shows Asterisk version information.. ## T 10.0.5.55:5038 -> 10.0.0.205:5164 [AP] --END COMMAND--.... # T 10.0.0.205:5164 -> 10.0.5.55:5038 [AP] action: Status..actionid: 16939420_3#.... # T 10.0.5.55:5038 -> 10.0.0.205:5164 [AP] Response: Success.. # T 10.0.5.55:5038 -> 10.0.0.205:5164 [AP] ActionID: 16939420_3#.. # T 10.0.5.55:5038 -> 10.0.0.205:5164 [AP] Message: Channel status will follow.... # T 10.0.5.55:5038 -> 10.0.0.205:5164 [AP] Event: Status..Channel: SIP/202-d2da..CallerID: <unknown>..Account: ..State : Up..Context: macro-vm..Extension: s-BUSY..Priority: 1..Seconds: 3483..Uni queid: 1131446599.1005..ActionID: 16939420_3#.... ## T 10.0.5.55:5038 -> 10.0.0.205:5164 [AP] Event: Status..Channel: SIP/202-dda4..CallerID: "brett" <202>..Account: ..S tate: Up..Context: macro-vm..Extension: s-CHANUNAVAIL..Priority: 1..Seconds : 5195..Uniqueid: 1131444894.829..ActionID: 16939420_3#....Event: Status..C hannel: SIP/202-fb58..CallerID: <unknown>..Account: ..State: Up..Context: m acro-vm..Extension: s-BUSY..Priority: 1..Seconds: 17593..Uniqueid: 11314324 90.18..ActionID: 16939420_3#....Event: StatusComplete..ActionID: 16939420_3 #.... ## T 10.0.0.205:5164 -> 10.0.5.55:5038 [AP] action: Originate..timeout: 30000..actionid: 16939420_4#..exten: 202..async : true..context: default..priority: 1..channel: SIP/202.... # T 10.0.5.55:5038 -> 10.0.0.205:5164 [AP] Event: Newchannel.. # T 10.0.5.55:5038 -> 10.0.0.205:5164 [AP] Channel: SIP/202-91c6..State: Down..CallerID: <unknown>..Uniqueid: 11314500 89.1011.. # T 10.0.5.55:5038 -> 10.0.0.205:5164 [AP] .. ## T 10.0.5.55:5038 -> 10.0.0.205:5164 [AP] Response: Success.. # T 10.0.5.55:5038 -> 10.0.0.205:5164 [AP] ActionID: 16939420_4#.. # T 10.0.5.55:5038 -> 10.0.0.205:5164 [AP] Message: Originate successfully queued.... ## T 10.0.5.55:5038 -> 10.0.0.205:5164 [AP] Event: Newchannel.. # T 10.0.5.55:5038 -> 10.0.0.205:5164 [AP] Channel: SIP/202-91c6..State: Ringing..CallerID: <unknown>..Uniqueid: 11314 50089.1011.. # T 10.0.5.55:5038 -> 10.0.0.205:5164 [AP] .. ## T 10.0.0.205:5164 -> 10.0.5.55:5038 [AP] action: Hangup..actionid: 16939420_5#..channel: SIP/202-91c6.... # T 10.0.5.55:5038 -> 10.0.0.205:5164 [AP] Response: Success.. # T 10.0.5.55:5038 -> 10.0.0.205:5164 [AP] ActionID: 16939420_5#.. # T 10.0.5.55:5038 -> 10.0.0.205:5164 [AP] Message: Channel Hungup.... ## T 10.0.5.55:5038 -> 10.0.0.205:5164 [AP] Event: Hangup.. # T 10.0.5.55:5038 -> 10.0.0.205:5164 [AP] Channel: SIP/202-91c6..Uniqueid: 1131450089.1011..Cause: 16.. # T 10.0.5.55:5038 -> 10.0.0.205:5164 [AP] .. # T 10.0.5.55:5038 -> 10.0.0.205:5164 [AP] Event: OriginateFailure.. ## T 10.0.5.55:5038 -> 10.0.0.205:5164 [AP] ActionID: 16939420_4#..Channel: SIP/202..Context: default..Exten: 202.. # T 10.0.5.55:5038 -> 10.0.0.205:5164 [AP] .. ### T 10.0.0.205:5164 -> 10.0.5.55:5038 [AP] action: Logoff..actionid: 16939420_6#.... # T 10.0.5.55:5038 -> 10.0.0.205:5164 [AP] Response: Goodbye.. # T 10.0.5.55:5038 -> 10.0.0.205:5164 [AP] ActionID: 16939420_6#.. # T 10.0.5.55:5038 -> 10.0.0.205:5164 [AP] Message: Thanks for all the fish..... #### Stefan Reuter wrote: >-----BEGIN PGP SIGNED MESSAGE----- >Hash: SHA1 > >Hi Brett, > >your code looks ok. >If you receive a TimeoutException this indicates that Astersik does not >return a Response to your Action. >I just tried your code with Asterisk CVS-HEAD and it I dont get any >timeout. What version of Asterisk and Asterisk-Java do you use? > >It would help if you could send me a trace of the conversation between >Asterisk and your application. >I usually generate them using ngrep on my Asterisk server >(ngrep -s 4000 port 5038 works well). >Then we can quickly see the cause of this problem. > >=Stefan >-----BEGIN PGP SIGNATURE----- >Version: GnuPG v1.4.2 (MingW32) >Comment: GnuPT 2.7.2 >Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org > >iD8DBQFDcIrSTUZ7XZofpgURAtaEAKCcVYMBzdjYOjJmxgvGW02B0JRi9QCgv+HM >OI5pyRbOG6hc0MoeqpyXLPk= >=cwpS >-----END PGP SIGNATURE----- > > >------------------------------------------------------- >SF.Net email is sponsored by: >Tame your development challenges with Apache's Geronimo App Server. Download >it for free - -and be entered to win a 42" plasma tv or your very own >Sony(tm)PSP. Click here to play: http://sourceforge.net/geronimo.php >_______________________________________________ >Asterisk-java-users mailing list >Ast...@li... >https://lists.sourceforge.net/lists/listinfo/asterisk-java-users > > |
From: Stefan R. <sr...@re...> - 2005-11-08 13:30:01
Attachments:
signature.asc
|
Hi Brett, thank you for the trace. Everything behaves correctly. I just had my brain switched off ;) At first some background information about how Asterisk-Java works: Generally when using the ManagerConnection there are two threads involved. Your "normal" application thread ("Main") and an addtional thread for the ManagerReader. The ManagerReader thread reads repsonses and events from Asterisk and dispatches them to the event and repsonse handlers. What happened in your case is the following: You register an event handler that is called on a NewChannelEvent. This event hanler is called from the ManagerReader thread. Now you send a HangupAction to Asterisk and wait for the response. The response can only be read and dispatched by the ManagerReader thread but that ManagerReader thread is already blocked waiting for the response... a deadlock. So what to do in such a case? Generally don't do anything that blocks the ManagerReader thread in event handlers. So you have two options: a) send the HangupAction in a non-blocking way: managerConnection.sendAction(hangup, null); b) create an additional thread that does the hangup and only asynchronously inform that thread in your handleEvent() method I think a) will be sufficient in this case. (but you loose the resposne to the hangup action) =Stefan |
From: Brett S. <bs...@no...> - 2005-11-08 21:53:10
|
Thanks for your help. I don't think you were the only one with you brain switched off, now that you point it out the problem is obvious. I'm not terribly interested in the result of the hangup so I would agree option 'a' should work nicely. Once again many thanks for you help. Brett. Stefan Reuter wrote: >Hi Brett, > >thank you for the trace. >Everything behaves correctly. I just had my brain switched off ;) > >At first some background information about how Asterisk-Java works: >Generally when using the ManagerConnection there are two threads >involved. Your "normal" application thread ("Main") and an addtional >thread for the ManagerReader. >The ManagerReader thread reads repsonses and events from Asterisk and >dispatches them to the event and repsonse handlers. > >What happened in your case is the following: > >You register an event handler that is called on a NewChannelEvent. This >event hanler is called from the ManagerReader thread. Now you send a >HangupAction to Asterisk and wait for the response. The response can >only be read and dispatched by the ManagerReader thread but that >ManagerReader thread is already blocked waiting for the response... a >deadlock. > >So what to do in such a case? >Generally don't do anything that blocks the ManagerReader thread in >event handlers. So you have two options: >a) send the HangupAction in a non-blocking way: >managerConnection.sendAction(hangup, null); >b) create an additional thread that does the hangup and only >asynchronously inform that thread in your handleEvent() method > >I think a) will be sufficient in this case. (but you loose the resposne >to the hangup action) > >=Stefan > > > > |