|
||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||
java.lang.Objectnet.zortrium.p2proto.Connection
public final class Connection
A connection to send messages to a remote peer. In general, the peer should
be listening on the specified port via a PeerListener. Unsolicited
messages may be sent over the connection (at which point they will be handled
by the remote PeerListener handler), and responses to these messages
are automatically retrieved and returned, independent of issues like network
delays and the order of message arrival.
Messages may optionally request that a response is returned from the server,
in which case the connection will wait for a response until a default or
specified timeout duration as passed. In general, the simplest way to do this
is to design the application such that a given message type either always
returns a response or never does. If a response times out (raising a
PeerErrorException) but the missed response is subsequently received,
it is simply discarded.
Messages may be sent both synchronously and asynchronously and may include timeouts and custom error handlers. Connections are thread-safe by default. This class also contains caching methods to facilitate reuse of Connection objects. However, use of the caching methods are optional and users are free to manage their connections manually if desired.
Msg,
PeerListener| Constructor Summary | |
|---|---|
Connection(InetAddress remoteAddress,
int port)
Set up the new socket connection to the given host. |
|
Connection(String remoteAddress,
int port)
Convenience constructor that calls InetAddress.getByName(String)
and passes it to Connection(InetAddress, int). |
|
| Method Summary | |
|---|---|
void |
close()
Close the connection and all internal streams. |
boolean |
equals(Object obj)
Two connections are equal if they are connected to the same address on the same port. |
InetAddress |
getAddress()
Get the remote address of this connection. |
static Connection |
getCached(InetAddress remoteAddress,
int port)
Get a cached connection to the given address and port. |
static Connection |
getCached(String remoteAddress,
int port)
Performs a hostname lookup and calls getCached(InetAddress, int). |
long |
getDefaultTimeout()
Get the default timeout (in ms) for receiving response messages. |
int |
getPort()
Get the remote port of this connection. |
int |
hashCode()
|
boolean |
isClosed()
Check whether the connection is still active. |
void |
send(Msg msg)
Send a message and do not wait for a response message. |
void |
sendAsync(Msg msg)
Send a message asynchronously and disregard any responses received. |
void |
sendAsync(Msg msg,
MsgCallback callback)
Send a message asynchronously and disregard any responses received. |
Msg |
sendRequest(Msg msg)
Send a message and block until either a response message is received or the default timeout has passed. |
Msg |
sendRequest(Msg msg,
long timeout)
Send a message and block until either a response message is received or the specified timeout (in ms) has passed. |
void |
sendRequestAsync(Msg msg,
MsgCallback callback)
Send a message asynchronously and call the given handler when a response is received, the default timeout has elapsed, or a send error occurs. |
void |
sendRequestAsync(Msg msg,
MsgCallback callback,
long timeout)
Send a message asynchronously and call the given handler when a response is received, the specified timeout (in ms) has passed, or a send error occurs. |
void |
setDefaultTimeout(long timeout)
Set the default timeout (in ms) for receiving response messages, used by sendRequest(Msg) and sendRequestAsync(Msg, MsgCallback) . |
void |
setThreadSafety(boolean safety)
Set whether the connection performs internal synchronization on the outgoing message queue. |
String |
toString()
|
| Methods inherited from class java.lang.Object |
|---|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait |
| Constructor Detail |
|---|
public Connection(InetAddress remoteAddress,
int port)
throws IOException
remoteAddress - Remote host to connect to.port - Remote port to connect to.
IOException - If the connection failed.
public Connection(String remoteAddress,
int port)
throws IOException
InetAddress.getByName(String)
and passes it to Connection(InetAddress, int).
remoteAddress - Textual IP address or hostname to connect to.port - Remote port to connect to.
IOException - If the host lookup or connection failed.| Method Detail |
|---|
public static Connection getCached(InetAddress remoteAddress,
int port)
throws IOException
This method is thread-safe; however, concurrent calls for the same address and port are NOT guaranteed to return the same Connection. If the specified connection is not cached, simultaneous calls may create more than one new connection (however, only one such connection will end up in the cache).
remoteAddress - The remote address to connect to.port - The remote port to connect to.
IOException - If establishing a new connection fails.
public static Connection getCached(String remoteAddress,
int port)
throws IOException
getCached(InetAddress, int).
IOExceptionpublic void setThreadSafety(boolean safety)
safety - True to (re)enable thread safety and false to disable.public void setDefaultTimeout(long timeout)
sendRequest(Msg) and sendRequestAsync(Msg, MsgCallback) .
The default setting is 5 seconds (5000 ms).
timeout - The new default timeout (in ms).public long getDefaultTimeout()
public void close()
public boolean isClosed()
public InetAddress getAddress()
public int getPort()
public void sendAsync(Msg msg)
sendAsync(Msg, MsgCallback).
msg - The message to send.
public void sendAsync(Msg msg,
MsgCallback callback)
MsgCallback.handleException(IOException).
msg - The message to send.callback - The callback handling send errors.
public void sendRequestAsync(Msg msg,
MsgCallback callback)
msg - The message to send.callback - The handler to call when a response is received or the response
times out.
public void sendRequestAsync(Msg msg,
MsgCallback callback,
long timeout)
msg - The message to send.callback - The handler to call when a response is received or the response
times out.timeout - The duration to wait for a response (in ms).
public void send(Msg msg)
throws IOException
msg - The message to send.
IOException - If a send error occurs.
public Msg sendRequest(Msg msg)
throws PeerTimeoutException,
PeerErrorException,
IOException
msg - The request message to send.
PeerTimeoutException - If the response times out.
PeerErrorException - If an error response is received.
IOException - If a send error occurs.
public Msg sendRequest(Msg msg,
long timeout)
throws PeerTimeoutException,
PeerErrorException,
IOException
msg - The request message to send.timeout - The response timeout (in ms).
PeerTimeoutException - If a response is not received in time.
PeerErrorException - If an error response is received.
IOException - If a send error occurs.public boolean equals(Object obj)
equals in class Objectpublic int hashCode()
hashCode in class Objectpublic String toString()
toString in class Object
|
||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||