Refactor: Session to use TransportLayer for socket communication
This commit is contained in:
@@ -38,7 +38,7 @@ public class NetworkManager implements Runnable {
|
|||||||
|
|
||||||
System.out.println("Accepted connection from " + clientSocket.getRemoteSocketAddress());
|
System.out.println("Accepted connection from " + clientSocket.getRemoteSocketAddress());
|
||||||
|
|
||||||
Session session = new Session(clientSocket);
|
Session session = new Session(new TcpTransport(clientSocket));
|
||||||
session.start();
|
session.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,29 +1,22 @@
|
|||||||
package ch.unibas.dmi.dbis.cs108.casono.server.network;
|
package ch.unibas.dmi.dbis.cs108.casono.server.network;
|
||||||
|
|
||||||
import java.io.DataInputStream;
|
|
||||||
import java.io.DataOutputStream;
|
|
||||||
import java.io.EOFException;
|
import java.io.EOFException;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.Socket;
|
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
|
|
||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
|
|
||||||
public class Session implements Runnable {
|
public class Session implements Runnable {
|
||||||
private SessionId id;
|
private SessionId id;
|
||||||
private Thread thread;
|
private Thread thread;
|
||||||
private Socket socket;
|
private TransportLayer transport;
|
||||||
private DataInputStream in;
|
|
||||||
private DataOutputStream out;
|
|
||||||
private Logger logger;
|
private Logger logger;
|
||||||
|
private Boolean running;
|
||||||
|
|
||||||
public Session(Socket socket) throws IOException {
|
public Session(TransportLayer transport) throws IOException {
|
||||||
this.id = new SessionId();
|
this.id = new SessionId();
|
||||||
this.thread = new Thread(this, "session-" + this.id.value());
|
this.thread = new Thread(this, "session-" + this.id.value());
|
||||||
this.socket = socket;
|
this.transport = transport;
|
||||||
this.in = new DataInputStream(socket.getInputStream());
|
this.running = true;
|
||||||
this.out = new DataOutputStream(socket.getOutputStream());
|
|
||||||
|
|
||||||
this.logger = LogManager.getLogger(Session.class.toString() + id.value());
|
this.logger = LogManager.getLogger(Session.class.toString() + id.value());
|
||||||
this.logger.info("Created new session");
|
this.logger.info("Created new session");
|
||||||
@@ -37,22 +30,16 @@ public class Session implements Runnable {
|
|||||||
thread.start();
|
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 {
|
public void close() throws IOException {
|
||||||
socket.close();
|
transport.close();
|
||||||
|
this.running = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
while (true) {
|
while (running) {
|
||||||
try {
|
try {
|
||||||
System.out.println("Recieved: " + read());
|
System.out.println("Recieved: " + transport.read());
|
||||||
} catch (EOFException e) {
|
} catch (EOFException e) {
|
||||||
logger.info("Client disconnected");
|
logger.info("Client disconnected");
|
||||||
break;
|
break;
|
||||||
|
|||||||
Reference in New Issue
Block a user