|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--robot3.LNPManager
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 |
private static LNPManager theInstance
private static final byte lnpHostMask
private static final byte lnpPortMask
private byte lnpHostAddress
private java.util.Vector listeners
private javax.comm.SerialPort IRTower
private static final int MAX_SIZE
Constructor Detail |
private LNPManager(java.lang.String comPort)
comPort
- the name of the serial port to which the IR tower is connected.Method Detail |
public static LNPManager getInstance(java.lang.String comPort)
comPort
- the name of the serial port to which the IR tower is connected.
public void addLNPListener(LNPListener listener)
listener
- the new object to call when a new message is received.public void removeLNPListener(LNPListener listener)
listener
- the object to remove from the list of the listeners to call when a new message is received.private void openPort(java.lang.String comPort)
comPort
- the serial port to which the IR tower is connected.public void run()
run
in interface java.lang.Runnable
private void integrityRead(java.io.InputStream in)
in
- the InputStream from which to read the data.private void sendIntegrityEvent(byte[] buffer)
buffer
- the bufferprivate void sendWrongChecksumEvent()
private void sendAddressingEvent(byte[] buffer)
buffer
- The buffer containing the received data.private void addressingRead(java.io.InputStream in)
in
- the InputStream from which to read the data.public void integrityWrite(java.lang.String data)
data
- the data to send.public void integrityWrite(byte[] data)
data
- the data to send.public void addressingWrite(java.lang.String data, byte sourcePort, byte destHostId, byte destPort)
data
- the data to send.sourcePort
- the port number from which you send the datadestHostId
- the host to which you send the datapublic void addressingWrite(byte[] data, byte sourcePort, byte destHostId, byte destPort)
data
- the data to send.sourcePort
- the port number from which you send the datadestHostId
- the host to which you send the dataprivate static int checksum(byte[] data, int length)
data
- the data on which to perform the checksumlength
- the length on which to perform the checksum
private static boolean checkChecksum(byte firstByte, byte length, byte[] data, byte checksum)
firstByte
- 0xF0 or 0XF1, depending on the kind of packetlength
- the length on which to check the checksumdata
- the data on which to check the checksumchecksum
- the received checksum
private void logicalWrite(byte[] data)
public void setHostAddress(int hostAddress)
hostAddress
- : the address of the LNPManager.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |