triptracker.client.net
Class SocketConnection<E extends SocketListener>

java.lang.Object
  extended by triptracker.client.net.SocketConnection<E>
Direct Known Subclasses:
GPSSocket, MapSocket

public abstract class SocketConnection<E extends SocketListener>
extends Object

Connects to the server through a socket.


Field Summary
protected  BufferedReader in
           
protected  InputStream inStream
           
protected  Set<E> listeners
          Listener set for Model View Controller (MVC) separation.
protected  boolean loggedIn
           
protected  PrintStream out
           
protected  OutputStream outStream
           
 
Constructor Summary
SocketConnection()
           
 
Method Summary
 void addListener(E listener)
          Register a listener for server events.
protected  void brokenListener(SocketListener listener, RuntimeException exception)
          Reports a broken listener, removes it from the listener queue and logs the error.
 void connect()
          Connects to the hostname and port number set by setHost(String) and setPort(int).
 void connect(String host, int port)
          Connect to server on specified port.
protected  void connectionUpdate(ConnectionState state)
          Publish a connection state change event to all listeners.
 void disconnect()
          Disconnect from the socket.
 String getHost()
          Returns the current hostname.
 int getPort()
          Returns the current port.
 void getRoutes(String username)
          Request a list of routes for a given user.
 String getUsername()
          Returns the name of the active user.
protected  void invalidMessage(String message)
          Publish an invalid message event to all listeners.
 boolean isClosed()
          Checks if the client connection is closed.
 boolean isConnected()
          Checks if the client is connected to the server.
 boolean isLoggedIn()
          Checks if the client is logged in.
protected  void logon(int type, String user, String pass)
          Logon a user.
protected abstract  boolean messageHandler(String message)
          Message handler for data received on socket.
protected  void messageLoop()
          Checks for messages recieved from the server, and pass them to the message handler for further processing.
 void removeListener(E listener)
          Remove listener from listener queue.
protected  void sendMessage(Object... message)
          Sends a message to the server
 void setHost(String host)
          Set hostname to connect to.
 void setPort(int port)
          Set port to connect to.
 void setRoute(Route route)
          Sets route to track
protected  void socketErrorEvent(IOException exception)
          Publish a socket error event to all listeners.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

inStream

protected InputStream inStream

outStream

protected OutputStream outStream

in

protected BufferedReader in

out

protected PrintStream out

loggedIn

protected boolean loggedIn

listeners

protected Set<E extends SocketListener> listeners
Listener set for Model View Controller (MVC) separation.

Constructor Detail

SocketConnection

public SocketConnection()
Method Detail

connect

public void connect()
             throws IOException
Connects to the hostname and port number set by setHost(String) and setPort(int).

Throws:
IOException - on connection failure
See Also:
connect(String, int)

connect

public void connect(String host,
                    int port)
             throws IOException
Connect to server on specified port.

Parameters:
host - server hostname
port - destination port
Throws:
IOException - on connection failure
See Also:
#connect(String)

logon

protected void logon(int type,
                     String user,
                     String pass)
              throws IOException
Logon a user.

Parameters:
type - type of user
user - username
pass - password
Throws:
IOException - on connection failure

setRoute

public void setRoute(Route route)
Sets route to track


getRoutes

public void getRoutes(String username)
Request a list of routes for a given user. When the requested list is received from the server.


sendMessage

protected void sendMessage(Object... message)
Sends a message to the server

Parameters:
message -

isConnected

public boolean isConnected()
Checks if the client is connected to the server.

Returns:
true if connected, false if disconnected

isClosed

public boolean isClosed()
Checks if the client connection is closed.

Returns:
true if connected, false if disconnected

isLoggedIn

public boolean isLoggedIn()
Checks if the client is logged in.

Returns:
true if logged in, false if not logged in

messageLoop

protected void messageLoop()
                    throws IOException
Checks for messages recieved from the server, and pass them to the message handler for further processing. Subclasses can override this method to provide their own message loop if message processing needs to be more fine grained than simple line based delimiting.

Throws:
IOException

messageHandler

protected abstract boolean messageHandler(String message)
Message handler for data received on socket. Subclasses must implement this method to provide handing of recieved messages.

Parameters:
message - received message from server
Returns:
true if more messages can be processed, false to end the message loop, stop message handling and close the socket connection

disconnect

public void disconnect()
                throws IOException
Disconnect from the socket.

Throws:
IOException - on socket error

addListener

public void addListener(E listener)
Register a listener for server events.

Parameters:
listener - event receiver to register

removeListener

public void removeListener(E listener)
Remove listener from listener queue.

Parameters:
listener - event receiver to remove

brokenListener

protected void brokenListener(SocketListener listener,
                              RuntimeException exception)
Reports a broken listener, removes it from the listener queue and logs the error.

Parameters:
listener - broken listener
exception - exception thrown by listener

socketErrorEvent

protected void socketErrorEvent(IOException exception)
Publish a socket error event to all listeners. This generally means that the client socket connection has been closed.

Parameters:
exception - socket exception thrown

invalidMessage

protected void invalidMessage(String message)
Publish an invalid message event to all listeners. This signals an unsupported or malformed message recieved from the server.

Parameters:
message - full message

connectionUpdate

protected void connectionUpdate(ConnectionState state)
Publish a connection state change event to all listeners. This notifies the clients that the connection state of the underlying socket has changed.

Parameters:
state - new connection state

getHost

public String getHost()
Returns the current hostname. By default the hostname is set to Protocol.HOST.

Returns:
hostname

setHost

public void setHost(String host)
Set hostname to connect to. By default the hostname is set to Protocol.HOST.

Parameters:
host - host to set

getPort

public int getPort()
Returns the current port. By default the port number is set to Protocol.PORTNR.

Returns:
port number

setPort

public void setPort(int port)
Set port to connect to. By default the port number is set to Protocol.PORTNR.

Parameters:
port - The port to set.

getUsername

public String getUsername()
Returns the name of the active user. This is set during login when connecting to the server.

Returns:
active user name


Copyright © 2006 Team Trip Tracker. All Rights Reserved.