From c2d5e0fc100ebc91aab352eec0d2f566c0f1a7d1 Mon Sep 17 00:00:00 2001
From: Kevin Jordil <31761059+KevinJordil@users.noreply.github.com>
Date: Fri, 19 Mar 2021 11:47:11 +0100
Subject: [PATCH 1/3] Add my specifications
---
specs/KevinJordil/PROTOCOL.md | 19 +++++++++++++++++++
1 file changed, 19 insertions(+)
create mode 100644 specs/KevinJordil/PROTOCOL.md
diff --git a/specs/KevinJordil/PROTOCOL.md b/specs/KevinJordil/PROTOCOL.md
new file mode 100644
index 0000000..018a632
--- /dev/null
+++ b/specs/KevinJordil/PROTOCOL.md
@@ -0,0 +1,19 @@
+# Specs - Kevin Jordil
+## What transport protocol do we use?
+TCP
+## How does the client find the server (addresses and ports)?
+The IP address of the server is that of the machine, the port must be chosen when launching the server. For the web the default port is 80.
+In order for our client to be able to connect to our server, it must be provided with the address and port of our server in one way or another
+## Who speaks first?
+The client because it is he who needs the server. The server, on the other hand, must already be waiting for a connection
+## What is the sequence of messages exchanged by the client and the server? (flow)
+Once the connection is established, the client can directly send a request to the server (multiplication or addition)
+## What happens when a message is received from the other party? (semantics)
+When the server receives a request it will check that it is compliant and therefore understand it. It will then process it and send the response
+When the client receives a request, it is the response to one of these requests, so it can read the response.
+## What is the syntax of the messages? How we generate and parse them? (syntax)
+The first idea is to do it as an assembly instruction: ADD 10 2 or MULT 2 3
+It is then easy for the server to parse the request with spaces.
+If the calculator was more complex it would have been interesting to use json.
+## Who closes the connection and when?
+The client closes the connection once it has finished doing these operations and therefore when the user closes the calculator.
From 43bb3db17849fe1e1c7154413c5634f557522356 Mon Sep 17 00:00:00 2001
From: Kevin Jordil <31761059+KevinJordil@users.noreply.github.com>
Date: Fri, 19 Mar 2021 13:29:31 +0100
Subject: [PATCH 2/3] Add example in flow
---
specs/KevinJordil/PROTOCOL.md | 13 ++++++++++++-
1 file changed, 12 insertions(+), 1 deletion(-)
diff --git a/specs/KevinJordil/PROTOCOL.md b/specs/KevinJordil/PROTOCOL.md
index 018a632..c85d0e7 100644
--- a/specs/KevinJordil/PROTOCOL.md
+++ b/specs/KevinJordil/PROTOCOL.md
@@ -3,11 +3,22 @@
TCP
## How does the client find the server (addresses and ports)?
The IP address of the server is that of the machine, the port must be chosen when launching the server. For the web the default port is 80.
-In order for our client to be able to connect to our server, it must be provided with the address and port of our server in one way or another
+In order for our client to be able to connect to our server, it must be provided with the address and port of our server in one way or another.
+The port used will be 8080
## Who speaks first?
The client because it is he who needs the server. The server, on the other hand, must already be waiting for a connection
## What is the sequence of messages exchanged by the client and the server? (flow)
Once the connection is established, the client can directly send a request to the server (multiplication or addition)
+SEVER - Wait for connection
+CLIENT - Connect to the server
+SERVER - Accept connection
+CLIENT - Connection established
+CLIENT - ADD 2 3
+SERVER - OK 5
+CLIENT - MULT 3 4
+SERVER - OK 12
+CLIENT - AB CD EF
+SERVER - NOK Unsupported
## What happens when a message is received from the other party? (semantics)
When the server receives a request it will check that it is compliant and therefore understand it. It will then process it and send the response
When the client receives a request, it is the response to one of these requests, so it can read the response.
From 3278308bab032054a26991aedcd63f15d7cacb45 Mon Sep 17 00:00:00 2001
From: KevinJordil <31761059+KevinJordil@users.noreply.github.com>
Date: Sat, 27 Mar 2021 15:55:47 +0100
Subject: [PATCH 3/3] Add Calc Server and client
---
specs/KevinJordil/CalcClient/.idea/.gitignore | 8 +
.../KevinJordil/CalcClient/.idea/compiler.xml | 13 ++
.../CalcClient/.idea/jarRepositories.xml | 20 +++
specs/KevinJordil/CalcClient/.idea/misc.xml | 14 ++
specs/KevinJordil/CalcClient/CalcClient.iml | 2 +
specs/KevinJordil/CalcClient/pom.xml | 41 +++++
.../CalcClient/src/main/java/Client.java | 61 +++++++
specs/KevinJordil/CalcServer/.idea/.gitignore | 8 +
specs/KevinJordil/CalcServer/.idea/.name | 1 +
.../KevinJordil/CalcServer/.idea/compiler.xml | 13 ++
.../CalcServer/.idea/jarRepositories.xml | 20 +++
specs/KevinJordil/CalcServer/.idea/misc.xml | 14 ++
specs/KevinJordil/CalcServer/CalcServer.iml | 2 +
specs/KevinJordil/CalcServer/pom.xml | 41 +++++
.../CalcServer/src/main/java/Server.java | 165 ++++++++++++++++++
15 files changed, 423 insertions(+)
create mode 100644 specs/KevinJordil/CalcClient/.idea/.gitignore
create mode 100644 specs/KevinJordil/CalcClient/.idea/compiler.xml
create mode 100644 specs/KevinJordil/CalcClient/.idea/jarRepositories.xml
create mode 100644 specs/KevinJordil/CalcClient/.idea/misc.xml
create mode 100644 specs/KevinJordil/CalcClient/CalcClient.iml
create mode 100644 specs/KevinJordil/CalcClient/pom.xml
create mode 100644 specs/KevinJordil/CalcClient/src/main/java/Client.java
create mode 100644 specs/KevinJordil/CalcServer/.idea/.gitignore
create mode 100644 specs/KevinJordil/CalcServer/.idea/.name
create mode 100644 specs/KevinJordil/CalcServer/.idea/compiler.xml
create mode 100644 specs/KevinJordil/CalcServer/.idea/jarRepositories.xml
create mode 100644 specs/KevinJordil/CalcServer/.idea/misc.xml
create mode 100644 specs/KevinJordil/CalcServer/CalcServer.iml
create mode 100644 specs/KevinJordil/CalcServer/pom.xml
create mode 100644 specs/KevinJordil/CalcServer/src/main/java/Server.java
diff --git a/specs/KevinJordil/CalcClient/.idea/.gitignore b/specs/KevinJordil/CalcClient/.idea/.gitignore
new file mode 100644
index 0000000..e333363
--- /dev/null
+++ b/specs/KevinJordil/CalcClient/.idea/.gitignore
@@ -0,0 +1,8 @@
+# Default ignored files
+/shelf/
+/workspace.xml
+# Datasource local storage ignored files
+/../../../../../../../:\HEIG\Semestre_4\RES\Labos\CalcClient\.idea/dataSources/
+/dataSources.local.xml
+# Editor-based HTTP Client requests
+/httpRequests/
diff --git a/specs/KevinJordil/CalcClient/.idea/compiler.xml b/specs/KevinJordil/CalcClient/.idea/compiler.xml
new file mode 100644
index 0000000..1ac2c7f
--- /dev/null
+++ b/specs/KevinJordil/CalcClient/.idea/compiler.xml
@@ -0,0 +1,13 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/specs/KevinJordil/CalcClient/.idea/jarRepositories.xml b/specs/KevinJordil/CalcClient/.idea/jarRepositories.xml
new file mode 100644
index 0000000..712ab9d
--- /dev/null
+++ b/specs/KevinJordil/CalcClient/.idea/jarRepositories.xml
@@ -0,0 +1,20 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/specs/KevinJordil/CalcClient/.idea/misc.xml b/specs/KevinJordil/CalcClient/.idea/misc.xml
new file mode 100644
index 0000000..d24ea8e
--- /dev/null
+++ b/specs/KevinJordil/CalcClient/.idea/misc.xml
@@ -0,0 +1,14 @@
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/specs/KevinJordil/CalcClient/CalcClient.iml b/specs/KevinJordil/CalcClient/CalcClient.iml
new file mode 100644
index 0000000..78b2cc5
--- /dev/null
+++ b/specs/KevinJordil/CalcClient/CalcClient.iml
@@ -0,0 +1,2 @@
+
+
\ No newline at end of file
diff --git a/specs/KevinJordil/CalcClient/pom.xml b/specs/KevinJordil/CalcClient/pom.xml
new file mode 100644
index 0000000..cc07803
--- /dev/null
+++ b/specs/KevinJordil/CalcClient/pom.xml
@@ -0,0 +1,41 @@
+
+
+ 4.0.0
+
+ org.example
+ CalcClient
+ 1.0-SNAPSHOT
+
+
+ 11
+ 11
+
+
+
+
+
+ org.apache.maven.plugins
+ maven-shade-plugin
+ 2.3
+
+
+ package
+
+ shade
+
+
+
+
+ Client
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/specs/KevinJordil/CalcClient/src/main/java/Client.java b/specs/KevinJordil/CalcClient/src/main/java/Client.java
new file mode 100644
index 0000000..748d60a
--- /dev/null
+++ b/specs/KevinJordil/CalcClient/src/main/java/Client.java
@@ -0,0 +1,61 @@
+import java.io.*;
+import java.net.Socket;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+public class Client {
+
+ static final Logger LOG = Logger.getLogger(Client.class.getName());
+ static final String host = "localhost";
+ static final int port = 2424;
+
+ public static void main(String[] args) {
+ Socket clientSocket = null;
+ PrintWriter out = null;
+ BufferedReader in = null;
+ BufferedReader userInput = null;
+
+ try {
+ clientSocket = new Socket(host, port);
+ in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
+ out = new PrintWriter(clientSocket.getOutputStream());
+ userInput = new BufferedReader(new InputStreamReader(System.in));
+
+ String input = "";
+ while(!input.equals("END")){
+ input = userInput.readLine();
+ out.println(input);
+ out.flush();
+ LOG.log(Level.INFO, "Response :" + in.readLine());
+ }
+ } catch (IOException ex) {
+ LOG.log(Level.SEVERE, null, ex);
+ } finally {
+ try {
+ assert in != null;
+ in.close();
+ } catch (Exception ex) {
+ Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
+ }
+ try {
+ assert out != null;
+ out.close();
+ } catch (Exception ex) {
+ Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
+ }
+ try {
+ assert userInput != null;
+ userInput.close();
+ } catch (Exception ex) {
+ Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
+ }
+ try {
+ clientSocket.close();
+ } catch (Exception ex) {
+ Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
+ }
+ }
+
+ }
+
+}
diff --git a/specs/KevinJordil/CalcServer/.idea/.gitignore b/specs/KevinJordil/CalcServer/.idea/.gitignore
new file mode 100644
index 0000000..c82f609
--- /dev/null
+++ b/specs/KevinJordil/CalcServer/.idea/.gitignore
@@ -0,0 +1,8 @@
+# Default ignored files
+/shelf/
+/workspace.xml
+# Datasource local storage ignored files
+/../../../../../../../:\HEIG\Semestre_4\RES\Labos\Labo3\.idea/dataSources/
+/dataSources.local.xml
+# Editor-based HTTP Client requests
+/httpRequests/
diff --git a/specs/KevinJordil/CalcServer/.idea/.name b/specs/KevinJordil/CalcServer/.idea/.name
new file mode 100644
index 0000000..c1a3659
--- /dev/null
+++ b/specs/KevinJordil/CalcServer/.idea/.name
@@ -0,0 +1 @@
+CalcServer
\ No newline at end of file
diff --git a/specs/KevinJordil/CalcServer/.idea/compiler.xml b/specs/KevinJordil/CalcServer/.idea/compiler.xml
new file mode 100644
index 0000000..9104af1
--- /dev/null
+++ b/specs/KevinJordil/CalcServer/.idea/compiler.xml
@@ -0,0 +1,13 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/specs/KevinJordil/CalcServer/.idea/jarRepositories.xml b/specs/KevinJordil/CalcServer/.idea/jarRepositories.xml
new file mode 100644
index 0000000..712ab9d
--- /dev/null
+++ b/specs/KevinJordil/CalcServer/.idea/jarRepositories.xml
@@ -0,0 +1,20 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/specs/KevinJordil/CalcServer/.idea/misc.xml b/specs/KevinJordil/CalcServer/.idea/misc.xml
new file mode 100644
index 0000000..d24ea8e
--- /dev/null
+++ b/specs/KevinJordil/CalcServer/.idea/misc.xml
@@ -0,0 +1,14 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/specs/KevinJordil/CalcServer/CalcServer.iml b/specs/KevinJordil/CalcServer/CalcServer.iml
new file mode 100644
index 0000000..78b2cc5
--- /dev/null
+++ b/specs/KevinJordil/CalcServer/CalcServer.iml
@@ -0,0 +1,2 @@
+
+
\ No newline at end of file
diff --git a/specs/KevinJordil/CalcServer/pom.xml b/specs/KevinJordil/CalcServer/pom.xml
new file mode 100644
index 0000000..a7d75f0
--- /dev/null
+++ b/specs/KevinJordil/CalcServer/pom.xml
@@ -0,0 +1,41 @@
+
+
+ 4.0.0
+
+ org.example
+ CalcServer
+ 1.0-SNAPSHOT
+
+
+ 11
+ 11
+
+
+
+
+
+ org.apache.maven.plugins
+ maven-shade-plugin
+ 2.3
+
+
+ package
+
+ shade
+
+
+
+
+ Server
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/specs/KevinJordil/CalcServer/src/main/java/Server.java b/specs/KevinJordil/CalcServer/src/main/java/Server.java
new file mode 100644
index 0000000..372e030
--- /dev/null
+++ b/specs/KevinJordil/CalcServer/src/main/java/Server.java
@@ -0,0 +1,165 @@
+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.logging.Level;
+import java.util.logging.Logger;
+
+public class Server {
+
+ final static Logger LOG = Logger.getLogger(Server.class.getName());
+
+ int port;
+
+ /**
+ * Constructor
+ *
+ * @param port the port to listen on
+ */
+ public Server(int port) {
+ this.port = port;
+ }
+
+ /**
+ * This method initiates the process. The server creates a socket and binds it
+ * to the previously specified port. It then waits for clients in a infinite
+ * loop. When a client arrives, the server will read its input line by line
+ * and send back the data converted to uppercase. This will continue until the
+ * client sends the "BYE" command.
+ */
+ public void serveClients() {
+ LOG.info("Starting the Receptionist Worker on a new thread...");
+ new Thread(new ReceptionistWorker()).start();
+ }
+
+ /**
+ * This inner class implements the behavior of the "receptionist", whose
+ * responsibility is to listen for incoming connection requests. As soon as a
+ * new client has arrived, the receptionist delegates the processing to a
+ * "servant" who will execute on its own thread.
+ */
+ private class ReceptionistWorker implements Runnable {
+
+ @Override
+ public void run() {
+ ServerSocket serverSocket;
+
+ try {
+ serverSocket = new ServerSocket(port);
+ } catch (IOException ex) {
+ LOG.log(Level.SEVERE, null, ex);
+ return;
+ }
+
+ while (true) {
+ LOG.log(Level.INFO, "Waiting (blocking) for a new client on port {0}", port);
+ try {
+ Socket clientSocket = serverSocket.accept();
+ LOG.info("A new client has arrived. Starting a new thread and delegating work to a new servant...");
+ new Thread(new ServantWorker(clientSocket)).start();
+ } catch (IOException ex) {
+ Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
+ }
+ }
+
+ }
+
+ /**
+ * This inner class implements the behavior of the "servants", whose
+ * responsibility is to take care of clients once they have connected. This
+ * is where we implement the application protocol logic, i.e. where we read
+ * data sent by the client and where we generate the responses.
+ */
+ private class ServantWorker implements Runnable {
+
+ Socket clientSocket;
+ BufferedReader in = null;
+ PrintWriter out = null;
+
+ public ServantWorker(Socket clientSocket) {
+ try {
+ this.clientSocket = clientSocket;
+ in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
+ out = new PrintWriter(clientSocket.getOutputStream());
+ } catch (IOException ex) {
+ Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
+ }
+ }
+
+ @Override
+ public void run() {
+ String line;
+
+ try {
+ LOG.info("Reading until client sends BYE or closes the connection...");
+
+ while(true){
+ line = in.readLine();
+ LOG.info("Read : " + line);
+ if (line.equalsIgnoreCase("END")){
+ out.println("BYE");
+ out.flush();
+ break;
+ }
+
+
+ String[] instructions = line.split(" ");
+ if(instructions.length != 3){
+ out.println("ERROR");
+ } else {
+ int nb1 = Integer.parseInt(instructions[1]), nb2 = Integer.parseInt(instructions[2]);
+ String result;
+ switch (instructions[0]){
+ case "ADD":
+ result = Integer.toString(nb1 + nb2);
+ break;
+ case "MULT":
+ result = Integer.toString(nb1 * nb2);
+ break;
+ default:
+ result = "ERROR";
+ }
+ out.println(result);
+ }
+ out.flush();
+ }
+
+ LOG.info("Cleaning up resources...");
+ clientSocket.close();
+ in.close();
+ out.close();
+
+ } catch (IOException ex) {
+ if (in != null) {
+ try {
+ in.close();
+ } catch (IOException ex1) {
+ LOG.log(Level.SEVERE, ex1.getMessage(), ex1);
+ }
+ }
+ if (out != null) {
+ out.close();
+ }
+ if (clientSocket != null) {
+ try {
+ clientSocket.close();
+ } catch (IOException ex1) {
+ LOG.log(Level.SEVERE, ex1.getMessage(), ex1);
+ }
+ }
+ LOG.log(Level.SEVERE, ex.getMessage(), ex);
+ }
+ }
+ }
+ }
+
+ public static void main(String[] args) {
+ System.setProperty("java.util.logging.SimpleFormatter.format", "%5$s %n");
+
+ Server multi = new Server(2424);
+ multi.serveClients();
+ }
+
+}