mirror of
https://github.com/inf112-v20/Fiasko.git
synced 2025-02-01 07:39:35 +01:00
Legger til en del objekter for å teste kryonet funksjonalitet
This commit is contained in:
parent
2219ea3dbe
commit
b0ebb2db3e
@ -0,0 +1,33 @@
|
|||||||
|
package inf112.fiasko.roborally.networking;
|
||||||
|
|
||||||
|
import com.esotericsoftware.kryonet.Client;
|
||||||
|
import com.esotericsoftware.kryonet.Connection;
|
||||||
|
import com.esotericsoftware.kryonet.Listener;
|
||||||
|
import inf112.fiasko.roborally.utility.NetworkUtil;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public class RoboRallyClient {
|
||||||
|
public RoboRallyClient() throws IOException {
|
||||||
|
Client client = new Client();
|
||||||
|
client.start();
|
||||||
|
NetworkUtil.registerClasses(client.getKryo());
|
||||||
|
client.connect(5000, "127.0.0.1", 54555, 54777);
|
||||||
|
|
||||||
|
SomeRequest request = new SomeRequest();
|
||||||
|
request.text = "Here is the request";
|
||||||
|
client.sendTCP(request);
|
||||||
|
|
||||||
|
client.addListener(new RoboRallyClientListener());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class RoboRallyClientListener extends Listener {
|
||||||
|
@Override
|
||||||
|
public void received (Connection connection, Object object) {
|
||||||
|
if (object instanceof SomeResponse) {
|
||||||
|
SomeResponse response = (SomeResponse)object;
|
||||||
|
System.out.println("Client received: " + response.text);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,62 @@
|
|||||||
|
package inf112.fiasko.roborally.networking;
|
||||||
|
|
||||||
|
import com.esotericsoftware.kryonet.Connection;
|
||||||
|
import com.esotericsoftware.kryonet.Listener;
|
||||||
|
import com.esotericsoftware.kryonet.Server;
|
||||||
|
import inf112.fiasko.roborally.utility.NetworkUtil;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public class RoboRallyServer {
|
||||||
|
private Server server;
|
||||||
|
|
||||||
|
public RoboRallyServer() throws IOException {
|
||||||
|
server = new Server();
|
||||||
|
server.start();
|
||||||
|
NetworkUtil.registerClasses(server.getKryo());
|
||||||
|
server.bind(54555, 54777);
|
||||||
|
server.addListener(new RoboRallyServerListener());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sends an object to all clients
|
||||||
|
* @param object The object to send
|
||||||
|
*/
|
||||||
|
public void sendToAllClients(Object object) {
|
||||||
|
server.sendToAllTCP(object);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class RoboRallyServerListener extends Listener {
|
||||||
|
Connection host;
|
||||||
|
|
||||||
|
public RoboRallyServerListener() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void received (Connection connection, Object object) {
|
||||||
|
if (object instanceof SomeRequest) {
|
||||||
|
SomeRequest request = (SomeRequest)object;
|
||||||
|
System.out.println(request.text);
|
||||||
|
|
||||||
|
SomeResponse response = new SomeResponse();
|
||||||
|
response.text = "Thanks";
|
||||||
|
connection.sendTCP(response);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void connected(Connection connection) {
|
||||||
|
//The first client to connect is assumed to be the host
|
||||||
|
if (host == null) {
|
||||||
|
host = connection;
|
||||||
|
}
|
||||||
|
System.out.println(connection.getRemoteAddressTCP() + " connected");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void disconnected(Connection connection) {
|
||||||
|
System.out.println(connection.getRemoteAddressTCP() + " disconnected");
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,8 @@
|
|||||||
|
package inf112.fiasko.roborally.networking;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents a request to the server
|
||||||
|
*/
|
||||||
|
public class SomeRequest {
|
||||||
|
public String text;
|
||||||
|
}
|
@ -0,0 +1,8 @@
|
|||||||
|
package inf112.fiasko.roborally.networking;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents a response from a client
|
||||||
|
*/
|
||||||
|
public class SomeResponse {
|
||||||
|
public String text;
|
||||||
|
}
|
@ -0,0 +1,17 @@
|
|||||||
|
package inf112.fiasko.roborally.utility;
|
||||||
|
|
||||||
|
import com.esotericsoftware.kryo.Kryo;
|
||||||
|
import inf112.fiasko.roborally.networking.SomeRequest;
|
||||||
|
import inf112.fiasko.roborally.networking.SomeResponse;
|
||||||
|
|
||||||
|
public final class NetworkUtil {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Registers all classes which can be sent between a server and a client
|
||||||
|
* @param kryo The kryo object to register the classes to
|
||||||
|
*/
|
||||||
|
public static void registerClasses(Kryo kryo) {
|
||||||
|
kryo.register(SomeRequest.class);
|
||||||
|
kryo.register(SomeResponse.class);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user