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