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

Client Server Protocol-Design OK #44

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions specs/zaccariach-NR09/.idea/$PRODUCT_WORKSPACE_FILE$

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions specs/zaccariach-NR09/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions specs/zaccariach-NR09/.idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions specs/zaccariach-NR09/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions specs/zaccariach-NR09/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions specs/zaccariach-NR09/PROTOCOL.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Exercise Protocol Design - Phase 1
Auteur : Christian Zaccaria & Nenad Rajic
Date : 18.03.2020

## What transport protocol do we use?
TCP
## How does the client find the server (addresses and ports)?

Le port est fixé à 3385 et actuellement uniquement par localhost

## Who speaks first?
Le serveur en proposant les différentes actions au client.

## What is the sequence of messages exchanged by the client and the server? (flow)

Le serveur initie la communication et envoie la liste des actions proposées. En cas d'erreur, le serveur notifie le client afin de pouvoir retaper sa commande.

## What happens when a message is received from the other party? (semantics)

Client : Lit le résultat.

Serveur : Calcul des 2 opérandes et envoi du résultat.

## What is the syntax of the messages? How we generate and parse them? (syntax)

Serveur envoi la liste des différentes commandes possible (String)

Client répond : [_OPERANDE NOMBRE1 NOMBRE2_]

Serveur récupére le resultat, le parse, effectue le calcul et re-envoie le résultat sous forme : [_RESULTAT_]

Le split de chaque information s'effectue au niveau des _WHITESPACES_.

## Who closes the connection and when?

Le client peut arrêter la connexion avec _STOP_. Quand au serveur il reste en écoute tout le temps afin de pouvoir directement intitié la connexion avec d'autres clients.
67 changes: 67 additions & 0 deletions specs/zaccariach-NR09/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?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>zaccariach-NR09</groupId>
<artifactId>zaccariach-NR09</artifactId>
<version>1.0-SNAPSHOT</version>

<build>
<plugins>
<plugin>
<!-- Compile the classes -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>7</source>
<target>7</target>
</configuration>

<version>3.8.1</version>
</plugin>
<!-- Build an executable JAR -->
<!--<plugin>

<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>tales.Story</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
-->
<!-- Include dependencies into Uber JAR -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<!-- add Main-Class to manifest file -->
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>tales.Story</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>

</plugins>
</build>
</project>

31 changes: 31 additions & 0 deletions specs/zaccariach-NR09/src/main/java/client/ApplicationClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Authors : Christian Zaccaria & Nenad Rajic
* Date 26.03.2020
* Exercice Protocol-Design
* File : ApplicationClient.java
*/
package client;

import protocol.Protocol;

import java.util.Scanner;

public class ApplicationClient {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Client client = new Client();
String request = "";
client.connect(args[0], Protocol.DEFAULT_PORT, "User");
boolean closeConnection = false;
do{
request = sc.nextLine();
//If user wants to stop connection
if(request.compareTo("STOP") == 0){
closeConnection = true;
}
client.calculate(request);
}while(!closeConnection);
client.disconnect();
}
}
131 changes: 131 additions & 0 deletions specs/zaccariach-NR09/src/main/java/client/Client.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/**
* Authors : Christian Zaccaria & Nenad Rajic
* Date 26.03.2020
* Exercice Protocol-Design
* File : Client.java
*/
package client;

import protocol.Protocol;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Client {

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

private int defaultPort;

/**
* Constructor of Client class, define the default port
*/
public Client(){
defaultPort = Protocol.DEFAULT_PORT;
}

Socket clientSocket = null;
BufferedReader reader = null;
PrintWriter writer = null;
boolean connected = false;
String userName;

/**
* This inner class implements the Runnable interface, so that the run()
* method can execute on its own thread. This method reads data sent from the
* server, line by line, until the connection is closed or lost.
*/
class NotificationListener implements Runnable {

@Override
public void run() {
String notification;
try {
while ((connected && (notification = reader.readLine()) != null)) {
LOG.log(Level.INFO, "Server notification for {1}: {0}", new Object[]{notification,userName});
}
} catch (IOException e) {
LOG.log(Level.SEVERE, "Connection problem in client used by {1}: {0}", new Object[]{e.getMessage(),userName});
connected = false;
} finally {
cleanup();
}
}
}


/**
* This method is used to connect to the server and to inform the server that
* the user "behind" the client has a name (in other words, the HELLO command
* is issued after successful connection).
*
* @param serverAddress the IP address used by the Presence Server
* @param serverPort the port used by the Presence Server
*/
public void connect(String serverAddress, int serverPort, String userName) {
try {
clientSocket = new Socket(serverAddress, serverPort);
reader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
writer = new PrintWriter(clientSocket.getOutputStream());
connected = true;
this.userName = userName;
} catch (IOException e) {
LOG.log(Level.SEVERE, "Unable to connect to server: {0}", e.getMessage());
cleanup();
return;
}
// Let us start a thread, so that we can listen for server notifications
new Thread(new NotificationListener()).start();

}

/**
* Request to calculate
* @param request
*/
public void calculate(String request){
writer.println(request);
writer.flush();
}

/**
* Disconnection with the server
*/
public void disconnect() {
LOG.log(Level.INFO, "{0} has requested to be disconnected.", userName);
connected = false;
writer.println("BYE");
cleanup();
}

/**
* Clean all stuff used to conmmunicate with the server
*/
private void cleanup() {

try {
if (reader != null) {
reader.close();
}
} catch (IOException ex) {
LOG.log(Level.SEVERE, ex.getMessage(), ex);
}

if (writer != null) {
writer.close();
}

try {
if (clientSocket != null) {
clientSocket.close();
}
} catch (IOException ex) {
LOG.log(Level.SEVERE, ex.getMessage(), ex);
}
}
}
15 changes: 15 additions & 0 deletions specs/zaccariach-NR09/src/main/java/protocol/Protocol.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Authors : Christian Zaccaria & Nenad Rajic
* Date 26.03.2020
* Exercice Protocol-Design
* File : Protocol.java
*/
package protocol;

public class Protocol {
public static final String ADD = "ADD";
public static final String SUB = "SUB";
public static final String MULTI = "MULTIPLY";
public static final String DIVIDE = "DIV";
public static final int DEFAULT_PORT = 3385;
}
15 changes: 15 additions & 0 deletions specs/zaccariach-NR09/src/main/java/server/ApplicationServer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Authors : Christian Zaccaria & Nenad Rajic
* Date 26.03.2020
* Exercice Protocol-Design
* File : ApplicationServer.java
*/
package server;

public class ApplicationServer {

public static void main(String[] args){
Server server = new Server();
server.start();
}
}
Loading