Add network adapter.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 9 Apr 2013 20:55:40 +0000 (22:55 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 9 Apr 2013 20:56:58 +0000 (22:56 +0200)
src/main/java/net/pterodactylus/xdcc/ui/stdin/NetworkAdapter.java [new file with mode: 0644]

diff --git a/src/main/java/net/pterodactylus/xdcc/ui/stdin/NetworkAdapter.java b/src/main/java/net/pterodactylus/xdcc/ui/stdin/NetworkAdapter.java
new file mode 100644 (file)
index 0000000..87fad77
--- /dev/null
@@ -0,0 +1,71 @@
+/*
+ * XdccDownloader - NetworkAcceptor.java - Copyright © 2013 David Roden
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.xdcc.ui.stdin;
+
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.net.ServerSocket;
+import java.net.Socket;
+import java.net.SocketTimeoutException;
+import java.util.concurrent.TimeUnit;
+
+import net.pterodactylus.xdcc.core.Core;
+
+import com.google.common.util.concurrent.AbstractExecutionThreadService;
+
+/**
+ * Listens on a TCP port and feeds input and output to a {@link CommandReader}.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class NetworkAdapter extends AbstractExecutionThreadService {
+
+       /** The core being controlled. */
+       private final Core core;
+
+       /**
+        * Creates a new network acceptor.
+        *
+        * @param core
+        *              The core being controlled
+        */
+       public NetworkAdapter(Core core) {
+               this.core = core;
+       }
+
+       @Override
+       protected void run() throws Exception {
+               ServerSocket serverSocket = new ServerSocket(45678);
+               serverSocket.setSoTimeout((int) TimeUnit.SECONDS.toMillis(1));
+               while (isRunning()) {
+                       try {
+                               Socket clientSocket = serverSocket.accept();
+                               InputStream socketInputStream = clientSocket.getInputStream();
+                               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();
+                       } catch (SocketTimeoutException ste1) {
+                               /* ignore, loop. */
+                       }
+               }
+       }
+
+}