net.zortrium.p2proto
Class MsgResponder

java.lang.Object
  extended by net.zortrium.p2proto.MsgResponder

public class MsgResponder
extends Object

Class to facilitate sending a response message to a host who previously sent the local host a query message. The response message will be automatically handled as a response to the query message once it reaches the other host. Note that only one message can be sent as a response per request message. Additionally, responses may only be sent if the sender requested a response when sending the message.

Author:
Sean Barker - www.zortrium.net

Method Summary
 InetAddress getPeerAddress()
          Get the address of the peer from which the request message was received.
 boolean responseRequested()
          Get whether a response was requested by the sender.
 void sendError(String error)
          Convenience method that simply calls sendError(String, Msg) with no content message.
 void sendError(String error, Msg msg)
          Send an error message to the other host that will raise an exception.
 void sendResponse(Msg msg)
          Send a message to the other host that will be associated as a response to the query message.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getPeerAddress

public InetAddress getPeerAddress()
Get the address of the peer from which the request message was received.

Returns:
The address of the remote peer.

responseRequested

public boolean responseRequested()
Get whether a response was requested by the sender. If no response was requested, sending a response or error message will throw an exception.

Returns:
True if a response was requested.

sendResponse

public void sendResponse(Msg msg)
                  throws IOException
Send a message to the other host that will be associated as a response to the query message. No response message is required, but a maximum of one response message may be sent. A second call to this method, or a call after an error has already been sent in response, will throw an IllegalStateException. If no response was requested (as given by responseRequested()), calling this method will also throw an IllegalStateException.

Parameters:
msg - The response message to send.
Throws:
IOException - If an error occurs while sending the message.
IllegalStateException - If a response or error has already been sent.

sendError

public void sendError(String error,
                      Msg msg)
               throws IOException
Send an error message to the other host that will raise an exception. You must provide a textual error message, and you may optionally provide additional data in the form of a regular message. A second call to this method, or a call after a response has already been sent, will throw an IllegalStateException.

Parameters:
error - A textual description of the error.
msg - A message to be sent with the error (may be null).
Throws:
IOException - If a send error occurs.
IllegalStateException - If a response or error has already been sent.

sendError

public void sendError(String error)
               throws IOException
Convenience method that simply calls sendError(String, Msg) with no content message.

Parameters:
error - A textual description of the error.
Throws:
IOException - If a send error occurs.
IllegalStateException - If a response or error has already been sent.