reduces the danger of race conditions

This commit is contained in:
hneemann 2021-05-01 11:40:15 +02:00
parent 4a8564198b
commit 1af59a16cf
2 changed files with 18 additions and 8 deletions

View File

@ -5,7 +5,7 @@
*/
package de.neemann.digital.core.io.telnet;
import de.neemann.digital.core.Observer;
import de.neemann.digital.core.SyncAccess;
import java.io.IOException;
import java.io.InputStream;
@ -22,7 +22,8 @@ public class Server {
private final ServerThread serverThread;
private boolean telnetEscape;
private ClientThread client;
private Observer notify;
private Telnet telnet;
private SyncAccess syncAccess;
Server(int port) throws IOException {
buffer = new ByteBuffer(1024);
@ -48,8 +49,15 @@ public class Server {
buffer.deleteAll();
}
void setNotify(Observer notify) {
this.notify = notify;
/**
* Connects the server with the telnet node
*
* @param telnet the telnet node
* @param syncAccess used to access the model
*/
public void setTelnetNode(Telnet telnet, SyncAccess syncAccess) {
this.telnet = telnet;
this.syncAccess = syncAccess;
}
boolean hasData() {
@ -65,9 +73,11 @@ public class Server {
}
private void dataReceived(int data) {
if (syncAccess != null)
syncAccess.modify(() -> {
buffer.put((byte) data);
if (notify != null)
notify.hasChanged();
telnet.hasChanged();
});
}
boolean isDead() {

View File

@ -103,7 +103,7 @@ public class Telnet extends Node implements Element {
throw new NodeException(Lang.get("err_couldNotCreateServer"), e);
}
server.setTelnetEscape(telnetEscape);
server.setNotify(() -> model.modify(Telnet.this::hasChanged));
server.setTelnetNode(this, model);
}
}