private <C extends Channel> C minChannel(Set<C> channels) { C min = null; for (C channel : channels) { if ((min == null)) { min = channel; } else if (channel2Id(channel) < channel2Id(min)) { min = channel; } } return min; }
@Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { component.channels.channelInactive(ctx, protocol); super.channelInactive(ctx); } }
@Override protected void messageReceived(ChannelHandlerContext ctx, Msg msg) throws Exception { component.channels.checkTCPChannel(msg, (SocketChannel) ctx.channel()); super.messageReceived(ctx, msg); }
protected void deliverMessage(Msg message, Channel c) { if (message instanceof DisambiguateConnection) { DisambiguateConnection msg = (DisambiguateConnection) message; channels.disambiguate(msg, c); if (msg.reply) { c.writeAndFlush(new MessageWrapper(new DisambiguateConnection(self, msg.getSource(), msg.getProtocol(), boundUDTPort, false))); channels.checkActive(msg, c); return; channels.flushAndClose(msg, c); return;
private void sendTCP(MessageWrapper msg) { Address peer = msg.msg.getDestination(); Queue<MessageWrapper> delays = tcpDelays.get(peer.asSocket()); if (delays != null) { component.extLog.debug("Delaying message while establishing connection: {}", msg); delays.add(msg); return; } Channel c = component.channels.getTCPChannel(peer); if (c == null) { c = component.channels.createTCPChannel(peer, component.bootstrapTCPClient); } if (c == null) { delays = new LinkedList<>(); tcpDelays.put(peer.asSocket(), delays); component.extLog.debug("Delaying message while establishing connection: {}", msg); delays.add(msg); return; } component.extLog.debug("Sending message {}. Local {}, Remote {}", new Object[]{msg, c.localAddress(), c.remoteAddress()}); ChannelFuture cf = c.writeAndFlush(msg); if (msg.notify.isPresent()) { cf.addListener(new NotifyListener(msg.notify.get())); } }
private void sendUDT(MessageWrapper msg) { Address peer = msg.msg.getDestination(); Queue<MessageWrapper> delays = udtDelays.get(peer.asSocket()); if (delays != null) { component.extLog.debug("Delaying message while establishing connection: {}", msg); delays.add(msg); return; } Channel c = component.channels.getUDTChannel(peer); if (c == null) { c = component.channels.createUDTChannel(peer, component.bootstrapUDTClient); } if (c == null) { delays = new LinkedList<>(); udtDelays.put(peer.asSocket(), delays); component.extLog.debug("Delaying message while establishing connection: {}", msg); delays.add(msg); return; } component.extLog.debug("Sending message {}. Local {}, Remote {}", new Object[]{msg, c.localAddress(), c.remoteAddress()}); ChannelFuture cf = c.writeAndFlush(msg); if (msg.notify.isPresent()) { cf.addListener(new NotifyListener(msg.notify.get())); } }
@Override public void channelActive(ChannelHandlerContext ctx) { super.channelActive(ctx); SocketChannel channel = (SocketChannel) ctx.channel(); component.channels.addLocalSocket(channel); InetSocketAddress other = channel.remoteAddress(); DisambiguateConnection r = new DisambiguateConnection(component.self, new NettyAddress(other), protocol, component.boundUDTPort, true); channel.writeAndFlush(r); } }
@Override protected void messageReceived(ChannelHandlerContext ctx, Msg msg) throws Exception { component.channels.checkUDTChannel(msg, (UdtChannel) ctx.channel()); super.messageReceived(ctx, msg); }
private void clearConnections() { long tstart = System.currentTimeMillis(); channels.clearConnections(); if (bindUDP) { try { udpChannel.close().syncUninterruptibly(); } catch (Exception ex) { logger.warn("Error during Netty shutdown. Messages might have been lost! \n {}", ex); } } messages.clear(); long tend = System.currentTimeMillis(); logger.info("Closed all connections in {}ms", tend - tstart); }
if (waitingForCreationUDT.remove(msg.getSource().asSocket())) { component.extLog.debug("Requesting creation of outstanding UDT channel to {}", msg.getSource()); createUDTChannel(msg.getSource(), component.bootstrapUDTClient);
component.extLog.trace("Need to find UDT port at {} before creating channel.", destination.asSocket()); DisambiguateConnection r = new DisambiguateConnection(component.self, new NettyAddress(destination), Transport.TCP, component.boundUDTPort, true); SocketChannel tcpC = this.getTCPChannel(destination); if (tcpC == null) { tcpC = this.createTCPChannel(destination, component.bootstrapTCPClient);
@Override public void channelActive(ChannelHandlerContext ctx) { super.channelActive(ctx); UdtChannel channel = (UdtChannel) ctx.channel(); component.channels.addLocalSocket(channel); InetSocketAddress other = channel.remoteAddress(); channel.writeAndFlush(new DisambiguateConnection(component.self, new NettyAddress(other), protocol, component.boundUDTPort, true)); }
private int channel2Id(Channel c) { if (c instanceof SocketChannel) { return channel2Id((SocketChannel) c); } if (c instanceof UdtChannel) { return channel2Id((UdtChannel) c); } throw new NotImplementedException(); }