Change protocol implementation
This commit is contained in:
committed by
Mathis Ginkel
parent
e533262c38
commit
892f0553f6
@@ -0,0 +1,26 @@
|
||||
package ch.unibas.dmi.dbis.cs108.casono.client.network;
|
||||
|
||||
public class ClientParser {
|
||||
public static final String Seperator = " ";
|
||||
|
||||
enum Protocol {}
|
||||
|
||||
public void parseMessage(String msg) {
|
||||
var parts = msg.split(Seperator);
|
||||
|
||||
switch (parts[0]) {
|
||||
case :
|
||||
|
||||
case :
|
||||
|
||||
case :
|
||||
|
||||
case :
|
||||
|
||||
default :
|
||||
System.out.println("Response " + msg + " is not recognized");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,8 @@ import java.io.InputStreamReader;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.net.Socket;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
@@ -30,6 +32,8 @@ public class ClientService {
|
||||
|
||||
private ExecutorService executor;
|
||||
|
||||
public static ArrayList<String> response;
|
||||
|
||||
/**
|
||||
* Creates a new ClientSession with a Socket, a Reader and Writer of the Input- and the Outputstream and a pool of threads to send requests and receive responses.
|
||||
* @param ip : ip-adress of the server
|
||||
@@ -57,8 +61,57 @@ public class ClientService {
|
||||
}
|
||||
|
||||
executor = Executors.newSingleThreadExecutor();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a Request to get all the messages, the other clients sent, and that the client is not currently aware of.
|
||||
*/
|
||||
|
||||
public List<String> getMessage() {
|
||||
return processMessage("GET_MESSAGE");
|
||||
}
|
||||
|
||||
private List<String> processMessage(String message) {
|
||||
List<String> response = new ArrayList<>();
|
||||
sendRequest(() -> {
|
||||
try {
|
||||
output.write(message+"\n");
|
||||
output.flush();
|
||||
String line;
|
||||
while ((line = input.readLine()) != null) {
|
||||
if (line.toLowerCase().startsWith(".")) {
|
||||
break;
|
||||
}
|
||||
response.add(line);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw getRuntimeException(e);
|
||||
}
|
||||
});
|
||||
return response;
|
||||
}
|
||||
|
||||
private void sendRequest(Runnable request) {
|
||||
Future<?> future = executor.submit(request);
|
||||
try {
|
||||
future.get();
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (ExecutionException e) {
|
||||
throw getRuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private static RuntimeException getRuntimeException(Exception e) {
|
||||
Throwable reason = e.getCause();
|
||||
RuntimeException re;
|
||||
if (reason == null) {
|
||||
reason = e;
|
||||
} else if (reason instanceof RuntimeException rte) {
|
||||
re = rte;
|
||||
}
|
||||
re = new RuntimeException(reason);
|
||||
return re;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -67,27 +120,51 @@ public class ClientService {
|
||||
* @return ArrayList<String> : The Response of the Server to be interpreted later
|
||||
*/
|
||||
|
||||
public Future<ArrayList<String>> sendMessage(Message message) {
|
||||
|
||||
public ArrayList<String> sendMessage(Message message) {
|
||||
String time = message.hourTime + ":" + message.minuteTime;
|
||||
|
||||
String request = "";
|
||||
|
||||
if (message.target == null && message.game_id == 0) {
|
||||
request = String.format("SEND_MESSAGE TYPE=GLOBAL GAME=null USER=%s TARGET=null TEXT=%s TIME=%s", message.name, message.getMessage(), time);
|
||||
request = String.format("SEND_MESSAGE TYPE=GLOBAL GAME=null USER=%s TARGET=null TIME=%s TEXT=%s", message.name, time, message.getMessage());
|
||||
}
|
||||
else if (message.target == null) {
|
||||
request = String.format("SEND_MESSAGE TYPE=LOBBY GAME=%d USER=%s TARGET=null TEXT=%s TIME=%s", message.game_id, message.name, message.getMessage(), time);
|
||||
request = String.format("SEND_MESSAGE TYPE=LOBBY GAME=%d USER=%s TARGET=null TIME=%s TEXT=%s", message.game_id, message.name, time, message.getMessage());
|
||||
} else {
|
||||
request = String.format("SEND_MESSAGE TYPE=WHISPER GAME=%d USER=%s TARGET=%s TEXT=%s TIME=%s", message.game_id, message.name, message.target, message.getMessage(), time);
|
||||
request = String.format("SEND_MESSAGE TYPE=WHISPER GAME=%d USER=%s TARGET=%s TIME=%s TEXT=%s", message.game_id, message.name, message.target, time, message.getMessage());
|
||||
}
|
||||
System.out.println("Writing following request: " + request);
|
||||
sendRequest task = new sendRequest(request, input, output);
|
||||
executor.submit(task);
|
||||
return this.response;
|
||||
}
|
||||
|
||||
System.out.println("Writing following request: " + request);
|
||||
|
||||
public ArrayList<String> getState(int game_id) {
|
||||
String request = String.format("GET_STATE GAME=%d", game_id);
|
||||
sendRequest task = new sendRequest(request, input, output);
|
||||
executor.submit(task);
|
||||
return this.response;
|
||||
|
||||
Future<ArrayList<String>> response = executor.submit(task);
|
||||
}
|
||||
|
||||
public ArrayList<String> joinGame(int game_id, String name) {
|
||||
String request = String.format("JOIN GAME=%d NAME=%s", game_id, name);
|
||||
sendRequest task = new sendRequest(request, input, output);
|
||||
executor.submit(task);
|
||||
return response;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param type : Keyword, as one-word description of the action the player did.
|
||||
* @param game_id : ID of the Lobby that the client is currently in
|
||||
* @param value : Dependent on the action of the User (money)
|
||||
* @param name : Username of the player of that Client
|
||||
* @return The Response of the Server -> Action successfull only if response is valid
|
||||
*/
|
||||
|
||||
public ArrayList<String>sendAction(String type, int game_id, String action, int value, String name) {
|
||||
String request = String.format("SEND_ACTION TYPE=%s GAME=%s ACTION=%s VALUE=%d NAME=%s", type, game_id, action, value, name);
|
||||
sendRequest task = new sendRequest(request, input, output);
|
||||
executor.submit(task);
|
||||
return response;
|
||||
}
|
||||
|
||||
@@ -96,7 +173,6 @@ public class ClientService {
|
||||
*/
|
||||
|
||||
public void closeSocket() {
|
||||
|
||||
try {
|
||||
executor.shutdown();
|
||||
input.close();
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
package ch.unibas.dmi.dbis.cs108.casono.client.network;
|
||||
|
||||
public class ResponseException extends Exception {
|
||||
public ResponseException() {
|
||||
super();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -3,11 +3,13 @@ package ch.unibas.dmi.dbis.cs108.casono.client.network;
|
||||
import java.io.BufferedReader;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
import ch.unibas.dmi.dbis.cs108.casono.client.network.ClientService;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
public class sendRequest implements Callable<ArrayList<String>> {
|
||||
public class sendRequest implements Runnable {
|
||||
|
||||
final private String request;
|
||||
final private BufferedReader input;
|
||||
@@ -20,30 +22,26 @@ public class sendRequest implements Callable<ArrayList<String>> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArrayList<String> call() throws Exception {
|
||||
public void run() {
|
||||
try {
|
||||
output.write(request+"\n");
|
||||
output.write(request + "\r\n");
|
||||
output.flush();
|
||||
System.out.println("Writing following request: " + request);
|
||||
//System.out.println("Writing following request: " + request);
|
||||
} catch (Exception e) {
|
||||
System.out.println(e);
|
||||
}
|
||||
|
||||
ArrayList<String> response = new ArrayList<>();
|
||||
|
||||
try {
|
||||
while (true) {
|
||||
String line;
|
||||
while ((line = input.readLine()) != null) {
|
||||
if (line.toLowerCase().startsWith("ok")) {
|
||||
line = input.readLine();
|
||||
if (line.toLowerCase().startsWith("+ok")) {
|
||||
break;
|
||||
}
|
||||
response.add(line);
|
||||
ClientService.response.add(line);
|
||||
}
|
||||
|
||||
System.out.println("Response from server: " + response.get(0));
|
||||
} catch (Exception e) {
|
||||
System.out.println(e);
|
||||
}
|
||||
catch (Exception e) {System.out.println(e);}
|
||||
|
||||
return response;
|
||||
}
|
||||
}
|
||||
@@ -25,6 +25,7 @@ public class ClientServiceTest {
|
||||
public void tearDown() {
|
||||
serverThread.interrupt();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClientService() throws ExecutionException, InterruptedException {
|
||||
ClientService client = new ClientService("localhost", 5000);
|
||||
|
||||
Reference in New Issue
Block a user