I decided to write my first network application with Java and LeJOS: I tried to implement kind of packets that are transmitted by a "NetworkManager" in a seperate thread. Essentially, those packets just write their id as a byte in the input stream and then their data (for example a String). It worked at the beginning, but now the NXT doesnt receive any data...
Main code (on PC):
- Code: Select all
DisplayMessagePacket packet = new DisplayMessagePacket();
packet.message = "TEST";
while (true) {
network.addToSendQueue(packet);
try {
Thread.sleep(20);
} catch (InterruptedException e) {
}
}
NetworkManager (on PC and NXT, shortened):
- Code: Select all
public class NetworkManager {
private class NetworkReaderThread extends Thread {
public NetworkReaderThread() {
super("NetworkManager Reader Thread");
}
@Override
public void run() {
while (isRunning()) {
try {
try {
System.out.println("reading!");
readPacket();
} catch (IOException e) {
if (in == null)
return;
e.printStackTrace();
shutdown();
return;
}
Thread.sleep(readFrequency);
} catch (InterruptedException e) {
}
}
}
}
private class NetworkWriterThread extends Thread {
public NetworkWriterThread() {
super("NetworkManager Writer Thread");
}
@Override
public void run() {
while (isRunning()) {
try {
while (writePacket())
;
} catch (IOException e1) {
e1.printStackTrace();
shutdown();
return;
}
try {
Thread.sleep(sendFrequency);
} catch (InterruptedException e) {
}
}
}
}
public short readFrequency = 500;
public short sendFrequency = 500;
private NetHandler handler;
private DataInputStream in;
private DataOutputStream out;
private List<Packet> packetSendQueue = new LinkedList<Packet>();
private NetworkReaderThread reader;
private NetworkWriterThread writer;
public NetworkManager(DataInputStream in, DataOutputStream out,
NetHandler handler) {
this.in = in;
this.out = out;
this.handler = handler;
handler.setNetworkManager(this);
writer = new NetworkWriterThread();
reader = new NetworkReaderThread();
reader.start();
writer.start();
}
/**
* Adds a packet to a queue of packets to be sent.
*
* @param packet
*/
public void addToSendQueue(Packet packet) {
synchronized (packetSendQueue) {
packetSendQueue.add(packet);
}
}
/**
* Reads a single packet from the {@link DataInputStream} of this object.
* This method is blocking; it waits until a full packet has been read.
*
* @throws IOException
*/
private void readPacket() throws IOException {
Packet packet = Packet.readPacket(in);
if (packet != null) {
packet.processPacket(handler);
} else {
shutdown();
}
}
/**
* Writes a single packet to the {@link DataOutputStream} of this object.
*
* @return If there are any more packets left in the send queue.
* @throws IOException
*/
private boolean writePacket() throws IOException {
synchronized (packetSendQueue) {
if (packetSendQueue.isEmpty())
return false;
Packet packet = packetSendQueue.remove(0);
Packet.writePacket(packet, out);
if (packetSendQueue.isEmpty())
return false;
else
return true;
}
}
}
While it works fine on the PC (it receives data sent from the NXT, it also shows "reading!"), the NXT only shows "reading!" one time and then receives nothing... Can somebody help?
