From cbb2c04b71b9a923068449823caaad686a038d8c Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Thu, 11 Apr 2013 08:18:04 +0200 Subject: [PATCH] Register all command readers with the event bus. --- .../xdcc/ui/stdin/NetworkAdapter.java | 38 ++++++++++++++++++++-- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/src/main/java/net/pterodactylus/xdcc/ui/stdin/NetworkAdapter.java b/src/main/java/net/pterodactylus/xdcc/ui/stdin/NetworkAdapter.java index 87fad77..cff376a 100644 --- a/src/main/java/net/pterodactylus/xdcc/ui/stdin/NetworkAdapter.java +++ b/src/main/java/net/pterodactylus/xdcc/ui/stdin/NetworkAdapter.java @@ -28,7 +28,9 @@ import java.util.concurrent.TimeUnit; import net.pterodactylus.xdcc.core.Core; +import com.google.common.eventbus.EventBus; import com.google.common.util.concurrent.AbstractExecutionThreadService; +import com.google.common.util.concurrent.MoreExecutors; /** * Listens on a TCP port and feeds input and output to a {@link CommandReader}. @@ -37,16 +39,20 @@ import com.google.common.util.concurrent.AbstractExecutionThreadService; */ public class NetworkAdapter extends AbstractExecutionThreadService { + /** The event bus. */ + private final EventBus eventBus; + /** The core being controlled. */ private final Core core; /** * Creates a new network acceptor. * + * @param eventBus * @param core - * The core being controlled */ - public NetworkAdapter(Core core) { + public NetworkAdapter(EventBus eventBus, Core core) { + this.eventBus = eventBus; this.core = core; } @@ -61,7 +67,33 @@ public class NetworkAdapter extends AbstractExecutionThreadService { OutputStream socketOutputStream = clientSocket.getOutputStream(); final InputStreamReader socketInputStreamReader = new InputStreamReader(socketInputStream, "UTF-8"); final OutputStreamWriter socketOutputStreamWriter = new OutputStreamWriter(socketOutputStream, "UTF-8"); - new CommandReader(core, socketInputStreamReader, socketOutputStreamWriter).start(); + final CommandReader commandReader = new CommandReader(core, socketInputStreamReader, socketOutputStreamWriter); + eventBus.register(commandReader); + commandReader.addListener(new Listener() { + + @Override + public void starting() { + } + + @Override + public void running() { + } + + @Override + public void stopping(State from) { + } + + @Override + public void terminated(State from) { + eventBus.unregister(commandReader); + } + + @Override + public void failed(State from, Throwable failure) { + eventBus.unregister(commandReader); + } + }, MoreExecutors.sameThreadExecutor()); + commandReader.start(); } catch (SocketTimeoutException ste1) { /* ignore, loop. */ } -- 2.7.4