forward.server
Class ServerSocketPropagandist

java.lang.Object
  extended by java.util.Observable
      extended by forward.server.ServerSocketPropagandist
All Implemented Interfaces:
java.lang.Runnable

public class ServerSocketPropagandist
extends java.util.Observable
implements java.lang.Runnable

This class registers the port of a ServerSocketManager at the infoservice (the IP is detected by the infoservice). So the blockees can find that interface and can connect to. This class is also observable. When the registration state or the current error code changes, the observers are notified (there is no message sent -> message = null). See the STATE and RETURN constants and the getCurrentState() and getCurrentErrorCode() method for more information.


Field Summary
private static int FORWARDER_RENEW_ERROR_CODE
          This is the error code the infoservice returns, if the registration was lost and the infoservice doesn't know any mor our forwarding id.
private static long FORWARDER_RENEW_PERIOD
          The period in milliseconds for renewing the forwarder entry at the infoservice.
private static int FORWARDER_VERIFY_ERROR_CODE
          This is the error code the infoservice returns, if verifying of our forwarding server was not successful.
private  int m_currentConnectionState
          Stores the current connection state.
private  int m_currentErrorCode
          Stores the error code of the first announcement try.
private  java.lang.String m_forwarderId
          Stores our by the infoservice assigned id, if the announcement was successful.
private  InfoServiceDBEntry m_infoService
          Stores the infoservice where we have to announce the local port number.
private  int m_portNumber
          Stores the local port nmuber which we have to announce to the infoservice.
private  java.lang.Thread m_propagandaThread
          Stores the instance of the propaganda thread.
private static int RETURN_FORWARDERID_ERROR
          This value is returned, if renewing of our forwarding entry fails at the infoservice because the infoservice does not know our forwarder id any more.
static int RETURN_INFOSERVICE_ERROR
          This value is returned, if we could not reach the infoservice or the infoservice has no forwarder list.
static int RETURN_SUCCESS
          This value is returned, if announcing or renewing of the forwarder entry at the infoservice was successful.
static int RETURN_UNKNOWN_ERROR
          This value is returned, if there was an unexpected error while infoservice communication.
static int RETURN_VERIFICATION_ERROR
          This value is returned, if the infoservice could not verify the local forwarding server.
static int STATE_CONNECTING
          This is the state, when we were never registered at the infoservice and we are trying to register the first time.
static int STATE_HALTED
          This is the state, when the propaganda thread was stopped.
static int STATE_RECONNECTING
          This is the state, when we were already registered at the infoservice, but the registration was lost and we are trying to register again.
static int STATE_REGISTERED
          This is the state, when we are registerd at the infoservice.
 
Constructor Summary
ServerSocketPropagandist(int a_portNumber, InfoServiceDBEntry a_infoService)
          Creates a new ServerSocketPropagandist.
 
Method Summary
private  int announceNewForwarder()
          This method announces the local forwarding server with the specified port to the specified infoservice.
 int getCurrentErrorCode()
          Returns the error code of the last announcement try.
 int getCurrentState()
          Returns the current registration state at the infoservice.
 InfoServiceDBEntry getInfoService()
          Returns the infoservice, where this propagandist is trying to get registrated.
private  int renewForwarder()
          This method renews our forwarding entry at the infoservice.
 void run()
          This is the implementation of the propaganda thread.
 void stopPropaganda()
          This will stop the propaganda thread.
 
Methods inherited from class java.util.Observable
addObserver, clearChanged, countObservers, deleteObserver, deleteObservers, hasChanged, notifyObservers, notifyObservers, setChanged
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

STATE_REGISTERED

public static final int STATE_REGISTERED
This is the state, when we are registerd at the infoservice.

See Also:
Constant Field Values

STATE_CONNECTING

public static final int STATE_CONNECTING
This is the state, when we were never registered at the infoservice and we are trying to register the first time.

See Also:
Constant Field Values

STATE_RECONNECTING

public static final int STATE_RECONNECTING
This is the state, when we were already registered at the infoservice, but the registration was lost and we are trying to register again.

See Also:
Constant Field Values

STATE_HALTED

public static final int STATE_HALTED
This is the state, when the propaganda thread was stopped.

See Also:
Constant Field Values

RETURN_SUCCESS

public static final int RETURN_SUCCESS
This value is returned, if announcing or renewing of the forwarder entry at the infoservice was successful.

See Also:
Constant Field Values

RETURN_VERIFICATION_ERROR

public static final int RETURN_VERIFICATION_ERROR
This value is returned, if the infoservice could not verify the local forwarding server.

See Also:
Constant Field Values

RETURN_INFOSERVICE_ERROR

public static final int RETURN_INFOSERVICE_ERROR
This value is returned, if we could not reach the infoservice or the infoservice has no forwarder list.

See Also:
Constant Field Values

RETURN_UNKNOWN_ERROR

public static final int RETURN_UNKNOWN_ERROR
This value is returned, if there was an unexpected error while infoservice communication.

See Also:
Constant Field Values

RETURN_FORWARDERID_ERROR

private static final int RETURN_FORWARDERID_ERROR
This value is returned, if renewing of our forwarding entry fails at the infoservice because the infoservice does not know our forwarder id any more. This value is only for internal usage.

See Also:
Constant Field Values

FORWARDER_VERIFY_ERROR_CODE

private static final int FORWARDER_VERIFY_ERROR_CODE
This is the error code the infoservice returns, if verifying of our forwarding server was not successful.

See Also:
Constant Field Values

FORWARDER_RENEW_ERROR_CODE

private static final int FORWARDER_RENEW_ERROR_CODE
This is the error code the infoservice returns, if the registration was lost and the infoservice doesn't know any mor our forwarding id.

See Also:
Constant Field Values

FORWARDER_RENEW_PERIOD

private static final long FORWARDER_RENEW_PERIOD
The period in milliseconds for renewing the forwarder entry at the infoservice. The default is 10 minutes.

See Also:
Constant Field Values

m_portNumber

private int m_portNumber
Stores the local port nmuber which we have to announce to the infoservice.


m_infoService

private InfoServiceDBEntry m_infoService
Stores the infoservice where we have to announce the local port number.


m_forwarderId

private java.lang.String m_forwarderId
Stores our by the infoservice assigned id, if the announcement was successful.


m_currentErrorCode

private int m_currentErrorCode
Stores the error code of the first announcement try. If there was an error while the first announcement try, this is normally because of a configuration error. With that error code it should be easier to find the configuration problem. See the RETURN constants in this class for a description of the values.


m_propagandaThread

private java.lang.Thread m_propagandaThread
Stores the instance of the propaganda thread.


m_currentConnectionState

private int m_currentConnectionState
Stores the current connection state. See the STATE constants in this class for a description of the values.

Constructor Detail

ServerSocketPropagandist

public ServerSocketPropagandist(int a_portNumber,
                                InfoServiceDBEntry a_infoService)
Creates a new ServerSocketPropagandist. The instance will register a local forwarding service at the infoservice. If the registration gets lost, a re-registration is tried automatically. This method blocks until the first registration try is done. The error code of that first try can be obtained via the getFirstErrorCode() method. If that first try was not successful, normally that's caused by configuration problem.

Parameters:
a_portNumber - The port number of a local ServerSocketManager.
a_infoService - The infoservice (which must have a forwarder list) where we shall get registered.
Method Detail

stopPropaganda

public void stopPropaganda()
This will stop the propaganda thread. This method doesn't block, so we set only a flag and don't wait for the end of the thread.


getCurrentState

public int getCurrentState()
Returns the current registration state at the infoservice. See the STATE constants in this class. Observers should call this method, after they have received a update message from this instance. So they can get the new registration state.

Returns:
The current infoservice registration state.

getCurrentErrorCode

public int getCurrentErrorCode()
Returns the error code of the last announcement try. If there was an error while the last announcement try, this is normally because of a configuration error. With that error code it should be easier to find the configuration problem. See the RETURN constants in this class for a description of the values. Only RETURN_SUCCESS, RETURN_VERIFICATION_ERROR, RETURN_INFOSERVICE_ERROR or RETURN_UNKNOWN_ERROR are possible here. In the states STATE_REGISTERED or STATE_HALTED, always RETURN_SUCCESS is returned.

Returns:
The error code of the last announcement try.

getInfoService

public InfoServiceDBEntry getInfoService()
Returns the infoservice, where this propagandist is trying to get registrated.

Returns:
The infoservice, which is updated by this propagandist.

run

public void run()
This is the implementation of the propaganda thread. It will register and (if needed) re-register the local forwarder at the infoservice.

Specified by:
run in interface java.lang.Runnable

announceNewForwarder

private int announceNewForwarder()
This method announces the local forwarding server with the specified port to the specified infoservice. Thie method returns one of the return constants in this class.

Returns:
RETURN_SUCCESS, if we could announce the forwarder to the infoservice successfully.
RETURN_VERIFICATION_ERROR, if verification of the local forwarding server by the infoservice failed.
RETURN_INFOSERVICE_ERROR, if the connection to the infoservice failed or if the infoservice has no forwarder list.
RETURN_UNKNOWN_ERROR, if there was an unexpected error, like an error while parsing the answer from the infoservice.

renewForwarder

private int renewForwarder()
This method renews our forwarding entry at the infoservice. The infoservice will throw away every entry after a timeout, so we have to renew it early enough.

Returns:
RETURN_SUCCESS, if we could announce the forwarder to the infoservice successfully.
RETURN_FORWARDERID_ERROR, if the registration was lost (the infoservice does not know our forwarder id any more) and we have to re-register.
RETURN_INFOSERVICE_ERROR, if the connection to the infoservice failed or if the infoservice has no forwarder list.
RETURN_UNKNOWN_ERROR, if there was an unexpected error, like an error while parsing the answer from the infoservice.