|
@@ -0,0 +1,126 @@
|
|
|
+/*
|
|
|
+ * Copyright (C) 2015 iZc
|
|
|
+ *
|
|
|
+ * 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 de.nplusc.izc.iZpl.RCUI;
|
|
|
+
|
|
|
+import de.nplusc.izc.iZpl.API.shared.networking.Packet;
|
|
|
+import java.io.BufferedReader;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStreamReader;
|
|
|
+import java.io.PrintStream;
|
|
|
+import java.net.Socket;
|
|
|
+import org.apache.logging.log4j.LogManager;
|
|
|
+import org.apache.logging.log4j.Logger;
|
|
|
+import org.yaml.snakeyaml.Yaml;
|
|
|
+
|
|
|
+/**
|
|
|
+ *
|
|
|
+ * @author iZc <nplusc.de>
|
|
|
+ */
|
|
|
+public class RemoteListener
|
|
|
+{
|
|
|
+ private RemoteUI core;
|
|
|
+ private Socket sck;
|
|
|
+ private String returnMsg;
|
|
|
+ private static final Logger l = LogManager.getLogger();
|
|
|
+ public RemoteListener(RemoteUI ui,Socket s)
|
|
|
+ {
|
|
|
+ core=ui;
|
|
|
+ sck=s;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void start()
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ BufferedReader r = new BufferedReader(new InputStreamReader(sck.getInputStream()));
|
|
|
+ PrintStream out = new PrintStream(sck.getOutputStream());
|
|
|
+ Thread t = new Thread(()->{
|
|
|
+ while(sck.isConnected())
|
|
|
+ {
|
|
|
+ String request = "";
|
|
|
+ try {
|
|
|
+ String ln = r.readLine();
|
|
|
+ while(!ln.equals("---"))
|
|
|
+ {
|
|
|
+ request+=ln+"\n";
|
|
|
+ l.trace(ln);
|
|
|
+ ln=r.readLine();
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (IOException ex) {
|
|
|
+ l.warn("Lost connection"); //TODO cleanup
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ Object packet = new Yaml().load(request);
|
|
|
+ if(packet instanceof Packet)
|
|
|
+ {
|
|
|
+ l.info("PACKET received");
|
|
|
+ l.trace(request);
|
|
|
+ processPacket((Packet)packet);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ //error();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ t.setName("InputCruncher");
|
|
|
+ t.start();
|
|
|
+ Thread t2 = new Thread(()->{
|
|
|
+ while(sck.isConnected())
|
|
|
+ {
|
|
|
+ synchronized(sck)
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ sck.wait();
|
|
|
+ } catch (InterruptedException ex) {
|
|
|
+ }
|
|
|
+ out.println(returnMsg);
|
|
|
+ out.flush();
|
|
|
+ returnMsg="";
|
|
|
+ l.info("MSG sent back");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ t2.setName("OutputCruncher");
|
|
|
+ t2.start();
|
|
|
+ } catch (IOException ex) {
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void respondToLinkedClient(Packet response) {
|
|
|
+ synchronized(sck)
|
|
|
+ {
|
|
|
+ l.info("ANTWORT");
|
|
|
+ returnMsg=new Yaml().dump(response)+"\n---\n";
|
|
|
+ sck.notify();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getClientIP()
|
|
|
+ {
|
|
|
+ String internalAddr=sck.getRemoteSocketAddress().toString().substring(1);
|
|
|
+ int lastCol=internalAddr.lastIndexOf(":");
|
|
|
+ return internalAddr.substring(0,lastCol); //HACK!
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private void processPacket(Packet p)
|
|
|
+ {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+}
|