From 518995debd939233c8b275d38dc6df735bf2ec40 Mon Sep 17 00:00:00 2001 From: Danny van Heumen Date: Sun, 24 Aug 2014 19:57:00 +0200 Subject: IRC: some refactoring of connect methods. --- .../communicator/impl/protocol/irc/IrcStack.java | 144 ++++++++++----------- 1 file changed, 71 insertions(+), 73 deletions(-) (limited to 'src/net') diff --git a/src/net/java/sip/communicator/impl/protocol/irc/IrcStack.java b/src/net/java/sip/communicator/impl/protocol/irc/IrcStack.java index 95a5e14..4845dd9 100644 --- a/src/net/java/sip/communicator/impl/protocol/irc/IrcStack.java +++ b/src/net/java/sip/communicator/impl/protocol/irc/IrcStack.java @@ -263,13 +263,41 @@ public class IrcStack irc.addListener(new DebugListener()); } - connectSynchronized(); + try + { + connectSynchronized(); + + // instantiate presence manager for the connection + this.presence = + new PresenceManager(irc, this.connectionState, + this.provider.getPersistentPresence()); + + queryIdentity(); - queryIdentity(); + // if connecting succeeded, set state to registered + this.provider + .setCurrentRegistrationState(RegistrationState.REGISTERED); - // TODO Read IRC network capabilities based on RPL_ISUPPORT (005) - // replies if available. This information should be available in - // irc-api if possible. + // TODO Read IRC network capabilities based on RPL_ISUPPORT + // (005) + // replies if available. This information should be available in + // irc-api if possible. + } + catch (IOException e) + { + // Also SSL exceptions will be caught here. + this.provider + .setCurrentRegistrationState( + RegistrationState.CONNECTION_FAILED); + throw e; + } + catch (InterruptedException e) + { + this.provider + .setCurrentRegistrationState( + RegistrationState.UNREGISTERED); + throw e; + } } } @@ -291,88 +319,57 @@ public class IrcStack synchronized (result) { // start connecting to the specified server ... - try + irc.connect(this.params, new Callback() { - irc.connect(this.params, new Callback() - { - - @Override - public void onSuccess(final IIRCState state) - { - synchronized (result) - { - LOGGER.trace("IRC connected successfully!"); - result.setDone(state); - result.notifyAll(); - } - } - @Override - public void onFailure(final Exception e) + @Override + public void onSuccess(final IIRCState state) + { + synchronized (result) { - synchronized (result) - { - LOGGER.trace("IRC connection FAILED!", e); - result.setDone(e); - result.notifyAll(); - } + LOGGER.trace("IRC connected successfully!"); + result.setDone(state); + result.notifyAll(); } - }); - - this.provider - .setCurrentRegistrationState(RegistrationState.REGISTERING); - - while (!result.isDone()) - { - LOGGER.trace("Waiting for the connection to be " - + "established ..."); - result.wait(); } - this.connectionState = result.getValue(); - // TODO Implement connection timeout and a way to recognize that - // the timeout occurred. - if (this.connectionState != null - && this.connectionState.isConnected()) - { - // instantiate presence manager for the connection - this.presence = - new PresenceManager(irc, this.connectionState, - this.provider.getPersistentPresence()); - - // if connecting succeeded, set state to registered - this.provider.setCurrentRegistrationState( - RegistrationState.REGISTERED); - } - else + @Override + public void onFailure(final Exception e) { - // if connecting failed, set state to unregistered and throw - // the exception if one exists - this.provider - .setCurrentRegistrationState( - RegistrationState.CONNECTION_FAILED); - Exception e = result.getException(); - if (e != null) + synchronized (result) { - throw e; + LOGGER.trace("IRC connection FAILED!", e); + result.setDone(e); + result.notifyAll(); } } - } - catch (IOException e) + }); + + this.provider + .setCurrentRegistrationState(RegistrationState.REGISTERING); + + while (!result.isDone()) { - // Also SSL exceptions will be caught here. - this.provider - .setCurrentRegistrationState( - RegistrationState.CONNECTION_FAILED); - throw e; + LOGGER.trace("Waiting for the connection to be " + + "established ..."); + result.wait(); } - catch (InterruptedException e) + + // TODO Implement connection timeout and a way to recognize that + // the timeout occurred. + + final Exception e = result.getException(); + if (e != null) { - this.provider - .setCurrentRegistrationState( - RegistrationState.UNREGISTERED); - throw e; + throw new IOException(e); } + + final IIRCState connectionState = result.getValue(); + if (connectionState == null) { + throw new IOException("Failed to connect: no connection state available."); + } + + this.connectionState = connectionState; } } @@ -381,6 +378,7 @@ public class IrcStack */ private void queryIdentity() { + // TODO Install temporary whois listener that handles the result. this.session.get().rawMessage( "WHOIS " + this.connectionState.getNickname()); } -- cgit v1.1