/** * Sets the value of this variable only when no value was set. * * @return {@code null} if the specified value was set. * An existing value if failed to set. */ public T setIfAbsent(Channel channel, T value) { if (value == null) { return get(channel); } else { if (channel == null) { throw new NullPointerException("channel"); } T mapping = map.putIfAbsent(channel, value); if (removeOnClose && mapping == null) { channel.getCloseFuture().addListener(remover); } return mapping; } }
public void operationComplete(ChannelFuture future) throws Exception { remove(future.getChannel()); } };
/** * Returns the value of this variable. */ public T get(Channel channel) { if (channel == null) { throw new NullPointerException("channel"); } T value = map.get(channel); if (value == null) { value = initialValue(channel); if (value != null) { T oldValue = setIfAbsent(channel, value); if (oldValue != null) { value = oldValue; } } } return value; }
@Override public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception { final ChannelBuffer message = (ChannelBuffer) e.getMessage(); final int size = message.readableBytes(); final byte[] array = new byte[size]; message.readBytes(array); this.nodeName = new String(array, Charsets.UTF_8); LOG.info("Channel {} is node {}.", ctx.getChannel(), nodeName); this.node = cluster.getNodeInfoByName(nodeName); if (node == null) { LOG.error("Node info for {} not found!", ctx.getChannel(), nodeName); throw new RuntimeException("No node info for channel"); } final SocketAddress address = ctx.getChannel().getRemoteAddress(); final InetAddress host = ((InetSocketAddress) address).getAddress(); if (!host.equals((InetAddress) node.get(IpConstants.IP_ADDRESS))) { LOG.error("Channel coming from {} claims to be node {} - host address mismatch!", address, node); throw new RuntimeException("Node identity problem!"); } LOG.info("Channel {} is {}.", ctx.getChannel(), node); ChannelNodeInfo.nodeInfo.set(ctx.getChannel(), node); assert connectEvent != null; ctx.sendUpstream(connectEvent); ctx.getPipeline().remove(this); }
/** * Removes the variable and returns the removed value. If no value was set, * this method returns the return value of {@link #initialValue(Channel)}, * which is {@code null} by default. * * @return the removed value. * {@linkplain #initialValue(Channel) an initial value} (by default * {@code null}) if no value was set. */ public T remove(Channel channel) { if (channel == null) { throw new NullPointerException("channel"); } T removed = map.remove(channel); if (removed == null) { return initialValue(channel); } else { if (removeOnClose) { channel.getCloseFuture().removeListener(remover); } return removed; } }
public ManageSieveChannelUpstreamHandler(ManageSieveProcessor manageSieveProcessor, SSLContext sslContext, String[] enabledCipherSuites, boolean sslServer, Logger logger) { this.logger = logger; this.attributes = new ChannelLocal<>(); this.manageSieveProcessor = manageSieveProcessor; this.sslContext = sslContext; this.enabledCipherSuites = enabledCipherSuites; this.sslServer = sslServer; }
@Override public void channelBound(final ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { try (Closeable closeable = IMAPMDCContext.from(ctx, attributes)) { ImapSession imapsession = new NettyImapSession(ctx.getChannel(), context, enabledCipherSuites, compress, plainAuthDisallowed); attributes.set(ctx.getChannel(), imapsession); super.channelBound(ctx, e); } }
private static NodeInfo getNodeInfo(Channel channel) { return ChannelNodeInfo.nodeInfo.get(channel); }
/** * Sets the value of this variable. * * @return the old value. {@code null} if there was no old value. */ public T set(Channel channel, T value) { if (value == null) { return remove(channel); } else { if (channel == null) { throw new NullPointerException("channel"); } T old = map.put(channel, value); if (removeOnClose) { channel.getCloseFuture().addListener(remover); } return old; } }
@Override public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { try (Closeable closeable = ManageSieveMDCContext.from(ctx, attributes)) { InetSocketAddress address = (InetSocketAddress) ctx.getChannel().getRemoteAddress(); logger.info("Connection established from {}", address.getAddress().getHostAddress()); Session session = new SettableSession(); if (sslServer) { session.setSslEnabled(true); } attributes.set(ctx.getChannel(), session); ctx.setAttachment(new ChannelManageSieveResponseWriter(ctx.getChannel())); super.channelBound(ctx, e); ((ChannelManageSieveResponseWriter) ctx.getAttachment()).write(manageSieveProcessor.getAdvertisedCapabilities()); } }
@Override protected short getNodeId(ChannelHandlerContext ctx, SocketAddress address) { final Channel channel = ctx.getChannel(); //final SocketAddress address = channel.getRemoteAddress(); NodeInfo node = ChannelNodeInfo.nodeInfo.get(channel); assert node != null; return node.getNodeId(); } }
@Override public boolean remove(Object o) { final Channel channel = (Channel) o; final boolean removed = super.remove(o); if (removed) channels.inverse().remove(channel); ChannelNodeInfo.nodeInfo.remove(channel); return removed; }
@Override public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { OdetteFtpSession session = new OdetteFtpSession(entityType); ChannelContext.SESSION.set(e.getChannel(), session); ChannelCallbackHandler channelWriter = new ChannelCallbackHandler(e.getChannel()); session.setChannelCallback(channelWriter); /* create new instance of the Oftplet implementation */ Oftplet oftplet = getOftpletFactory().createProvider(); setSessionOftplet(session, oftplet); /* let the Oftplet implementation adjust the parameters */ oftplet.init(session); // configure channel idle based on configured session timeout int timeoutSeconds = session.getTimeout(); ctx.getPipeline().addFirst("Timeout-HANDLER", new IdleStateHandler(timer, timeoutSeconds, timeoutSeconds, 0)); // Add all accepted channels to the group so that they are closed // properly on shutdown. If the added channel is closed before shutdown, // it will be removed from the group automatically. if (channelGroup != null) { channelGroup.add(e.getChannel()); } super.channelOpen(ctx, e); }
@Override public void writeRequested(ChannelHandlerContext ctx, MessageEvent e) throws Exception { if (!(e.getMessage() instanceof OdetteFtpExchangeBuffer)) { super.writeRequested(ctx, e); return; } OdetteFtpSession session = ChannelContext.SESSION.get(ctx.getChannel()); OdetteFtpExchangeBuffer message = (OdetteFtpExchangeBuffer) e.getMessage(); // set last command sent in session attribute session.setLastCommandSent(message.getIdentifier()); super.writeRequested(ctx, e); }
@Override public boolean remove(Object o) { final Channel channel = (Channel) o; final boolean removed = super.remove(o); if (removed) channels.inverse().remove(channel); ChannelNodeInfo.nodeInfo.remove(channel); return removed; }
/** * Cleanup temporary files * * @param channel */ protected void cleanup(Channel channel) { // Make sure we dispose everything on exit on session close SMTPSession smtpSession = (SMTPSession) attributes.get(channel); if (smtpSession != null) { LifecycleUtil.dispose(smtpSession.getState().get(SMTPConstants.MAIL)); LifecycleUtil.dispose(smtpSession.getState().get(SMTPConstants.DATA_MIMEMESSAGE_STREAMSOURCE)); } super.cleanup(channel); }
@Override public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { try (Closeable closeable = ManageSieveMDCContext.from(ctx, attributes)) { InetSocketAddress address = (InetSocketAddress) ctx.getChannel().getRemoteAddress(); logger.info("Connection closed for {}", address.getAddress().getHostAddress()); attributes.remove(ctx.getChannel()); super.channelClosed(ctx, e); } }
@Override public boolean add(Channel channel) { if (channel instanceof ServerChannel) return super.add(channel); else { final NodeInfo node = ChannelNodeInfo.nodeInfo.get(channel); if (node == null) { LOG.warn("Received connection from an unknown address {}.", channel.getRemoteAddress()); throw new RuntimeException("Unknown node for address " + channel.getRemoteAddress()); } final short nodeId = node.getNodeId(); if (channels.containsKey(nodeId)) { LOG.warn("Received connection from address {} of node {}, but this node is already connected.", channel.getRemoteAddress(), nodeId); throw new RuntimeException("Node " + nodeId + " already connected."); } final boolean added = super.add(channel); if (added) channels.put(nodeId, channel); return added; } }
private void logout(ChannelHandlerContext ctx) { // logout on error not sure if that is the best way to handle it attributes.remove(ctx.getChannel()); // Make sure we close the channel after all the buffers were flushed out Channel channel = ctx.getChannel(); if (channel.isConnected()) { channel.write(ChannelBuffers.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE); } }
@Override public void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { OdetteFtpSession session = ChannelContext.SESSION.get(ctx.getChannel()); Oftplet oftplet = getSessionOftplet(session); oftplet.destroy(); super.channelDisconnected(ctx, e); }