Minor refactoring and logging for reliability layer.

This commit is contained in:
akwizgran
2012-12-07 15:00:16 +00:00
parent 28086e1a7f
commit 9528a8b6d6
2 changed files with 13 additions and 11 deletions

View File

@@ -69,7 +69,7 @@ class ModemImpl implements Modem, WriteHandler, SerialPortEventListener {
} }
if(!foundBaudRate) if(!foundBaudRate)
throw new IOException("Could not find a suitable baud rate"); throw new IOException("Could not find a suitable baud rate");
reliabilityLayer.init(); reliabilityLayer.start();
port.addEventListener(this); port.addEventListener(this);
port.purgePort(PURGE_RXCLEAR | PURGE_TXCLEAR); port.purgePort(PURGE_RXCLEAR | PURGE_TXCLEAR);
port.writeBytes("ATZ\r\n".getBytes("US-ASCII")); // Reset port.writeBytes("ATZ\r\n".getBytes("US-ASCII")); // Reset
@@ -144,7 +144,7 @@ class ModemImpl implements Modem, WriteHandler, SerialPortEventListener {
tryToClose(port); tryToClose(port);
throw new IOException(e.toString()); throw new IOException(e.toString());
} }
reliabilityLayer.invalidate(); reliabilityLayer.stop();
reliabilityLayer = new ReliabilityLayer(this); reliabilityLayer = new ReliabilityLayer(this);
connected.set(false); connected.set(false);
offHook.release(); offHook.release();

View File

@@ -23,14 +23,14 @@ class ReliabilityLayer implements ReadHandler, WriteHandler {
private volatile ReceiverInputStream inputStream = null; private volatile ReceiverInputStream inputStream = null;
private volatile SenderOutputStream outputStream = null; private volatile SenderOutputStream outputStream = null;
private volatile Thread writer = null; private volatile Thread writer = null;
private volatile boolean valid = true; private volatile boolean running = false;
ReliabilityLayer(WriteHandler writeHandler) { ReliabilityLayer(WriteHandler writeHandler) {
this.writeHandler = writeHandler; this.writeHandler = writeHandler;
writes = new LinkedBlockingQueue<byte[]>(); writes = new LinkedBlockingQueue<byte[]>();
} }
void init() { void start() {
SlipEncoder encoder = new SlipEncoder(this); SlipEncoder encoder = new SlipEncoder(this);
Sender sender = new Sender(encoder); Sender sender = new Sender(encoder);
receiver = new Receiver(sender); receiver = new Receiver(sender);
@@ -41,7 +41,7 @@ class ReliabilityLayer implements ReadHandler, WriteHandler {
@Override @Override
public void run() { public void run() {
try { try {
while(valid) { while(running) {
byte[] b = writes.take(); byte[] b = writes.take();
if(b.length == 0) return; // Poison pill if(b.length == 0) return; // Poison pill
if(LOG.isLoggable(INFO)) if(LOG.isLoggable(INFO))
@@ -51,15 +51,16 @@ class ReliabilityLayer implements ReadHandler, WriteHandler {
} catch(InterruptedException e) { } catch(InterruptedException e) {
if(LOG.isLoggable(WARNING)) if(LOG.isLoggable(WARNING))
LOG.warning("Interrupted while writing"); LOG.warning("Interrupted while writing");
valid = false; running = false;
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
} catch(IOException e) { } catch(IOException e) {
if(LOG.isLoggable(WARNING)) if(LOG.isLoggable(WARNING))
LOG.warning("Interrupted while writing"); LOG.warning("Interrupted while writing");
valid = false; running = false;
} }
} }
}; };
running = true;
writer.start(); writer.start();
} }
@@ -71,22 +72,23 @@ class ReliabilityLayer implements ReadHandler, WriteHandler {
return outputStream; return outputStream;
} }
void invalidate() { void stop() {
valid = false; if(LOG.isLoggable(INFO)) LOG.info("Stopping reliability layer");
running = false;
receiver.invalidate(); receiver.invalidate();
writes.add(new byte[0]); // Poison pill writes.add(new byte[0]); // Poison pill
} }
// The modem calls this method to pass data up to the SLIP decoder // The modem calls this method to pass data up to the SLIP decoder
public void handleRead(byte[] b) throws IOException { 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"); if(LOG.isLoggable(INFO)) LOG.info("Read " + b.length + " bytes");
decoder.handleRead(b); decoder.handleRead(b);
} }
// The SLIP encoder calls this method to pass data down to the modem // The SLIP encoder calls this method to pass data down to the modem
public void handleWrite(byte[] b) throws IOException { 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(LOG.isLoggable(INFO)) LOG.info("Queueing " + b.length + " bytes");
if(b.length > 0) writes.add(b); if(b.length > 0) writes.add(b);
} }