com.monica.javacom.transport
Class MJCTCPTransport

java.lang.Object
  |
  +--com.monica.javacom.transport.MJCTCPTransport
All Implemented Interfaces:
MJCConfigureable, MJCReadable, MJCTransport, MJCWriteable, java.lang.Runnable

public class MJCTCPTransport
extends java.lang.Object
implements MJCTransport

This class provides TCP based connection over IP as a MJCTransport. MJCTCPTransport provides both server and client services. This class implements buffering in both directions.

Configureable values:


Constructor Summary
MJCTCPTransport()
          Normal constructor.
MJCTCPTransport(java.net.Socket client)
          Create a new thread for a new server.
 
Method Summary
 java.lang.String checkSum(java.lang.String s)
          Generates a checksum for a String.
 void closeConnections()
          Close all active connections
 void flush()
          Flushes the contents of the write buffer to bottom layers.
 int getAvailable()
          Returns the number of bytes that are available for read out from this MJCReadable.
 MJCConfiguration getConfiguration()
          Returns the MJCConfiguration with which this object is customized.
 java.lang.String getInput()
          Get back the data arrived from the socket and read out by listenForRead.
 java.lang.String getLastWriteableError()
          Returns the error message of the last occured error.
 java.lang.String getReadableName()
          Returns the MJC name of the MJCReadable.
 java.lang.String getReadableVersion()
          Returns the MJC version of the MJCReadable.
 MJCReader getReader()
          Returns the MJCReader to which readEvents will be sent.
 MJCServerManager getServerManager()
          Returns the MJCServerManager which will be notified if a client connects.
 java.lang.String getTransportName()
          Returns the MJC name of the MJCTransport.
 java.lang.String getTransportVersion()
          Returns the MJC version of the MJCTransport.
 java.lang.String getWriteableName()
          Returns the MJC name of the MJCWriteable.
 java.lang.String getWriteableVersion()
          Returns the MJC version of the MJCWriteable.
 void initAsClient(java.lang.String host, int port)
          Initializes the MJCTransport as the client side.
 void initAsServer(int port)
          Initializes the MJCTransport as the server side.
 boolean isReadableConnected()
          Returns wether the MJCReadable is still connected to its source.
 boolean isWriteableConnected()
          Returns wether the MJCWriteable is connected to its target.
 void run()
          Runnable run method.
 void setConfiguration(MJCConfiguration configuration)
          Sets the MJCConfiguration for this object.
 void setReader(MJCReader reader)
          Sets the MJCReader to which the readEvents have to be sent.
 void setServerManager(MJCServerManager serverManager)
          Sets the MJCServerManager which will be notified if a client connects.
 boolean write(char c)
          Writes one single char.
 boolean write(char[] c)
          Writes a char array.
 boolean write(java.lang.String s)
          Writes a String.
 void writeOut(java.lang.String s)
          Writes out a String directly to the TCP socket without buffering.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MJCTCPTransport

public MJCTCPTransport()
Normal constructor. Should be called upon creation.

MJCTCPTransport

public MJCTCPTransport(java.net.Socket client)
                throws java.io.IOException
Create a new thread for a new server.
Parameters:
Socket - client: the client of this server thread
Method Detail

initAsServer

public void initAsServer(int port)
                  throws java.io.IOException
Initializes the MJCTransport as the server side. Clients can connect to the specified port. To every client a new server thread will be started.
Specified by:
initAsServer in interface MJCTransport
Parameters:
int - port: the port where the clients can connect
Throws:
IOException: - if the server could not be started

initAsClient

public void initAsClient(java.lang.String host,
                         int port)
                  throws java.io.IOException
Initializes the MJCTransport as the client side. This client will try to connect to the specified host and port. The client reads from the server on new thread.
Specified by:
initAsClient in interface MJCTransport
Parameters:
String - host: the destination host
int - port: the destination port
Throws:
IOException: - if the client cannot connect to the specified host and port

run

public void run()
Runnable run method. Entry point for the read thread.
Specified by:
run in interface java.lang.Runnable

flush

public void flush()
Flushes the contents of the write buffer to bottom layers.
Specified by:
flush in interface MJCWriteable

writeOut

public void writeOut(java.lang.String s)
Writes out a String directly to the TCP socket without buffering.
Parameters:
String - s: the String to be written out

getInput

public java.lang.String getInput()
Get back the data arrived from the socket and read out by listenForRead.
Specified by:
getInput in interface MJCReadable
Returns:
String s: the contents of the read buffer

closeConnections

public void closeConnections()
Close all active connections
Specified by:
closeConnections in interface MJCTransport

getTransportName

public java.lang.String getTransportName()
Returns the MJC name of the MJCTransport.
Specified by:
getTransportName in interface MJCTransport
Returns:
String: the MJC name of the MJCTransport

getTransportVersion

public java.lang.String getTransportVersion()
Returns the MJC version of the MJCTransport.
Specified by:
getTransportVersion in interface MJCTransport
Returns:
String: the MJC version of the MJCTransport

setReader

public void setReader(MJCReader reader)
Sets the MJCReader to which the readEvents have to be sent.
Specified by:
setReader in interface MJCReadable
Parameters:
MJCReader - reader: the MJCReader to which the readEvents have to be sent

getReader

public MJCReader getReader()
Returns the MJCReader to which readEvents will be sent.
Specified by:
getReader in interface MJCReadable
Returns:
MJCReader: the MJCReader to which readEvents will be sent

setServerManager

public void setServerManager(MJCServerManager serverManager)
Sets the MJCServerManager which will be notified if a client connects.
Specified by:
setServerManager in interface MJCTransport
Parameters:
MJCServerManager - serverManager: the MJCServerManager that has to be notified if a client connects

getServerManager

public MJCServerManager getServerManager()
Returns the MJCServerManager which will be notified if a client connects.
Specified by:
getServerManager in interface MJCTransport
Returns:
MJCServerManager: the MJCServerManager which will be notified if a client connects

setConfiguration

public void setConfiguration(MJCConfiguration configuration)
Sets the MJCConfiguration for this object.
Specified by:
setConfiguration in interface MJCConfigureable
Parameters:
MJCConfiguration - configuration: the MJCConfiguration object for the customization

getConfiguration

public MJCConfiguration getConfiguration()
Returns the MJCConfiguration with which this object is customized.
Specified by:
getConfiguration in interface MJCConfigureable
Returns:
MJCConfiguration: the MJCConfiguration with which this object is customized

getWriteableName

public java.lang.String getWriteableName()
Returns the MJC name of the MJCWriteable.
Specified by:
getWriteableName in interface MJCWriteable
Returns:
String: the MJC name of the MJCWriteable

getWriteableVersion

public java.lang.String getWriteableVersion()
Returns the MJC version of the MJCWriteable.
Specified by:
getWriteableVersion in interface MJCWriteable
Returns:
String: the MJC version of the MJCWriteable

write

public boolean write(char c)
Writes one single char. Writing is synchronous and can be buffered.
Specified by:
write in interface MJCWriteable
Parameters:
char - c: the char to be written
Returns:
boolean: wether the writing has been successful

write

public boolean write(java.lang.String s)
Writes a String. Writing is synchronous and can be buffered.
Specified by:
write in interface MJCWriteable
Parameters:
String - s: the String to be written
Returns:
boolean: wether the writing has been successful

write

public boolean write(char[] c)
Writes a char array. Writing is synchronous and can be buffered.
Specified by:
write in interface MJCWriteable
Parameters:
char[] - c: the char array to be written
Returns:
boolean: wether the writing has been successful

getLastWriteableError

public java.lang.String getLastWriteableError()
Returns the error message of the last occured error. After returning it, the error message will be deleted and cannot be queried again.
Specified by:
getLastWriteableError in interface MJCWriteable
Returns:
String: the last error message

isWriteableConnected

public boolean isWriteableConnected()
Returns wether the MJCWriteable is connected to its target.
Specified by:
isWriteableConnected in interface MJCWriteable
Returns:
boolean: wether the MJCWriteable is connected to its target

getReadableName

public java.lang.String getReadableName()
Returns the MJC name of the MJCReadable.
Specified by:
getReadableName in interface MJCReadable
Returns:
String: the MJC name of the MJCReadable

getReadableVersion

public java.lang.String getReadableVersion()
Returns the MJC version of the MJCReadable.
Specified by:
getReadableVersion in interface MJCReadable
Returns:
String: the MJC version of the MJCReadable

getAvailable

public int getAvailable()
Returns the number of bytes that are available for read out from this MJCReadable.
Specified by:
getAvailable in interface MJCReadable
Returns:
int: the number of bytes available for read out

isReadableConnected

public boolean isReadableConnected()
Returns wether the MJCReadable is still connected to its source.
Specified by:
isReadableConnected in interface MJCReadable
Returns:
boolean: wether the MJCReadable is still connected to its source

checkSum

public java.lang.String checkSum(java.lang.String s)
Generates a checksum for a String.
Specified by:
checkSum in interface MJCTransport
Parameters:
String: - the String for which a checksum is needed
Returns:
String: the checksum