public Map<String, Construct> evaluate(BindableEvent e) throws EventException { Map<String, Construct> retn = new HashMap<String, Construct>(); if (e instanceof Connected) { Connected msg = (Connected) e; retn.put("id", new CString(msg.getBot().getID(), Target.UNKNOWN)); retn.put("server", new CString(msg.getServer(), Target.UNKNOWN)); retn.put("port", new CInt(msg.getPort(), Target.UNKNOWN)); } return retn; }
@Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { State currentState = state.get(); if (currentState instanceof Disconnecting) { Idle nextState = new Idle(); state.compareAndSet(currentState, nextState); ((Disconnecting) currentState).disconnectFuture.complete(Unit.VALUE); } else { Reconnecting nextState = new Reconnecting(); if (state.compareAndSet(currentState, nextState)) { if (currentState instanceof Connected && !client.getConfig().isSecureChannelReauthenticationEnabled()) { ((Connected) currentState).connected.thenAccept(sc -> sc.setChannelId(0)); } reconnect(nextState, 0); } } super.channelInactive(ctx); }
public CompletableFuture<Unit> disconnect() { State currentState = state.get(); logger.trace("disconnect(), currentState={}", currentState.getClass().getSimpleName()); if (currentState instanceof Idle) { CompletableFuture<Unit> f = new CompletableFuture<>(); f.complete(Unit.VALUE); return f; } else if (currentState instanceof Connected) { Disconnecting disconnecting = new Disconnecting(); if (state.compareAndSet(currentState, disconnecting)) { ((Connected) currentState) .connected.whenCompleteAsync( (sc, ex) -> { if (sc != null) { disconnect(sc, disconnecting.disconnectFuture); } else { disconnecting.disconnectFuture.complete(null); } disconnecting.disconnectFuture.whenComplete( (u, ex2) -> { if (state.compareAndSet(disconnecting, new Idle())) { logger.debug("disconnect complete, state set to Idle"); } }); }); return disconnecting.disconnectFuture; } else { return disconnect(); } } else if (currentState instanceof Connecting) { Disconnecting disconnecting = new Disconnecting(); if (state.compareAndSet(currentState, disconnecting)) { ((Connecting) currentState) .connected.whenCompleteAsync( (sc, ex) -> { if (sc != null) { disconnect(sc, disconnecting.disconnectFuture); } else { disconnecting.disconnectFuture.complete(Unit.VALUE); } disconnecting.disconnectFuture.whenComplete( (u, ex2) -> { if (state.compareAndSet(disconnecting, new Idle())) { logger.debug("disconnect complete, state set to Idle"); } }); }); return disconnecting.disconnectFuture; } else { return disconnect(); } } else if (currentState instanceof Reconnecting) { Disconnecting disconnecting = new Disconnecting(); if (state.compareAndSet(currentState, disconnecting)) { ((Reconnecting) currentState) .reconnected.whenCompleteAsync( (sc, ex) -> { if (sc != null) { disconnect(sc, disconnecting.disconnectFuture); } else { disconnecting.disconnectFuture.complete(Unit.VALUE); } disconnecting.disconnectFuture.whenComplete( (u, ex2) -> { if (state.compareAndSet(disconnecting, new Idle())) { logger.debug("disconnect complete, state set to Idle"); } }); }); return disconnecting.disconnectFuture; } else { return disconnect(); } } else if (currentState instanceof Disconnecting) { return ((Disconnecting) currentState).disconnectFuture; } else { throw new IllegalStateException(currentState.getClass().getSimpleName()); } }