net.grinder.communication
Class ClientSender

java.lang.Object
  extended by net.grinder.communication.StreamSender
      extended by net.grinder.communication.ClientSender
All Implemented Interfaces:
BlockingSender, Sender

public final class ClientSender
extends StreamSender
implements BlockingSender

Class that manages the sending of messages to a server.

Author:
Philip Aston

Nested Class Summary
 
Nested classes/interfaces inherited from interface net.grinder.communication.BlockingSender
BlockingSender.NoResponseException
 
Method Summary
 Message blockingSend(Message message)
          Send the given message and await a response.
static ClientSender connect(ClientReceiver clientReceiver)
          Factory method that makes a Sender around the existing TCP connection owned by the supplied ClientReceiver.
static ClientSender connect(Connector connector, Address address)
          Factory method that makes a TCP connection and returns a corresponding Sender.
 boolean isShutdown()
          Return whether we are shutdown.
 void send(Message message)
          Send the given message.
 void sendKeepAlive()
          Send a no-op message that keeps this connection alive.
 void shutdown()
          Cleanly shutdown the Sender.
protected static void writeMessageToStream(Message message, OutputStream stream)
           
 
Methods inherited from class net.grinder.communication.StreamSender
writeMessage
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

connect

public static ClientSender connect(Connector connector,
                                   Address address)
                            throws CommunicationException
Factory method that makes a TCP connection and returns a corresponding Sender.

This class only supports synchronous communication, so the server can only use address for identification purposes. For asynchronous communication, see ClientReceiver.

Parameters:
connector - Connector to use to make the connection to the server.
address - Identifies the client process.
Returns:
The ClientSender.
Throws:
CommunicationException - If failed to connect.

connect

public static ClientSender connect(ClientReceiver clientReceiver)
                            throws CommunicationException
Factory method that makes a Sender around the existing TCP connection owned by the supplied ClientReceiver.

Parameters:
clientReceiver - We create a paired Sender for this Receiver.
Returns:
The ClientSender.
Throws:
CommunicationException - If failed to connect.

shutdown

public void shutdown()
Cleanly shutdown the Sender. Ignore most errors, connection has probably been reset by peer.

Specified by:
shutdown in interface BlockingSender
Specified by:
shutdown in interface Sender
Overrides:
shutdown in class StreamSender

blockingSend

public Message blockingSend(Message message)
                     throws CommunicationException
Send the given message and await a response.

The input stream is that of our socket. This method should only be used where the sender can guarantee that the input stream will be free for exclusive use - we don't lock out external processes from interrupting the stream.

Specified by:
blockingSend in interface BlockingSender
Parameters:
message - A Message.
Returns:
The response message.
Throws:
CommunicationException - If an error occurs.

sendKeepAlive

public void sendKeepAlive()
                   throws CommunicationException
Send a no-op message that keeps this connection alive.

Throws:
CommunicationException - If the connection has been terminated.

send

public final void send(Message message)
                throws CommunicationException
Send the given message.

Specified by:
send in interface Sender
Parameters:
message - A Message.
Throws:
CommunicationException - If an error occurs.

writeMessageToStream

protected static final void writeMessageToStream(Message message,
                                                 OutputStream stream)
                                          throws IOException
Throws:
IOException

isShutdown

public boolean isShutdown()
Return whether we are shutdown.

Returns:
true if and only if we are shut down.


Copyright © 2000-2013. All Rights Reserved.