From: David ‘Bombe’ Roden Date: Tue, 9 Apr 2013 20:55:40 +0000 (+0200) Subject: Add network adapter. X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=f3cc380e2d4bdb0e6cd3d975531d18eee69dad01;p=xudocci.git Add network adapter. --- 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. */ + } + } + } + +}