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

[TOREVIEW] Client-server Calc #24

Open
wants to merge 9 commits 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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ buildNumber.properties
.mvn/timing.properties
# https://github.com/takari/maven-wrapper#usage-without-binary-jar
.mvn/wrapper/maven-wrapper.jar
alfavre-alfavre/client/.idea
alfavre-alfavre/client/out
alfavre-alfavre/server/.idea
alfavre-alfavre/server/out
88 changes: 88 additions & 0 deletions alfavre-alfavre/PROTOCOL.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# Calculator Protocol
## Authors Alban Favre and Alban Favre
This is a small server-client application: the client sends an arithmetical instruction like ADD 1 1, the server does the calculation, and sends the result back.

### What transport protocol do we use?

TCP

### How does the client find the server (addresses and ports)?

With it's ip address (or 127.0.0.1 if it's on localhost) and port 2020 (that's this year).

### Who speaks first?

the client send the meeting message (START).

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

- client sends start, indicating it's ready to work

- server answers OK

- client sends operator

- server sends ok or nok (nok is not ok)

- client sends lefthand value

- server sends ok or nok

- client sends righthand value

- server sends ok or nok

- server sends result

- client sends if it want to do another operation (nok if it wants to, ok is ok for leaving)

- client stops if ok, do another round if ok

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

the server does the calculation (and the verification, even if it's useless).

the client checks that the value are correct, then sends them to the server for calculation, then it displays the answer.

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

the parsing of double is done with methods from java.util.Scanner and with Double.parseDouble(Double_that_became_a_string_for_the_writer);

the string signals are shared, they are found in the Protocol class

### Who closes the connection and when?

the client can close it's connection: when asked to do another operation, and no is choosen, the server sends an OK which means that the client is ok to be disconnected.

the server is never closed, as another client might emerge and ask something

### Notes

protocol:
```java
public class Protocol {

public static final int CALC_DEFAULT_PORT = 2020;

public static final String EOL = "\n";
public static final String ADD = "ADD";
public static final String SUB = "SUB";
public static final String MUL = "MUL";
public static final String DIV = "DIV";
public static final String OK = "OK";
public static final String NOK = "NOT OK";
public static final String START = "START";

public static final String OP[]={ADD,SUB,MUL,DIV};

}
```

0/0 throws an error (not X/0 as this has at least some sense), this is a choice, as NaN is used to check if the lefthand and righthand are correctly initialized

the input verification is done in both the client and server for the time being ( making the server one redundant)

I don't know how to use maven, therefore there is no pom.xml file

I don't know how to make a shared library out of Protocol.java for both the server and client
11 changes: 11 additions & 0 deletions alfavre-alfavre/client/client.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>
19 changes: 19 additions & 0 deletions alfavre-alfavre/client/src/main/java/ch/heigvd/res/App.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package main.java.ch.heigvd.res;

import java.io.IOException;
import java.net.InetAddress;


public class App {

/**
*
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
Client client = new Client(InetAddress.getLocalHost());
client.start();

}

}
186 changes: 186 additions & 0 deletions alfavre-alfavre/client/src/main/java/ch/heigvd/res/Client.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
package main.java.ch.heigvd.res;


import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;


public class Client {


private Socket serverSocket;
private BufferedReader reader;
private BufferedWriter writer;
private final InetAddress host;
private double leftHand;
private double rightHand;
private String op;
private OutputStream out;
private InputStream in;


/**
* constructor
* @param host
*/
public Client(InetAddress host) {
this.host = host;
}

private static boolean opControl(String op){
for(String s:Protocol.OP){
if(op.equals(s)){
return true;
}
}
return false;
}


private void fillValue(){
boolean isReading;

do {
System.out.println("Instruction (EX: ADD 1 1): ");
System.out.println("ADD SUB MUL DIV only");
System.out.println("All extraneous token will be ignored");
System.out.println("For some reason, you might have to use 1,2 instead of 1.2");
Scanner scanner = new Scanner(System.in);

try {
op = scanner.next();

if(!opControl(op)){
//je devrait laisser ca au serveur
throw new RuntimeException("Incorrect operand");
}

try {
leftHand = scanner.nextDouble();
} catch (Throwable e){
throw new RuntimeException("Incorrect lefthand type: "+e);
}

try {
rightHand = scanner.nextDouble();
} catch (Throwable e){
throw new RuntimeException("Incorrect righthand type: "+e);
}

/*if(!scanner.hasNext()){
throw new RuntimeException("Too many arguments");
}
// THIS DOESN'T WORK
*/

isReading=false;

} catch (Throwable e) {
isReading=true;
System.out.println("scanner failure type: " + e);
}

}while (isReading);


}

private void meeting() throws IOException {
writer.write(Protocol.START + Protocol.EOL);
writer.flush();

if (!reader.readLine().equals(Protocol.OK)) {
throw new IOException("Failed meeting");
}
System.out.println("Connection successful!");
}

private void communication() throws IOException {

writer.write(op + Protocol.EOL);
writer.flush();

if (!reader.readLine().equals(Protocol.OK)) {
throw new IOException("failed op");
}

writer.write(leftHand + Protocol.EOL);
writer.flush();

if (!reader.readLine().equals(Protocol.OK)) {
throw new IOException("failed lefthand");
}

writer.write(rightHand + Protocol.EOL);
writer.flush();

if (!reader.readLine().equals(Protocol.OK)) {
throw new IOException("failed righthand");
}

String result = reader.readLine();
if (result.equals(Protocol.NOK)) {
throw new IOException("failed calculation");
}

System.out.println("the result is: " + result);
}

private boolean nextCalc() throws IOException {
String again;
System.out.println("Do you want to do another calculation?\n0 yes\n1 no");
Scanner s = new Scanner(System.in);

if (s.nextInt() == 0) {
again = Protocol.NOK;

} else {
System.out.println("Actually, we don't care if you entered 1, if it's not 0 then it's no");
again = Protocol.OK;

}
writer.write(again + Protocol.EOL);
writer.flush();
return (again.equals(Protocol.NOK));
}
/**
* run?
* @throws IOException
*/
public void start() throws IOException {


try {
serverSocket = new Socket(host, Protocol.CALC_DEFAULT_PORT);

out = serverSocket.getOutputStream();
in = serverSocket.getInputStream();

writer = new BufferedWriter(new OutputStreamWriter(out, StandardCharsets.UTF_8));
reader = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8));

boolean isRunning=true;

while(isRunning) {

meeting();

fillValue();

communication();

isRunning=nextCalc();

}
} catch (Throwable e) {
System.out.println(e);
} finally {
serverSocket.close();
}
}


}
18 changes: 18 additions & 0 deletions alfavre-alfavre/client/src/main/java/ch/heigvd/res/Protocol.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main.java.ch.heigvd.res;

public class Protocol {

public static final int CALC_DEFAULT_PORT = 2020;

public static final String EOL = "\n";
public static final String ADD = "ADD";
public static final String SUB = "SUB";
public static final String MUL = "MUL";
public static final String DIV = "DIV";
public static final String OK = "OK";
public static final String NOK = "NOT OK";
public static final String START = "START";

public static final String OP[]={ADD,SUB,MUL,DIV};

}
11 changes: 11 additions & 0 deletions alfavre-alfavre/server/server.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>
17 changes: 17 additions & 0 deletions alfavre-alfavre/server/src/main/java/ch/heigvd/res/App.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package main.java.ch.heigvd.res;

import java.io.IOException;

public class App {

/**
*
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {

Server server = new Server();
server.run();
}

}
18 changes: 18 additions & 0 deletions alfavre-alfavre/server/src/main/java/ch/heigvd/res/Protocol.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main.java.ch.heigvd.res;

public class Protocol {

public static final int CALC_DEFAULT_PORT = 2020;

public static final String EOL = "\n";
public static final String ADD = "ADD";
public static final String SUB = "SUB";
public static final String MUL = "MUL";
public static final String DIV = "DIV";
public static final String OK = "OK";
public static final String NOK = "NOT OK";
public static final String START = "START";

public static final String OP[]={ADD,SUB,MUL,DIV};

}
Loading