/** * Creates an instance of {@link NioServer} that opens a server channel and listens for connections. * Needs to be started next. * @param thread_factory The thread factory used to create new threads * @param socket_factory The socket factory used to create socket channels * @param bind_addr The local address to bind to. If null, the address will be picked by the OS * @param srv_port The local port to bind to If 0, the port will be picked by the OS. * @param end_port If srv_port is taken, the next port is tried, until end_port has been reached, in which case an * exception will be thrown. If srv_port == end_port, only 1 port will be tried. * @param external_addr The external address in case of NAT. Ignored if null. * @param external_port The external port on the NA. If 0, srv_port is used. * @throws Exception Thrown if the creation failed */ public NioServer(ThreadFactory thread_factory, SocketFactory socket_factory, InetAddress bind_addr, int srv_port, int end_port, InetAddress external_addr, int external_port) throws Exception { super(thread_factory, socket_factory); // channel=this.socket_factory.createServerSocketChannel("jgroups.nio.server"); // channel.setOption(StandardSocketOptions.SO_REUSEADDR, reuse_addr); // Util.bind(channel, bind_addr, srv_port, end_port); channel=Util.createServerSocketChannel(this.socket_factory, "jgroups.nio.server", bind_addr, srv_port, end_port); channel.configureBlocking(false); selector=Selector.open(); acceptor=factory.newThread(new Acceptor(), "NioServer.Selector [" + channel.getLocalAddress() + "]"); channel.register(selector, SelectionKey.OP_ACCEPT, null); local_addr=localAddress(bind_addr, channel.socket().getLocalPort(), external_addr, external_port); }
protected boolean doSelect() { try { int num=selector.select(); num_selects++; checkforPendingRegistrations(); if(num == 0) return true; } catch(ClosedSelectorException closed_ex) { log.trace("selector was closed; acceptor terminating"); return false; } catch(Throwable t) { log.warn("acceptor failure", t); } return true; }
public void run() { Iterator<SelectionKey> it=null; while(running.get() && doSelect()) { try { it=selector.selectedKeys().iterator();
public void run() { Iterator<SelectionKey> it=null; while(running.get() && doSelect()) { try { it=selector.selectedKeys().iterator();
/** * Creates an instance of {@link NioServer} that opens a server channel and listens for connections. * Needs to be started next. * @param thread_factory The thread factory used to create new threads * @param socket_factory The socket factory used to create socket channels * @param bind_addr The local address to bind to. If null, the address will be picked by the OS * @param srv_port The local port to bind to If 0, the port will be picked by the OS. * @param end_port If srv_port is taken, the next port is tried, until end_port has been reached, in which case an * exception will be thrown. If srv_port == end_port, only 1 port will be tried. * @param external_addr The external address in case of NAT. Ignored if null. * @param external_port The external port on the NA. If 0, srv_port is used. * @throws Exception Thrown if the creation failed */ public NioServer(ThreadFactory thread_factory, SocketFactory socket_factory, InetAddress bind_addr, int srv_port, int end_port, InetAddress external_addr, int external_port) throws Exception { super(thread_factory, socket_factory); // channel=this.socket_factory.createServerSocketChannel("jgroups.nio.server"); // channel.setOption(StandardSocketOptions.SO_REUSEADDR, reuse_addr); // Util.bind(channel, bind_addr, srv_port, end_port); channel=Util.createServerSocketChannel(this.socket_factory, "jgroups.nio.server", bind_addr, srv_port, end_port); channel.configureBlocking(false); selector=Selector.open(); acceptor=factory.newThread(new Acceptor(), "NioServer.Selector [" + channel.getLocalAddress() + "]"); channel.register(selector, SelectionKey.OP_ACCEPT, null); local_addr=localAddress(bind_addr, channel.socket().getLocalPort(), external_addr, external_port); }
@Override public void start() throws Exception { if(running.compareAndSet(false, true)) { super.start(); selector=Selector.open(); conn=createConnection(remote_addr); conn.connect(remote_addr, false); local_addr=conn.localAddress(); if(use_peer_connections) conn.sendLocalAddress(local_addr); conn.start(); acceptor=factory.newThread(new Acceptor(), "NioClient.Acceptor [srv=" + remote_addr + "]"); acceptor.setDaemon(true); acceptor.start(); } }
protected boolean doSelect() { try { int num=selector.select(); num_selects++; checkforPendingRegistrations(); if(num == 0) return true; } catch(ClosedSelectorException closed_ex) { log.trace("selector was closed; acceptor terminating"); return false; } catch(Throwable t) { log.warn("acceptor failure", t); } return true; }
@Override public void start() throws Exception { if(running.compareAndSet(false, true)) { super.start(); selector=Selector.open(); conn=createConnection(remote_addr); conn.connect(remote_addr, false); local_addr=conn.localAddress(); if(use_peer_connections) conn.sendLocalAddress(local_addr); conn.start(); acceptor=factory.newThread(new Acceptor(), "NioClient.Acceptor [srv=" + remote_addr + "]"); acceptor.setDaemon(true); acceptor.start(); } }