First pass at a modem plugin.

This commit is contained in:
akwizgran
2012-11-26 14:09:18 +00:00
parent 6cc8463209
commit 721a6b8950
3 changed files with 23 additions and 17 deletions

View File

@@ -6,14 +6,14 @@ import java.io.OutputStream;
/** /**
* A modem that can be used for multiple sequential incoming and outgoing * 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 * calls.
* cannot continue to be used.
*/ */
interface Modem { interface Modem {
/** /**
* Call this method once after creating the modem and before making any * Call this method after creating the modem and before making any calls.
* calls. * If an exception is thrown while using the modem, this method must be
* called again.
*/ */
void init() throws IOException; void init() throws IOException;

View File

@@ -41,7 +41,6 @@ class ModemImpl implements Modem, SerialPortEventListener {
private int lineLen = 0; private int lineLen = 0;
ModemImpl(Executor executor, Callback callback, String portName) { ModemImpl(Executor executor, Callback callback, String portName) {
this.executor = executor; this.executor = executor;
this.callback = callback; this.callback = callback;

View File

@@ -80,10 +80,24 @@ class ModemPlugin implements DuplexPlugin, Modem.Callback {
return false; return false;
} }
public void stop() { // Synchronized to avoid a race condition with resetModem()
public synchronized void stop() {
running = false; 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() { public boolean shouldPoll() {
return true; return true;
} }
@@ -121,9 +135,7 @@ class ModemPlugin implements DuplexPlugin, Modem.Callback {
try { try {
if(!modem.dial(number)) continue; if(!modem.dial(number)) continue;
} catch(IOException e) { } catch(IOException e) {
// FIXME: Race condition with stop() if(resetModem()) continue;
running = false;
if(start()) continue;
else break; else break;
} }
ModemTransportConnection conn = new ModemTransportConnection(); ModemTransportConnection conn = new ModemTransportConnection();
@@ -149,9 +161,8 @@ class ModemPlugin implements DuplexPlugin, Modem.Callback {
try { try {
if(!modem.dial(number)) return null; if(!modem.dial(number)) return null;
} catch(IOException e) { } catch(IOException e) {
// FIXME: Race condition with stop() // Reinitialise the modem
running = false; resetModem();
start();
return null; return null;
} }
return new ModemTransportConnection(); return new ModemTransportConnection();
@@ -199,11 +210,7 @@ class ModemPlugin implements DuplexPlugin, Modem.Callback {
if(LOG.isLoggable(WARNING)) LOG.warning(e.toString()); if(LOG.isLoggable(WARNING)) LOG.warning(e.toString());
exception = true; exception = true;
} }
if(exception) { if(exception) resetModem();
// FIXME: Race condition with stop()
running = false;
start();
}
finished.countDown(); finished.countDown();
} }