Add: create and disconnect methods to SessionManager
This commit is contained in:
+54
-5
@@ -1,24 +1,73 @@
|
||||
package ch.unibas.dmi.dbis.cs108.casono.server.network.sessions;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import ch.unibas.dmi.dbis.cs108.casono.server.network.events.EventBus;
|
||||
import ch.unibas.dmi.dbis.cs108.casono.server.network.transport.TransportLayer;
|
||||
|
||||
/** Manages active sessions in the server. */
|
||||
public class SessionManager {
|
||||
private Map<SessionId, SessionHandle> sessions;
|
||||
private final EventBus eventBus;
|
||||
private final Logger logger;
|
||||
|
||||
/** Constructs a new SessionManager. */
|
||||
public SessionManager() {
|
||||
public SessionManager(EventBus eventBus) {
|
||||
this.sessions = new ConcurrentHashMap<>();
|
||||
this.eventBus = eventBus;
|
||||
this.logger = LogManager.getLogger(SessionManager.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a session to the manager.
|
||||
* Create new Session from provided transport.
|
||||
*
|
||||
* @param session the session to add
|
||||
* <p> Will create both worker threads and start them.
|
||||
*
|
||||
* @param transport to create session from
|
||||
* @return newly created session
|
||||
*/
|
||||
public void addSession(SessionHandle handle) {
|
||||
sessions.put(handle.session().getId(), handle);
|
||||
public Session create(TransportLayer transport) {
|
||||
Session session = new Session(transport, eventBus);
|
||||
|
||||
SessionReader reader = new SessionReader(session, eventBus);
|
||||
Thread readerThread = new Thread(reader, "session-" + session.getId().value() + "-reader");
|
||||
readerThread.start();
|
||||
|
||||
SessionWriter writer = new SessionWriter(session);
|
||||
Thread writerThread = new Thread(writer, "session-" + session.getId().value() + "-writer");
|
||||
writerThread.start();
|
||||
|
||||
SessionHandle handle = new SessionHandle(session, readerThread, writerThread);
|
||||
sessions.put(session.getId(), handle);
|
||||
logger.debug("Created new session {}", session.getId().value());
|
||||
return session;
|
||||
}
|
||||
|
||||
/**
|
||||
* Disconnect specified client
|
||||
*
|
||||
* <p> WARNING: Client will be uninformed about disconnect. Use with caution.
|
||||
*
|
||||
* @param id of the client to disconnect
|
||||
*/
|
||||
public void disconnect(SessionId id) {
|
||||
SessionHandle handle = sessions.get(id);
|
||||
if (handle == null) {
|
||||
logger.warn("Requested to disconnect client with id {}. Failed as client is not found", id.value());
|
||||
return;
|
||||
}
|
||||
logger.debug("Disconnecting session {}", id.value());
|
||||
|
||||
handle.reader().interrupt();
|
||||
handle.writer().interrupt();
|
||||
try {
|
||||
handle.session().getTransport().close();
|
||||
} catch (IOException e) {
|
||||
logger.trace("Unexpected exception while closing transport", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user