public ConnectFuture connect(SocketAddress localAddress, IoSessionInitializer<? extends ConnectFuture> sessionInitializer) { MulticastAddress multicastAddress = (MulticastAddress) localAddress; String uri = format("udp://%s:%d", multicastAddress.getGroupAddress().getHostAddress(), multicastAddress.getBindPort()); ResourceAddress resourceAddress = resourceAddressFactory.newResourceAddress(uri); final MulticastSession newSession = new MulticastSession(MulticastConnectorImpl.this, processor, socket, resourceAddress, remoteAddress); DefaultConnectFuture connectFuture = new DefaultConnectFuture(); initSession(newSession, connectFuture, sessionInitializer); newSession.getProcessor().add(newSession); dispatchSessions.add(newSession); newSession.getCloseFuture().addListener(new IoFutureListener<CloseFuture>() { @Override public void operationComplete(CloseFuture future) { dispatchSessions.remove(newSession); } }); return connectFuture; }
@Override public void add(MulticastSession session) { boolean notified = false; try { // Build the filter chain of this session. session.getService().getFilterChainBuilder().buildFilterChain( session.getFilterChain()); // DefaultIoFilterChain.CONNECT_FUTURE is cleared inside here // in AbstractIoFilterChain.fireSessionOpened(). session.getService().getListeners() .fireSessionCreated(session); notified = true; } catch (Throwable e) { if (notified) { // Clear the DefaultIoFilterChain.CONNECT_FUTURE attribute // and call ConnectFuture.setException(). session.close(true); IoFilterChain filterChain = session.getFilterChain(); filterChain.fireExceptionCaught(e); } else { ExceptionMonitor.getInstance().exceptionCaught(e, session); } } }
@Override public void remove(MulticastSession session) { session.getSocket().close(); getSessionRecycler().remove(session); getListeners().fireSessionDestroyed(session); }
private IoSessionEx newSessionWithoutLock( SocketAddress remoteAddress, SocketAddress localAddress) throws Exception { Handle handle = boundHandles.get(localAddress); if (handle == null) { throw new IllegalArgumentException("Unknown local address: " + localAddress); } IoSessionEx session; IoSessionRecycler sessionRecycler = getSessionRecycler(); synchronized (sessionRecycler) { session = (IoSessionEx) sessionRecycler.recycle(localAddress, remoteAddress); if (session != null && !session.isClosing()) { return session; } // If a new session needs to be created. MulticastSession newSession = new MulticastSession(this, processor, handle.socket, handle.localAddress, remoteAddress); sessionRecycler.put(newSession); session = newSession; } initSession(session, null, null); try { this.getFilterChainBuilder().buildFilterChain(session.getFilterChain()); getListeners().fireSessionCreated(session); } catch (Throwable t) { ExceptionMonitor.getInstance().exceptionCaught(t, session); } return session; }