From 721a6b895037679faf1e43c82464215cd803ab98 Mon Sep 17 00:00:00 2001 From: akwizgran <michael@briarproject.org> Date: Mon, 26 Nov 2012 14:09:18 +0000 Subject: [PATCH] First pass at a modem plugin. --- src/net/sf/briar/plugins/modem/Modem.java | 8 ++--- src/net/sf/briar/plugins/modem/ModemImpl.java | 1 - .../sf/briar/plugins/modem/ModemPlugin.java | 31 ++++++++++++------- 3 files changed, 23 insertions(+), 17 deletions(-) diff --git a/src/net/sf/briar/plugins/modem/Modem.java b/src/net/sf/briar/plugins/modem/Modem.java index 42c3693bf3..3d55e3c24c 100644 --- a/src/net/sf/briar/plugins/modem/Modem.java +++ b/src/net/sf/briar/plugins/modem/Modem.java @@ -6,14 +6,14 @@ import java.io.OutputStream; /** * A modem that can be used for multiple sequential incoming and outgoing - * calls. If the modem or its input or output streams throw any exceptions they - * cannot continue to be used. + * calls. */ interface Modem { /** - * Call this method once after creating the modem and before making any - * calls. + * Call this method after creating the modem and before making any calls. + * If an exception is thrown while using the modem, this method must be + * called again. */ void init() throws IOException; diff --git a/src/net/sf/briar/plugins/modem/ModemImpl.java b/src/net/sf/briar/plugins/modem/ModemImpl.java index da217a881c..eb85422599 100644 --- a/src/net/sf/briar/plugins/modem/ModemImpl.java +++ b/src/net/sf/briar/plugins/modem/ModemImpl.java @@ -41,7 +41,6 @@ class ModemImpl implements Modem, SerialPortEventListener { private int lineLen = 0; - ModemImpl(Executor executor, Callback callback, String portName) { this.executor = executor; this.callback = callback; diff --git a/src/net/sf/briar/plugins/modem/ModemPlugin.java b/src/net/sf/briar/plugins/modem/ModemPlugin.java index e51dd7b035..f2ea847390 100644 --- a/src/net/sf/briar/plugins/modem/ModemPlugin.java +++ b/src/net/sf/briar/plugins/modem/ModemPlugin.java @@ -80,10 +80,24 @@ class ModemPlugin implements DuplexPlugin, Modem.Callback { return false; } - public void stop() { + // Synchronized to avoid a race condition with resetModem() + public synchronized void stop() { running = false; } + // Synchronized to avoid a race condition with stop() + private synchronized boolean resetModem() { + if(!running) return false; + try { + modem.init(); + return true; + } catch(IOException e) { + if(LOG.isLoggable(WARNING)) LOG.warning(e.toString()); + running = false; + return false; + } + } + public boolean shouldPoll() { return true; } @@ -121,9 +135,7 @@ class ModemPlugin implements DuplexPlugin, Modem.Callback { try { if(!modem.dial(number)) continue; } catch(IOException e) { - // FIXME: Race condition with stop() - running = false; - if(start()) continue; + if(resetModem()) continue; else break; } ModemTransportConnection conn = new ModemTransportConnection(); @@ -149,9 +161,8 @@ class ModemPlugin implements DuplexPlugin, Modem.Callback { try { if(!modem.dial(number)) return null; } catch(IOException e) { - // FIXME: Race condition with stop() - running = false; - start(); + // Reinitialise the modem + resetModem(); return null; } return new ModemTransportConnection(); @@ -199,11 +210,7 @@ class ModemPlugin implements DuplexPlugin, Modem.Callback { if(LOG.isLoggable(WARNING)) LOG.warning(e.toString()); exception = true; } - if(exception) { - // FIXME: Race condition with stop() - running = false; - start(); - } + if(exception) resetModem(); finished.countDown(); } -- GitLab