lejos.remote.rcx.LLCReliableHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of lejos-ev3-api Show documentation
Show all versions of lejos-ev3-api Show documentation
leJOS (pronounced like the Spanish word "lejos" for "far") is a tiny Java Virtual Machine. In 2013 it was ported to the LEGO EV3 brick.
The newest version!
package lejos.remote.rcx;
/**
* A Packet handler that guarantees reliable delivery using checksums,
* acks, and a single bit sequence number.
*
**/
public class LLCReliableHandler extends PacketHandler {
private byte [] inPacket = new byte[3];
private byte [] outPacket = new byte[3];
private byte [] inAck = new byte[2];
private byte [] outAck = new byte [2];
private int inPacketLength = 0;
private boolean sequence = false, receiveSequence = false;
public LLCReliableHandler(PacketHandler handler) {
super(handler);
}
/**
* Reset sequence numbers.
**/
public void reset() {
sequence = false;
receiveSequence = false;
}
/** Send a packet.
* @param packet the bytes to send
* @param len the number of bytes to send
* @return true if send successful, else false
*/
public boolean sendPacket(byte [] packet, int len) {
byte b = (byte) (0xf0 + len); // Send byte
if (sequence) b |= 8;
outPacket[0] = b;
int sum = b;
for(int i=0; i 0) return true;
while (lowerHandler.isPacketAvailable()) {
int len = lowerHandler.receivePacket(inPacket);
int sum = 0;
for(int i=0; i