solar.service.transport
Class TcpTransport

java.lang.Object
  |
  +--solar.service.transport.TcpTransport
All Implemented Interfaces:
java.lang.Runnable, Transport
Direct Known Subclasses:
TcpObjectTransport

public class TcpTransport
extends java.lang.Object
implements Transport, java.lang.Runnable

The TCP transport layer, converting ADU to bytes before sending.

Version:
$Id: TcpTransport.java,v 1.16 2003/10/12 21:36:18 glchen Exp $
Author:
Guanling Chen

Field Summary
protected  AduFactory _aduFactory
          The factory to parse received byte array into ADU.
protected  java.util.Hashtable _flows
          The table of live connections.
protected  java.lang.String _hostname
          Particular interface to bind to.
protected  Address _laddr
          The local address for TCP.
protected  java.util.List _listeners
          The list of TCP listeners.
protected static org.apache.log4j.Logger _log
          Logging facility.
protected  int _port
          The listening server port.
protected  java.lang.Thread _server
          The server thread accepting new connection.
protected  java.net.ServerSocket _ssock
          The listening server socket.
 
Constructor Summary
TcpTransport()
          Constructs a new TCP transport, which listens on an available port.
TcpTransport(int port)
          Constructs a new TCP transport, which listens on a specified port.
TcpTransport(java.lang.String hostname, int port)
           
 
Method Summary
protected  void addConnection(SocketAddress peer, TcpConnection conn)
          Add a new connection to peer address.
 void addTransportListener(TransportListener listener)
          Add a transport listener.
protected  void clearConnections()
          Remove all the connections.
 Address getAddressByKey(java.lang.String key)
          Return a peer address by specified key.
 AduFactory getAduFactory()
          Return the factory producing Adu from byte array.
protected  TcpConnection getConnection(SocketAddress peer)
          Get the connection for specified peer address.
 Address getLocalAddress()
          Return the local address of this transport layer.
 org.apache.log4j.Logger getLogger()
          Return the logger of this transport.
 int getPort()
          Return the port number listened by this transport.
protected  TcpConnection makeTcpConnection(java.net.Socket socket, SocketAddress peer)
          Create a TcpConenction based on the socket and peer address.
 void notifyConnectionClosed(SocketAddress peer)
          Notify transport listeners about closed connection.
 void notifyConnectionOpened(SocketAddress peer)
          Notify transport listeners about new connection.
 void notifyDataReceived(Adu adu, Header header)
          Notify transport listeners about received data.
protected  TcpConnection openConnection(SocketAddress dest)
          Open a new connection to a specified address.
protected  void removeConnection(SocketAddress peer)
          Remove a connection to a peer address.
 void removeTransportListener(TransportListener listener)
          Remove a transport listener.
 void run()
           
 void send(Adu adu, Address dest)
          Send the adu over transport to destination.
 void send(Adu adu, Address dest, Id multiplex)
          Send the adu over transport to destination, with a multipex ID.
 void send(byte[] data, Address dest)
          Send an data unit to destination address.
 void send(byte[] data, Address dest, Id multiplex)
          Send an data unit to destination address with a multiplexer.
 void setAduFactory(AduFactory factory)
          Set the factory that parse the received bytes into data unit.
private  void start()
          Start the TCP transport.
 void stop()
          Stop the transport layer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_log

protected static org.apache.log4j.Logger _log
Logging facility.


_hostname

protected java.lang.String _hostname
Particular interface to bind to.


_port

protected int _port
The listening server port.


_ssock

protected java.net.ServerSocket _ssock
The listening server socket.


_server

protected java.lang.Thread _server
The server thread accepting new connection.


_flows

protected java.util.Hashtable _flows
The table of live connections.


_listeners

protected java.util.List _listeners
The list of TCP listeners.


_laddr

protected Address _laddr
The local address for TCP.


_aduFactory

protected AduFactory _aduFactory
The factory to parse received byte array into ADU.

Constructor Detail

TcpTransport

public TcpTransport()
             throws java.lang.Exception
Constructs a new TCP transport, which listens on an available port.


TcpTransport

public TcpTransport(int port)
             throws java.lang.Exception
Constructs a new TCP transport, which listens on a specified port.


TcpTransport

public TcpTransport(java.lang.String hostname,
                    int port)
             throws java.lang.Exception
Method Detail

start

private void start()
            throws java.lang.Exception
Start the TCP transport.

java.lang.Exception

setAduFactory

public void setAduFactory(AduFactory factory)
Description copied from interface: Transport
Set the factory that parse the received bytes into data unit.

Specified by:
setAduFactory in interface Transport

getLocalAddress

public Address getLocalAddress()
Description copied from interface: Transport
Return the local address of this transport layer.

Specified by:
getLocalAddress in interface Transport

getAddressByKey

public Address getAddressByKey(java.lang.String key)
                        throws TransportException
Description copied from interface: Transport
Return a peer address by specified key.

Specified by:
getAddressByKey in interface Transport
TransportException

send

public void send(byte[] data,
                 Address dest)
          throws TransportException
Description copied from interface: Transport
Send an data unit to destination address.

Specified by:
send in interface Transport
TransportException

send

public void send(byte[] data,
                 Address dest,
                 Id multiplex)
          throws TransportException
Description copied from interface: Transport
Send an data unit to destination address with a multiplexer.

Specified by:
send in interface Transport
TransportException

send

public void send(Adu adu,
                 Address dest)
          throws TransportException
Description copied from interface: Transport
Send the adu over transport to destination.

Specified by:
send in interface Transport
TransportException

send

public void send(Adu adu,
                 Address dest,
                 Id multiplex)
          throws TransportException
Description copied from interface: Transport
Send the adu over transport to destination, with a multipex ID.

Specified by:
send in interface Transport
TransportException

addTransportListener

public void addTransportListener(TransportListener listener)
Description copied from interface: Transport
Add a transport listener.

Specified by:
addTransportListener in interface Transport

removeTransportListener

public void removeTransportListener(TransportListener listener)
Description copied from interface: Transport
Remove a transport listener.

Specified by:
removeTransportListener in interface Transport

stop

public void stop()
Description copied from interface: Transport
Stop the transport layer.

Specified by:
stop in interface Transport

makeTcpConnection

protected TcpConnection makeTcpConnection(java.net.Socket socket,
                                          SocketAddress peer)
                                   throws java.lang.Exception
Create a TcpConenction based on the socket and peer address.

java.lang.Exception

getPort

public int getPort()
Return the port number listened by this transport.


getLogger

public org.apache.log4j.Logger getLogger()
Return the logger of this transport.


getAduFactory

public AduFactory getAduFactory()
Return the factory producing Adu from byte array.


notifyDataReceived

public void notifyDataReceived(Adu adu,
                               Header header)
Notify transport listeners about received data.


notifyConnectionOpened

public void notifyConnectionOpened(SocketAddress peer)
Notify transport listeners about new connection.


notifyConnectionClosed

public void notifyConnectionClosed(SocketAddress peer)
Notify transport listeners about closed connection.


getConnection

protected TcpConnection getConnection(SocketAddress peer)
Get the connection for specified peer address.


addConnection

protected void addConnection(SocketAddress peer,
                             TcpConnection conn)
Add a new connection to peer address.


removeConnection

protected void removeConnection(SocketAddress peer)
Remove a connection to a peer address.


openConnection

protected TcpConnection openConnection(SocketAddress dest)
                                throws java.lang.Exception
Open a new connection to a specified address.

java.lang.Exception

clearConnections

protected void clearConnections()
Remove all the connections.


run

public void run()
Specified by:
run in interface java.lang.Runnable