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}.
*/
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;
}
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. */
}