Fixed Datacontroller to allow for Data sendback
This commit is contained in:
@@ -12,7 +12,6 @@ import com.badlogic.gdx.graphics.g2d.BitmapFont;
|
||||
import com.badlogic.gdx.graphics.g2d.GlyphLayout;
|
||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||
import com.badlogic.gdx.graphics.g2d.TextureRegion;
|
||||
import com.badlogic.gdx.utils.TimeUtils;
|
||||
import netwerkprog.game.client.game.GAMESTATE;
|
||||
import netwerkprog.game.client.game.characters.Agent;
|
||||
import netwerkprog.game.client.game.characters.Hacker;
|
||||
@@ -25,6 +24,7 @@ import netwerkprog.game.client.game.map.Map;
|
||||
import netwerkprog.game.client.game.map.MapRenderer;
|
||||
import netwerkprog.game.util.data.Data;
|
||||
import netwerkprog.game.util.data.DataCallback;
|
||||
import netwerkprog.game.util.data.DataSource;
|
||||
import netwerkprog.game.util.game.Faction;
|
||||
import netwerkprog.game.util.game.GameCharacter;
|
||||
import netwerkprog.game.util.graphics.FrameRate;
|
||||
@@ -346,11 +346,11 @@ public class MainGame extends ApplicationAdapter implements DataCallback {
|
||||
|
||||
public void send(Data data) {
|
||||
System.out.println("[MAINGAME] sending data " + data);
|
||||
this.client.send(data);
|
||||
this.client.writeData(data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDataReceived(Data data) {
|
||||
public void onDataReceived(Data data, DataSource source) {
|
||||
System.out.println("[MAINGAME] Got data: " + data.toString());
|
||||
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ 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 netwerkprog.game.util.data.DataSource;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
@@ -11,7 +12,7 @@ import java.io.ObjectOutputStream;
|
||||
import java.net.Socket;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class Client extends Controller {
|
||||
public class Client extends Controller implements DataSource {
|
||||
private final int port;
|
||||
private final String hostname;
|
||||
private boolean isConnected;
|
||||
@@ -69,7 +70,7 @@ public class Client extends Controller {
|
||||
public void register(ObjectInputStream in) {
|
||||
while (connecting) {
|
||||
String username = "DEV";
|
||||
send(new ConnectionData("Connect", username));
|
||||
writeData(new ConnectionData("Connect", username));
|
||||
try {
|
||||
Object object = in.readObject();
|
||||
if (object instanceof Data) {
|
||||
@@ -93,7 +94,7 @@ public class Client extends Controller {
|
||||
*
|
||||
* @param data The message to send.
|
||||
*/
|
||||
public void send(Data data) {
|
||||
public void writeData(Data data) {
|
||||
try {
|
||||
System.out.println("[CLIENT] writing data " + data);
|
||||
this.outputStream.writeObject(data);
|
||||
@@ -114,7 +115,7 @@ public class Client extends Controller {
|
||||
Object object = in.readObject();
|
||||
System.out.println("[CLIENT] got object " + object);
|
||||
if (object instanceof Data) {
|
||||
callback.onDataReceived((Data) object);
|
||||
callback.onDataReceived((Data) object, this);
|
||||
}
|
||||
} catch (IOException | ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
@@ -130,7 +131,7 @@ public class Client extends Controller {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
send(new ConnectionData("Disconnect", "DEV"));
|
||||
writeData(new ConnectionData("Disconnect", "DEV"));
|
||||
|
||||
try {
|
||||
this.socket.close();
|
||||
|
||||
@@ -5,12 +5,13 @@ 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 netwerkprog.game.util.data.DataSource;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
|
||||
public class ServerClient implements Runnable {
|
||||
public class ServerClient implements Runnable, DataSource {
|
||||
private ObjectInputStream in;
|
||||
private ObjectOutputStream out;
|
||||
private final String name;
|
||||
@@ -53,7 +54,7 @@ public class ServerClient implements Runnable {
|
||||
} else {
|
||||
// callback.onDataReceived((Data) this.in.readObject());
|
||||
System.out.println("[SERVERCLIENT] got data: " + data + ", sending callback");
|
||||
callback.onDataReceived(data);
|
||||
callback.onDataReceived(data, this);
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
package netwerkprog.game.server.controllers;
|
||||
|
||||
import netwerkprog.game.server.ServerClient;
|
||||
import netwerkprog.game.util.data.CharacterData;
|
||||
import netwerkprog.game.util.data.Data;
|
||||
import netwerkprog.game.util.data.DataCallback;
|
||||
import netwerkprog.game.util.data.DataSource;
|
||||
import netwerkprog.game.util.game.GameCharacter;
|
||||
|
||||
import java.util.Arrays;
|
||||
@@ -50,7 +50,7 @@ public class DataController implements DataCallback {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDataReceived(Data data, ServerClient client) {
|
||||
public void onDataReceived(Data data, DataSource source) {
|
||||
System.out.println("[DATACONTROLLER] got data: " + data);
|
||||
switch (data.getType()) {
|
||||
case "Character" :
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
package netwerkprog.game.util.data;
|
||||
|
||||
import netwerkprog.game.server.ServerClient;
|
||||
|
||||
public interface DataCallback {
|
||||
void onDataReceived(Data data, ServerClient client);
|
||||
void onDataReceived(Data data, DataSource source);
|
||||
}
|
||||
|
||||
5
core/src/netwerkprog/game/util/data/DataSource.java
Normal file
5
core/src/netwerkprog/game/util/data/DataSource.java
Normal file
@@ -0,0 +1,5 @@
|
||||
package netwerkprog.game.util.data;
|
||||
|
||||
public interface DataSource {
|
||||
void writeData(Data data);
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
package netwerkprog.game.util.data;
|
||||
|
||||
public interface ParserCallback {
|
||||
void onDataReceived(String data);
|
||||
}
|
||||
Reference in New Issue
Block a user