com.java4less.comm
Class CommConnector

java.lang.Object
  |
  +--com.java4less.comm.CommConnector
Direct Known Subclasses:
SerialPortConnector

public abstract class CommConnector
extends java.lang.Object

Abstract class to be used for a variety of communication connections. The basic idea is to have a very generic way to use any kind of bi-directional communication.
Generic Example:

 
 CommConnector cx = SpecificConnector( param1, param2, ... paramN ); 
 
 try {
    cx.open();
    ...
    while ( !done ) {
       ...
       cx.out.write( ... );
       ...
       cx.in.read( ... );
       ...
    }
    ...
    cx.close();
    ...
 } catch ( CommException e ) {
    this.logger.severe( "Comm. Error: " + e.getMessage() );
 } catch ( IOException e ) {
    cx.close();
    this.logger.severe( "I/O Error: " + e.getMessage() );
 }
 
The hard work will be usually done in the open() method, and parameters needed to establish a connection will be usually passed in the sub-classes contructors or with sub-class specific methods.

Exceptions

A CommException is usually thrown when the connection cannot be established in the open() method.
Input & Output Streams can throw an IOException if a problem occurs after a succesful connection.
The close() method will do its best to perform an eventual termination phase of a protocol and release any reserved resource. If a problem occurs the default behaviour is to log it but not to throw any exception.


Field Summary
 java.io.InputStream in
          Input Stream that retrieves data from the communications port.
 Logger logger
          Logger object to be used to monitor the connexion attempts.
 java.io.OutputStream out
          Output Stream that sends data to the communications port.
 
Method Summary
abstract  void close()
          Closes communication channel.
abstract  void open()
          Opens Communications channel so in & out streams can be used.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

in

public java.io.InputStream in
Input Stream that retrieves data from the communications port. It's automatically created and closed by the open() and close() methods.


out

public java.io.OutputStream out
Output Stream that sends data to the communications port. It's automatically created and closed by the open() and close() methods.


logger

public Logger logger
Logger object to be used to monitor the connexion attempts. Note that it won't log the communication itself using the in & out streams but any operations performed by the open() & close() methods.
Default null value means no logging. Therefore, to activate logging you should provide a logging object. Example:
 CommConnector cx = SpecificConnector( param1, param2, ... paramN ); 
 cx.logger = Logger.getLogger("com.java4less.comm.logger");
 cx.logger.setLevel( 7 );
 

See Also:
Logger
Method Detail

open

public abstract void open()
                   throws CommException,
                          java.io.IOException
Opens Communications channel so in & out streams can be used. Performs any initializations needed to make channel available. You will need to configure the comm paramenters, usually through the subclass constructor's parameters.

CommException
java.io.IOException

close

public abstract void close()
Closes communication channel. in & out streams are closed and any reserved resources are released.