[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(); } } |