robot3
Class LNPManager

java.lang.Object
  |
  +--robot3.LNPManager
All Implemented Interfaces:
java.lang.Runnable

public final class LNPManager
extends java.lang.Object
implements java.lang.Runnable

This class handles the whole LNP protocol. As soon as you create a new instance of this class, i.e. the first time you call the getInstance method, a new thread is created that keeps the IR tower awake. The tower sends then a 0 every 4th second. Example of use :
LNPManager manager = LNPManager.getInstance("COM1");
manager.addLNPListener(this);
manager.integrityWrite("This is a test");

History:
01/13/2001: 1st release date
Problems known :
- the manager receives its own packets
- the manager can only handle one IR tower at a time
- there is no simple way to stop the manager from keeping the IR tower alive and from reading data from the IR tower, i.e. even when your main method is finished, the JVM doesn't stop.


Nested Class Summary
private  class LNPManager.WakeupThread
          This class keeps the tower awake by sending a 0 every 4th second.
 
Field Summary
private  javax.comm.SerialPort IRTower
          The serial port to which the IRTower is connected
private  java.util.Vector listeners
          The Vector containing the listeners
private  byte lnpHostAddress
          The hostAddress, which can be changed by calling setHostAddress, 0x10 by default.
private static byte lnpHostMask
          The lnpHostMask, 0xF0 by default
private static byte lnpPortMask
          The lnpPortMask, 0x0F by default
private static int MAX_SIZE
          The maximum size of a LNP packet
private static LNPManager theInstance
          Contains the only instance of the LNPManager.
 
Constructor Summary
private LNPManager(java.lang.String comPort)
          Private constructor of the class.
 
Method Summary
 void addLNPListener(LNPListener listener)
          This methods adds the parameter to the list of the listener to call when a new message (integrity or addressing) is received.
private  void addressingRead(java.io.InputStream in)
          Called by run when a 0xF1 is received.
 void addressingWrite(byte[] data, byte sourcePort, byte destHostId, byte destPort)
          Sends a addressing packet containing the data of the parameter.
 void addressingWrite(java.lang.String data, byte sourcePort, byte destHostId, byte destPort)
          Sends a addressing packet containing the data of the parameter.
private static boolean checkChecksum(byte firstByte, byte length, byte[] data, byte checksum)
          Checks if the checksum is correct.
private static int checksum(byte[] data, int length)
          Computes the checksum.
static LNPManager getInstance(java.lang.String comPort)
          Static mlethod that return an instance of the current class.
private  void integrityRead(java.io.InputStream in)
          Called by the read loop when a 0xF0 is read from the serial port.
 void integrityWrite(byte[] data)
          Sends a integrity packet containing the data of the parameter.
 void integrityWrite(java.lang.String data)
          Sends a integrity packet containing the data of the parameter.
private  void logicalWrite(byte[] data)
          Used to send a logical packet, i.e.
private  void openPort(java.lang.String comPort)
          Opens the serial port to which the IR tower is connected.
 void removeLNPListener(LNPListener listener)
          Removes the parameter from the list of the listeners to call when a new message is received.
 void run()
          DO NOT CALL THIS METHOD DIRECTLY : it's public because it is the implementation of a interface.
private  void sendAddressingEvent(byte[] buffer)
          Sends a WrongChecksumEvent to all the listeners.
private  void sendIntegrityEvent(byte[] buffer)
          Sends a IntegrityEvent to all the listeners.
private  void sendWrongChecksumEvent()
          Sends a WrongChecksumEvent to all the listeners.
 void setHostAddress(int hostAddress)
          Sets the host address of the LNPManager.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

theInstance

private static LNPManager theInstance
Contains the only instance of the LNPManager.


lnpHostMask

private static final byte lnpHostMask
The lnpHostMask, 0xF0 by default

See Also:
Constant Field Values

lnpPortMask

private static final byte lnpPortMask
The lnpPortMask, 0x0F by default

See Also:
Constant Field Values

lnpHostAddress

private byte lnpHostAddress
The hostAddress, which can be changed by calling setHostAddress, 0x10 by default.


listeners

private java.util.Vector listeners
The Vector containing the listeners


IRTower

private javax.comm.SerialPort IRTower
The serial port to which the IRTower is connected


MAX_SIZE

private static final int MAX_SIZE
The maximum size of a LNP packet

See Also:
Constant Field Values
Constructor Detail

LNPManager

private LNPManager(java.lang.String comPort)
Private constructor of the class. The constructor is private because this class implements the singleton pattern (i.e., to get an instance, you call the static method getInstance(). The method stats a new thread that keeps the IR tower awake.

Parameters:
comPort - the name of the serial port to which the IR tower is connected.
Method Detail

getInstance

public static LNPManager getInstance(java.lang.String comPort)
Static mlethod that return an instance of the current class. If no other instance exists, creates one. The class cannot handle two IR towers at that time. If you ever try to do so, it will lead to some undefined behavior.

Parameters:
comPort - the name of the serial port to which the IR tower is connected.
Returns:
a new instance of the class if no other instance was created, or the last isntance used.

addLNPListener

public void addLNPListener(LNPListener listener)
This methods adds the parameter to the list of the listener to call when a new message (integrity or addressing) is received. At the current time, the LNPManager dispatches all the messages received, even if the listners don't have the corrct port number.

Parameters:
listener - the new object to call when a new message is received.

removeLNPListener

public void removeLNPListener(LNPListener listener)
Removes the parameter from the list of the listeners to call when a new message is received.

Parameters:
listener - the object to remove from the list of the listeners to call when a new message is received.

openPort

private void openPort(java.lang.String comPort)
Opens the serial port to which the IR tower is connected. This methods also sets some parameter to the serial port : baud rate : 2400, 8 databits, 1 stopbit, no parity.

Parameters:
comPort - the serial port to which the IR tower is connected.

run

public void run()
DO NOT CALL THIS METHOD DIRECTLY : it's public because it is the implementation of a interface. When a new LNPMAnager is created, the constructor creates a new thread, that calls this method. It starts a new wake up thread, and loops on what is received on the serial port.

Specified by:
run in interface java.lang.Runnable

integrityRead

private void integrityRead(java.io.InputStream in)
Called by the read loop when a 0xF0 is read from the serial port. Reads the packet, checks the checksum, and (if ok) sends a event to all the listeners. If the checksum is wrong, sends a wrong checksum event to the listeners.

Parameters:
in - the InputStream from which to read the data.

sendIntegrityEvent

private void sendIntegrityEvent(byte[] buffer)
Sends a IntegrityEvent to all the listeners.

Parameters:
buffer - the buffer

sendWrongChecksumEvent

private void sendWrongChecksumEvent()
Sends a WrongChecksumEvent to all the listeners.


sendAddressingEvent

private void sendAddressingEvent(byte[] buffer)
Sends a WrongChecksumEvent to all the listeners. Extracts from, to and data informations from the buffer, and call the appropriate method of the interface.

Parameters:
buffer - The buffer containing the received data.

addressingRead

private void addressingRead(java.io.InputStream in)
Called by run when a 0xF1 is received. Reads the length, the data, checks the checksum, sends an addressing event to the listeners if the checksum is Ok, a wrong checksum event otherwise.

Parameters:
in - the InputStream from which to read the data.

integrityWrite

public void integrityWrite(java.lang.String data)
Sends a integrity packet containing the data of the parameter.

Parameters:
data - the data to send.

integrityWrite

public void integrityWrite(byte[] data)
Sends a integrity packet containing the data of the parameter.

Parameters:
data - the data to send.

addressingWrite

public void addressingWrite(java.lang.String data,
                            byte sourcePort,
                            byte destHostId,
                            byte destPort)
Sends a addressing packet containing the data of the parameter.

Parameters:
data - the data to send.
sourcePort - the port number from which you send the data
destHostId - the host to which you send the data

addressingWrite

public void addressingWrite(byte[] data,
                            byte sourcePort,
                            byte destHostId,
                            byte destPort)
Sends a addressing packet containing the data of the parameter.

Parameters:
data - the data to send.
sourcePort - the port number from which you send the data
destHostId - the host to which you send the data

checksum

private static int checksum(byte[] data,
                            int length)
Computes the checksum.

Parameters:
data - the data on which to perform the checksum
length - the length on which to perform the checksum
Returns:
the checksum

checkChecksum

private static boolean checkChecksum(byte firstByte,
                                     byte length,
                                     byte[] data,
                                     byte checksum)
Checks if the checksum is correct.

Parameters:
firstByte - 0xF0 or 0XF1, depending on the kind of packet
length - the length on which to check the checksum
data - the data on which to check the checksum
checksum - the received checksum
Returns:
true if the checksum is ok, false otherwise.

logicalWrite

private void logicalWrite(byte[] data)
Used to send a logical packet, i.e. a raw packet. Waits for the IR tower to be available, and sends the data to it. param data the data to send.


setHostAddress

public void setHostAddress(int hostAddress)
Sets the host address of the LNPManager. This method automatically applies the hostMask to the parameter.

Parameters:
hostAddress - : the address of the LNPManager.