diff --git a/specs/rya-sge/PROTOCOLE.md b/specs/rya-sge/PROTOCOLE.md new file mode 100644 index 0000000..8266d3c --- /dev/null +++ b/specs/rya-sge/PROTOCOLE.md @@ -0,0 +1,50 @@ + + +1. What transport protocol do we use? + +Il faut utiliser TCP car le client va établir une connexion avec le serveur + +2. How does the client find the server (adresses and ports ) ? + +-Si le serveur a une IP et une adresse fixe, on peut coder en dur dans le code du client + +-Il fait un broadcast avec UDP pour savoir qui offre les services recherchés et le serveur + +lui répond en lui donnant son adresse + +Remarques : Je n'ai pas bien compris comment on pouvait connaitre l'adresse IP du serveur sans découverte dynamique et sans coder en dur son IP(afin de faire tourner les serveurs sur différents machines ) + +- Who speaks first? + +C'est le client qui parle en 1er, il contacte le serveur. Le serveur lui est en attente d'un client. + +- What is the sequence of messages exchanged by the client and the server? (flow) + +Server ---> Client : "Opération disponible : "DIV, MUL, SUB, ADD" + +On peut se passer de l'étape ci-dessus pour simplifier le protocole. + +Client --> Serveur : OPERATION n1 n2" + +Exemple : DIV 1 2 veut dire que le serveur doit faire : 1 / 2 + +Serveur --> Client : RESULT res où res est le résultat + +Client --> Server : FIN ou une autre OPERATION + +- What happens when a message is received from the other party? (semantics) + +L'autre partie récupère le message et le traite + +- What is the syntax of the messages? How we generate and parse them? (syntax) + +Il s'agit de commande, sauf le premier message du serveur qui est une string, il faut récupérer les arguments en fonction de la séquence du message + +- Who closes the connection and when? + +-Le serveur quand le client lui dit "FIN" + +-Le serveur si le délai d'attente est dépassée(par exemple 5 minutes) afin d'éviter que le serveur soit en attente de client. Le délai s'applique dès que le serveur a envoyé un message au client et qu'il attend sa réponse. Cela "empêche"des attaques DDOS, smurf autre qui auraient pour but de monopoliser les ressources du serveur. + +-Le client une fois qu'il a envoyé FIN au serveur + diff --git a/specs/rya-sge/StreamingTimeServer.java b/specs/rya-sge/StreamingTimeServer.java new file mode 100644 index 0000000..f3f0463 --- /dev/null +++ b/specs/rya-sge/StreamingTimeServer.java @@ -0,0 +1,107 @@ +package ch.heigvd.res.examples; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.PrintWriter; +import java.net.ServerSocket; +import java.net.Socket; +import java.util.Date; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * A very simple example of TCP server. When the server starts, it binds a + * server socket on any of the available network interfaces and on port 2205. It + * then waits until one (only one!) client makes a connection request. When the + * client arrives, the server does not even check if the client sends data. It + * simply writes the current time, every second, during 15 seconds. + * + * To test the server, simply open a terminal, do a "telnet localhost 2205" and + * see what you get back. Use Wireshark to have a look at the transmitted TCP + * segments. + * + * @author Olivier Liechti + */ +public class StreamingTimeServer { + + static final Logger LOG = Logger.getLogger(StreamingTimeServer.class.getName()); + + private final int TEST_DURATION = 5000; + private final int PAUSE_DURATION = 1000; + private final int NUMBER_OF_ITERATIONS = TEST_DURATION / PAUSE_DURATION; + private final int LISTEN_PORT = 2205; + + /** + * This method does the entire processing. + */ + private void start() throws Exception { + LOG.info("Starting server..."); + + ServerSocket serverSocket = null; + Socket clientSocket = null; + BufferedReader reader = null; + PrintWriter writer = null; + + LOG.log(Level.INFO, "Creating a server socket and binding it on any of the available network interfaces and on port {0}", new Object[]{Integer.toString(LISTEN_PORT)}); + serverSocket = new ServerSocket(LISTEN_PORT); + logServerSocketAddress(serverSocket); + + while (true) { + LOG.log(Level.INFO, "Waiting (blocking) for a connection request on {0} : {1}", new Object[]{serverSocket.getInetAddress(), Integer.toString(serverSocket.getLocalPort())}); + clientSocket = serverSocket.accept(); + + LOG.log(Level.INFO, "A client has arrived. We now have a client socket with following attributes:"); + logSocketAddress(clientSocket); + + LOG.log(Level.INFO, "Getting a Reader and a Writer connected to the client socket..."); + reader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); + writer = new PrintWriter(clientSocket.getOutputStream()); + + LOG.log(Level.INFO, "Starting my job... sending current time to the client for {0} ms", TEST_DURATION); + for (int i = 0; i < NUMBER_OF_ITERATIONS; i++) { + writer.println(String.format("{'time' : '%s'}", new Date())); + writer.flush(); + LOG.log(Level.INFO, "Sent data to client, doing a pause..."); + Thread.sleep(PAUSE_DURATION); + } + reader.close(); + writer.close(); + clientSocket.close(); + } + } + + /** + * A utility method to print server socket information + * + * @param serverSocket the socket that we want to log + */ + private void logServerSocketAddress(ServerSocket serverSocket) { + LOG.log(Level.INFO, " Local IP address: {0}", new Object[]{serverSocket.getLocalSocketAddress()}); + LOG.log(Level.INFO, " Local port: {0}", new Object[]{Integer.toString(serverSocket.getLocalPort())}); + LOG.log(Level.INFO, " is bound: {0}", new Object[]{serverSocket.isBound()}); + } + + /** + * A utility method to print socket information + * + * @param clientSocket the socket that we want to log + */ + private void logSocketAddress(Socket clientSocket) { + LOG.log(Level.INFO, " Local IP address: {0}", new Object[]{clientSocket.getLocalAddress()}); + LOG.log(Level.INFO, " Local port: {0}", new Object[]{Integer.toString(clientSocket.getLocalPort())}); + LOG.log(Level.INFO, " Remote Socket address: {0}", new Object[]{clientSocket.getRemoteSocketAddress()}); + LOG.log(Level.INFO, " Remote port: {0}", new Object[]{Integer.toString(clientSocket.getPort())}); + } + + /** + * @param args the command line arguments + */ + public static void main(String[] args) throws Exception { + System.setProperty("java.util.logging.SimpleFormatter.format", "%5$s %n"); + + StreamingTimeServer server = new StreamingTimeServer(); + server.start(); + } + +} diff --git a/specs/rya-sge/server/.idea/.gitignore b/specs/rya-sge/server/.idea/.gitignore new file mode 100644 index 0000000..5391f8d --- /dev/null +++ b/specs/rya-sge/server/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/../../../../../../../../../../:\Users\super\switchdrive\HEIG\s4\RES\labo\server\.idea/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/specs/rya-sge/server/.idea/.name b/specs/rya-sge/server/.idea/.name new file mode 100644 index 0000000..093f6f3 --- /dev/null +++ b/specs/rya-sge/server/.idea/.name @@ -0,0 +1 @@ +Client.java \ No newline at end of file diff --git a/specs/rya-sge/server/.idea/misc.xml b/specs/rya-sge/server/.idea/misc.xml new file mode 100644 index 0000000..2a824a2 --- /dev/null +++ b/specs/rya-sge/server/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/specs/rya-sge/server/.idea/modules.xml b/specs/rya-sge/server/.idea/modules.xml new file mode 100644 index 0000000..6adfea6 --- /dev/null +++ b/specs/rya-sge/server/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/specs/rya-sge/server/.idea/uiDesigner.xml b/specs/rya-sge/server/.idea/uiDesigner.xml new file mode 100644 index 0000000..e96534f --- /dev/null +++ b/specs/rya-sge/server/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/specs/rya-sge/server/out/production/server/src/Client.class b/specs/rya-sge/server/out/production/server/src/Client.class new file mode 100644 index 0000000..5874e8d Binary files /dev/null and b/specs/rya-sge/server/out/production/server/src/Client.class differ diff --git a/specs/rya-sge/server/out/production/server/src/ServerThread$ReceptionistWorker.class b/specs/rya-sge/server/out/production/server/src/ServerThread$ReceptionistWorker.class new file mode 100644 index 0000000..9f1f84e Binary files /dev/null and b/specs/rya-sge/server/out/production/server/src/ServerThread$ReceptionistWorker.class differ diff --git a/specs/rya-sge/server/out/production/server/src/ServerThread.class b/specs/rya-sge/server/out/production/server/src/ServerThread.class new file mode 100644 index 0000000..a5e6ec7 Binary files /dev/null and b/specs/rya-sge/server/out/production/server/src/ServerThread.class differ diff --git a/specs/rya-sge/server/out/production/server/src/StreamingTimeServer.class b/specs/rya-sge/server/out/production/server/src/StreamingTimeServer.class new file mode 100644 index 0000000..46a1f6d Binary files /dev/null and b/specs/rya-sge/server/out/production/server/src/StreamingTimeServer.class differ diff --git a/specs/rya-sge/server/server.iml b/specs/rya-sge/server/server.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/specs/rya-sge/server/server.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/specs/rya-sge/server/src/src/Client.java b/specs/rya-sge/server/src/src/Client.java new file mode 100644 index 0000000..ce79adf --- /dev/null +++ b/specs/rya-sge/server/src/src/Client.java @@ -0,0 +1,91 @@ +package src; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.io.PrintWriter; +import java.net.ServerSocket; +import java.net.Socket; +import java.util.logging.Level; +import java.util.logging.Logger; + +public class Client { + + static final Logger LOG = Logger.getLogger(src.StreamingTimeServer.class.getName()); + + private final int TEST_DURATION = 5000; + private final int PAUSE_DURATION = 1000; + private final int NUMBER_OF_ITERATIONS = TEST_DURATION / PAUSE_DURATION; + private final int LISTEN_PORT = 2205; + + /** + * This method does the entire processing. + */ + private void start() throws Exception { + final Socket clientSocket; + final BufferedReader in; + final PrintWriter writer; + /* + * les informations du serveur ( port et adresse IP ou nom d'hote + * 127.0.0.1 est l'adresse local de la machine + */ + clientSocket = new Socket("0.0.0.0", 3101); + LOG.log(Level.INFO, "Creating a client socket and binding it on any of the available network interfaces and on port {0}", new Object[]{Integer.toString(LISTEN_PORT)}); + logSocketAddress(clientSocket); + writer = new PrintWriter(clientSocket.getOutputStream()); + + //flux pour recevoir + in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); + String msg = "Serveur : "; + System.out.println(msg); + while ((msg = in.readLine()) != null) { + System.out.println(msg); + if (msg.equals("FIN")) { + break; + } + } + //Compute + writer.println("COMPUTE ADD 1 2 \n"); + writer.flush(); + msg = in.readLine(); + System.out.println(msg); + + //Error + writer.println("LOL \n"); + writer.flush(); + + msg = in.readLine(); + while(msg.isEmpty()){ + msg = in.readLine(); + } + System.out.println(msg); + //Quitter + writer.println("QUIT \n"); + writer.flush(); + + in.close(); + writer.close(); + clientSocket.close(); + } + + /** + * A utility method to print socket information + * + * @param clientSocket the socket that we want to log + */ + private void logSocketAddress(Socket clientSocket) { + LOG.log(Level.INFO, " Local IP address: {0}", new Object[]{clientSocket.getLocalAddress()}); + LOG.log(Level.INFO, " Local port: {0}", new Object[]{Integer.toString(clientSocket.getLocalPort())}); + LOG.log(Level.INFO, " Remote Socket address: {0}", new Object[]{clientSocket.getRemoteSocketAddress()}); + LOG.log(Level.INFO, " Remote port: {0}", new Object[]{Integer.toString(clientSocket.getPort())}); + } + + /** + * @param args the command line arguments + */ + public static void main(String[] args) throws Exception { + System.setProperty("java.util.logging.SimpleFormatter.format", "%5$s %n"); + + Client client = new Client(); + client.start(); + } +} diff --git a/specs/rya-sge/server/src/src/ServerThread.java b/specs/rya-sge/server/src/src/ServerThread.java new file mode 100644 index 0000000..10ec4c8 --- /dev/null +++ b/specs/rya-sge/server/src/src/ServerThread.java @@ -0,0 +1,276 @@ +package src; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.PrintWriter; +import java.net.ServerSocket; +import java.net.Socket; +import java.util.Date; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * A very simple example of TCP server. When the server starts, it binds a + * server socket on any of the available network interfaces and on port 2205. It + * then waits until one (only one!) client makes a connection request. When the + * client arrives, the server does not even check if the client sends data. It + * simply writes the current time, every second, during 15 seconds. + * + * To test the server, simply open a terminal, do a "telnet localhost 2205" and + * see what you get back. Use Wireshark to have a look at the transmitted TCP + * segments. + * + * @author Olivier Liechti + */ +public class ServerThread { + + static final Logger LOG = Logger.getLogger(StreamingTimeServer.class.getName()); + + private final int TEST_DURATION = 5000; + private final int PAUSE_DURATION = 1000; + private final int NUMBER_OF_ITERATIONS = TEST_DURATION / PAUSE_DURATION; + private final int LISTEN_PORT = 3101; + private String[] availableOperations = {"ADD", "MULT", "DIV", "END OPERATIONS"}; + private int[] nbreOfOperands = {2,2,2}; + + public void serveClients() { + LOG.info("Starting the Receptionist Worker on a new thread..."); + new Thread(new ReceptionistWorker()).start(); + } + + + + private class ReceptionistWorker implements Runnable { + @Override + public void run() { + ServerSocket serverSocket; + Socket clientSocket = null; + BufferedReader reader = null; + PrintWriter writer = null; + LOG.log(Level.INFO, "Creating a server socket and binding it on any of the available network interfaces and on port {0}", new Object[]{Integer.toString(LISTEN_PORT)}); + + try { + serverSocket = new ServerSocket(LISTEN_PORT); + logServerSocketAddress(serverSocket); + } catch (IOException ex) { + LOG.log(Level.SEVERE, null, ex); + return; + } + + while (true) { + + LOG.log(Level.INFO, "Waiting (blocking) for a connection request on {0} : {1}", new Object[]{serverSocket.getInetAddress(), Integer.toString(serverSocket.getLocalPort())}); + try { + clientSocket = serverSocket.accept(); + } catch (IOException e) { + e.printStackTrace(); + } + + LOG.log(Level.INFO, "A client has arrived. We now have a client socket with following attributes:"); + logSocketAddress(clientSocket); + + LOG.log(Level.INFO, "Getting a Reader and a Writer connected to the client socket..."); + try { + try { + writer = new PrintWriter(clientSocket.getOutputStream()); + reader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); + String str = "WELCOME \n"; + for (int i = 0; i < availableOperations.length; ++i) { + String nbreOperands = ""; + if (i < nbreOfOperands.length) { + nbreOperands = Integer.toString(nbreOfOperands[i]) + ' '; + } + + str += "- " + availableOperations[i] + ' ' + nbreOperands + '\n'; + } + writer.println(str + "FIN"); + writer.flush(); + //Thread.sleep(PAUSE_DURATION); + + //LOG.log(Level.INFO, "Starting my job... sending current time to the client for {0} ms", TEST_DURATION); + String msg = "Client : "; + System.out.println(msg); + while (true) { + msg = reader.readLine(); + while (msg.isEmpty()) { + msg = reader.readLine(); + + } + + + System.out.println(msg); + String arg[] = msg.split(" "); + boolean error = false; + if (arg[0].equals("COMPUTE")) { + int calcul = 0; + if (arg[1].equals("ADD")) { + calcul = Integer.parseInt(arg[2]) + Integer.parseInt(arg[3]); + } else if (arg[1].equals("SUB")) { + calcul = Integer.parseInt(arg[2]) - Integer.parseInt(arg[3]); + } else if (arg[1].equals("DIV")) { + calcul = Integer.parseInt(arg[2]) / Integer.parseInt(arg[3]); + } else if (arg[1].equals("MUL")) { + calcul = Integer.parseInt(arg[2]) * Integer.parseInt(arg[3]); + } else { + writer.println("ERROR \n"); + writer.flush(); + error = true; + } + if (!error) { + writer.println("RESULT " + calcul + " \n"); + } + writer.flush(); + + } else if (arg[0].equals("QUIT")) { + reader.close(); + writer.close(); + clientSocket.close(); + LOG.log(Level.INFO, "Fin de la connexion avec le client", TEST_DURATION); + break; + } else { + writer.println("ERROR \n"); + writer.flush(); + } + } + + } finally { + reader.close(); + writer.close(); + clientSocket.close(); + } +}catch (IOException ex) { + Logger.getLogger(ServerThread.class.getName()).log(Level.SEVERE, null, ex); +} + + + } + + } + } + + /** + * This method does the entire processing. + */ + private void start() throws Exception { + LOG.info("Starting server..."); + ServerSocket serverSocket; + Socket clientSocket; + BufferedReader reader = null; + PrintWriter writer = null; + + LOG.log(Level.INFO, "Creating a server socket and binding it on any of the available network interfaces and on port {0}", new Object[]{Integer.toString(LISTEN_PORT)}); + serverSocket = new ServerSocket(LISTEN_PORT); + logServerSocketAddress(serverSocket); + while (true) { + + LOG.log(Level.INFO, "Waiting (blocking) for a connection request on {0} : {1}", new Object[]{serverSocket.getInetAddress(), Integer.toString(serverSocket.getLocalPort())}); + clientSocket = serverSocket.accept(); + + LOG.log(Level.INFO, "A client has arrived. We now have a client socket with following attributes:"); + logSocketAddress(clientSocket); + + LOG.log(Level.INFO, "Getting a Reader and a Writer connected to the client socket..."); + + try{ + writer = new PrintWriter(clientSocket.getOutputStream()); + reader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); + String str = "WELCOME \n"; + for(int i = 0; i < availableOperations.length; ++i){ + String nbreOperands = ""; + if(i < nbreOfOperands.length){ + nbreOperands = Integer.toString(nbreOfOperands[i]) + ' '; + } + + str += "- " + availableOperations[i] + ' ' + nbreOperands + '\n'; + } + writer.println(str + "FIN"); + writer.flush(); + //Thread.sleep(PAUSE_DURATION); + + //LOG.log(Level.INFO, "Starting my job... sending current time to the client for {0} ms", TEST_DURATION); + String msg = "Client : "; + System.out.println(msg); + while(true){ + msg = reader.readLine(); + while(msg.isEmpty()){ + msg = reader.readLine(); + + } + System.out.println(msg); + String arg[] = msg.split(" "); + boolean error = false; + if(arg[0].equals("COMPUTE")){ + int calcul = 0; + if(arg[1].equals("ADD")){ + calcul = Integer.parseInt(arg[2]) + Integer.parseInt(arg[3]); + } else if(arg[1].equals("SUB")){ + calcul = Integer.parseInt(arg[2]) - Integer.parseInt(arg[3]); + }else if(arg[1].equals("DIV")){ + calcul = Integer.parseInt(arg[2]) / Integer.parseInt(arg[3]); + }else if(arg[1].equals("MUL")){ + calcul = Integer.parseInt(arg[2]) * Integer.parseInt(arg[3]); + }else{ + writer.println("ERROR \n"); + writer.flush(); + error = true; + } + if(!error){ + writer.println("RESULT " + calcul + " \n"); + } + writer.flush(); + + }else if(arg[0].equals("QUIT")){ + reader.close(); + writer.close(); + clientSocket.close(); + LOG.log(Level.INFO, "Fin de la connexion avec le client", TEST_DURATION); + break; + }else{ + writer.println("ERROR \n"); + writer.flush(); + } + } + + }finally{ + reader.close(); + writer.close(); + clientSocket.close(); + } + + } + } + + /** + * A utility method to print server socket information + * + * @param serverSocket the socket that we want to log + */ + private void logServerSocketAddress(ServerSocket serverSocket) { + LOG.log(Level.INFO, " Local IP address: {0}", new Object[]{serverSocket.getLocalSocketAddress()}); + LOG.log(Level.INFO, " Local port: {0}", new Object[]{Integer.toString(serverSocket.getLocalPort())}); + LOG.log(Level.INFO, " is bound: {0}", new Object[]{serverSocket.isBound()}); + } + + /** + * A utility method to print socket information + * + * @param clientSocket the socket that we want to log + */ + private void logSocketAddress(Socket clientSocket) { + LOG.log(Level.INFO, " Local IP address: {0}", new Object[]{clientSocket.getLocalAddress()}); + LOG.log(Level.INFO, " Local port: {0}", new Object[]{Integer.toString(clientSocket.getLocalPort())}); + LOG.log(Level.INFO, " Remote Socket address: {0}", new Object[]{clientSocket.getRemoteSocketAddress()}); + LOG.log(Level.INFO, " Remote port: {0}", new Object[]{Integer.toString(clientSocket.getPort())}); + } + + /** + * @param args the command line arguments + */ + public static void main(String[] args) throws Exception { + System.setProperty("java.util.logging.SimpleFormatter.format", "%5$s %n"); + + ServerThread server = new ServerThread(); + server.serveClients(); + } + +} diff --git a/specs/rya-sge/server/src/src/StreamingTimeServer.java b/specs/rya-sge/server/src/src/StreamingTimeServer.java new file mode 100644 index 0000000..5c876ad --- /dev/null +++ b/specs/rya-sge/server/src/src/StreamingTimeServer.java @@ -0,0 +1,161 @@ +package src; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.PrintWriter; +import java.net.ServerSocket; +import java.net.Socket; +import java.util.Date; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * A very simple example of TCP server. When the server starts, it binds a + * server socket on any of the available network interfaces and on port 2205. It + * then waits until one (only one!) client makes a connection request. When the + * client arrives, the server does not even check if the client sends data. It + * simply writes the current time, every second, during 15 seconds. + * + * To test the server, simply open a terminal, do a "telnet localhost 2205" and + * see what you get back. Use Wireshark to have a look at the transmitted TCP + * segments. + * + * @author Olivier Liechti + */ +public class StreamingTimeServer { + + static final Logger LOG = Logger.getLogger(StreamingTimeServer.class.getName()); + + private final int TEST_DURATION = 5000; + private final int PAUSE_DURATION = 1000; + private final int NUMBER_OF_ITERATIONS = TEST_DURATION / PAUSE_DURATION; + private final int LISTEN_PORT = 3101; + private String[] availableOperations = {"ADD", "MULT", "DIV", "END OPERATIONS"}; + private int[] nbreOfOperands = {2,2,2}; + + /** + * This method does the entire processing. + */ + private void start() throws Exception { + LOG.info("Starting server..."); + ServerSocket serverSocket; + Socket clientSocket; + BufferedReader reader = null; + PrintWriter writer = null; + + LOG.log(Level.INFO, "Creating a server socket and binding it on any of the available network interfaces and on port {0}", new Object[]{Integer.toString(LISTEN_PORT)}); + serverSocket = new ServerSocket(LISTEN_PORT); + logServerSocketAddress(serverSocket); + while (true) { + + LOG.log(Level.INFO, "Waiting (blocking) for a connection request on {0} : {1}", new Object[]{serverSocket.getInetAddress(), Integer.toString(serverSocket.getLocalPort())}); + clientSocket = serverSocket.accept(); + + LOG.log(Level.INFO, "A client has arrived. We now have a client socket with following attributes:"); + logSocketAddress(clientSocket); + + LOG.log(Level.INFO, "Getting a Reader and a Writer connected to the client socket..."); + + try{ + writer = new PrintWriter(clientSocket.getOutputStream()); + reader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); + String str = "WELCOME \n"; + for(int i = 0; i < availableOperations.length; ++i){ + String nbreOperands = ""; + if(i < nbreOfOperands.length){ + nbreOperands = Integer.toString(nbreOfOperands[i]) + ' '; + } + + str += "- " + availableOperations[i] + ' ' + nbreOperands + '\n'; + } + writer.println(str + "FIN"); + writer.flush(); + //Thread.sleep(PAUSE_DURATION); + + //LOG.log(Level.INFO, "Starting my job... sending current time to the client for {0} ms", TEST_DURATION); + String msg = "Client : "; + System.out.println(msg); + while(true){ + msg = reader.readLine(); + while(msg.isEmpty()){ + msg = reader.readLine(); + + } + System.out.println(msg); + String arg[] = msg.split(" "); + boolean error = false; + if(arg[0].equals("COMPUTE")){ + int calcul = 0; + if(arg[1].equals("ADD")){ + calcul = Integer.parseInt(arg[2]) + Integer.parseInt(arg[3]); + } else if(arg[1].equals("SUB")){ + calcul = Integer.parseInt(arg[2]) - Integer.parseInt(arg[3]); + }else if(arg[1].equals("DIV")){ + calcul = Integer.parseInt(arg[2]) / Integer.parseInt(arg[3]); + }else if(arg[1].equals("MUL")){ + calcul = Integer.parseInt(arg[2]) * Integer.parseInt(arg[3]); + }else{ + writer.println("ERROR \n"); + writer.flush(); + error = true; + } + if(!error){ + writer.println("RESULT " + calcul + " \n"); + } + writer.flush(); + + }else if(arg[0].equals("QUIT")){ + reader.close(); + writer.close(); + clientSocket.close(); + LOG.log(Level.INFO, "Fin de la connexion avec le client", TEST_DURATION); + break; + }else{ + writer.println("ERROR \n"); + writer.flush(); + } + } + + }finally{ + reader.close(); + writer.close(); + clientSocket.close(); + } + + } + } + + /** + * A utility method to print server socket information + * + * @param serverSocket the socket that we want to log + */ + private void logServerSocketAddress(ServerSocket serverSocket) { + LOG.log(Level.INFO, " Local IP address: {0}", new Object[]{serverSocket.getLocalSocketAddress()}); + LOG.log(Level.INFO, " Local port: {0}", new Object[]{Integer.toString(serverSocket.getLocalPort())}); + LOG.log(Level.INFO, " is bound: {0}", new Object[]{serverSocket.isBound()}); + } + + /** + * A utility method to print socket information + * + * @param clientSocket the socket that we want to log + */ + private void logSocketAddress(Socket clientSocket) { + LOG.log(Level.INFO, " Local IP address: {0}", new Object[]{clientSocket.getLocalAddress()}); + LOG.log(Level.INFO, " Local port: {0}", new Object[]{Integer.toString(clientSocket.getLocalPort())}); + LOG.log(Level.INFO, " Remote Socket address: {0}", new Object[]{clientSocket.getRemoteSocketAddress()}); + LOG.log(Level.INFO, " Remote port: {0}", new Object[]{Integer.toString(clientSocket.getPort())}); + } + + /** + * @param args the command line arguments + */ + public static void main(String[] args) throws Exception { + System.setProperty("java.util.logging.SimpleFormatter.format", "%5$s %n"); + + StreamingTimeServer server = new StreamingTimeServer(); + server.start(); + } + +}