Add (v1) NetworkManager, Session and Transport #173

Merged
lars.winzer merged 26 commits from feat/network-manager into main 2026-03-13 15:49:56 +01:00
2 changed files with 10 additions and 23 deletions
Showing only changes of commit d4bf69dc59 - Show all commits
@@ -38,7 +38,7 @@ public class NetworkManager implements Runnable {
System.out.println("Accepted connection from " + clientSocket.getRemoteSocketAddress());
Session session = new Session(clientSocket);
Session session = new Session(new TcpTransport(clientSocket));
session.start();
}
@@ -1,29 +1,22 @@
package ch.unibas.dmi.dbis.cs108.casono.server.network;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class Session implements Runnable {
private SessionId id;
private Thread thread;
private Socket socket;
private DataInputStream in;
private DataOutputStream out;
private TransportLayer transport;
private Logger logger;
private Boolean running;
public Session(Socket socket) throws IOException {
public Session(TransportLayer transport) throws IOException {
this.id = new SessionId();
this.thread = new Thread(this, "session-" + this.id.value());
this.socket = socket;
this.in = new DataInputStream(socket.getInputStream());
this.out = new DataOutputStream(socket.getOutputStream());
this.transport = transport;
this.running = true;
this.logger = LogManager.getLogger(Session.class.toString() + id.value());
this.logger.info("Created new session");
@@ -37,22 +30,16 @@ public class Session implements Runnable {
thread.start();
}
public String read() throws IOException {
int length = in.readInt();
byte[] payload = new byte[length];
in.readFully(payload);
return new String(payload, StandardCharsets.UTF_8);
}
public void close() throws IOException {
socket.close();
transport.close();
this.running = false;
}
@Override
public void run() {
while (true) {
while (running) {
try {
System.out.println("Recieved: " + read());
System.out.println("Recieved: " + transport.read());
} catch (EOFException e) {
logger.info("Client disconnected");
break;