Add: Move read logic to SessionReader runnable
This commit is contained in:
@@ -1,24 +1,13 @@
|
||||
package ch.unibas.dmi.dbis.cs108.casono.server.network.sessions;
|
||||
|
||||
import ch.unibas.dmi.dbis.cs108.casono.server.network.events.DisconnectEvent;
|
||||
import ch.unibas.dmi.dbis.cs108.casono.server.network.events.EventBus;
|
||||
import ch.unibas.dmi.dbis.cs108.casono.server.network.parser.PrimitiveRequest;
|
||||
import ch.unibas.dmi.dbis.cs108.casono.server.network.parser.ProtocolParser;
|
||||
import ch.unibas.dmi.dbis.cs108.casono.server.network.transport.RawPacket;
|
||||
import ch.unibas.dmi.dbis.cs108.casono.server.network.transport.TransportLayer;
|
||||
import java.io.EOFException;
|
||||
import java.io.IOException;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
/** Represents a client session in the network server. */
|
||||
public class Session implements Runnable {
|
||||
public class Session {
|
||||
private SessionId id;
|
||||
private Thread thread;
|
||||
private TransportLayer transport;
|
||||
private Logger logger;
|
||||
private Boolean running;
|
||||
private EventBus eventBus;
|
||||
|
||||
/**
|
||||
* Creates a new Session with the given transport and event bus.
|
||||
@@ -27,15 +16,9 @@ public class Session implements Runnable {
|
||||
* @param eventBus the event bus for publishing events
|
||||
* @throws IOException if an I/O error occurs during initialization
|
||||
*/
|
||||
public Session(TransportLayer transport, EventBus eventBus) throws IOException {
|
||||
public Session(TransportLayer transport, EventBus eventBus) {
|
||||
this.id = new SessionId();
|
||||
this.thread = new Thread(this, "session-" + this.id.value());
|
||||
this.transport = transport;
|
||||
this.running = true;
|
||||
this.eventBus = eventBus;
|
||||
|
||||
this.logger = LogManager.getLogger(Session.class.toString() + id.value());
|
||||
this.logger.info("Created new session");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -49,46 +32,10 @@ public class Session implements Runnable {
|
||||
|
||||
/**
|
||||
* Returns the TransportLayer of this session
|
||||
*
|
||||
*
|
||||
* @return the transport layer of the session
|
||||
*/
|
||||
public TransportLayer getTransport() {
|
||||
return transport;
|
||||
}
|
||||
|
||||
/** Starts the session thread. */
|
||||
public void start() {
|
||||
thread.start();
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes the session and its transport.
|
||||
*
|
||||
* @throws IOException if an I/O error occurs
|
||||
*/
|
||||
public void close() throws IOException {
|
||||
transport.close();
|
||||
this.running = false;
|
||||
}
|
||||
|
||||
/** Runs the session loop, reading from the transport. */
|
||||
@Override
|
||||
public void run() {
|
||||
while (running) {
|
||||
try {
|
||||
RawPacket rawPacket = transport.read();
|
||||
logger.debug("Recieved: {}", rawPacket);
|
||||
|
||||
PrimitiveRequest primitiveRequest = ProtocolParser.parse(rawPacket);
|
||||
logger.debug("Parsed request to {}", primitiveRequest);
|
||||
} catch (EOFException e) {
|
||||
logger.info("Client disconnected");
|
||||
eventBus.publish(new DisconnectEvent(id));
|
||||
break;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
package ch.unibas.dmi.dbis.cs108.casono.server.network.sessions;
|
||||
|
||||
import ch.unibas.dmi.dbis.cs108.casono.server.network.events.DisconnectEvent;
|
||||
import ch.unibas.dmi.dbis.cs108.casono.server.network.events.EventBus;
|
||||
import ch.unibas.dmi.dbis.cs108.casono.server.network.parser.PrimitiveRequest;
|
||||
import ch.unibas.dmi.dbis.cs108.casono.server.network.parser.ProtocolParser;
|
||||
import ch.unibas.dmi.dbis.cs108.casono.server.network.parser.ProtocolParserException;
|
||||
import ch.unibas.dmi.dbis.cs108.casono.server.network.transport.RawPacket;
|
||||
import ch.unibas.dmi.dbis.cs108.casono.server.network.transport.TransportLayer;
|
||||
import ch.unibas.dmi.dbis.cs108.casono.server.tokenizer.TokenizerException;
|
||||
import java.io.EOFException;
|
||||
import java.io.IOException;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
public class SessionReader implements Runnable {
|
||||
private final Session session;
|
||||
private final TransportLayer transport;
|
||||
private final EventBus eventBus;
|
||||
private final Logger logger;
|
||||
|
||||
public SessionReader(Session session, EventBus eventBus) {
|
||||
this.session = session;
|
||||
this.transport = session.getTransport();
|
||||
this.eventBus = eventBus;
|
||||
this.logger =
|
||||
LogManager.getLogger(
|
||||
SessionReader.class.toString() + "-" + session.getId().value());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
while (!Thread.currentThread().isInterrupted()) {
|
||||
try {
|
||||
RawPacket rawPacket = transport.read();
|
||||
logger.debug("Recieved: {}", rawPacket);
|
||||
|
||||
PrimitiveRequest primitiveRequest = ProtocolParser.parse(rawPacket);
|
||||
logger.debug("Parsed request to {}", primitiveRequest);
|
||||
} catch (EOFException e) {
|
||||
logger.info("Client disconnected");
|
||||
eventBus.publish(new DisconnectEvent(session.getId()));
|
||||
break;
|
||||
} catch (TokenizerException | ProtocolParserException e) {
|
||||
logger.trace("Error occured while parsing request", e);
|
||||
|
||||
// TODO: Send error response to client
|
||||
} catch (IOException e) {
|
||||
logger.trace("Unexpected exception while reading from transport", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user