a45a341d1f14867f268de3c41ab1e94650b8f2e1
[xudocci.git] / src / main / java / net / pterodactylus / xdcc / ui / stdin / NetworkAdapter.java
1 /*
2  * XdccDownloader - NetworkAcceptor.java - Copyright © 2013 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.xdcc.ui.stdin;
19
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;
28
29 import net.pterodactylus.xdcc.core.Core;
30
31 import com.google.common.eventbus.EventBus;
32 import com.google.common.util.concurrent.AbstractExecutionThreadService;
33 import com.google.common.util.concurrent.MoreExecutors;
34
35 /**
36  * Listens on a TCP port and feeds input and output to a {@link CommandReader}.
37  *
38  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
39  */
40 public class NetworkAdapter extends AbstractExecutionThreadService {
41
42         /** The event bus. */
43         private final EventBus eventBus;
44
45         /** The core being controlled. */
46         private final Core core;
47         private final int port;
48
49         /**
50          * Creates a new network acceptor.
51          *
52          * @param eventBus
53          * @param core
54          */
55         public NetworkAdapter(EventBus eventBus, Core core, int port) {
56                 this.eventBus = eventBus;
57                 this.core = core;
58                 this.port = port;
59         }
60
61         @Override
62         protected void run() throws Exception {
63                 ServerSocket serverSocket = new ServerSocket(port);
64                 serverSocket.setSoTimeout((int) TimeUnit.SECONDS.toMillis(1));
65                 while (isRunning()) {
66                         try {
67                                 Socket clientSocket = serverSocket.accept();
68                                 InputStream socketInputStream = clientSocket.getInputStream();
69                                 OutputStream socketOutputStream = clientSocket.getOutputStream();
70                                 final InputStreamReader socketInputStreamReader = new InputStreamReader(socketInputStream, "UTF-8");
71                                 final OutputStreamWriter socketOutputStreamWriter = new OutputStreamWriter(socketOutputStream, "UTF-8");
72                                 final CommandReader commandReader = new CommandReader(core, socketInputStreamReader, socketOutputStreamWriter);
73                                 eventBus.register(commandReader);
74                                 commandReader.addListener(new Listener() {
75
76                                         @Override
77                                         public void starting() {
78                                         }
79
80                                         @Override
81                                         public void running() {
82                                         }
83
84                                         @Override
85                                         public void stopping(State from) {
86                                         }
87
88                                         @Override
89                                         public void terminated(State from) {
90                                                 eventBus.unregister(commandReader);
91                                         }
92
93                                         @Override
94                                         public void failed(State from, Throwable failure) {
95                                                 eventBus.unregister(commandReader);
96                                         }
97                                 }, MoreExecutors.sameThreadExecutor());
98                                 commandReader.start();
99                         } catch (SocketTimeoutException ste1) {
100                                 /* ignore, loop. */
101                         }
102                 }
103         }
104
105 }