f00f.net.irc.martyr.services
Class AutoReconnect

java.lang.Object
  extended by f00f.net.irc.martyr.GenericCommandAutoService
      extended by f00f.net.irc.martyr.GenericAutoService
          extended by f00f.net.irc.martyr.services.AutoReconnect
All Implemented Interfaces:
java.util.Observer

public class AutoReconnect
extends GenericAutoService

AutoReconnect performs the job of reconnecting to the server, if the connection is terminated unexpectedly. AutoReconnect will try to reconnect 5 times, and then give up. If AutoReconnect intercepts a QUIT command before the state change that is issued by us (bounced back from the server) then it will not try to reconnect.

Note that AutoReconnect has no play in nick negotiation, and as such, a failed nick negotiation does not count as a connection retry.

Testing note: Does a server send a QUIT before a forceful removal from the network? Should AutoReconnect not intercept QUITs? Certainly not all servers send QUITs even when you QUIT on your own; this class should be put into a command-out chain as well.


Field Summary
static int DEFAULT_CONNECT_SLEEPTIME
           
static boolean DEFAULT_DISABLE_ON_QUIT
           
static int DEFAULT_MAX_ATTEMPTS
           
 
Fields inherited from class f00f.net.irc.martyr.GenericCommandAutoService
connection, enabled
 
Constructor Summary
AutoReconnect(IRCConnection connection)
          Initializes with reasonable defaults.
AutoReconnect(IRCConnection connection, int maxAttempts, int sleepBetween)
           
AutoReconnect(IRCConnection connection, int maxAttempts, int sleepBetween, boolean disableOnQuit)
           
 
Method Summary
protected  void connect()
          Attempts a single connection to the server.
protected  void connect(java.lang.String server, int port)
          Attempts a single connection to the server.
protected  void doConnectionLoop()
          Calls connect() followed by failedToConnect(...) until a connection is made.
protected  void doConnectionLoop(java.lang.String server, int port)
          Calls connect() followed by failedToConnect(...) until a connection is made, or the service is disabled.
protected  boolean failedToConnect(java.lang.Exception error)
          Called when a failure to connect occurs.
protected  void finalFailure()
          Called when the final failure has occurred.
 void go(java.lang.String server, int port)
          Attempts to connect, returning only when a connection has been made or repeated connections have failed.
 java.lang.String toString()
           
protected  void updateCommand(InCommand command)
          AutoReconnect will disable itself if it sees a quit command generated by returned from the server.
protected  void updateState(State state)
           
 
Methods inherited from class f00f.net.irc.martyr.GenericAutoService
disable, enable, update
 
Methods inherited from class f00f.net.irc.martyr.GenericCommandAutoService
getConnection, isEnabled
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

DEFAULT_MAX_ATTEMPTS

public static final int DEFAULT_MAX_ATTEMPTS
See Also:
Constant Field Values

DEFAULT_CONNECT_SLEEPTIME

public static final int DEFAULT_CONNECT_SLEEPTIME
See Also:
Constant Field Values

DEFAULT_DISABLE_ON_QUIT

public static final boolean DEFAULT_DISABLE_ON_QUIT
See Also:
Constant Field Values
Constructor Detail

AutoReconnect

public AutoReconnect(IRCConnection connection,
                     int maxAttempts,
                     int sleepBetween,
                     boolean disableOnQuit)
Parameters:
connection - The IRCConnection
maxAttempts - The number of tries to do before giving up
sleepBetween - Milliseconds to sleep between retries
disableOnQuit - Automatically disable on quit event

AutoReconnect

public AutoReconnect(IRCConnection connection,
                     int maxAttempts,
                     int sleepBetween)

AutoReconnect

public AutoReconnect(IRCConnection connection)
Initializes with reasonable defaults.

Parameters:
connection - Connection we are associated with
Method Detail

go

public void go(java.lang.String server,
               int port)
Attempts to connect, returning only when a connection has been made or repeated connections have failed.

Parameters:
server - Server to connect to
port - Port to connect to

connect

protected void connect()
                throws java.io.IOException
Attempts a single connection to the server. The connection is done by asking the client state what server and port we are supposed to be connected to, and then calling IRCConnection.connect(...).

Throws:
java.io.IOException - if we could not connect successfully

connect

protected void connect(java.lang.String server,
                       int port)
                throws java.io.IOException
Attempts a single connection to the server.

Parameters:
server - Server to connect to
port - Port to connect to
Throws:
java.io.IOException - if we could not connect successfully

updateState

protected void updateState(State state)
Specified by:
updateState in class GenericAutoService

doConnectionLoop

protected void doConnectionLoop()
Calls connect() followed by failedToConnect(...) until a connection is made. Gets the server and port from the client state, implying that a connection was already attempted before.


doConnectionLoop

protected void doConnectionLoop(java.lang.String server,
                                int port)
Calls connect() followed by failedToConnect(...) until a connection is made, or the service is disabled.

Parameters:
server - Server to connect to
port - Port to connect to

finalFailure

protected void finalFailure()
Called when the final failure has occurred. Default implementation does nothing.


failedToConnect

protected boolean failedToConnect(java.lang.Exception error)
Called when a failure to connect occurs. This method should do whatever needs to be done between connection attempts; usually this will be sleeping the current thread and checking if we should stop trying to reconnect.

Parameters:
error - Exception that caused the failure
Returns:
true if another attempt at connecting should occur

updateCommand

protected void updateCommand(InCommand command)
AutoReconnect will disable itself if it sees a quit command generated by returned from the server. This usually happens just before the server terminates the connection. Note that this would really be better if we could intercept messages on their way out, but Martyr doesn't do that.

Specified by:
updateCommand in class GenericCommandAutoService

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object


Copyright © 2000-2007 Ben Damm, Daniel Henninger, et al.