List of usage examples for io.netty.channel ChannelHandlerContext attr
@Deprecated @Override <T> Attribute<T> attr(AttributeKey<T> key);
From source file:org.jboss.aerogear.sync.DiffSyncHandler.java
License:Apache License
private void checkForReconnect(final String documentId, final String clientId, final ChannelHandlerContext ctx) { if (ctx.attr(DOC_ADD).get() == Boolean.TRUE) { return;//from ww w . ja v a 2 s . c om } logger.info("Reconnected client [" + clientId + "]. Adding as listener."); // the context was used to reconnect so we need to add client as a listener addSubscriber(ctx, clientId, documentId); }
From source file:org.jboss.aerogear.sync.server.netty.DiffSyncHandler.java
License:Apache License
@Override protected void channelRead0(final ChannelHandlerContext ctx, final WebSocketFrame frame) throws Exception { if (frame instanceof CloseWebSocketFrame) { logger.debug("Received closeFrame"); ctx.close();/*w w w . j av a2s. c om*/ return; } if (frame instanceof TextWebSocketFrame) { final JsonNode json = JsonMapper.asJsonNode(((TextWebSocketFrame) frame).text()); logger.info("Doc:" + json); switch (MessageType.from(json.get("msgType").asText())) { case ADD: final Document<T> doc = syncEngine.documentFromJson(json); final String clientId = json.get("clientId").asText(); final PatchMessage<S> patchMessage = addSubscriber(doc, clientId, ctx); ctx.attr(DOC_ADD).set(true); ctx.channel().writeAndFlush(textFrame(patchMessage.asJson())); break; case PATCH: final PatchMessage<S> clientPatchMessage = syncEngine.patchMessageFromJson(json.toString()); checkForReconnect(clientPatchMessage.documentId(), clientPatchMessage.clientId(), ctx); logger.debug("Client Edits = " + clientPatchMessage); patch(clientPatchMessage); break; case DETACH: // detach the client from a specific document. break; case UNKNOWN: unknownMessageType(ctx, json); break; } } else { ctx.fireChannelRead(frame); } }
From source file:org.jboss.aerogear.sync.server.netty.DiffSyncHandler.java
License:Apache License
private void checkForReconnect(final String documentId, final String clientId, final ChannelHandlerContext ctx) { if (ctx.attr(DOC_ADD).get() == Boolean.TRUE) { return;// ww w . j ava 2s . c o m } logger.info("Reconnected client [" + clientId + "]. Adding as listener."); // the context was used to reconnect so we need to add client as a listener final NettySubscriber subscriber = new NettySubscriber(clientId, ctx); syncEngine.connectSubscriber(subscriber, documentId); addCloseHandler(ctx, subscriber, documentId); }
From source file:org.jboss.aerogear.webpush.netty.WebPushFrameListener.java
License:Apache License
private void handleReceivingPushMessages(final ChannelHandlerContext ctx, final int streamId, final Http2Headers headers, final String path) { extractToken(path).flatMap(webpushServer::subscriptionById).ifPresent(sub -> { final Client client = new Client(ctx, streamId, encoder); monitoredStreams.put(sub.id(), client); //close the storage // all new messages will be sent to the connected client try {/*from ww w . j av a2 s .c om*/ List<PushMessage> newMessages = null; while (!(newMessages = webpushServer.waitingDeliveryMessages(sub.id())).isEmpty()) { for (PushMessage pushMessage : newMessages) { receivePushMessage(pushMessage, client); } } final Optional<CharSequence> wait = Optional.ofNullable(headers.get(PREFER_HEADER)) .filter(val -> "wait=0".equals(val.toString())); if (wait.isPresent()) { monitoredStreams.remove(sub.id()); //open the storage encoder.writeHeaders(ctx, streamId, noContentHeaders(), 0, true, ctx.newPromise()); LOGGER.info("204 No Content has sent to client={}", client); } else { ctx.attr(SUBSCRIPTION_ID).set(sub.id()); LOGGER.info("Registered client={}", client); } } catch (Exception e) { LOGGER.error("Error handleReceivingPushMessages", e); monitoredStreams.remove(sub.id()); //open the storage if exception occurred //TODO probably, we also should send something to client } }); }
From source file:org.jboss.aerogear.webpush.netty.WebPushFrameListener.java
License:Apache License
private void handleReceivingPushMessageReceipts(final ChannelHandlerContext ctx, final int streamId, final String path) { final Optional<String> receiptToken = extractToken(path); receiptToken.flatMap(webpushServer::subscriptionByReceiptToken).ifPresent(sub -> { final Client client = new Client(ctx, streamId, encoder); acksStreams.put(receiptToken.get(), client); ctx.attr(RECEIPT_SUBSCRIPTION_ID).set(receiptToken.get()); LOGGER.info("Registered application for acks={}", client); });//w ww .ja v a2s . co m }
From source file:org.jboss.aerogear.webpush.netty.WebPushFrameListener.java
License:Apache License
public void disconnect(final ChannelHandlerContext ctx) { final Optional<String> subId = Optional.ofNullable(ctx.attr(SUBSCRIPTION_ID).get()); removeClient(subId, monitoredStreams); final Optional<String> recSubId = Optional.ofNullable(ctx.attr(RECEIPT_SUBSCRIPTION_ID).get()); removeClient(recSubId, acksStreams); LOGGER.info("Disconnected channel {}", ctx.channel().id()); }
From source file:org.jmqtt.broker.metrics.BytesMetricsHandler.java
License:Open Source License
@Override public void close(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { BytesMetrics metrics = ctx.attr(ATTR_KEY_METRICS).get(); collector.sumReadBytes(metrics.readBytes()); collector.sumWroteBytes(metrics.wroteBytes()); super.close(ctx, promise); }
From source file:org.jmqtt.broker.metrics.MessageMetricsHandler.java
License:Open Source License
@Override public void close(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { MessageMetrics metrics = ctx.attr(ATTR_KEY_METRICS).get(); collector.sumReadMessages(metrics.messagesRead()); collector.sumWroteMessages(metrics.messagesWrote()); super.close(ctx, promise); }
From source file:org.l2junity.gameserver.network.telnet.TelnetServerHandler.java
License:Open Source License
@Override public void channelActive(ChannelHandlerContext ctx) { String ip = ctx.channel().remoteAddress().toString(); ip = ip.substring(1, ip.lastIndexOf(':')); // Trim out /127.0.0.1:14013 if (!Config.TELNET_HOSTS.contains(ip)) { final ChannelFuture future = ctx .write("Your ip: " + ip + " is not allowed to connect." + System.lineSeparator()); future.addListener(ChannelFutureListener.CLOSE); ctx.flush();// w ww . j a va 2 s . c om return; } // Send greeting for a new connection. ctx.write("Welcome to the L2J Unity telnet session." + System.lineSeparator()); ctx.write("It is " + new Date() + " now." + System.lineSeparator()); ctx.write("Please enter your password:" + System.lineSeparator()); if (!Config.TELNET_PASSWORD.isEmpty()) { // Ask password ctx.write("Password:"); ctx.attr(AUTHORIZED).set(Boolean.FALSE); } else { ctx.write("Type 'help' to see all available commands." + System.lineSeparator()); ctx.attr(AUTHORIZED).set(Boolean.TRUE); } ctx.flush(); }
From source file:org.l2junity.gameserver.network.telnet.TelnetServerHandler.java
License:Open Source License
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) { // Cast to a String first. // We know it is a String because we put some codec in TelnetPipelineFactory. String request = (String) msg; // Generate and write a response. String response = null;//from w w w .j a va2 s.c o m boolean close = false; if (Boolean.FALSE.equals(ctx.attr(AUTHORIZED).get())) { if (Config.TELNET_PASSWORD.equals(request)) { ctx.attr(AUTHORIZED).set(Boolean.TRUE); request = ""; } else { response = "Wrong password!" + System.lineSeparator(); close = true; } } if (Boolean.TRUE.equals(ctx.attr(AUTHORIZED).get())) { if (request.isEmpty()) { response = "Type 'help' to see all available commands." + System.lineSeparator(); } else if (request.toLowerCase().equals("exit")) { response = "Have a good day!" + System.lineSeparator(); close = true; } else { final Matcher m = COMMAND_ARGS_PATTERN.matcher(request); if (m.find()) { String command = m.group(); List<String> args = new ArrayList<>(); String arg; while (m.find()) { arg = m.group(1); if (arg == null) { arg = m.group(0); } args.add(arg); } response = tryHandleCommand(ctx, command, args.toArray(new String[args.size()])); if (!response.endsWith(System.lineSeparator())) { response += System.lineSeparator(); } } } } // We do not need to write a ChannelBuffer here. // We know the encoder inserted at TelnetPipelineFactory will do the conversion. final ChannelFuture future = ctx.write(response); // Close the connection after sending 'Have a good day!' // if the client has sent 'exit'. if (close) { future.addListener(ChannelFutureListener.CLOSE); } }