josx.rcxcomm
Class LNP

java.lang.Object
  extended byjava.lang.Thread
      extended byjosx.rcxcomm.LNP

public class LNP
extends Thread

An interface for sending and receiving Lego Network Protocol addressing and integrity layer packets. It uses an integrity packet handler to send and receive integrity and addressing packet. Listeners can be registered to process incoming broadcasts of integrity packets or addressing packets on multiple ports. Currently assumes that the most significant nibble of the address byte is the machine and the least significant nibble is the port number. The machine addresses is defined by the startListening call, but is also part of the source address sent with addressing packets.


Field Summary
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Method Summary
static void addAddressingHandler(AddressingHandler handler, byte port)
          Register an integrity handler to process incoming addressing packets for a specific port.
static void addIntegrityHandler(IntegrityHandler handler)
          Register an integrity handler to process incoming broadcasts
static boolean addressingWrite(byte[] packet, int len, byte dest, byte source)
          Sends packet of up to 59 bytes to the destination address from the source address.
static boolean integrityWrite(byte[] packet, int len)
          Broadcasts an integrity packet of up to 61 bytes
 void run()
          Background thread to listen for incoming packets and call the appropriate listeners.
static void startListening(byte addr)
          Start listening for incoming broadcasts and addressing packets.
 
Methods inherited from class java.lang.Thread
currentThread, getPriority, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, setDaemon, setPriority, sleep, start, yield
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait
 

Method Detail

addressingWrite

public static boolean addressingWrite(byte[] packet,
                                      int len,
                                      byte dest,
                                      byte source)
Sends packet of up to 59 bytes to the destination address from the source address. The source address is used for replying to the packet.

Parameters:
packet - the packet to send
len - the length of the packet
dest - the destination machine and port
source - the source machine and address
Returns:
true for successful send, else false, e.g. for collision detection

integrityWrite

public static boolean integrityWrite(byte[] packet,
                                     int len)
Broadcasts an integrity packet of up to 61 bytes

Parameters:
packet - the packet to broadcast
len - the length of the packet
Returns:
true if sucessful, else false e.g. for collision detected

addIntegrityHandler

public static void addIntegrityHandler(IntegrityHandler handler)
Register an integrity handler to process incoming broadcasts

Parameters:
handler - the integrity handler

addAddressingHandler

public static void addAddressingHandler(AddressingHandler handler,
                                        byte port)
Register an integrity handler to process incoming addressing packets for a specific port.

Parameters:
handler - the addressing handler

startListening

public static void startListening(byte addr)
Start listening for incoming broadcasts and addressing packets.

Parameters:
addr - the address of this machine. The least significant 4 bits should be zero.

run

public void run()
Background thread to listen for incoming packets and call the appropriate listeners.

Specified by:
run in class Thread