Finished Server/Client registration and communications
This commit is contained in:
@@ -90,7 +90,7 @@ public class MainGame extends ApplicationAdapter implements DataCallback {
|
||||
// playSong();
|
||||
|
||||
|
||||
// connectToServer();
|
||||
connectToServer();
|
||||
}
|
||||
|
||||
private void initCharacters() {
|
||||
|
||||
@@ -1,21 +1,25 @@
|
||||
package netwerkprog.game.client.game.connections;
|
||||
|
||||
import netwerkprog.game.util.application.Controller;
|
||||
import netwerkprog.game.util.data.ConnectionData;
|
||||
import netwerkprog.game.util.data.Data;
|
||||
import netwerkprog.game.util.data.DataCallback;
|
||||
|
||||
import java.io.*;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.net.Socket;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class Client extends Controller {
|
||||
private final int port;
|
||||
private final String hostname;
|
||||
private boolean isConnected = true;
|
||||
private boolean isConnected;
|
||||
private Socket socket;
|
||||
private Thread receiveThread;
|
||||
private DataCallback callback;
|
||||
private ObjectOutputStream outputStream;
|
||||
private boolean connecting;
|
||||
|
||||
public Client(String hostname, DataCallback callback) {
|
||||
this.port = Data.port();
|
||||
@@ -37,14 +41,15 @@ public class Client extends Controller {
|
||||
*/
|
||||
public void connect() {
|
||||
System.out.println("[CLIENT] connecting to server on port " + this.port);
|
||||
this.connecting = true;
|
||||
try {
|
||||
this.socket = new Socket(this.hostname, this.port);
|
||||
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
|
||||
this.outputStream = new ObjectOutputStream(socket.getOutputStream());
|
||||
|
||||
register(in);
|
||||
this.receiveThread = new Thread( () -> receive(in));
|
||||
|
||||
} catch (IOException e) {
|
||||
this.connecting = false;
|
||||
System.out.println("[CLIENT] there was an error connecting : " + e.getMessage());
|
||||
StringBuilder sb = new StringBuilder(" Stacktrace : ");
|
||||
Arrays.stream(e.getStackTrace()).forEach(n -> sb.append("\t\t").append(n).append("\n"));
|
||||
@@ -52,6 +57,28 @@ public class Client extends Controller {
|
||||
}
|
||||
}
|
||||
|
||||
public void register(ObjectInputStream in) {
|
||||
while (connecting) {
|
||||
String username = "DEV";
|
||||
send(new ConnectionData("Connect", username));
|
||||
try {
|
||||
Object object = in.readObject();
|
||||
if (object instanceof Data) {
|
||||
Data data = (Data) object;
|
||||
if (data.getPayload() instanceof ConnectionData) {
|
||||
ConnectionData connectionData = (ConnectionData) data.getPayload();
|
||||
if (connectionData.getAction().equals("Connect") && connectionData.getMessage().equals("Confirm")){
|
||||
this.connecting = false;
|
||||
this.isConnected = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (IOException | ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a message to the server.
|
||||
* @param data The message to send.
|
||||
@@ -89,7 +116,7 @@ public class Client extends Controller {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
//send("Disconnect");
|
||||
send(new ConnectionData("Disconnect", "DEV"));
|
||||
|
||||
try {
|
||||
this.socket.close();
|
||||
|
||||
@@ -2,13 +2,13 @@ package netwerkprog.game.server;
|
||||
|
||||
import netwerkprog.game.server.controllers.DataController;
|
||||
import netwerkprog.game.server.controllers.SessionController;
|
||||
import netwerkprog.game.util.data.ConnectionData;
|
||||
import netwerkprog.game.util.data.Data;
|
||||
import netwerkprog.game.util.data.DataCallback;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.net.Socket;
|
||||
|
||||
public class ServerClient implements Runnable {
|
||||
private ObjectInputStream in;
|
||||
@@ -18,18 +18,13 @@ public class ServerClient implements Runnable {
|
||||
private final DataCallback callback;
|
||||
private boolean isConnected;
|
||||
|
||||
public ServerClient(String name, Socket socket, SessionController server, DataController dataController) {
|
||||
public ServerClient(String name, ObjectInputStream in, ObjectOutputStream out, SessionController server, DataController dataController) {
|
||||
this.name = name;
|
||||
this.server = server;
|
||||
this.callback = dataController;
|
||||
try {
|
||||
this.in = new ObjectInputStream(socket.getInputStream());
|
||||
this.out = new ObjectOutputStream(socket.getOutputStream());
|
||||
this.isConnected = true;
|
||||
} catch (IOException e) {
|
||||
this.isConnected = false;
|
||||
e.printStackTrace();
|
||||
}
|
||||
this.in = in;
|
||||
this.out = out;
|
||||
this.isConnected = true;
|
||||
}
|
||||
|
||||
public void writeData(Data data) {
|
||||
@@ -44,7 +39,19 @@ public class ServerClient implements Runnable {
|
||||
public void run() {
|
||||
while (this.isConnected) {
|
||||
try {
|
||||
callback.onDataReceived((Data) this.in.readObject());
|
||||
Object object = this.in.readObject();
|
||||
if (object instanceof Data) {
|
||||
Data data = (Data) object;
|
||||
if (data.getPayload() instanceof ConnectionData) {
|
||||
ConnectionData connectionData = (ConnectionData) data.getPayload();
|
||||
if (connectionData.getAction().equals("Disconnect")) {
|
||||
this.isConnected = false;
|
||||
//todo properly remove thread.
|
||||
}
|
||||
} else {
|
||||
callback.onDataReceived((Data) this.in.readObject());
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
System.out.println("[SERVERCLIENT] caught exception - " + e.getMessage());
|
||||
System.out.println("[SERVERCLIENT] terminating failing connection...");
|
||||
|
||||
@@ -65,34 +65,34 @@ public class SessionController extends Controller {
|
||||
ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream());
|
||||
ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream());
|
||||
|
||||
String username;
|
||||
String username = "";
|
||||
boolean registering = true;
|
||||
|
||||
outputStream.writeObject(new ConnectionData("Connect", "Please give a username"));
|
||||
Object object = inputStream.readObject();
|
||||
if (object instanceof Data) {
|
||||
Data data = (Data) object;
|
||||
if (data instanceof ConnectionData) {
|
||||
ConnectionData connectionData = (ConnectionData) data.getPayload();
|
||||
if (connectionData.getAction().equals("Connect")) {
|
||||
username = connectionData.getMessage();
|
||||
while (registering) {
|
||||
outputStream.writeObject(new ConnectionData("Connect", "Please give a username"));
|
||||
Object object = inputStream.readObject();
|
||||
|
||||
if (object instanceof Data) {
|
||||
Data data = (Data) object;
|
||||
if (data instanceof ConnectionData) {
|
||||
ConnectionData connectionData = (ConnectionData) data.getPayload();
|
||||
if (connectionData.getAction().equals("Connect")) {
|
||||
username = connectionData.getMessage();
|
||||
outputStream.writeObject(new ConnectionData("Connect", "Confirm"));
|
||||
registering = false;
|
||||
} else {
|
||||
//todo error messaging.
|
||||
}
|
||||
} else {
|
||||
//todo error messaging.
|
||||
registerClient(socket);
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
//todo error messaging.
|
||||
registerClient(socket);
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
//todo error messaging.
|
||||
registerClient(socket);
|
||||
return;
|
||||
}
|
||||
|
||||
System.out.println("[SERVER] got username " + username);
|
||||
ServerClient serverClient = new ServerClient(username, socket, this, server.getDataController());
|
||||
ServerClient serverClient = new ServerClient(username, inputStream, outputStream, this, server.getDataController());
|
||||
|
||||
Thread t = new Thread(serverClient);
|
||||
t.start();
|
||||
|
||||
@@ -2,7 +2,9 @@ package netwerkprog.game.util.data;
|
||||
|
||||
import netwerkprog.game.util.game.GameCharacter;
|
||||
|
||||
public class CharacterData extends Data {
|
||||
import java.io.Serializable;
|
||||
|
||||
public class CharacterData extends Data implements Serializable {
|
||||
private final String name;
|
||||
private final GameCharacter character;
|
||||
|
||||
|
||||
@@ -1,14 +1,16 @@
|
||||
package netwerkprog.game.util.data;
|
||||
|
||||
public class ConnectionData extends Data {
|
||||
import java.io.Serializable;
|
||||
|
||||
public class ConnectionData extends Data implements Serializable {
|
||||
private final String action;
|
||||
private final String message;
|
||||
|
||||
public ConnectionData(String action, String username) {
|
||||
public ConnectionData(String action, String message) {
|
||||
super("Connection");
|
||||
super.setPayload(this);
|
||||
this.action = action;
|
||||
this.message = username;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public String getAction() {
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package netwerkprog.game.util.data;
|
||||
|
||||
public class Data {
|
||||
import java.io.Serializable;
|
||||
|
||||
public class Data implements Serializable {
|
||||
public static int port() {
|
||||
return 8000;
|
||||
}
|
||||
|
||||
@@ -1,17 +1,15 @@
|
||||
package netwerkprog.game.util.game;
|
||||
|
||||
|
||||
import com.badlogic.gdx.graphics.Texture;
|
||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||
import com.badlogic.gdx.graphics.g2d.TextureRegion;
|
||||
import com.badlogic.gdx.scenes.scene2d.Actor;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public abstract class GameCharacter extends Actor implements Comparable<GameCharacter> {
|
||||
public abstract class GameCharacter extends Actor implements Comparable<GameCharacter>, Serializable {
|
||||
protected String name;
|
||||
protected Faction faction;
|
||||
protected HashSet<Ability> abilities;
|
||||
|
||||
Reference in New Issue
Block a user