/** * Disconnect connection from scope * * @param conn Connection object */ public void disconnect(IConnection conn) { log.debug("Disconnect: {}", conn); // call disconnect handlers in reverse order of connection. ie. roomDisconnect is called before // appDisconnect. final IClient client = conn.getClient(); if (client == null) { // early bail out removeEventListener(conn); connectionStats.decrement(); if (hasParent()) { parent.disconnect(conn); } return; } // remove it if it exists if (clients.remove(client)) { IScopeHandler handler = getHandler(); if (handler != null) { try { handler.disconnect(conn, this); } catch (Exception e) { log.error( "Error while executing \"disconnect\" for connection {} on handler {}. {}", new Object[] {conn, handler, e}); } try { // there may be a timeout here ? handler.leave(client, this); } catch (Exception e) { log.error( "Error while executing \"leave\" for client {} on handler {}. {}", new Object[] {conn, handler, e}); } } // remove listener removeEventListener(conn); // decrement if there was a set of connections connectionStats.decrement(); if (this.equals(conn.getScope())) { final IServer server = getServer(); if (server instanceof Server) { ((Server) server).notifyDisconnected(conn); } } } if (hasParent()) { parent.disconnect(conn); } }
public boolean add(IBasicScope scope) { // log.trace("add - Hold counts - read: {} write: {} queued: {}", // internalLock.getReadHoldCount(), internalLock.getWriteHoldCount(), // internalLock.hasQueuedThreads()); boolean added = false; // check #1 if (!containsKey(scope)) { log.debug("Adding child scope: {} to {}", (((IBasicScope) scope).getName()), this); if (hasHandler()) { // get the handler for the scope to which we are adding this new scope IScopeHandler hdlr = getHandler(); // add the scope to the handler if (!hdlr.addChildScope(scope)) { log.warn("Failed to add child scope: {} to {}", scope, this); return false; } } else { log.debug("No handler found for {}", this); } try { // check #2 for entry if (!containsKey(scope)) { // add the entry // expected return from put is null; indicating this scope didn't already exist added = (super.put(scope, Boolean.TRUE) == null); if (added) { subscopeStats.increment(); } else { log.debug("Subscope was not added"); } } else { log.debug("Subscope already exists"); } } catch (Exception e) { log.warn("Exception on add", e); } if (added && scope instanceof Scope) { // cast it Scope scp = (Scope) scope; // start the scope if (scp.start()) { log.debug("Child scope started"); } else { log.debug("Failed to start child scope: {} in {}", scope, this); } } } return added; }
/** * Starts scope * * @return * <pre> * true * </pre> * if scope has handler and it's start method returned true, * <pre> * false * </pre> * otherwise */ public boolean start() { log.debug("Start scope"); boolean result = false; if (enabled && !running) { // check for any handlers if (handler != null) { log.debug("Scope {} has a handler {}", this.getName(), handler); } else { log.debug("{} has no handler, adding parent handler", this); handler = parent.getHandler(); } try { // if we dont have a handler of our own dont try to start it if (handler != null) { result = handler.start(this); } else { // always start scopes without handlers log.debug("{} has no handler of its own, allowing start", this); result = true; } } catch (Throwable e) { log.error("Could not start scope {}", this, e); } finally { // post notification ((Server) getServer()).notifyScopeCreated(this); } running = result; } return result; }
@Override public Boolean remove(Object scope) { log.debug("Remove child scope: {}", scope); // log.trace("remove - Hold counts - read: {} write: {} queued: {}", // internalLock.getReadHoldCount(), internalLock.getWriteHoldCount(), // internalLock.hasQueuedThreads()); if (hasHandler()) { IScopeHandler hdlr = getHandler(); log.debug("Removing child scope: {}", (((IBasicScope) scope).getName())); hdlr.removeChildScope((IBasicScope) scope); if (scope instanceof Scope) { // cast it Scope scp = (Scope) scope; // stop the scope scp.stop(); } } else { log.debug("No handler found for {}", this); } boolean removed = false; try { if (containsKey(scope)) { // remove the entry, ensure removed value is equal to the given object removed = super.remove(scope).equals(Boolean.TRUE); if (removed) { subscopeStats.decrement(); } else { log.debug("Subscope was not removed"); } } else { log.debug("Subscope was not removed, it was not found"); } } catch (Exception e) { log.warn("Exception on remove", e); } return removed; }
/** Stops scope */ public void stop() { log.debug("stop: {}", name); if (enabled && running && handler != null) { try { // if we dont have a handler of our own dont try to stop it handler.stop(this); } catch (Throwable e) { log.error("Could not stop scope {}", this, e); } finally { // post notification ((Server) getServer()).notifyScopeRemoved(this); } // remove all children removeChildren(); } running = false; }