All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class AcmeNet.Assn2.Connection

AcmeNet.Assn2.Connection

public class Connection
One end of a network connection. Think of a connection as being like one end of a telephone call. It is paired with another Connection object in another process somewhere. Anything put into this Connection will pop out on the other end, and anything put into the other end will pop out here.

A connection exposes two unidirectional I/O streams, one outbound to the other end, and the other incoming. Each stream can carry an arbitrary stream of data.


Variable Index

 o csni
The network interface used to send and receive packets on this connection.
 o inStream
An input stream that can be given to applications that want to get data that arrives on this connection.
 o MaxPacketLength
The maximum packet size that can be sent without triggering an exception in a lower-level network interface.
 o outStream
An output stream that can be given to applications that want to send data on this connection.

Constructor Index

 o Connection()
For use only by subclasses that override all methods.
 o Connection(NetAddress, ClientServerNI)
Create a connection.

Method Index

 o ()
Set up a daemon thread to watch for broken connections.
 o connect(ServiceAddress, ClientServerNI)
Create a Connection that is connected to a specified remote service.
 o doPings()
Send Ping packets to all peer processes, and check whether the Pings we sent last time have elicited Pong in response.
 o find(int)
Look up a connection, given its connection ID number.
 o getInputStream()
Get an InputStream that can be used to get data arriving on this Connection.
 o getOutPacketStream()
Get an output stream that can be used for building a packet to send on this connection.
 o getOutputStream()
Get an OutputStream that can be used to send data on this Connection.
 o getPacket()
Get the next data packet that has arrived on this Connection.
 o getPeerAddress()
Get the address of the other end of this connection.
 o handleAcceptPacket(PacketInputStream)
Do the processing required to handle an Accept packet that arrived.
 o handleConnectPacket(PacketInputStream, ClientServerNI)
Do the processing required to handle a Connect packet that arrived.
 o handleDataPacket(PacketInputStream)
Do the processing required to handle a Data packet that arrived.
 o handleEodPacket(PacketInputStream)
Do the processing required to handle an EndOfData packet that arrived.
 o handlePingPacket(PacketInputStream)
Do the processing required to handle a Ping packet that arrived.
 o handlePongPacket()
Do the processing required to handle a Pong packet that arrived.
 o sendEod()
Send an EndOfData on this connection; this is like closing the outgoing side of the connection.

Variables

 o MaxPacketLength
 static final int MaxPacketLength
The maximum packet size that can be sent without triggering an exception in a lower-level network interface.

 o csni
 protected ClientServerNI csni
The network interface used to send and receive packets on this connection.

 o inStream
 protected InputStream inStream
An input stream that can be given to applications that want to get data that arrives on this connection.

 o outStream
 protected OutputStream outStream
An output stream that can be given to applications that want to send data on this connection.

Constructors

 o Connection
 protected Connection()
For use only by subclasses that override all methods.

 o Connection
 protected Connection(NetAddress peerNetAddr,
                      ClientServerNI ni)
Create a connection. The resulting Connection will not (yet) be connected to another process. It is the responsibility of the caller to connect this object to a remote process.

Parameters:
peerNetAddr - the address of the process that will eventually be at the other end of the connection
ni - the local network interface to use for sending and receiving packets

Methods

 o connect
 static Connection connect(ServiceAddress sa,
                           ClientServerNI ni) throws IOException
Create a Connection that is connected to a specified remote service.

Parameters:
sa - the address of the service to connect to
ni - the local network interface to use for sending and receiving packets
Returns:
the connection that was created
Throws: IOException
some I/O error occurred
 o handleConnectPacket
 static Connection handleConnectPacket(PacketInputStream pis,
                                       ClientServerNI ni) throws IOException
Do the processing required to handle a Connect packet that arrived.

Parameters:
pis - the arrived packet
ni - the network interface it arrived on
Throws: IOException
some I/O error occurred
 o find
 static synchronized Connection find(int num)
Look up a connection, given its connection ID number.

Parameters:
num - the connection number to look up
Returns:
the connection with local ID number num, or null if there is no connection with that number
 o handleAcceptPacket
 synchronized void handleAcceptPacket(PacketInputStream pis) throws IOException
Do the processing required to handle an Accept packet that arrived.

Parameters:
pis - the arrived packet
Throws: IOException
some I/O error occurred
 o handleDataPacket
 synchronized void handleDataPacket(PacketInputStream pis)
Do the processing required to handle a Data packet that arrived.

Parameters:
pis - the arrived packet
Throws: IOException
some I/O error occurred
 o handleEodPacket
 synchronized void handleEodPacket(PacketInputStream pis)
Do the processing required to handle an EndOfData packet that arrived.

Parameters:
pis - the arrived packet
 o getPacket
 synchronized PacketInputStream getPacket()
Get the next data packet that has arrived on this Connection. If no unread packets have arrived, the calling thread is blocked until a packet shows up.

Returns:
the next arrived data packet
 o getOutPacketStream
 synchronized DataOutputStream getOutPacketStream() throws IOException
Get an output stream that can be used for building a packet to send on this connection. The packet will be sent automatically when the resulting stream is flushed.

Returns:
an output stream that can be used for building a new packet
Throws: IOException
some I/O error occurred
 o sendEod
 synchronized void sendEod() throws IOException
Send an EndOfData on this connection; this is like closing the outgoing side of the connection. Once EndOfData has been sent, no further data can be sent on this connection. The EndOfData marker will be put into the stream. The EndOfData marker will be presented as an end-of-input-stream to the process on the other end of the connection.

Throws: IOException
some I/O error occurred
 o getInputStream
 public InputStream getInputStream()
Get an InputStream that can be used to get data arriving on this Connection.

 o getOutputStream
 public OutputStream getOutputStream()
Get an OutputStream that can be used to send data on this Connection. Any data written to the resulting stream will be sent through this Connection and will show up on the InputStream on the other end.

The resulting OutputStream may buffer data, so you must call flush() if you want to be sure your data has been sent.

Returns:
an OutputStream that can be used to send data through this Connection
 o getPeerAddress
 public NetAddress getPeerAddress()
Get the address of the other end of this connection.

Returns:
the address of the other end of this connection
 o doPings
 static void doPings()
Send Ping packets to all peer processes, and check whether the Pings we sent last time have elicited Pong in response.

 o handlePingPacket
 synchronized void handlePingPacket(PacketInputStream pis)
Do the processing required to handle a Ping packet that arrived.

Parameters:
pis - the Ping packet
 o handlePongPacket
 synchronized void handlePongPacket()
Do the processing required to handle a Pong packet that arrived.

 o
 static void ()
Set up a daemon thread to watch for broken connections.


All Packages  Class Hierarchy  This Package  Previous  Next  Index