From 13fd613bb856332316f6ac1dd4bd297bb4d5c0e3 Mon Sep 17 00:00:00 2001 From: zzz <zzz@mail.i2p> Date: Wed, 25 Nov 2015 17:27:37 +0000 Subject: [PATCH] more client test enhancements --- .../src/net/i2p/sam/client/SAMReader.java | 24 +++++++++++++++---- .../src/net/i2p/sam/client/SAMStreamSend.java | 5 ++++ 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/apps/sam/java/src/net/i2p/sam/client/SAMReader.java b/apps/sam/java/src/net/i2p/sam/client/SAMReader.java index d25d1f0537..bc8d989581 100644 --- a/apps/sam/java/src/net/i2p/sam/client/SAMReader.java +++ b/apps/sam/java/src/net/i2p/sam/client/SAMReader.java @@ -20,6 +20,7 @@ public class SAMReader { private final InputStream _inRaw; private final SAMClientEventListener _listener; private volatile boolean _live; + private Thread _thread; public SAMReader(I2PAppContext context, InputStream samIn, SAMClientEventListener listener) { _log = context.logManager().getLog(SAMReader.class); @@ -27,13 +28,22 @@ public class SAMReader { _listener = listener; } - public void startReading() { + public synchronized void startReading() { + if (_live) + throw new IllegalStateException(); _live = true; I2PAppThread t = new I2PAppThread(new Runner(), "SAM reader"); t.start(); + _thread = t; } - public void stopReading() { _live = false; } + public synchronized void stopReading() { + _live = false; + if (_thread != null) { + _thread.interrupt(); + _thread = null; + } + } /** * Async event notification interface for SAM clients @@ -89,10 +99,11 @@ public class SAMReader { } if (c == -1) { _log.error("Error reading from the SAM bridge"); - return; + break; } } catch (IOException ioe) { _log.error("Error reading from SAM", ioe); + break; } String line = new String(baos.toByteArray()); @@ -103,14 +114,15 @@ public class SAMReader { break; } - _log.debug("Line read from the bridge: " + line); + if (_log.shouldDebug()) + _log.debug("Line read from the bridge: " + line); StringTokenizer tok = new StringTokenizer(line); if (tok.countTokens() < 2) { _log.error("Invalid SAM line: [" + line + "]"); _live = false; - return; + break; } String major = tok.nextToken(); @@ -133,6 +145,8 @@ public class SAMReader { processEvent(major, minor, params); } + if (_log.shouldWarn()) + _log.warn("SAMReader exiting"); } } diff --git a/apps/sam/java/src/net/i2p/sam/client/SAMStreamSend.java b/apps/sam/java/src/net/i2p/sam/client/SAMStreamSend.java index 29e040394d..4e034dfa57 100644 --- a/apps/sam/java/src/net/i2p/sam/client/SAMStreamSend.java +++ b/apps/sam/java/src/net/i2p/sam/client/SAMStreamSend.java @@ -263,6 +263,11 @@ public class SAMStreamSend { } closed(); + if (_log.shouldLog(Log.DEBUG)) + _log.debug("Runner exiting"); + // stop the reader, since we're only doing this once for testing + // you wouldn't do this in a real application + _reader.stopReading(); } } } -- GitLab