diff --git a/java_serv/src/ConnexionFluxWebcam.java b/java_serv/src/ConnexionFluxWebcam.java index 1d28d88..b08077f 100644 --- a/java_serv/src/ConnexionFluxWebcam.java +++ b/java_serv/src/ConnexionFluxWebcam.java @@ -5,8 +5,6 @@ import java.io.PrintWriter; import java.net.Socket; import java.net.UnknownHostException; -import java.nio.CharBuffer; -import java.util.ArrayList; import java.util.concurrent.TimeUnit; /** diff --git a/java_serv/src/ConnexionJava.java b/java_serv/src/ConnexionJava.java index 5cdc4a1..743402d 100644 --- a/java_serv/src/ConnexionJava.java +++ b/java_serv/src/ConnexionJava.java @@ -1,90 +1,88 @@ -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.io.PrintWriter; -import java.net.Socket; -import java.util.concurrent.TimeUnit; - -import org.json.JSONObject; -/** - * Classe gérant la connexion en java. (Surtout utilisé pour des tests) - * @author lalandef - * - */ - -public class ConnexionJava implements Runnable { - private ServerRobotino serverRobotino; - private Socket socketClient; - private PrintWriter out; - private BufferedReader in; - public ConnexionJava(ServerRobotino serverRobotino, Socket socketClient, String firstLine, BufferedReader in) { - try { - this.out = new PrintWriter(socketClient.getOutputStream(), true); - this.in = in; - out.println("{\"type\":\"init\",\"infoInit\":\"Connection accepté\"}"); - } catch (IOException e) { - e.printStackTrace(); - } - this.serverRobotino=serverRobotino; - this.socketClient=socketClient; - System.out.println("CoJ\tgetIntputStreamServer: "+firstLine); - JSONObject JSON = new JSONObject(firstLine); - try{ - String info = JSON.getString("infoInit"); - System.out.println("CoRobot\tinfo: "+info); - }catch(org.json.JSONException e){ - } - } - - /** - * Initie la connexion et attend les requête de l'application Java - */ - public void run() { - serverRobotino.addConnexionJava(this); - try { - String inLine = ""; - while(this.serverRobotino.isServerRunning()&&inLine!=null){//lecture des nouveau message - inLine = in.readLine(); - System.out.println("CoJ\tgetIntputStreamServer2: "+inLine); - this.decodeurJson(inLine); - } - } catch (IOException e) {/*e.printStackTrace();*/}//connexion fermé - System.out.println("CoJ\ttest fin de conection par rupture de connexion: "); - serverRobotino.removeConnexionJava(this); - } - - /** - * Fonction permettant le decodage du JSON et le traitement selon les params du projet - * appelle la lib JSON pour faire sa magie - * TODO faire autre chose que renvoyer les messages - * @param j - */ - public void decodeurJson(String j) { - try{ - JSONObject JSON = new JSONObject(j); - String type = JSON.getString("type"); - System.out.println("CoJ\ttype:"+type); - - if(type.equals("init")){//inutilisé ici, uniquement au début de la classe connexion - String info = JSON.getString("infoStart"); - System.out.println("CoJ\tinfo: "+info); - - }else if(type.equals("message")){//message - String message = JSON.getString("message"); - System.out.println("CoJ\tMessage: "+message); - } - }catch(org.json.JSONException e){ - System.out.println("CoJ\terreur decodage JSON: "+e); - System.out.println("CoJ\tJSON: "+j); - } - } - - /** - * Envoie un message JSON aux autre utilisateur(Un ou plusieur selon ce qui est précisé dans le message) - * @param m message JSON à envoyer - */ - public void envoyerMessage(String m){ - out.println(m); - } - -} +import java.io.BufferedReader; +import java.io.IOException; +import java.io.PrintWriter; +import java.net.Socket; + +import org.json.JSONObject; +/** + * Classe gérant la connexion en java. (Surtout utilisé pour des tests) + * @author lalandef + * + */ + +public class ConnexionJava implements Runnable { + private ServerRobotino serverRobotino; + private Socket socketClient; + private PrintWriter out; + private BufferedReader in; + public ConnexionJava(ServerRobotino serverRobotino, Socket socketClient, String firstLine, BufferedReader in) { + try { + this.out = new PrintWriter(socketClient.getOutputStream(), true); + this.in = in; + out.println("{\"type\":\"init\",\"infoInit\":\"Connection accepté\"}"); + } catch (IOException e) { + e.printStackTrace(); + } + this.serverRobotino=serverRobotino; + this.socketClient=socketClient; + System.out.println("CoJ\tgetIntputStreamServer: "+firstLine); + JSONObject JSON = new JSONObject(firstLine); + try{ + String info = JSON.getString("infoInit"); + System.out.println("CoRobot\tinfo: "+info); + }catch(org.json.JSONException e){ + } + } + + /** + * Initie la connexion et attend les requête de l'application Java + */ + public void run() { + serverRobotino.addConnexionJava(this); + try { + String inLine = ""; + while(this.serverRobotino.isServerRunning()&&inLine!=null){//lecture des nouveau message + inLine = in.readLine(); + System.out.println("CoJ\tgetIntputStreamServer2: "+inLine); + this.decodeurJson(inLine); + } + } catch (IOException e) {/*e.printStackTrace();*/}//connexion fermé + System.out.println("CoJ\ttest fin de conection par rupture de connexion: "); + serverRobotino.removeConnexionJava(this); + } + + /** + * Fonction permettant le decodage du JSON et le traitement selon les params du projet + * appelle la lib JSON pour faire sa magie + * TODO faire autre chose que renvoyer les messages + * @param j + */ + public void decodeurJson(String j) { + try{ + JSONObject JSON = new JSONObject(j); + String type = JSON.getString("type"); + System.out.println("CoJ\ttype:"+type); + + if(type.equals("init")){//inutilisé ici, uniquement au début de la classe connexion + String info = JSON.getString("infoStart"); + System.out.println("CoJ\tinfo: "+info); + + }else if(type.equals("message")){//message + String message = JSON.getString("message"); + System.out.println("CoJ\tMessage: "+message); + } + }catch(org.json.JSONException e){ + System.out.println("CoJ\terreur decodage JSON: "+e); + System.out.println("CoJ\tJSON: "+j); + } + } + + /** + * Envoie un message JSON aux autre utilisateur(Un ou plusieur selon ce qui est précisé dans le message) + * @param m message JSON à envoyer + */ + public void envoyerMessage(String m){ + out.println(m); + } + +} diff --git a/java_serv/src/ConnexionWeb.java b/java_serv/src/ConnexionWeb.java index 0d917e0..57d2dce 100644 --- a/java_serv/src/ConnexionWeb.java +++ b/java_serv/src/ConnexionWeb.java @@ -1,14 +1,12 @@ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; -import java.io.InputStreamReader; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; import java.net.Socket; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.ArrayList; -import java.util.concurrent.TimeUnit; import javax.xml.bind.DatatypeConverter; diff --git a/java_serv/src/ServerRobotino.java b/java_serv/src/ServerRobotino.java index 0d80713..f5e2853 100644 --- a/java_serv/src/ServerRobotino.java +++ b/java_serv/src/ServerRobotino.java @@ -2,24 +2,22 @@ import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; -import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; import java.net.UnknownHostException; import java.util.ArrayList; import java.util.HashMap; -import java.util.Map; -import java.util.concurrent.Semaphore; import org.json.JSONObject; +import picocli.CommandLine.Option; + /** * Classe qui represente le serveur principal. * Tout le monde s'y connecte, il redistribue les messages, * ouvre les connexions qui vont bien toussa. */ -public class ServerRobotino { - private int portServeur; +public class ServerRobotino implements Runnable { private ServerSocket socketServer = null; private ArrayList connexionsJava = new ArrayList(); private ArrayList connexionsRobotino = new ArrayList(); @@ -28,19 +26,21 @@ public class ServerRobotino { public ArrayList connexionsFluxWebcam = new ArrayList(); public ArrayList waitNewConnexionSendFluxWebcams = new ArrayList(); public ArrayList connexionsSensorsDatabase = new ArrayList(); - //public ArrayList connexionsSendFluxWebcam = new ArrayList(); - //private ArrayList connexionsRobotino = new ArrayList(); - public int portDispo = 50010; HashMap mapNameWebcam_Port = new HashMap(); - - //private Thread t1; - private boolean serverRunning = true; - public ServerRobotino(int port) { + private boolean serverRunning; + + //Config stuff + @Option(names = {"--portDisp","-pd"}, description = "Sets which port is available for camera restreaming. Needs to have the following port open as well for multiple cameras (default: ${DEFAULT-VALUE}).") + public int portDispo = 50010; + @Option(names = {"--port","-p"}, description = "Sets on which port the server will listen for incoming connexions (default: ${DEFAULT-VALUE}). ") + private int portServeur = 50008; + + public ServerRobotino() {} + + public void run(){ try { - this.portServeur=port; - //ip = InetAddress.getLocalHost ().getHostAddress (); - //nom = "Server Robotion v1"; socketServer = new ServerSocket(this.portServeur); + serverRunning = true; } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { @@ -48,6 +48,7 @@ public ServerRobotino(int port) { } this.waitNewConnexion(); } + /** * Attend les connexions puis les analyse pour savoir si c'est une connexion web, robotino où autre */ @@ -285,4 +286,12 @@ public synchronized String getLinkVideoRobot(String ipRobot) { //return "193.48.125.71:1337"; return "193.48.125.70:50008"; } + + public int getPortServeur() { + return portServeur; + } + + public void setPortServeur(int portServeur) { + this.portServeur = portServeur; + } } diff --git a/java_serv/src/config.properties b/java_serv/src/config.properties deleted file mode 100644 index c58d34f..0000000 --- a/java_serv/src/config.properties +++ /dev/null @@ -1,7 +0,0 @@ -#Custodia properties -javaPort=50007 -webSocketPort=50007 -#URL of img -imgLinkout=http://tp-epu.univ-savoie.fr/~prospere/img.png -#Where to write image. Remember to escape path -imgWriteTo=E:\\public_html\\img.png diff --git a/java_serv/src/configIO.java b/java_serv/src/configIO.java deleted file mode 100644 index 6cd32ad..0000000 --- a/java_serv/src/configIO.java +++ /dev/null @@ -1,67 +0,0 @@ -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileReader; -import java.io.IOException; -import java.util.Properties; - -/** - * Reads and Writes to config.properties, placed at project root. - * Has a number of handy methods to get quickly stuff. - * Grab with: configIO.getInstance.getJavaPort - * Uses Singleton Pattern. - * (Or whatever properties you need) - * @author prospere - * - */ -public class configIO { - private static File configFile = new File("java_serv/config.properties"); - private static configIO Instance = null; - - private configIO(){} - - private Properties getProps(){ - try { - FileReader reader = new FileReader(configFile); - Properties props = new Properties(); - props.load(reader); - reader.close(); - return props; - } catch (FileNotFoundException ex) { - // file does not exist - ex.printStackTrace(); - } catch (IOException ex) { - // I/O error - ex.printStackTrace(); - } - return null; - } - /** - * Get the java port from the config.properties - * @return int the port for java comms - */ - public static int getJavaPort(){ - String port = configIO.getInstance().getProps().getProperty("javaPort"); - return Integer.parseInt(port); - } - - /** - * Gets the WebSocket port from config.properties - * @return int port for Websocket - */ - public static int getWebsocketPort(){ - String port = configIO.getInstance().getProps().getProperty("websocketPort"); - return Integer.parseInt(port); - } - - public static configIO getInstance(){ - if(Instance!=null){ - return Instance; - } - else{ - Instance = new configIO(); - return Instance; - } - } - - -} diff --git a/java_serv/src/main.java b/java_serv/src/main.java index 2431889..0022482 100644 --- a/java_serv/src/main.java +++ b/java_serv/src/main.java @@ -1,13 +1,15 @@ -/** - * Execute le server - * @author lalandef - * - */ -public class main { - - public static void main(String[] args) { - int port= 50008; //configIO.getJavaPort() ; - ServerRobotino server = new ServerRobotino(port); - } - -} +/** + * Execute le server + * @author lalandef + * + */ +public class main { + + public static void main(String[] args) { + int port= 50008; //configIO.getJavaPort() ; + ServerRobotino server = new ServerRobotino(); + server.setPortServeur(port); + server.run(); + } + +} diff --git a/java_serv/src/main_with_cmd.java b/java_serv/src/main_with_cmd.java new file mode 100644 index 0000000..00d65b4 --- /dev/null +++ b/java_serv/src/main_with_cmd.java @@ -0,0 +1,11 @@ +import picocli.CommandLine; + + +public class main_with_cmd { + + public static void main(String[] args) { + // TODO Auto-generated method stub + CommandLine.run(new ServerRobotino(),args); + } + +} diff --git a/java_serv/src/test1.java b/java_serv/src/test1.java deleted file mode 100644 index ed839d8..0000000 --- a/java_serv/src/test1.java +++ /dev/null @@ -1,50 +0,0 @@ -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.io.PrintWriter; -import java.net.Socket; -import java.util.concurrent.TimeUnit; - -public class test1 { - - public test1() { - // TODO Auto-generated constructor stub - } - public static void main(String[] args) { - PrintWriter out; - BufferedReader in; - Socket clientSocket; - int port=50008; - String ipServer="192.168.56.1";//iplocal - //ipServer="193.48.125.70"; - //ipServer="193.48.125.219"; - //new Thread(new Client(ipServer,port,"C1")).start(); - //Min + (Math.random() * (Max - Min)) - //ClientUtilisateur c1 = new ClientUtilisateur(ipServer,port,"C1_A"+(int)(Math.random() * (100000))); - //new Thread(c1).start(); - try { - clientSocket = new Socket(ipServer, port); - out = new PrintWriter(clientSocket.getOutputStream(), true); - in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); - out.println("{\"type\":\"init\",\"infoInit\":\"Client-->Server demande de connexion\", \"clientName\": \""+""+"\", \"clientType\":\"Java\"}"); - out.println("{\"type\":\"message\",\"message\":\"test1\"}"); - out.println("{\"type\":\"message\",\"message\":\"test2\"}"); - out.println("{\"type\":\"message\",\"message\":\"test3\"}"); - out.println("{\"type\":\"message\",\"message\":\"test4\"}"); - out.println("{\"type\":\"message\",\"message\":\"test5\"}"); - //try {TimeUnit.MILLISECONDS.sleep(3000);} catch (InterruptedException e) {e.printStackTrace();} - String inLine=""; - while(inLine!=null){//&&inLine!=null){//lecture des nouveau message - //try {TimeUnit.MILLISECONDS.sleep(500);} catch (InterruptedException e) {e.printStackTrace();} - inLine = in.readLine(); - System.out.println("client\tgetIntputStreamServer: "+inLine); - } - //clientSocket.close(); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - //System.out.println(""+this.nom+"\tgetOutputStream: "+clientSocket.getOutputStream()); - - } -} \ No newline at end of file diff --git a/pom.xml b/pom.xml index a03066b..7c90900 100644 --- a/pom.xml +++ b/pom.xml @@ -22,29 +22,42 @@ + + maven-assembly-plugin + + + + main_with_cmd + + + + jar-with-dependencies + + + + + make-assembly + package + + single + + + + - - org.java-websocket - Java-WebSocket - 1.3.9 - org.json json 20180813 - + - javax.websocket - javax.websocket-api - 1.1 - provided + info.picocli + picocli + 3.9.5 - - - \ No newline at end of file