List of usage examples for java.nio.channels ServerSocketChannel register
public final SelectionKey register(Selector sel, int ops, Object att) throws ClosedChannelException
From source file:me.xingrz.prox.tcp.TcpProxy.java
@Override protected ServerSocketChannel createChannel(Selector selector) throws IOException { ServerSocketChannel channel = ServerSocketChannel.open(); channel.configureBlocking(false);/* www .jav a2 s .co m*/ channel.socket().bind(new InetSocketAddress(0)); channel.register(selector, SelectionKey.OP_ACCEPT, this); return channel; }
From source file:Proxy.java
public void start() throws Exception { Map.Entry entry;/*from w w w .j a v a 2 s . c om*/ Selector selector; ServerSocketChannel sock_channel; MyInetSocketAddress key, value; if (remote != null && local != null) mappings.put(new InetSocketAddress(local, local_port), new InetSocketAddress(remote, remote_port)); if (mapping_file != null) { try { populateMappings(mapping_file); } catch (Exception ex) { log("Failed reading " + mapping_file); throw ex; } } log("\nProxy started at " + new java.util.Date()); if (verbose) { log("\nMappings:\n---------"); for (Iterator it = mappings.entrySet().iterator(); it.hasNext();) { entry = (Map.Entry) it.next(); log(toString((InetSocketAddress) entry.getKey()) + " <--> " + toString((InetSocketAddress) entry.getValue())); } log("\n"); } // 1. Create a Selector selector = Selector.open(); // Create a thread pool (Executor) executor = new ThreadPoolExecutor(MIN_THREAD_POOL_SIZE, MAX_THREAD_POOL_SIZE, 30000, TimeUnit.MILLISECONDS, new LinkedBlockingQueue(1000)); for (Iterator it = mappings.keySet().iterator(); it.hasNext();) { key = (MyInetSocketAddress) it.next(); value = (MyInetSocketAddress) mappings.get(key); // if either source or destination are SSL, we cannot use JDK 1.4 // NIO selectors, but have to fall back on separate threads per connection if (key.ssl() || value.ssl()) { // if(2 == 2) { SocketAcceptor acceptor = new SocketAcceptor(key, value); executor.execute(acceptor); continue; } // 2. Create a ServerSocketChannel sock_channel = ServerSocketChannel.open(); sock_channel.configureBlocking(false); sock_channel.socket().bind(key); // 3. Register the selector with all server sockets. 'Key' is attachment, so we get it again on // select(). That way we can associate it with the mappings hashmap to find the corresponding // value sock_channel.register(selector, SelectionKey.OP_ACCEPT, key); } // 4. Start main loop. won't return until CTRL-C'ed loop(selector); }
From source file:xbird.server.services.RemotePagingService.java
private void acceptConnections(final ServerSocketChannel channel) throws IOException { // set to non blocking mode channel.configureBlocking(false);//from w w w .jav a 2 s .c om // Bind the server socket to the local host and port InetAddress host = InetAddress.getLocalHost(); InetSocketAddress sockAddr = new InetSocketAddress(host, PORT); ServerSocket socket = channel.socket(); //socket.setReuseAddress(true); socket.bind(sockAddr); // Register accepts on the server socket with the selector. This // step tells the selector that the socket wants to be put on the // ready list when accept operations occur. Selector selector = Selector.open(); ByteBuffer cmdBuffer = ByteBuffer.allocateDirect(MAX_COMMAND_BUFLEN); IOHandler ioHandler = new IOHandler(cmdBuffer); AcceptHandler acceptHandler = new AcceptHandler(ioHandler); channel.register(selector, SelectionKey.OP_ACCEPT, acceptHandler); int n; while ((n = selector.select()) > 0) { // Someone is ready for I/O, get the ready keys Set<SelectionKey> selectedKeys = selector.selectedKeys(); final Iterator<SelectionKey> keyItor = selectedKeys.iterator(); while (keyItor.hasNext()) { SelectionKey key = keyItor.next(); keyItor.remove(); // The key indexes into the selector so you // can retrieve the socket that's ready for I/O Handler handler = (Handler) key.attachment(); try { handler.handle(key); } catch (CancelledKeyException cke) { ; } catch (IOException ioe) { LOG.fatal(ioe); NIOUtils.cancelKey(key); } catch (Throwable e) { LOG.fatal(e); NIOUtils.cancelKey(key); } } } }