peerbase
Class PeerConnection

java.lang.Object
  extended by peerbase.PeerConnection

public class PeerConnection
extends java.lang.Object

Encapsulates a socket connection to a peer, providing simple, reliable send and receive functionality. All data sent to a peer through this class must be formatted as a PeerMessage object.

Author:
Nadeem Abdul Hamid

Constructor Summary
PeerConnection(PeerInfo info)
          Opens a new connection to the specified peer.
PeerConnection(PeerInfo info, SocketInterface socket)
          Constructs a connection for which a socket has already been opened.
 
Method Summary
 void close()
          Closes the peer connection.
 PeerInfo getPeerInfo()
           
 PeerMessage recvData()
          Receives a PeerMessage from the connected peer.
 void sendData(PeerMessage msg)
          Sends a PeerMessage to the connected peer.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

PeerConnection

public PeerConnection(PeerInfo info)
               throws java.io.IOException,
                      java.net.UnknownHostException
Opens a new connection to the specified peer.

Parameters:
info - the peer node to connect to
Throws:
java.io.IOException - if an I/O error occurs
java.net.UnknownHostException

PeerConnection

public PeerConnection(PeerInfo info,
                      SocketInterface socket)
Constructs a connection for which a socket has already been opened.

Parameters:
info -
socket -
Method Detail

sendData

public void sendData(PeerMessage msg)
Sends a PeerMessage to the connected peer.

Parameters:
msg - the message object to send

recvData

public PeerMessage recvData()
Receives a PeerMessage from the connected peer.

Returns:
the message object received, or null if error

close

public void close()
Closes the peer connection.


getPeerInfo

public PeerInfo getPeerInfo()

toString

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