[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 final Mote mote;
private final Simulation simulation;
public SerialSocketClient(Mote mote, Simulation simulation, final Cooja gui) {
super("Serial Socket (CLIENT) (" + mote + ")", gui, false);
this.mote = mote;
this.simulation = simulation;
/* GUI components */
if (Cooja.isVisualized()) {
@ -411,9 +413,19 @@ public class SerialSocketClient extends VisPlugin implements MotePlugin {
}
if (numRead >= 0) {
for (int i = 0; i < numRead; i++) {
serialPort.writeByte(data[i]);
}
final int finalNumRead = numRead;
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;
if (Cooja.isVisualized()) {
socketToMoteLabel.setText(inBytes + " bytes");

View File

@ -113,10 +113,12 @@ public class SerialSocketServer extends VisPlugin implements MotePlugin {
private Socket clientSocket;
private Mote mote;
private Simulation simulation;
public SerialSocketServer(Mote mote, Simulation simulation, final Cooja gui) {
super("Serial Socket (SERVER) (" + mote + ")", gui, false);
this.mote = mote;
this.simulation = simulation;
updateTimer.start();
@ -475,10 +477,19 @@ public class SerialSocketServer extends VisPlugin implements MotePlugin {
logger.info("Forwarder: socket -> serial port");
while (numRead >= 0) {
for (int i = 0; i < numRead; i++) {
serialPort.writeByte(data[i]);
}
inBytes += numRead;
final int finalNumRead = numRead;
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 += finalNumRead;
}
});
try {
numRead = in.read(data);