Parcourir la source

added some code for network remote control of iZpl, (interaction interface
for other programs )

LH il y a 9 ans
Parent
commit
955bcc29c1

+ 126 - 0
iZpl/src/main/java/de/nplusc/izc/iZpl/RCUI/RemoteListener.java

@@ -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)
+    {
+        
+    }
+    
+}

+ 119 - 0
iZpl/src/main/java/de/nplusc/izc/iZpl/RCUI/RemoteUI.java

@@ -0,0 +1,119 @@
+/*
+ * 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.UIPlugin;
+import java.awt.Image;
+import java.io.IOException;
+import java.net.ServerSocket;
+import java.net.Socket;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ *
+ * @author iZc <nplusc.de>
+ */
+public class RemoteUI implements UIPlugin
+{
+    private Thread rt;
+    private List<RemoteListener> connectedClients = new ArrayList<>();
+    @Override
+    public void setAlbumArt(Image i)
+    {
+        //NOP, useless on remote UI
+    }
+
+    @Override
+    public void setTrackLength(int seconds)
+    {
+        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+    }
+
+    @Override
+    public void setPlaybackPositionInSeconds(int seconds)
+    {
+        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+    }
+
+    @Override
+    public void setVisible(boolean visiblility)
+    {
+       
+    }
+
+    @Override
+    public void setTrackName(String trackName)
+    {
+        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+    }
+
+    @Override
+    public void loadPlayListEditScreen()
+    {
+        
+    }
+
+    @Override
+    public void initializeUI()
+    {
+        if(!rt.isAlive())
+        {
+            rt.start();
+        }
+    }
+
+    @Override
+    public String getPluginName()
+    {
+        return "RemoteControl";
+    }
+
+    @Override
+    public void initializePlugin()
+    {
+        rt = new Thread(()->{            
+            try
+            {
+                ServerSocket serverSocket = new ServerSocket(9264);
+                while(true)
+                {
+                    Socket socket = serverSocket.accept();
+                    Thread t = new Thread(()->{
+                        RemoteListener c =  new RemoteListener(this,socket);
+                        connectedClients.add(c);
+                        c.start();
+                        
+                    });
+                    t.setName("protocolParser-"+socket.getRemoteSocketAddress());
+                    t.start();
+                }
+            }
+            catch (IOException ex)
+            {
+                ex.printStackTrace();
+            }
+});
+        rt.setName("RemoConn listener");
+    }
+
+    @Override
+    public void prepareUpgrade()
+    {
+    }
+    
+}

+ 37 - 0
izpl-shared/src/main/java/de/nplusc/izc/iZpl/API/shared/networking/Packet.java

@@ -0,0 +1,37 @@
+/*
+ * 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.API.shared.networking;
+
+import java.util.HashMap;
+
+/**
+ *
+ * @author iZc <nplusc.de>
+ */
+public class Packet
+{
+    private HashMap<String,Object> data;
+
+    public HashMap<String, Object> getData() {
+        return data;
+    }
+
+    public void setData(HashMap<String, Object> data) {
+        this.data = data;
+    }
+    
+}