[Asterisk-java-cvs] CVS: asterisk-java/src/java/net/sf/asterisk/manager AsteriskServer.java,1.3,1.4
Brought to you by:
srt
From: Stefan R. <sr...@us...> - 2005-03-04 22:21:16
|
Update of /cvsroot/asterisk-java/asterisk-java/src/java/net/sf/asterisk/manager In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv3989/src/java/net/sf/asterisk/manager Modified Files: AsteriskServer.java DefaultManagerConnection.java Log Message: Added unit tests for DefaultManagerConnection Index: AsteriskServer.java =================================================================== RCS file: /cvsroot/asterisk-java/asterisk-java/src/java/net/sf/asterisk/manager/AsteriskServer.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -d -p -r1.3 -r1.4 --- AsteriskServer.java 23 Feb 2005 22:50:57 -0000 1.3 +++ AsteriskServer.java 4 Mar 2005 22:21:04 -0000 1.4 @@ -99,4 +99,40 @@ public class AsteriskServer implements S { this.port = port; } + + public boolean equals(Object o) + { + if (o == null || !(o instanceof AsteriskServer)) + { + return false; + } + + AsteriskServer s = (AsteriskServer) o; + if (this.getHostname() != null) + { + if (!this.getHostname().equals(s.getHostname())) + { + return false; + } + } + else + { + if (s.getHostname() != null) + { + return false; + } + } + + if (this.getPort() != s.getPort()) + { + return false; + } + + return true; + } + + public String toString() + { + return "AsteriskServer[hostname='" + hostname + "',port=" + port + "]"; + } } Index: DefaultManagerConnection.java =================================================================== RCS file: /cvsroot/asterisk-java/asterisk-java/src/java/net/sf/asterisk/manager/DefaultManagerConnection.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -d -p -r1.4 -r1.5 --- DefaultManagerConnection.java 2 Mar 2005 23:15:49 -0000 1.4 +++ DefaultManagerConnection.java 4 Mar 2005 22:21:04 -0000 1.5 @@ -71,12 +71,12 @@ public class DefaultManagerConnection im /** * The reader to use to receive events and responses from asterisk. */ - private final ManagerReader reader; + private ManagerReader reader; /** * The writer to use to send actions to asterisk. */ - private final ManagerWriter writer; + private ManagerWriter writer; private String protocolIdentifier; private final Map responseHandlers; @@ -87,8 +87,6 @@ public class DefaultManagerConnection im { this.asteriskServer = new AsteriskServer(); - this.reader = new ManagerReaderImpl(this, asteriskServer); - this.writer = new ManagerWriterImpl(); this.responseHandlers = new HashMap(); this.eventHandlers = new HashSet(); } @@ -103,6 +101,16 @@ public class DefaultManagerConnection im setPassword(password); } + protected ManagerReader createReader(Dispatcher dispatcher, AsteriskServer server) + { + return new ManagerReaderImpl(dispatcher, server); + } + + protected ManagerWriter createWriter() + { + return new ManagerWriterImpl(); + } + /** * Sets the hostname of the asterisk server to connect to.<br> * Default is <code>localhost</code>. @@ -289,10 +297,20 @@ public class DefaultManagerConnection im logger.info("Successfully logged in"); } - private synchronized void connect() throws IOException + protected synchronized void connect() throws IOException { logger.info("Connecting to " + asteriskServer.getHostname() + " port " + asteriskServer.getPort()); + if (this.reader == null) + { + this.reader = createReader(this, asteriskServer); + } + + if (this.writer == null) + { + this.writer = createWriter(); + } + this.socket = createSocket(); this.reader.setSocket(socket); @@ -512,6 +530,10 @@ public class DefaultManagerConnection im { this.responseHandlers.remove(internalActionId); } + else + { + logger.warn("No response handler registered for internalActionId '" + internalActionId + "'"); + } } } else @@ -574,27 +596,24 @@ public class DefaultManagerConnection im } } } - + if (event instanceof ConnectEvent) { setProtocolIdentifier(((ConnectEvent) event).getProtocolIdentifier()); } if (event instanceof DisconnectEvent) { - handleDisconnection(); + reconnect(); } } /** - * This method is called by the reader when the asterisk server receives the protocol - * identifier, i.e. a string like "Asterisk Call Manager/1.0". Having received a correct - * protocol identifier is the precodition for logging in. - * - * @param protocolIdentifier the protocol version received by the reader + * This method is called when a {@link ConnectEvent} is received from the reader. Having + * received a correct protocol identifier is the precodition for logging in. * - * @see ManagerReaderImpl + * @param protocolIdentifier the protocol version used by the asterisk server. */ - void setProtocolIdentifier(String protocolIdentifier) + private void setProtocolIdentifier(final String protocolIdentifier) { logger.info("Connected via " + protocolIdentifier); @@ -609,13 +628,14 @@ public class DefaultManagerConnection im } /** - * This method is called by the reader if the connection to the asterisk server is lost. While - * keepAlive is <code>true</code> we will try to reconnect. Reconnection attempts will be - * stopped when the {@link #logoff()} method is called or when the login after a successful + * Reconnects to the asterisk server when the connection is lost.<br> + * While keepAlive is <code>true</code> we will try to reconnect. Reconnection attempts will + * be stopped when the {@link #logoff()} method is called or when the login after a successful * reconnect results in an {@link AuthenticationFailedException} suggesting that the manager - * credentials have changed and keepAliveAfterAuthenticationFailure is not set. + * credentials have changed and keepAliveAfterAuthenticationFailure is not set.<br> + * This method is called when a {@link DisconnectEvent} is received from the reader. */ - void handleDisconnection() + private void reconnect() { int numTries; |