public boolean isSameEventLoop(ChannelHandlerContext ctx) { return isSameEventLoop(ctx.channel()); }
private void createSessionAndReregister(ChannelHandlerContext ctx, User user, DashBoard dash, Device device, int msgId) { HardwareStateHolder hardwareStateHolder = new HardwareStateHolder(user, dash, device); ChannelPipeline pipeline = ctx.pipeline(); pipeline.replace(this, "HHArdwareHandler", new HardwareHandler(holder, hardwareStateHolder)); Session session = holder.sessionDao.getOrCreateSessionByUser( hardwareStateHolder.userKey, ctx.channel().eventLoop()); if (session.isSameEventLoop(ctx)) { completeLogin(ctx.channel(), session, user, dash, device, msgId); } else { log.debug("Re registering hard channel. {}", ctx.channel()); ReregisterChannelUtil.reRegisterChannel(ctx, session, channelFuture -> completeLogin(channelFuture.channel(), session, user, dash, device, msgId)); } }
private void login(ChannelHandlerContext ctx, int messageId, User user, Version version) { var pipeline = (DefaultChannelPipeline) ctx.pipeline(); cleanPipeline(pipeline); var appStateHolder = new MobileStateHolder(user, version); pipeline.addLast("AAppHandler", new MobileHandler(holder, appStateHolder)); var channel = ctx.channel(); //todo back compatibility code. remove in future. if (user.region == null || user.region.isEmpty()) { user.region = holder.props.region; } var session = holder.sessionDao.getOrCreateSessionByUser(appStateHolder.userKey, channel.eventLoop()); if (session.isSameEventLoop(channel)) { completeLogin(channel, session, user, messageId, version); } else { log.debug("Re registering app channel. {}", ctx.channel()); ReregisterChannelUtil.reRegisterChannel(ctx, session, channelFuture -> completeLogin(channelFuture.channel(), session, user, messageId, version)); } }
mobileShareStateHolder.userKey, ctx.channel().eventLoop()); if (session.isSameEventLoop(ctx)) { completeLogin(ctx.channel(), session, user.email, messageId); } else {
@Override protected void channelRead0(ChannelHandlerContext ctx, MqttConnectMessage message) { String username = message.payload().userName().trim().toLowerCase(); String token = new String(message.payload().passwordInBytes(), CharsetUtil.UTF_8); TokenValue tokenValue = holder.tokenManager.getTokenValueByToken(token); if (tokenValue == null || !tokenValue.user.email.equalsIgnoreCase(username)) { log.debug("MqttHardwareLogic token is invalid. Token '{}', '{}'", token, ctx.channel().remoteAddress()); ctx.writeAndFlush(createConnAckMessage(CONNECTION_REFUSED_BAD_USER_NAME_OR_PASSWORD), ctx.voidPromise()); return; } User user = tokenValue.user; Device device = tokenValue.device; DashBoard dash = tokenValue.dash; ChannelPipeline pipeline = ctx.pipeline(); HardwareStateHolder hardwareStateHolder = new HardwareStateHolder(user, tokenValue.dash, device); pipeline.replace(this, "HHArdwareMqttHandler", new MqttHardwareHandler(holder, hardwareStateHolder)); Session session = holder.sessionDao.getOrCreateSessionByUser( hardwareStateHolder.userKey, ctx.channel().eventLoop()); if (session.isSameEventLoop(ctx)) { completeLogin(ctx.channel(), session, user, dash, device, -1); } else { log.debug("Re registering hard channel. {}", ctx.channel()); ReregisterChannelUtil.reRegisterChannel(ctx, session, channelFuture -> completeLogin(channelFuture.channel(), session, user, dash, device, -1)); } }
@Override public void finishHttp(ChannelHandlerContext ctx, URIDecoder uriDecoder, HandlerWrapper handler, Object[] params) { String tokenPathParam = uriDecoder.pathData.get("token"); if (tokenPathParam == null) { ctx.writeAndFlush(Response.badRequest("No token provided.")); return; } //reregister logic TokenValue tokenValue = tokenManager.getTokenValueByToken(tokenPathParam); if (tokenValue == null) { log.debug("Requested token {} not found.", tokenPathParam); ctx.writeAndFlush(Response.badRequest("Invalid token."), ctx.voidPromise()); return; } Session session = sessionDao.getOrCreateSessionByUser(new UserKey(tokenValue.user), ctx.channel().eventLoop()); if (session.isSameEventLoop(ctx)) { completeLogin(ctx.channel(), handler.invoke(params)); } else { log.trace("Re registering http channel. {}", ctx.channel()); ReregisterChannelUtil.reRegisterChannel(ctx, session, channelFuture -> completeLogin( channelFuture.channel(), handler.invoke(params))); } }