Skip to content
Snippets Groups Projects
Commit 9528a8b6 authored by akwizgran's avatar akwizgran
Browse files

Minor refactoring and logging for reliability layer.

parent 28086e1a
No related branches found
No related tags found
No related merge requests found
......@@ -69,7 +69,7 @@ class ModemImpl implements Modem, WriteHandler, SerialPortEventListener {
}
if(!foundBaudRate)
throw new IOException("Could not find a suitable baud rate");
reliabilityLayer.init();
reliabilityLayer.start();
port.addEventListener(this);
port.purgePort(PURGE_RXCLEAR | PURGE_TXCLEAR);
port.writeBytes("ATZ\r\n".getBytes("US-ASCII")); // Reset
......@@ -144,7 +144,7 @@ class ModemImpl implements Modem, WriteHandler, SerialPortEventListener {
tryToClose(port);
throw new IOException(e.toString());
}
reliabilityLayer.invalidate();
reliabilityLayer.stop();
reliabilityLayer = new ReliabilityLayer(this);
connected.set(false);
offHook.release();
......
......@@ -23,14 +23,14 @@ class ReliabilityLayer implements ReadHandler, WriteHandler {
private volatile ReceiverInputStream inputStream = null;
private volatile SenderOutputStream outputStream = null;
private volatile Thread writer = null;
private volatile boolean valid = true;
private volatile boolean running = false;
ReliabilityLayer(WriteHandler writeHandler) {
this.writeHandler = writeHandler;
writes = new LinkedBlockingQueue<byte[]>();
}
void init() {
void start() {
SlipEncoder encoder = new SlipEncoder(this);
Sender sender = new Sender(encoder);
receiver = new Receiver(sender);
......@@ -41,7 +41,7 @@ class ReliabilityLayer implements ReadHandler, WriteHandler {
@Override
public void run() {
try {
while(valid) {
while(running) {
byte[] b = writes.take();
if(b.length == 0) return; // Poison pill
if(LOG.isLoggable(INFO))
......@@ -51,15 +51,16 @@ class ReliabilityLayer implements ReadHandler, WriteHandler {
} catch(InterruptedException e) {
if(LOG.isLoggable(WARNING))
LOG.warning("Interrupted while writing");
valid = false;
running = false;
Thread.currentThread().interrupt();
} catch(IOException e) {
if(LOG.isLoggable(WARNING))
LOG.warning("Interrupted while writing");
valid = false;
running = false;
}
}
};
running = true;
writer.start();
}
......@@ -71,22 +72,23 @@ class ReliabilityLayer implements ReadHandler, WriteHandler {
return outputStream;
}
void invalidate() {
valid = false;
void stop() {
if(LOG.isLoggable(INFO)) LOG.info("Stopping reliability layer");
running = false;
receiver.invalidate();
writes.add(new byte[0]); // Poison pill
}
// The modem calls this method to pass data up to the SLIP decoder
public void handleRead(byte[] b) throws IOException {
if(!valid) throw new IOException("Connection closed");
if(!running) throw new IOException("Connection closed");
if(LOG.isLoggable(INFO)) LOG.info("Read " + b.length + " bytes");
decoder.handleRead(b);
}
// The SLIP encoder calls this method to pass data down to the modem
public void handleWrite(byte[] b) throws IOException {
if(!valid) throw new IOException("Connection closed");
if(!running) throw new IOException("Connection closed");
if(LOG.isLoggable(INFO)) LOG.info("Queueing " + b.length + " bytes");
if(b.length > 0) writes.add(b);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment