[Asterisk-java-cvs] CVS: asterisk-java/src/java/net/sf/asterisk/manager Dispatcher.java,NONE,1.1 Man
Brought to you by:
srt
From: Stefan R. <sr...@us...> - 2005-03-02 23:16:01
|
Update of /cvsroot/asterisk-java/asterisk-java/src/java/net/sf/asterisk/manager In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv29560/src/java/net/sf/asterisk/manager Modified Files: ManagerReader.java DefaultManagerConnection.java Added Files: Dispatcher.java ManagerWriterImpl.java ManagerWriter.java ManagerReaderImpl.java Log Message: Added unit tests for ManagerReader and ManagerWriter (and did some refactoring to make this work) --- NEW FILE: Dispatcher.java --- /* * Copyright 2004-2005 Stefan Reuter * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ package net.sf.asterisk.manager; import net.sf.asterisk.manager.event.ManagerEvent; import net.sf.asterisk.manager.response.ManagerResponse; public interface Dispatcher { /** * This method is called by the reader whenever a {@link ManagerResponse} is received. The * response is dispatched to the associated {@link ManagerResponseHandler}. * * @param response the resonse received by the reader * * @see ManagerReader */ void dispatchResponse(ManagerResponse response); /** * This method is called by the reader whenever a ManagerEvent is received. The event is * dispatched to all registered ManagerEventHandlers. * * @param event the event received by the reader * * @see ManagerReader */ void dispatchEvent(ManagerEvent event); } --- NEW FILE: ManagerWriterImpl.java --- /* * Copyright 2004-2005 Stefan Reuter * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ package net.sf.asterisk.manager; import java.io.IOException; import net.sf.asterisk.manager.action.ManagerAction; import net.sf.asterisk.manager.io.SocketConnectionFacade; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * Default implementation of ManagerWriter interface. * * @author srt * @version $Id: ManagerWriterImpl.java,v 1.1 2005/03/02 23:15:49 srt Exp $ */ class ManagerWriterImpl implements ManagerWriter { /** * Instance logger. */ private final Log logger = LogFactory.getLog(getClass()); /** * The action builder utility to convert ManagerAction to a String suitable to be sent to the * asterisk server. */ private final ActionBuilder actionBuilder; private SocketConnectionFacade socket; /** * Creates a new ManagerWriterImpl. */ ManagerWriterImpl() { this.actionBuilder = new ActionBuilder(); } public synchronized void setSocket(final SocketConnectionFacade socket) { this.socket = socket; } public synchronized void sendAction(final ManagerAction action) throws IOException { final String actionString; if (socket == null) { throw new IllegalStateException("Unable to send action: socket is null"); } actionString = actionBuilder.buildAction(action); socket.print(actionString); socket.flush(); logger.debug("Sent " + action.getAction() + " action with actionId '" + action.getActionId() + "':\n" + actionString); } } --- NEW FILE: ManagerWriter.java --- /* * Copyright 2004-2005 Stefan Reuter * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ package net.sf.asterisk.manager; import java.io.IOException; import net.sf.asterisk.manager.action.ManagerAction; import net.sf.asterisk.manager.io.SocketConnectionFacade; /** * The ManagerWriter transforms actions using the ActionBuilder and sends them to the asterisk * server.<br> * This class is intended to be used only by the DefaultManagerConnection. * * @see net.sf.asterisk.manager.ActionBuilder * @see net.sf.asterisk.manager.DefaultManagerConnection * * @author srt * @version $Id: ManagerWriter.java,v 1.1 2005/03/02 23:15:49 srt Exp $ */ interface ManagerWriter { void setSocket(final SocketConnectionFacade socket); /** * Sends the given action to the asterisk server. * * @param action the action to send to the asterisk server. * @throws IOException if there is a problem sending the action. */ void sendAction(final ManagerAction action) throws IOException; } --- NEW FILE: ManagerReaderImpl.java --- /* * Copyright 2004-2005 Stefan Reuter * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ package net.sf.asterisk.manager; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import net.sf.asterisk.manager.event.ConnectEvent; import net.sf.asterisk.manager.event.DisconnectEvent; import net.sf.asterisk.manager.event.ManagerEvent; import net.sf.asterisk.manager.io.SocketConnectionFacade; import net.sf.asterisk.manager.response.CommandResponse; import net.sf.asterisk.manager.response.ManagerResponse; import net.sf.asterisk.manager.util.DateUtil; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * Default implementation of the ManagerReader interface. * * @author srt * @version $Id: ManagerReaderImpl.java,v 1.1 2005/03/02 23:15:50 srt Exp $ */ class ManagerReaderImpl implements ManagerReader { /** * Instance logger. */ private final Log logger = LogFactory.getLog(getClass()); /** * The event builder utility to convert a map of attributes reveived from asterisk to instances * of registered event classes. */ private final EventBuilder eventBuilder; /** * The response builder utility to convert a map of attributes reveived from asterisk to * instances of well known response classes. */ private final ResponseBuilder responseBuilder; /** * The dispatcher to use for dispatching events and responses. */ private final Dispatcher dispatcher; /** * The asterisk server we are reading from. */ private final AsteriskServer asteriskServer; /** * The socket to use for reading from the asterisk server. */ private SocketConnectionFacade socket; /** * If set to <code>true</code>, terminates and closes the reader. */ private boolean die = false; /** * Creates a new ManagerReaderImpl. * @param dispatcher the dispatcher to use for dispatching events and responses. */ ManagerReaderImpl(final Dispatcher dispatcher, AsteriskServer asteriskServer) { this.dispatcher = dispatcher; this.asteriskServer = asteriskServer; this.eventBuilder = new EventBuilder(); this.responseBuilder = new ResponseBuilder(); } /** * Sets the socket to use for reading from the asterisk server. * @param socket the socket to use for reading from the asterisk server. */ public void setSocket(final SocketConnectionFacade socket) { this.socket = socket; } /** * Reads line by line from the asterisk server, sets the protocol identifier as soon as it is * received and dispatches the received events and responses via the associated dispatcher. * * @see DefaultManagerConnection#dispatchEvent(ManagerEvent) * @see DefaultManagerConnection#dispatchResponse(ManagerResponse) * @see DefaultManagerConnection#setProtocolIdentifier(String) */ public void run() { final Map buffer = new HashMap(); final List commandResult = new ArrayList(); String line; boolean processingCommandResult = false; if (socket == null) { throw new IllegalStateException("Unable to run: socket is null."); } this.die = false; try { while ((line = socket.readLine()) != null && !this.die) { // dirty hack for handling the CommandAction. Needs fix when manager protocol is // enhanced. if (processingCommandResult) { if ("--END COMMAND--".equals(line)) { CommandResponse commandResponse = new CommandResponse(); if (!commandResult.isEmpty() && ((String) commandResult.get(0)).startsWith("ActionID")) { String tmp = (String) commandResult.get(0); int delimiterIndex = tmp.indexOf(":"); if (delimiterIndex > 0 && tmp.length() > delimiterIndex + 2) { commandResult.remove(0); commandResponse.setActionId(tmp.substring(delimiterIndex + 2)); } } commandResponse.setResponse("Follows"); commandResponse.setDateReceived(DateUtil.getDate()); commandResponse.setResult(commandResult); dispatcher.dispatchResponse(commandResponse); processingCommandResult = false; } else { commandResult.add(line); } continue; } // Reponse: Follows indicates that the output starting on the next line until // --END COMMAND-- must be treated as raw output of a command executed by a // CommandAction. if ("Response: Follows".equalsIgnoreCase(line)) { processingCommandResult = true; commandResult.clear(); continue; } // maybe we will find a better way to identify the protocol identifier but for now // this works quite well. if (line.startsWith("Asterisk Call Manager/")) { ConnectEvent connectEvent = new ConnectEvent(asteriskServer); connectEvent.setProtocolIdentifier(line); connectEvent.setDateReceived(DateUtil.getDate()); dispatcher.dispatchEvent(connectEvent); continue; } // an empty line indicates a normal response's or event's end so we build // the corresponding value object and dispatch it through the ManagerConnection. if (line.length() == 0) { if (buffer.containsKey("response")) { ManagerResponse response = buildResponse(buffer); if (response != null) { dispatcher.dispatchResponse(response); } } else if (buffer.containsKey("event")) { ManagerEvent event = buildEvent(asteriskServer, buffer); if (event != null) { dispatcher.dispatchEvent(event); } } buffer.clear(); } else { int delimiterIndex; delimiterIndex = line.indexOf(":"); if (delimiterIndex > 0 && line.length() > delimiterIndex + 2) { String name; String value; name = line.substring(0, delimiterIndex).toLowerCase(); value = line.substring(delimiterIndex + 2); buffer.put(name, value); } } } logger.info("Reached end of stream, terminating reader."); } catch (IOException e) { logger.info("IOException while reading from asterisk server, terminating reader thread: " + e.getMessage()); } finally { // cleans resources and reconnects if needed DisconnectEvent disconnectEvent = new DisconnectEvent(asteriskServer); disconnectEvent.setDateReceived(DateUtil.getDate()); dispatcher.dispatchEvent(disconnectEvent); } } public void die() { this.die = true; } private ManagerResponse buildResponse(Map buffer) { ManagerResponse response; response = responseBuilder.buildResponse(buffer); if (response != null) { response.setDateReceived(DateUtil.getDate()); } return response; } private ManagerEvent buildEvent(Object source, Map buffer) { ManagerEvent event; event = eventBuilder.buildEvent(source, buffer); if (event != null) { event.setDateReceived(DateUtil.getDate()); } return event; } } Index: ManagerReader.java =================================================================== RCS file: /cvsroot/asterisk-java/asterisk-java/src/java/net/sf/asterisk/manager/ManagerReader.java,v retrieving revision 1.5 retrieving revision 1.6 diff -u -d -p -r1.5 -r1.6 --- ManagerReader.java 2 Mar 2005 00:37:03 -0000 1.5 +++ ManagerReader.java 2 Mar 2005 23:15:38 -0000 1.6 @@ -16,231 +16,31 @@ */ package net.sf.asterisk.manager; -import java.io.BufferedReader; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Date; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import net.sf.asterisk.manager.event.ManagerEvent; -import net.sf.asterisk.manager.response.CommandResponse; -import net.sf.asterisk.manager.response.ManagerResponse; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; +import net.sf.asterisk.manager.io.SocketConnectionFacade; /** - * The ManagerReader is a Thread that reads events and responses from an asterisk server, parses - * them to the corresponding ManagerEvent and ManagerResponse objects and dispatches them via the - * associated DefaultManagerConnection. <br> + * The ManagerReader reads events and responses from the asterisk server, parses them using + * EventBuilder and ResponseBuilder and dispatches them to the associated ManagerConnection.<br> * This class is intended to be used only by the DefaultManagerConnection. * - * @see net.sf.asterisk.manager.event.ManagerEvent - * @see net.sf.asterisk.manager.response.ManagerResponse + * @see net.sf.asterisk.manager.EventBuilder + * @see net.sf.asterisk.manager.ResponseBuilder * @see net.sf.asterisk.manager.DefaultManagerConnection * * @author srt * @version $Id$ */ -class ManagerReader extends Thread -{ - private Log log = LogFactory.getLog(getClass()); - - private static int threadCount = 0; - - /** - * The event builder utility to convert a map of attributes reveived from asterisk to instances - * of registered event classes. - */ - private EventBuilder eventBuilder; - - /** - * The response builder utility to convert a map of attributes reveived from asterisk to - * instances of well known response classes. - */ - private ResponseBuilder responseBuilder; - - /** - * If set to <code>true</code>, terminates and closes the reader. - */ - private boolean die = false; - private BufferedReader reader; - private DefaultManagerConnection connection; - - ManagerReader(DefaultManagerConnection connection, BufferedReader reader) - { - int i = threadCount++; - setName("ManagerReader-" + i); - - this.connection = connection; - this.reader = reader; - this.eventBuilder = new EventBuilder(); - this.responseBuilder = new ResponseBuilder(); - } +interface ManagerReader extends Runnable +{ /** - * Reads line by line from the asterisk server, sets the protocol identifier as soon as it is - * received and dispatches the received events and responses via the associated connection. + * Sets the socket to use for reading from the asterisk server. * - * @see DefaultManagerConnection#dispatchEvent(ManagerEvent) - * @see DefaultManagerConnection#dispatchResponse(ManagerResponse) - * @see DefaultManagerConnection#setProtocolIdentifier(String) + * @param socket the socket to use for reading from the asterisk server. */ - public void run() - { - String line; - List commandResult = new ArrayList(); - boolean processingCommandResult = false; - Map buffer = new HashMap(); - - try - { - while ((line = reader.readLine()) != null && !die) - { - // dirty hack for handling the CommandAction. Needs fix when manager protocol is - // enhanced. - if (processingCommandResult) - { - if ("--END COMMAND--".equals(line)) - { - CommandResponse commandResponse = new CommandResponse(); - - if (!commandResult.isEmpty() && ((String) commandResult.get(0)).startsWith("ActionID")) - { - String tmp = (String) commandResult.get(0); - int delimiterIndex = tmp.indexOf(":"); - if (delimiterIndex > 0 && tmp.length() > delimiterIndex + 2) - { - commandResult.remove(0); - commandResponse.setActionId(tmp.substring(delimiterIndex + 2)); - } - } - commandResponse.setResponse("Follows"); - commandResponse.setDateReceived(new Date()); - commandResponse.setResult(commandResult); - connection.dispatchResponse(commandResponse); - processingCommandResult = false; - } - else - { - commandResult.add(line); - } - continue; - } - - // Reponse: Follows indicates that the output starting on the next line until - // --END COMMAND-- must be treated as raw output of a command executed by a - // CommandAction. - if ("Response: Follows".equalsIgnoreCase(line)) - { - processingCommandResult = true; - commandResult.clear(); - continue; - } - - // maybe we will find a better way to identify the protocol identifier but for now - // this works quite well. - if (line.startsWith("Asterisk Call Manager/") && connection.getProtocolIdentifier() == null) - { - connection.setProtocolIdentifier(line); - continue; - } - - // an empty line indicates a normal response's or event's end so we build - // the corresponding value object and dispatch it through the ManagerConnection. - if (line.length() == 0) - { - if (buffer.containsKey("response")) - { - ManagerResponse response = buildResponse(buffer); - if (response != null) - { - connection.dispatchResponse(response); - } - } - else if (buffer.containsKey("event")) - { - ManagerEvent event = buildEvent(connection.getAsteriskServer(), buffer); - if (event != null) - { - connection.dispatchEvent(event); - } - } - - buffer.clear(); - } - else - { - int delimiterIndex; - - delimiterIndex = line.indexOf(":"); - if (delimiterIndex > 0 && line.length() > delimiterIndex + 2) - { - String name; - String value; - - name = line.substring(0, delimiterIndex).toLowerCase(); - value = line.substring(delimiterIndex + 2); - - buffer.put(name, value); - } - } - } - } - catch (IOException e) - { - log.warn("IOException while reading from asterisk server, terminating thread.", e); - } - finally - { - log.info("Disconnected, closing reader."); - try - { - reader.close(); - } - catch (IOException ex) - { - log.warn("Unable to close reader", ex); - } - - // cleans resources and reconnects if needed - connection.handleDisconnection(); - } - } - - public void die() - { - this.die = true; - } - - private ManagerResponse buildResponse(Map buffer) - { - ManagerResponse response; - - response = responseBuilder.buildResponse(buffer); - - if (response != null) - { - response.setDateReceived(new Date()); - } - - return response; - } - - private ManagerEvent buildEvent(Object source, Map buffer) - { - ManagerEvent event; - - event = eventBuilder.buildEvent(source, buffer); + void setSocket(final SocketConnectionFacade socket); - if (event != null) - { - event.setDateReceived(new Date()); - } + void die(); - return event; - } } Index: DefaultManagerConnection.java =================================================================== RCS file: /cvsroot/asterisk-java/asterisk-java/src/java/net/sf/asterisk/manager/DefaultManagerConnection.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -d -p -r1.3 -r1.4 --- DefaultManagerConnection.java 2 Mar 2005 00:37:38 -0000 1.3 +++ DefaultManagerConnection.java 2 Mar 2005 23:15:49 -0000 1.4 @@ -16,13 +16,8 @@ */ package net.sf.asterisk.manager; -import java.io.BufferedReader; import java.io.IOException; -import java.io.InputStreamReader; -import java.io.OutputStreamWriter; -import java.io.PrintWriter; import java.io.Serializable; -import java.net.Socket; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Collection; @@ -38,6 +33,8 @@ import net.sf.asterisk.manager.action.Ma import net.sf.asterisk.manager.event.ConnectEvent; import net.sf.asterisk.manager.event.DisconnectEvent; import net.sf.asterisk.manager.event.ManagerEvent; +import net.sf.asterisk.manager.io.SocketConnectionFacade; +import net.sf.asterisk.manager.io.SocketConnectionFacadeImpl; import net.sf.asterisk.manager.response.ChallengeResponse; import net.sf.asterisk.manager.response.ManagerError; import net.sf.asterisk.manager.response.ManagerResponse; @@ -51,7 +48,7 @@ import org.apache.commons.logging.LogFac * @author srt * @version $Id$ */ -public class DefaultManagerConnection implements ManagerConnection +public class DefaultManagerConnection implements ManagerConnection, Dispatcher { /** * Instance logger. @@ -68,23 +65,40 @@ public class DefaultManagerConnection im private long sleepTime = 50; private boolean keepAliveAfterAuthenticationFailure = false; - private Socket socket; - private ManagerReader reader; - private PrintWriter writer; - private ActionBuilder actionBuilder = new ActionBuilder(); + private SocketConnectionFacade socket; + private Thread readerThread; + + /** + * The reader to use to receive events and responses from asterisk. + */ + private final ManagerReader reader; + + /** + * The writer to use to send actions to asterisk. + */ + private final ManagerWriter writer; + private String protocolIdentifier; - private Map responseHandlers = new HashMap(); - private Collection eventHandlers = new HashSet(); + private final Map responseHandlers; + private final Collection eventHandlers; private boolean keepAlive = false; public DefaultManagerConnection() { this.asteriskServer = new AsteriskServer(); + + this.reader = new ManagerReaderImpl(this, asteriskServer); + this.writer = new ManagerWriterImpl(); + this.responseHandlers = new HashMap(); + this.eventHandlers = new HashSet(); } public DefaultManagerConnection(String hostname, int port, String username, String password) { - this.asteriskServer = new AsteriskServer(hostname, port); + this(); + + setHostname(hostname); + setPort(port); setUsername(username); setPassword(password); } @@ -273,24 +287,24 @@ public class DefaultManagerConnection im this.keepAlive = true; logger.info("Successfully logged in"); - - // notify event listeners - dispatchEvent(new ConnectEvent(this.asteriskServer)); } private synchronized void connect() throws IOException { - BufferedReader bufferedReader; - logger.info("Connecting to " + asteriskServer.getHostname() + " port " + asteriskServer.getPort()); - this.socket = new Socket(asteriskServer.getHostname(), asteriskServer.getPort()); + this.socket = createSocket(); - bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream())); + this.reader.setSocket(socket); + this.readerThread = new Thread(reader); + this.readerThread.start(); - this.reader = new ManagerReader(this, bufferedReader); - this.reader.start(); - this.writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream())); + this.writer.setSocket(socket); + } + + protected SocketConnectionFacade createSocket() throws IOException + { + return new SocketConnectionFacadeImpl(asteriskServer.getHostname(), asteriskServer.getPort()); } /** @@ -300,7 +314,7 @@ public class DefaultManagerConnection im * @return <code>true</code> if there is a socket connection to the asterisk server, * <code>false</code> otherwise. */ - protected synchronized boolean isConnected() + private synchronized boolean isConnected() { // return socket != null && socket.isConnected(); // JDK 1.4 return socket != null; @@ -317,7 +331,7 @@ public class DefaultManagerConnection im this.keepAlive = false; logoffAction = new LogoffAction(); - + if (isConnected()) { sendAction(logoffAction); @@ -327,23 +341,16 @@ public class DefaultManagerConnection im private synchronized void disconnect() { - if (this.writer != null) - { - logger.info("Closing writer"); - this.writer.close(); - this.writer = null; - } - if (this.socket != null) { - logger.info("Closing socket"); + logger.info("Closing socket."); try { this.socket.close(); } catch (IOException ex) { - logger.warn("Unable to close socket", ex); + logger.warn("Unable to close socket: " + ex.getMessage()); } this.socket = null; } @@ -389,12 +396,15 @@ public class DefaultManagerConnection im public void sendAction(ManagerAction action, ManagerResponseHandler callbackHandler) throws IOException { String internalActionId; - String actionString; if (action == null) { - logger.error("Unable to send null action"); - throw new IllegalArgumentException("Unable to send null action"); + throw new IllegalArgumentException("Unable to send action: action is null."); + } + + if (!isConnected()) + { + throw new IllegalStateException("Unable to send " + action.getAction() + " action: not connected."); } internalActionId = createInternalActionId(); @@ -416,21 +426,7 @@ public class DefaultManagerConnection im } } - actionString = actionBuilder.buildAction(action); - - synchronized (this) - { - if (!isConnected()) - { - throw new IllegalStateException("Unable to send " + action.getAction() + " action: not connected."); - } - - this.writer.print(actionString); - this.writer.flush(); - } - - logger.debug("Sent " + action.getAction() + " action with internalActionId '" + internalActionId + "':\n" - + actionString); + writer.sendAction(action); } /** @@ -439,7 +435,7 @@ public class DefaultManagerConnection im */ private String createInternalActionId() { - StringBuffer sb; + final StringBuffer sb; sb = new StringBuffer(); sb.append(this.hashCode()); @@ -449,7 +445,7 @@ public class DefaultManagerConnection im return sb.toString(); } - public void addEventHandler(ManagerEventHandler eventHandler) + public void addEventHandler(final ManagerEventHandler eventHandler) { synchronized (this.eventHandlers) { @@ -457,7 +453,7 @@ public class DefaultManagerConnection im } } - public void removeEventHandler(ManagerEventHandler eventHandler) + public void removeEventHandler(final ManagerEventHandler eventHandler) { synchronized (this.eventHandlers) { @@ -473,7 +469,7 @@ public class DefaultManagerConnection im return this.protocolIdentifier; } - /* Callbacks for ManagerReader */ + /* Callbacks for ManagerReaderImpl */ /** * This method is called by the reader whenever a {@link ManagerResponse} is received. The @@ -481,11 +477,11 @@ public class DefaultManagerConnection im * * @param response the resonse received by the reader * - * @see ManagerReader + * @see ManagerReaderImpl */ - void dispatchResponse(ManagerResponse response) + public void dispatchResponse(ManagerResponse response) { - String actionId; + final String actionId; String internalActionId; ManagerResponseHandler responseHandler; @@ -545,9 +541,9 @@ public class DefaultManagerConnection im * * @see #addEventHandler(ManagerEventHandler) * @see #removeEventHandler(ManagerEventHandler) - * @see ManagerReader + * @see ManagerReaderImpl */ - void dispatchEvent(ManagerEvent event) + public void dispatchEvent(ManagerEvent event) { if (event == null) { @@ -578,6 +574,15 @@ public class DefaultManagerConnection im } } } + + if (event instanceof ConnectEvent) + { + setProtocolIdentifier(((ConnectEvent) event).getProtocolIdentifier()); + } + if (event instanceof DisconnectEvent) + { + handleDisconnection(); + } } /** @@ -587,7 +592,7 @@ public class DefaultManagerConnection im * * @param protocolIdentifier the protocol version received by the reader * - * @see ManagerReader + * @see ManagerReaderImpl */ void setProtocolIdentifier(String protocolIdentifier) { @@ -617,9 +622,6 @@ public class DefaultManagerConnection im // clean up at first disconnect(); - // notify event listeners - dispatchEvent(new DisconnectEvent(this.asteriskServer)); - // try to reconnect numTries = 0; while (this.keepAlive) @@ -671,7 +673,10 @@ public class DefaultManagerConnection im { // shouldn't happen logger.error("TimeoutException while trying to log in after reconnect."); - socket.close(); + synchronized (this) + { + socket.close(); + } } } catch (IOException e) |