MANY UPDATES TO CLIENT/SERVER:Introduced a protocol parser interface and created an implementing class.
Introduced Input-Type Interface which is implemendet by the parameter class. This allowes the clients parser to more easily create an the NTtB-Protocol adhering message. furthermore a ProtocolMessage interface implemented by NTtBFormatMsg abstract class. parseMsg in NTtBProtocolParser
This commit is contained in:
parent
ccf36dff19
commit
7bb1407ef7
@ -8,7 +8,7 @@ public class Client {
|
|||||||
|
|
||||||
private Socket socket;
|
private Socket socket;
|
||||||
private BufferedReader in;
|
private BufferedReader in;
|
||||||
private BufferedWriter out;
|
private BufferedWriter out;
|
||||||
public String userName;
|
public String userName;
|
||||||
|
|
||||||
public Client(Socket socket, String userName) {
|
public Client(Socket socket, String userName) {
|
||||||
@ -25,14 +25,10 @@ public class Client {
|
|||||||
|
|
||||||
public void sendMessage() {
|
public void sendMessage() {
|
||||||
try {
|
try {
|
||||||
out.write(userName);
|
|
||||||
out.newLine();
|
|
||||||
out.flush();
|
|
||||||
|
|
||||||
Scanner sc = new Scanner(System.in);
|
Scanner sc = new Scanner(System.in);
|
||||||
while (socket.isConnected()) {
|
while (socket.isConnected()) {
|
||||||
String msg = sc.nextLine();
|
String msg = sc.nextLine();
|
||||||
out.write(userName + ": " + msg);
|
out.write(userName + "says: " + msg);
|
||||||
out.newLine();
|
out.newLine();
|
||||||
out.flush();
|
out.flush();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,44 @@
|
|||||||
|
package ch.unibas.dmi.dbis.cs108.Multiplayer.Client;
|
||||||
|
|
||||||
|
import ch.unibas.dmi.dbis.cs108.Multiplayer.Protocol.NTtBCommands;
|
||||||
|
import ch.unibas.dmi.dbis.cs108.Multiplayer.Protocol.NTtBFormatMsg;
|
||||||
|
import ch.unibas.dmi.dbis.cs108.Multiplayer.Protocol.NTtBParameter;
|
||||||
|
import ch.unibas.dmi.dbis.cs108.Multiplayer.Protocol.ProtocolParser;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class NTtBProtocolParser implements ProtocolParser {
|
||||||
|
//TODO Possibly bad name, rename to clientMsgParser?
|
||||||
|
public static HashMap<String, NTtBCommands> inputMapper;
|
||||||
|
|
||||||
|
public NTtBProtocolParser(){
|
||||||
|
this.inputMapper = new HashMap<>();
|
||||||
|
this.inputMapper.put("chat",NTtBCommands.CHATG);
|
||||||
|
//TODO by far not done!
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public NTtBFormatMsg parseMsg(String msg) {
|
||||||
|
Scanner sc = new Scanner(msg);
|
||||||
|
ArrayList<String> input = new ArrayList<>();
|
||||||
|
while(sc.hasNext()){
|
||||||
|
input.add(sc.next());
|
||||||
|
}
|
||||||
|
buildProtocolMsg(input);
|
||||||
|
return null;
|
||||||
|
//TODO needs to be finnished
|
||||||
|
}
|
||||||
|
|
||||||
|
private String buildProtocolMsg(ArrayList<String> input) {
|
||||||
|
//TODO
|
||||||
|
String cmd = parseCmd(input.get(0));
|
||||||
|
input.remove(0);
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
private String parseCmd(String s){
|
||||||
|
//TODO
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,5 @@
|
|||||||
|
package ch.unibas.dmi.dbis.cs108.Multiplayer.Protocol;
|
||||||
|
|
||||||
|
public enum NTtBCommands {
|
||||||
|
CRTGM, CHATL, CHATG, CHATP, LEAVG, JOING, VOTEE,
|
||||||
|
}
|
||||||
@ -0,0 +1,9 @@
|
|||||||
|
package ch.unibas.dmi.dbis.cs108.Multiplayer.Protocol;
|
||||||
|
|
||||||
|
public interface NTtBInputType {
|
||||||
|
String msg = null;
|
||||||
|
public String getValue();
|
||||||
|
public void setValue();
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,18 @@
|
|||||||
|
package ch.unibas.dmi.dbis.cs108.Multiplayer.Protocol;
|
||||||
|
|
||||||
|
public class NTtBParameter implements NTtBInputType {
|
||||||
|
String parameterValue;
|
||||||
|
|
||||||
|
public NTtBParameter(String parameterValue) {
|
||||||
|
this.parameterValue = parameterValue;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public String getValue() {
|
||||||
|
return parameterValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setValue() {
|
||||||
|
//Possibly do not need
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,9 @@
|
|||||||
|
package ch.unibas.dmi.dbis.cs108.Multiplayer.Protocol;
|
||||||
|
/*
|
||||||
|
The NightTrainProtocol implements the Communication-Protocol of the
|
||||||
|
"Night Train to Budapest"" game. It acts as an Interface between Client and server. All Client Messages are
|
||||||
|
piped through this protocol, in order for the Server to execute the correct action. It is used by the ClientHandler
|
||||||
|
for this purpose.
|
||||||
|
*/
|
||||||
|
public class NightTrainProtocol {
|
||||||
|
}
|
||||||
@ -1,3 +0,0 @@
|
|||||||
package ch.unibas.dmi.dbis.cs108.Multiplayer.Protocol;
|
|
||||||
|
|
||||||
public enum Protocol { }
|
|
||||||
@ -0,0 +1,6 @@
|
|||||||
|
package ch.unibas.dmi.dbis.cs108.Multiplayer.Protocol;
|
||||||
|
|
||||||
|
public class ProtocolMessage {
|
||||||
|
//TODO ProtocolMessage Implementation
|
||||||
|
//TODO definition of abilities and properties of class
|
||||||
|
}
|
||||||
@ -0,0 +1,8 @@
|
|||||||
|
package ch.unibas.dmi.dbis.cs108.Multiplayer.Protocol;
|
||||||
|
|
||||||
|
import ch.unibas.dmi.dbis.cs108.Multiplayer.Protocol.NTtBFormatMsg;
|
||||||
|
|
||||||
|
public interface ProtocolParser {
|
||||||
|
|
||||||
|
public NTtBFormatMsg parseMsg(String msg);
|
||||||
|
}
|
||||||
@ -5,13 +5,15 @@ import java.net.Socket;
|
|||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
public class ClientHandler implements Runnable{
|
public class ClientHandler implements Runnable {
|
||||||
private String clientUserName;
|
private String clientUserName;
|
||||||
private BufferedWriter out;
|
private BufferedWriter out;
|
||||||
private BufferedReader in;
|
private BufferedReader in;
|
||||||
private Socket socket;
|
private Socket socket;
|
||||||
Scanner sc;
|
Scanner sc;
|
||||||
public static HashSet<ClientHandler> clientHandlers = new HashSet<>();
|
public static HashSet<ClientHandler> clientHandlers = new HashSet<>();
|
||||||
|
public static HashSet<ClientHandler> inGameClients = new HashSet<>();
|
||||||
|
public static HashSet<ClientHandler> ghostClients = new HashSet<>();
|
||||||
|
|
||||||
public ClientHandler(Socket socket) {
|
public ClientHandler(Socket socket) {
|
||||||
try {
|
try {
|
||||||
@ -35,8 +37,9 @@ public class ClientHandler implements Runnable{
|
|||||||
try {
|
try {
|
||||||
|
|
||||||
msg = in.readLine();
|
msg = in.readLine();
|
||||||
if( msg.equals("QUIT")){
|
if( msg.equalsIgnoreCase("QUIT")){
|
||||||
broadcastMessage("Client: " + clientUserName + " has left the Server");
|
broadcastMessage("Client: " + clientUserName + " has left the Server");
|
||||||
|
removeClientHandler();
|
||||||
}
|
}
|
||||||
broadcastMessage(msg);
|
broadcastMessage(msg);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
@ -91,4 +94,8 @@ public class ClientHandler implements Runnable{
|
|||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void decodeMsg(String msg){
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,11 +3,13 @@ package ch.unibas.dmi.dbis.cs108.Multiplayer.Server;
|
|||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.net.ServerSocket;
|
import java.net.ServerSocket;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
|
import java.util.HashSet;
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
public class Server {
|
public class Server {
|
||||||
|
|
||||||
private static final int gamePort = 42069;
|
private static final int gamePort = 42069;
|
||||||
|
private HashSet<ClientHandler> connectedClients = new HashSet<>();
|
||||||
private ServerSocket serverSocket;
|
private ServerSocket serverSocket;
|
||||||
Scanner sc = new Scanner(System.in);
|
Scanner sc = new Scanner(System.in);
|
||||||
|
|
||||||
@ -19,12 +21,10 @@ public class Server {
|
|||||||
try {
|
try {
|
||||||
System.out.println("Port 42069 open on ");
|
System.out.println("Port 42069 open on ");
|
||||||
while (!serverSocket.isClosed()) {
|
while (!serverSocket.isClosed()) {
|
||||||
if(sc.next().equalsIgnoreCase("quit")) {
|
|
||||||
this.closeServerSocket();
|
|
||||||
}
|
|
||||||
Socket socket = serverSocket.accept();
|
Socket socket = serverSocket.accept();
|
||||||
ClientHandler nextClient = new ClientHandler(socket);
|
ClientHandler nextClient = new ClientHandler(socket);
|
||||||
Thread th = new Thread(nextClient);
|
Thread th = new Thread(nextClient);
|
||||||
|
connectedClients.add(nextClient);
|
||||||
th.start();
|
th.start();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user