networking basis

This commit is contained in:
Luca Conte 2024-11-30 13:45:06 +01:00
parent d425e64fa0
commit c92b3b3551
6 changed files with 195 additions and 9 deletions

80
src/AsyncSocket.java Normal file
View File

@ -0,0 +1,80 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
public class AsyncSocket {
private Socket socket;
private Thread checkerThread;
private AsyncSocketListener handler;
private boolean shouldStop = false;
private BufferedReader in;
private PrintWriter out;
public AsyncSocket(Socket socket, AsyncSocketListener handler) throws IOException {
this.socket = socket;
this.in = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
this.out = new PrintWriter(new OutputStreamWriter(this.socket.getOutputStream()));
this.handler = handler;
this.checkerThread = new Thread(() -> {
while (!this.shouldStop) {
try {
if (!this.in.ready()) continue;
if (this.handler == null) continue;
String message = this.in.readLine();
if (message.length() <= 0) continue;
this.handler.receive(message);
} catch (IOException e) {
e.printStackTrace();
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
this.checkerThread.start();
}
public void setHandler(AsyncSocketListener handler) {
this.handler = handler;
}
public void send(SocketPackage socketPackage) {
this.sendLine(socketPackage.toString());
}
public void send(String packageName) {
this.send(packageName, "");
}
public void send(String packageName, String packageContent) {
if (packageContent.length() > 0) {
packageContent = " " + packageContent;
}
this.sendLine(packageName + packageContent);
}
public void sendLine(String message) {
this.out.print(message + "\r\n");
}
public void close() {
this.shouldStop = true;
try {
this.socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,3 @@
public interface AsyncSocketListener {
public void receive(String message);
}

View File

@ -1,11 +1,45 @@
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.util.Arrays;
import java.util.HashMap;
public class GameController { public class GameController {
// TODO: fix syntax i guess
public HashMap<String, Class<? extends OnlinePlayer>> supportedVersions = [{ "1.1.0" : OnlinePlayer_1_1_0.class }];
public void startOnlineGame() { public void startOnlineGame(Class<? extends LocalPlayer> localPlayerClass, InetSocketAddress address) {
// fuck you Luca and Ole von Florian und nicht von Florian if (address.getHostName() == null) {
} // SERVER MODE
try {
ServerSocket serverSocket = new ServerSocket(address.getPort());
public void startLocalGame(Class<? extends LocalPlayer> localPlayerClass, Class<? extends AiPlayer> enemyClass, int size) throws InstantiationException, IllegalAccessException { System.out.println("Waiting for client connection...");
AsyncSocket clientSocket = new AsyncSocket(serverSocket.accept(), null);
clientSocket.send("VERSION", "Gruppe03 1.1.0"); // TODO: adjust versioning list
clientSocket.setHandler((message) -> {
new OnlinePlayer_1_1_0(0, clientSocket);
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
});
} catch (IOException e) {
e.printStackTrace();
return;
}
} else {
// CLIENT MODE
}
}
public void startLocalGame(Class<? extends LocalPlayer> localPlayerClass, Class<? extends AiPlayer> enemyClass, int size) {
LocalPlayer localPlayer; LocalPlayer localPlayer;
AiPlayer aiPlayer; AiPlayer aiPlayer;

View File

@ -1,11 +1,12 @@
import java.net.Socket; public abstract class OnlinePlayer extends Player implements AsyncSocketListener{
private AsyncSocket socket;
public abstract class OnlinePlayer extends Player{ public OnlinePlayer(int size, AsyncSocket socket) {
private Socket socket;
public OnlinePlayer(int size, Socket socket) {
super(size); super(size);
this.socket = socket; this.socket = socket;
//TODO Auto-generated constructor stub //TODO Auto-generated constructor stub
} }
public abstract void receive(String message);
} }

View File

@ -0,0 +1,13 @@
public class OnlinePlayer_1_1_0 extends OnlinePlayer {
public OnlinePlayer_1_1_0(int size, AsyncSocket socket) {
super(size, socket);
}
@Override
public void receive(String message) {
SocketPackage p = new SocketPackage(message);
// TODO: parse package
}
}

55
src/SocketPackage.java Normal file
View File

@ -0,0 +1,55 @@
import java.util.Arrays;
import java.util.List;
public class SocketPackage {
private String name;
private String data;
public SocketPackage(String name, String data) {
this.setName(name);
this.setData(data);
}
public SocketPackage() {
this("","");
}
public SocketPackage(String message) {
if (message.length() <= 0) {
throw new IllegalArgumentException("Socket message cannot be empty.");
}
String[] components = message.split(" ");
this.setName(components[0]);
if (components.length > 1) {
this.data = message.substring(components[0].length() + 1);
} else {
this.data = null;
}
}
public void setName(String name) {
this.name = name.toUpperCase();
}
public void setData(String data) {
this.data = data;
}
public String getName() {
return this.name;
}
public String getData() {
return this.data;
}
public String toString() {
if (this.data == null || this.data.length() == 0) {
return this.name;
} else {
return this.name + " " + this.data;
}
}
public List<String> splitData() {
return Arrays.asList(this.data.split(" "));
}
}