org.jppf.comm.socket
Class SocketClient

java.lang.Object
  extended by org.jppf.comm.socket.AbstractSocketWrapper
      extended by org.jppf.comm.socket.SocketClient
All Implemented Interfaces:
SocketWrapper

public class SocketClient
extends AbstractSocketWrapper

This class provides a simple API to transfer objects over a TCP socket connection.

Author:
Laurent Cohen

Field Summary
 
Fields inherited from class org.jppf.comm.socket.AbstractSocketWrapper
dis, dos, host, opened, port, serializer, socket
 
Fields inherited from interface org.jppf.comm.socket.SocketWrapper
SOCKET_RECEIVE_BUFFER_SIZE
 
Constructor Summary
SocketClient()
          Default constructor is invisible to other classes.
SocketClient(Socket socket)
          Initialize this socket client with an already opened and connected socket.
SocketClient(String host, int port)
          Initialize this socket client and connect it to the specified host on the specified port.
SocketClient(String host, int port, ObjectSerializer serializer)
          Initialize this socket client and connect it to the specified host on the specified port.
 
Method Summary
 ObjectSerializer getSerializer()
          Get an object serializer / deserializer to convert an object to or from an array of bytes.
 Object receive(int timeout)
          Read an object from a TCP socket connection.
 void send(Object o)
          Send an object over a TCP socket connection.
 void setSerializer(ObjectSerializer serializer)
          Set the object serializer / deserializer to convert an object to or from an array of bytes.
 String toString()
          Generate a string representation of this socket client.
 
Methods inherited from class org.jppf.comm.socket.AbstractSocketWrapper
checkOpened, close, flush, getHost, getPort, getSocket, getSocketTimestamp, initStreams, isOpened, open, read, readInt, receive, receiveBytes, sendBytes, setHost, setPort, setSocket, skip, updateSocketTimestamp, write, writeInt
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

SocketClient

public SocketClient()
Default constructor is invisible to other classes.


SocketClient

public SocketClient(String host,
                    int port)
             throws Exception
Initialize this socket client and connect it to the specified host on the specified port.

Parameters:
host - the remote host this socket client connects to.
port - the remote port on the host this socket client connects to.
Throws:
Exception - if there is an issue with the socket streams.

SocketClient

public SocketClient(String host,
                    int port,
                    ObjectSerializer serializer)
             throws Exception
Initialize this socket client and connect it to the specified host on the specified port.

Parameters:
host - the remote host this socket client connects to.
port - the remote port on the host this socket client connects to.
serializer - the object serializer used by this socket client.
Throws:
Exception - if there is an issue with the socket streams.

SocketClient

public SocketClient(Socket socket)
             throws JPPFException
Initialize this socket client with an already opened and connected socket.

Parameters:
socket - the underlying socket this socket client wraps around.
Throws:
JPPFException - if the socket connection fails.
Method Detail

send

public void send(Object o)
          throws Exception
Send an object over a TCP socket connection.

Parameters:
o - the object to send.
Throws:
Exception - if the underlying output stream throws an exception.
See Also:
SocketWrapper.send(java.lang.Object)

receive

public Object receive(int timeout)
               throws Exception
Read an object from a TCP socket connection. This method blocks until an object is received or the specified timeout has expired, whichever happens first.

Parameters:
timeout - timeout after which the operation is aborted. A timeout of zero is interpreted as an infinite timeout.
Returns:
the object that was read from the underlying input stream or null if the operation timed out.
Throws:
Exception - if the underlying input stream throws an exception.
See Also:
SocketWrapper.receive(int)

getSerializer

public ObjectSerializer getSerializer()
Get an object serializer / deserializer to convert an object to or from an array of bytes.

Specified by:
getSerializer in interface SocketWrapper
Overrides:
getSerializer in class AbstractSocketWrapper
Returns:
an ObjectSerializer instance.
See Also:
SocketWrapper.getSerializer()

setSerializer

public void setSerializer(ObjectSerializer serializer)
Set the object serializer / deserializer to convert an object to or from an array of bytes.

Specified by:
setSerializer in interface SocketWrapper
Overrides:
setSerializer in class AbstractSocketWrapper
Parameters:
serializer - an ObjectSerializer instance.
See Also:
SocketWrapper.setSerializer(org.jppf.utils.ObjectSerializer)

toString

public String toString()
Generate a string representation of this socket client.

Overrides:
toString in class Object
Returns:
as string describing this object.
See Also:
Object.toString()


Copyright © 2005-2010 JPPF Team.