Connections, Applied Data objects, removed parsers and Made genereric data handling
This commit is contained in:
@@ -1,28 +1,30 @@
|
||||
package netwerkprog.game.server;
|
||||
|
||||
import netwerkprog.game.server.controllers.DataController;
|
||||
import netwerkprog.game.server.controllers.SessionController;
|
||||
import netwerkprog.game.util.data.ParserCallback;
|
||||
import netwerkprog.game.util.data.Data;
|
||||
import netwerkprog.game.util.data.DataCallback;
|
||||
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.net.Socket;
|
||||
|
||||
public class ServerClient implements Runnable, ParserCallback {
|
||||
private DataInputStream in;
|
||||
private DataOutputStream out;
|
||||
public class ServerClient implements Runnable {
|
||||
private ObjectInputStream in;
|
||||
private ObjectOutputStream out;
|
||||
private final String name;
|
||||
private final SessionController server;
|
||||
private final Parser parser;
|
||||
private final DataCallback callback;
|
||||
private boolean isConnected;
|
||||
|
||||
public ServerClient(String name, Socket socket, SessionController server) {
|
||||
public ServerClient(String name, Socket socket, SessionController server, DataController dataController) {
|
||||
this.name = name;
|
||||
this.server = server;
|
||||
this.parser = new Parser(this);
|
||||
this.callback = dataController;
|
||||
try {
|
||||
this.in = new DataInputStream(socket.getInputStream());
|
||||
this.out = new DataOutputStream(socket.getOutputStream());
|
||||
this.in = new ObjectInputStream(socket.getInputStream());
|
||||
this.out = new ObjectOutputStream(socket.getOutputStream());
|
||||
this.isConnected = true;
|
||||
} catch (IOException e) {
|
||||
this.isConnected = false;
|
||||
@@ -30,9 +32,9 @@ public class ServerClient implements Runnable, ParserCallback {
|
||||
}
|
||||
}
|
||||
|
||||
public void writeUTF(String text) {
|
||||
public void writeData(Data data) {
|
||||
try {
|
||||
this.out.writeUTF(text);
|
||||
this.out.writeObject(data);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@@ -42,14 +44,14 @@ public class ServerClient implements Runnable, ParserCallback {
|
||||
public void run() {
|
||||
while (this.isConnected) {
|
||||
try {
|
||||
String received = this.in.readUTF();
|
||||
this.parser.parse(received);
|
||||
|
||||
callback.onDataReceived((Data) this.in.readObject());
|
||||
} catch (IOException e) {
|
||||
System.out.println("[SERVERCLIENT] caught exception - " + e.getMessage());
|
||||
System.out.println("[SERVERCLIENT] terminating failing connection...");
|
||||
this.isConnected = false;
|
||||
System.out.println("[SERVERCLIENT] done!");
|
||||
} catch (ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -57,9 +59,4 @@ public class ServerClient implements Runnable, ParserCallback {
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDataReceived(String data) {
|
||||
writeUTF(data);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user