Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft Specifications #12

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
21 changes: 21 additions & 0 deletions specs/hansArn/PROTOCOL.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Phase 1

- Utilisation du protocole TCP
- On se connecte sur le port 31415 sur localhost
- le client effectue la première requête.
- **Ne pas oublier l'encodage**
- **Les opérations prise en compte**
- **amélioré l'acceptation d'un client**
- **comportement après une erreur**
- La séquence de message envoyé est tel que suit :
- Client : envoie sa requête "nombre<espace>signe<espace>nombre"
- Serveur :
- requête correct effectue le calcul "nombre"
- requête incorrect envoie une erreur "ERROR"
- le client ferme la connexion "BYE" ou le serveur après un timeout "BYE"

# Phase 3

![](/home/jerome/HEIG/Labo/RES/Teaching-HEIGVD-RES-2020-Exercise-Protocol-Design/specs/hansArn/img/PROTOCOL.md.png)


11 changes: 11 additions & 0 deletions specs/hansArn/hansArn.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
Binary file added specs/hansArn/img/PROTOCOL.md.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
41 changes: 41 additions & 0 deletions specs/hansArn/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ch.heigvd.res.examples</groupId>
<artifactId>DumbHttpClient</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>exo client serveur</name>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>standalone</shadedClassifierName> <!-- Can be any name that makes sense -->
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>ch.heigvd.res.examples.DumbHttpClient</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
</project>
37 changes: 37 additions & 0 deletions specs/hansArn/src/Calculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

public class Calculator {
private String sign;
private Float op1;
private Float op2;

/**
* configure for a calcul with two operands
* @param sign operation to do
* @param op1 first operand
* @param op2 second operand
*/
public Calculator(String sign, Float op1, Float op2) {
this.sign = sign;
this.op1 = op1;
this.op2 = op2;
}

/**
* do the operation
* @return result of the operation
*/
public Float Op(){
switch (sign){
case "+":
return op1 + op2;
case "-":
return op1 - op2;
case "/":
return op1 / op2;
case "*":
return op1 * op2;
default:
throw new IllegalArgumentException("the sign is not code yet");
}
}
}
7 changes: 7 additions & 0 deletions specs/hansArn/src/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
public class Main {
public static void main(String[] args) {
Server S = new Server(31415, 10);
//Server S = new Server(31415);
S.serveClients();
}
}
141 changes: 141 additions & 0 deletions specs/hansArn/src/Server.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@

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.net.SocketTimeoutException;
import java.util.concurrent.TimeoutException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Server {
int port;
int timeOut;

/**
* Constructor
* @param port the port to listen on
* @param timeOutInSecond set timeout in seconds
*/
public Server(int port, int timeOutInSecond) {
this.port = port;
this.timeOut = timeOutInSecond * 1000;
}

/**
* Constructor
* @param port the port to listen on
*/
public Server(int port) {
this.port = port;
this.timeOut = 0;
}


public void serveClients() {
ServerSocket serverSocket;
Socket clientSocket = null;
BufferedReader in = null;
PrintWriter out = null;

// test si le port est déjà utilisé
try {
serverSocket = new ServerSocket(port);
} catch (IOException ex) {
System.out.println(ex);
return;
}

while (true) {
try {
// configuration du client
clientSocket = serverSocket.accept();
clientSocket.setSoTimeout(timeOut);
// configuration des IOs
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
out = new PrintWriter(clientSocket.getOutputStream());
String line;
// booléen d'arrêt
boolean shouldRun = true;


while ( (shouldRun) && (line = in.readLine()) != null ) {
// si le client dit bye on ferme la connexion
if (line.equalsIgnoreCase("bye")) {
shouldRun = false;
}else{
out.print("> ");
String[] argumentArray = line.split(" ");
// si moins de 3 arguments on ferme la connexion
if (argumentArray.length != 3){
out.println("ERROR : Bad Use");
out.flush();
clientSocket.close();
in.close();
out.close();
}else {
try {
Calculator C = new Calculator(argumentArray[1], Float.parseFloat(argumentArray[0]), Float.parseFloat(argumentArray[2]));
out.println(C.Op());
out.flush();
}
// dans le cas ou la transformation en float échoue
catch (NumberFormatException e) {
out.println("ERROR : Bad Argument");
out.flush();
clientSocket.close();
in.close();
out.close();

}
// dans le cas d'un signe non implémenté
catch (IllegalArgumentException e) {
out.println("ERROR : Bad Argument");
out.flush();

clientSocket.close();
in.close();
out.close();
}
}
}
}

clientSocket.close();
in.close();
out.close();

} catch (SocketTimeoutException e ){
out.println("Timeout");
out.flush();
try {
clientSocket.close();
in.close();
out.close();
}catch (IOException error){
System.out.println(error);
}
}catch (IOException ex) {
if (in != null) {
try {
in.close();
} catch (IOException ex1) {
System.out.println(ex1);
}
}
if (out != null) {
out.close();
}
if (clientSocket != null) {
try {
clientSocket.close();
} catch (IOException ex1) {
System.out.println(ex1);
}
}
}
}
}
}
84 changes: 84 additions & 0 deletions specs/hansArn/src/main/java/ch/heigvd/res/examples/calcClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package ch.heigvd.res.examples;

import java.io.*;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* This is not really an HTTP client, but rather a very simple program that
* establishes a TCP connection with a real HTTP server. Once connected, the
* client sends "garbage" to the server (the client does not send a proper
* HTTP request that the server would understand). The client then reads the
* response sent back by the server and logs it onto the console.
*
* @author Olivier Liechti
*/
public class calcClient {

static final Logger LOG = Logger.getLogger(calcClient.class.getName());

final static int BUFFER_SIZE = 1024;

/**
* This method does the whole processing
*/
public void sendWrongHttpRequest() {
Socket clientSocket = null;
BufferedReader in = null;
PrintWriter out = null;
Scanner sc = new Scanner(System.in);


try {
clientSocket = new Socket("localhost", 31415);
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
out = new PrintWriter(clientSocket.getOutputStream());


ByteArrayOutputStream responseBuffer = new ByteArrayOutputStream();

String str;
out.println(sc.nextLine());
out.flush();
while ((str = in.readLine()) != null ) {
LOG.log(Level.INFO, str);
if( str.contains("bye"))
break;
out.println(sc.nextLine());
out.flush();
}

LOG.log(Level.INFO, str);
} catch (IOException ex) {
LOG.log(Level.SEVERE, null, ex);
} finally {
try {
in.close();
} catch (IOException ex) {
Logger.getLogger(calcClient.class.getName()).log(Level.SEVERE, null, ex);
}
out.close();
try {
clientSocket.close();
} catch (IOException ex) {
Logger.getLogger(calcClient.class.getName()).log(Level.SEVERE, null, ex);
}
}
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
System.setProperty("java.util.logging.SimpleFormatter.format", "%5$s %n");

calcClient client = new calcClient();
client.sendWrongHttpRequest();

}

}