Register all command readers with the event bus.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 11 Apr 2013 06:18:04 +0000 (08:18 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 11 Apr 2013 06:18:04 +0000 (08:18 +0200)
src/main/java/net/pterodactylus/xdcc/ui/stdin/NetworkAdapter.java

index 87fad77..cff376a 100644 (file)
@@ -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. */
                        }