CarobNS::JavaSocket Class Reference

Generic socket wrapper class to exchange data with Java world. More...

#include <JavaSocket.hpp>

Inheritance diagram for CarobNS::JavaSocket:

CarobNS::BufferedSocket CarobNS::DriverSocket List of all members.

Public Member Functions

bool isValid () const
 Gives socket validity status.
bool isConnected () const
 Gives connection status.
void create (int domain) throw (ConnectionException, UnexpectedException)
 Creates the socket.
bool connectTo (const SocketAddress &addr) throw (ConnectionException, UnexpectedException)
 Connects to the given socket address.
virtual bool closeSystemSocket () throw (SocketIOException, UnexpectedException)
 Closes TCP socket.
size_t writeJavaUTF (const std::wstring &str) const throw (SocketIOException, CodecException, UnexpectedException)
 Writes a UTF-8 encoded wstring to the socket.
size_t readJavaUTF (std::wstring &str) const throw (SocketIOException, CodecException, UnexpectedException)
 Reads UTF-8 encoded string from socket.
void writeJavaInt (int32_t i) const throw (SocketIOException, UnexpectedException)
 Writes 32bits-integer to socket.
void readJavaInt (int32_t &i) const throw (SocketIOException, UnexpectedException)
 Reads 32bits-integer from socket.
void writeJavaLong (int64_t l) const throw (SocketIOException, UnexpectedException)
 Writes 64bits-long to socket.
void readJavaLong (int64_t &l) const throw (SocketIOException, UnexpectedException)
 Reads 64bits-long from socket.
void writeJavaBool (bool b) const throw (SocketIOException, UnexpectedException)
 Writes a boolean (as a byte) to the socket.
void readJavaBool (bool &b) const throw (SocketIOException, UnexpectedException)
 Reads a boolean (byte) from the socket.
void readJavaBytes (int32_t length, java_byte *data) const throw (SocketIOException, UnexpectedException)
 Reads an array of bytes from the socket.
void writeJavaBytes (int32_t length, java_byte *data) const throw (SocketIOException, UnexpectedException)
 Writes an array of bytes to the socket.
void shutdown ()
 Interrupts read operations on this socket, mark it as disconnected and makes read operations throw a SocketIOException.

Static Public Member Functions

static int pollOnSingleFd (int socketFd, int pollTimeoutInMs, bool pollOnWrites=false)
 Execute poll (or select depending on compilation flag CAROB_USE_SELECT) on the given file descriptor and return after either data is ready to be read or given timeout is elapsed.

Protected Member Functions

 JavaSocket () throw (CodecException)
 Default constructor - No connection here, just creates and empty socket.
virtual ~JavaSocket ()
 Destructor for disconnection - closes the socket.
virtual int recvFully (void *buf, const int len, const int flags) const throw (SocketIOException, UnexpectedException)
 Substitute for recv.
virtual void sendToSocket (const wchar_t fctName[], const void *buf, int len, int flags) const throw (SocketIOException, UnexpectedException)
 Wrapper over send(.
void receiveFromSocket (const wchar_t fctName[], void *buf, int len, int flags) const throw (SocketIOException, UnexpectedException)
 Wrapper around recv(.

Protected Attributes

int socket_fd
 Socket file descriptor.
bool connected
 true if the socket is connected to a host
bool canceled
 true cancels all input and connections attempts

Detailed Description

Generic socket wrapper class to exchange data with Java world.

This class can read and write basic Java types over a socket. It does *not* follow any protocol but java pure serialization.

See also:
DriverSocket class for sequoia protocol.


Constructor & Destructor Documentation

virtual CarobNS::JavaSocket::~JavaSocket (  )  [protected, virtual]

Destructor for disconnection - closes the socket.

Exceptions:
SocketIOException 


Member Function Documentation

virtual bool CarobNS::JavaSocket::closeSystemSocket (  )  throw (SocketIOException, UnexpectedException) [virtual]

Closes TCP socket.

Why public?

Returns:
true if the socket was successfully closed
Exceptions:
SocketIOException 

bool CarobNS::JavaSocket::connectTo ( const SocketAddress addr  )  throw (ConnectionException, UnexpectedException)

Connects to the given socket address.

Parameters:
addr socket address of the host to connect to
Returns:
true upon successfull connection, false otherwise
Exceptions:
ConnectionException 

void CarobNS::JavaSocket::create ( int  domain  )  throw (ConnectionException, UnexpectedException)

Creates the socket.

Parameters:
domain communication domain (supported = PF_INET or PF_INET6)
Exceptions:
ConnectionException if the socket could not be create or its options could not be set

bool CarobNS::JavaSocket::isConnected (  )  const [inline]

Gives connection status.

Returns:
true if the socket is connected to a host

bool CarobNS::JavaSocket::isValid (  )  const [inline]

Gives socket validity status.

Returns:
true if the socket is valid (has been created) so it can be connected

static int CarobNS::JavaSocket::pollOnSingleFd ( int  socketFd,
int  pollTimeoutInMs,
bool  pollOnWrites = false 
) [static]

Execute poll (or select depending on compilation flag CAROB_USE_SELECT) on the given file descriptor and return after either data is ready to be read or given timeout is elapsed.

Parameters:
socketFd socket file descriptor on which to wait for incomming data
pollTimeoutInMs timeout after which to return if no data is ready to be read on socket
pollOnWrites true makes poll on write (false by default = on read)
Returns:
>0 if data is ready to be read, 0 if timeout reached, -1 if an error occured

void CarobNS::JavaSocket::readJavaBool ( bool &  b  )  const throw (SocketIOException, UnexpectedException)

Reads a boolean (byte) from the socket.

Parameters:
b resulting boolean to read from stream
Exceptions:
SocketIOException 

void CarobNS::JavaSocket::readJavaBytes ( int32_t  length,
java_byte *  data 
) const throw (SocketIOException, UnexpectedException)

Reads an array of bytes from the socket.

The array must be allocated !

Parameters:
length number of bytes to read
data allocated array of (at least) length bytes
Exceptions:
SocketIOException 

void CarobNS::JavaSocket::readJavaInt ( int32_t &  i  )  const throw (SocketIOException, UnexpectedException)

Reads 32bits-integer from socket.

Parameters:
i resulting integer read from stream
Exceptions:
SocketIOException 

void CarobNS::JavaSocket::readJavaLong ( int64_t &  l  )  const throw (SocketIOException, UnexpectedException)

Reads 64bits-long from socket.

Parameters:
l resulting long read from stream
Exceptions:
SocketIOException 

size_t CarobNS::JavaSocket::readJavaUTF ( std::wstring &  str  )  const throw (SocketIOException, CodecException, UnexpectedException)

Reads UTF-8 encoded string from socket.

This is NOT Java's modified UTF-8, see SEQUOIA-133.

Parameters:
str string to read
Returns:
the length read in bytes
Exceptions:
SocketIOException 

void CarobNS::JavaSocket::receiveFromSocket ( const wchar_t  fctName[],
void *  buf,
int  len,
int  flags 
) const throw (SocketIOException, UnexpectedException) [protected]

Wrapper around recv(.

..) function to handle errors and throw exceptions

Parameters:
fctName name of the calling function (for logging purposes)
buf buffer in which to put the received data in
len length of buf
flags send option, see send man page
Exceptions:
SocketIOException in case of error

virtual int CarobNS::JavaSocket::recvFully ( void *  buf,
const int  len,
const int  flags 
) const throw (SocketIOException, UnexpectedException) [protected, virtual]

Substitute for recv.

Waits for incomming data by calling pollOnSingleFd and loops until full length has been received or an erro occured. If shutdown() is called during the loop, throws a SocketIOException to inform callers that the socket is not longer readable

Parameters:
buf data to send
len full buffer length
flags send options, see recv man page
Returns:
the total number of bytes send, -1 in case of failure
Exceptions:
SocketIOException if interrupted by shutdown() function

Reimplemented in CarobNS::BufferedSocket.

virtual void CarobNS::JavaSocket::sendToSocket ( const wchar_t  fctName[],
const void *  buf,
int  len,
int  flags 
) const throw (SocketIOException, UnexpectedException) [protected, virtual]

Wrapper over send(.

..) function to handle errors and throw exceptions

Parameters:
fctName name of the calling function (for logging purposes)
buf data to be send
len length of buf
flags send option, see recv man page
Exceptions:
SocketIOException 

Reimplemented in CarobNS::BufferedSocket.

void CarobNS::JavaSocket::shutdown (  )  [inline]

Interrupts read operations on this socket, mark it as disconnected and makes read operations throw a SocketIOException.

See also:
recvFully(void *buf, const int len, const int flags) const

void CarobNS::JavaSocket::writeJavaBool ( bool  b  )  const throw (SocketIOException, UnexpectedException)

Writes a boolean (as a byte) to the socket.

Parameters:
b the boolean to send
Exceptions:
SocketIOException 

void CarobNS::JavaSocket::writeJavaBytes ( int32_t  length,
java_byte *  data 
) const throw (SocketIOException, UnexpectedException)

Writes an array of bytes to the socket.

Parameters:
length number of bytes to write
data array of (at least) length bytes
Exceptions:
SocketIOException 

void CarobNS::JavaSocket::writeJavaInt ( int32_t  i  )  const throw (SocketIOException, UnexpectedException)

Writes 32bits-integer to socket.

Parameters:
i integer to send
Exceptions:
SocketIOException 

void CarobNS::JavaSocket::writeJavaLong ( int64_t  l  )  const throw (SocketIOException, UnexpectedException)

Writes 64bits-long to socket.

Parameters:
l long to send
Exceptions:
SocketIOException 

size_t CarobNS::JavaSocket::writeJavaUTF ( const std::wstring &  str  )  const throw (SocketIOException, CodecException, UnexpectedException)

Writes a UTF-8 encoded wstring to the socket.

This is NOT Java's modified UTF-8, see SEQUOIA-133.

Parameters:
str string to write
Returns:
the number of bytes send
Exceptions:
SocketIOException 


The documentation for this class was generated from the following file:
Generated on Fri Mar 9 17:40:20 2007 for Carob by  doxygen 1.4.7