2 * XdccDownloader - NetworkAcceptor.java - Copyright © 2013 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.xdcc.ui.stdin;
20 import java.io.InputStream;
21 import java.io.InputStreamReader;
22 import java.io.OutputStream;
23 import java.io.OutputStreamWriter;
24 import java.net.ServerSocket;
25 import java.net.Socket;
26 import java.net.SocketTimeoutException;
27 import java.util.concurrent.TimeUnit;
29 import net.pterodactylus.xdcc.core.Core;
31 import com.google.common.eventbus.EventBus;
32 import com.google.common.util.concurrent.AbstractExecutionThreadService;
33 import com.google.common.util.concurrent.MoreExecutors;
36 * Listens on a TCP port and feeds input and output to a {@link CommandReader}.
38 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
40 public class NetworkAdapter extends AbstractExecutionThreadService {
43 private final EventBus eventBus;
45 private final CommandReaderFactory commandReaderFactory;
46 private final int port;
49 * Creates a new network acceptor.
52 * @param commandReaderFactory
54 public NetworkAdapter(EventBus eventBus, CommandReaderFactory commandReaderFactory, int port) {
55 this.eventBus = eventBus;
56 this.commandReaderFactory = commandReaderFactory;
61 protected void run() throws Exception {
62 ServerSocket serverSocket = new ServerSocket(port);
63 serverSocket.setSoTimeout((int) TimeUnit.SECONDS.toMillis(1));
66 Socket clientSocket = serverSocket.accept();
67 InputStream socketInputStream = clientSocket.getInputStream();
68 OutputStream socketOutputStream = clientSocket.getOutputStream();
69 final InputStreamReader socketInputStreamReader = new InputStreamReader(socketInputStream, "UTF-8");
70 final OutputStreamWriter socketOutputStreamWriter = new OutputStreamWriter(socketOutputStream, "UTF-8");
71 final CommandReader commandReader = commandReaderFactory.create(socketInputStreamReader, socketOutputStreamWriter);
72 eventBus.register(commandReader);
73 commandReader.addListener(new Listener() {
76 public void starting() {
80 public void running() {
84 public void stopping(State from) {
88 public void terminated(State from) {
89 eventBus.unregister(commandReader);
93 public void failed(State from, Throwable failure) {
94 eventBus.unregister(commandReader);
96 }, MoreExecutors.sameThreadExecutor());
97 commandReader.start();
98 } catch (SocketTimeoutException ste1) {