br.shob
Class AbstractCommLayer

java.lang.Object
  |
  +--br.shob.AbstractCommLayer
Direct Known Subclasses:
ClientCommLayer, ServerCommLayer

public abstract class AbstractCommLayer
extends Object

This class contains all common processing for the Communications Layer, which involves connecting to peer machines and sending/receiving signed messages from a socket.

Version:
$Revision: 1.11 $
Author:
crolmos, dukejeffrie
Created:
August 30, 2002

Field Summary
static String keyPairGeneratorAlgorithm
          KeyPair generator algorithm name.
protected  Logger logger
          Description of the Field
protected  HostId myId
          The HostId of this host.
static String signatureAlgorithm
          The algorithm used in all digital signatures in all hosts.
 
Constructor Summary
protected AbstractCommLayer()
          Creates new CommLayer
  AbstractCommLayer(HostId myId, PrivateKey pKey)
          Constructs a new instance of the communications layer.
  AbstractCommLayer(String hostname, int port)
          Constructs a new instance of the communications layer.
 
Method Summary
 Socket connect(String hostname, int port)
          connects with the peer.
 Socket connect(URL url)
          connects with the peer.
 Logger getLogger()
          Gets the logger for this comm layer.
 boolean isOnline()
          Checks if this commlayer is online (i.e., running and listening).
 Message recvMessage(Socket s, HostId peer)
          Receives a message using the Root class loader.
 Message recvMessage(Socket s, HostId peer, ClassLoader loader)
          Reads a message coming from the given socket and verifies its authenticity using the public key from peer.
 void sendMessage(Socket s, Message m)
          Signs and send a message through the given socket.
 void setLogger(Logger logger)
          Sets the logger for this instance
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

keyPairGeneratorAlgorithm

public static final String keyPairGeneratorAlgorithm
KeyPair generator algorithm name. Currently "RSA"

See Also:
Constant Field Values

signatureAlgorithm

public static final String signatureAlgorithm
The algorithm used in all digital signatures in all hosts.

See Also:
Constant Field Values

logger

protected Logger logger
Description of the Field


myId

protected HostId myId
The HostId of this host.

Constructor Detail

AbstractCommLayer

public AbstractCommLayer(HostId myId,
                         PrivateKey pKey)
Constructs a new instance of the communications layer.

Parameters:
myId - HostId of the local host.
pKey - PrivateKey to be used for signing.

AbstractCommLayer

public AbstractCommLayer(String hostname,
                         int port)
                  throws CommLayerException
Constructs a new instance of the communications layer. This constructor generates a HostId and a Key Pair for the instance on the fly.

Parameters:
hostname - Hostname for this host's HostId
port - Listening port for this comm layer
Throws:
CommLayerException - If a URL cannot be formed using the provided hostname and port or if the intended KeyPair Generator Algorithm is not found.
See Also:
keyPairGeneratorAlgorithm

AbstractCommLayer

protected AbstractCommLayer()
Creates new CommLayer

Method Detail

connect

public Socket connect(String hostname,
                      int port)
               throws CommLayerException
connects with the peer.

Parameters:
hostname - the peer hostname
port - the peer's listening port number
Returns:
an open connection to the peer
Throws:
CommLayerException - if any communication error occurs.

connect

public Socket connect(URL url)
               throws CommLayerException
connects with the peer.

Parameters:
url - the url of the peer.
Returns:
an open connection to the peer
Throws:
CommLayerException - if any communication error occurs.

getLogger

public Logger getLogger()
Gets the logger for this comm layer.

Returns:
The logger

isOnline

public boolean isOnline()
Checks if this commlayer is online (i.e., running and listening).

Returns:
true if it is running, false otherwise

recvMessage

public Message recvMessage(Socket s,
                           HostId peer,
                           ClassLoader loader)
                    throws CommLayerException
Reads a message coming from the given socket and verifies its authenticity using the public key from peer.

Parameters:
peer - HostId of the peer.
s - the socket through which the message comes
loader - the class loader used to load remote classes
Returns:
the received and verified Message .
Throws:
CommLayerException - if any communication error occurs.

recvMessage

public Message recvMessage(Socket s,
                           HostId peer)
                    throws CommLayerException
Receives a message using the Root class loader. Equivalent of calling recvMessage(s, peer, null)

Parameters:
s - the socket through which the message comes
peer - HostId of the peer.
Returns:
the received and verified Message
Throws:
CommLayerException - if any communication error occurs.

sendMessage

public void sendMessage(Socket s,
                        Message m)
                 throws CommLayerException
Signs and send a message through the given socket. The message is signed using this host's private key.

Parameters:
s - socket connecting the two hosts.
m - Message to be sent.
Throws:
CommLayerException - if any communication error occurs.

setLogger

public void setLogger(Logger logger)
Sets the logger for this instance

Parameters:
logger - The new logger