[cooja/serialsocket] Write serial data only on simulation thread

This commit is contained in:
Enrico Joerns 2014-05-02 21:46:20 +02:00
parent 894a88d08e
commit db2f4e6818
2 changed files with 30 additions and 7 deletions

View file

@ -115,10 +115,12 @@ public class SerialSocketClient extends VisPlugin implements MotePlugin {
private DataOutputStream out; private DataOutputStream out;
private final Mote mote; private final Mote mote;
private final Simulation simulation;
public SerialSocketClient(Mote mote, Simulation simulation, final Cooja gui) { public SerialSocketClient(Mote mote, Simulation simulation, final Cooja gui) {
super("Serial Socket (CLIENT) (" + mote + ")", gui, false); super("Serial Socket (CLIENT) (" + mote + ")", gui, false);
this.mote = mote; this.mote = mote;
this.simulation = simulation;
/* GUI components */ /* GUI components */
if (Cooja.isVisualized()) { if (Cooja.isVisualized()) {
@ -411,9 +413,19 @@ public class SerialSocketClient extends VisPlugin implements MotePlugin {
} }
if (numRead >= 0) { if (numRead >= 0) {
for (int i = 0; i < numRead; i++) { final int finalNumRead = numRead;
serialPort.writeByte(data[i]); final byte[] finalData = data;
} /* We are not on the simulation thread */
simulation.invokeSimulationThread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < finalNumRead; i++) {
serialPort.writeByte(finalData[i]);
}
}
});
inBytes += numRead; inBytes += numRead;
if (Cooja.isVisualized()) { if (Cooja.isVisualized()) {
socketToMoteLabel.setText(inBytes + " bytes"); socketToMoteLabel.setText(inBytes + " bytes");

View file

@ -113,10 +113,12 @@ public class SerialSocketServer extends VisPlugin implements MotePlugin {
private Socket clientSocket; private Socket clientSocket;
private Mote mote; private Mote mote;
private Simulation simulation;
public SerialSocketServer(Mote mote, Simulation simulation, final Cooja gui) { public SerialSocketServer(Mote mote, Simulation simulation, final Cooja gui) {
super("Serial Socket (SERVER) (" + mote + ")", gui, false); super("Serial Socket (SERVER) (" + mote + ")", gui, false);
this.mote = mote; this.mote = mote;
this.simulation = simulation;
updateTimer.start(); updateTimer.start();
@ -475,10 +477,19 @@ public class SerialSocketServer extends VisPlugin implements MotePlugin {
logger.info("Forwarder: socket -> serial port"); logger.info("Forwarder: socket -> serial port");
while (numRead >= 0) { while (numRead >= 0) {
for (int i = 0; i < numRead; i++) { final int finalNumRead = numRead;
serialPort.writeByte(data[i]); final byte[] finalData = data;
} /* We are not on the simulation thread */
inBytes += numRead; simulation.invokeSimulationThread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < finalNumRead; i++) {
serialPort.writeByte(finalData[i]);
}
inBytes += finalNumRead;
}
});
try { try {
numRead = in.read(data); numRead = in.read(data);