From f3cc380e2d4bdb0e6cd3d975531d18eee69dad01 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Tue, 9 Apr 2013 22:55:40 +0200 Subject: [PATCH] Add network adapter. --- .../xdcc/ui/stdin/NetworkAdapter.java | 71 ++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 src/main/java/net/pterodactylus/xdcc/ui/stdin/NetworkAdapter.java 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 index 0000000..87fad77 --- /dev/null +++ b/src/main/java/net/pterodactylus/xdcc/ui/stdin/NetworkAdapter.java @@ -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 . + */ + +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 David ‘Bombe’ Roden + */ +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. */ + } + } + } + +} -- 2.7.4