Java Code Examples for java.nio.channels.Selector#open()
The following examples show how to use
java.nio.channels.Selector#open() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WakeupSpeed.java From openjdk-8-source with GNU General Public License v2.0 | 6 votes |
public static void main(String argv[]) throws Exception { int waitTime = 4000; Selector selector = Selector.open(); try { selector.wakeup(); long t1 = System.currentTimeMillis(); selector.select(waitTime); long t2 = System.currentTimeMillis(); long totalTime = t2 - t1; if (totalTime > waitTime) throw new RuntimeException("Test failed"); } finally { selector.close(); } }
Example 2
Source File: SelectorImpl.java From TencentKona-8 with GNU General Public License v2.0 | 6 votes |
private void startSelector() { try { selector = Selector.open(); } catch (IOException e) { if (orb.transportDebugFlag) { dprint(".startSelector: Selector.open: IOException: ", e); } // REVISIT - better handling/reporting RuntimeException rte = new RuntimeException(".startSelector: Selector.open exception"); rte.initCause(e); throw rte; } setDaemon(true); start(); selectorStarted = true; if (orb.transportDebugFlag) { dprint(".startSelector: selector.start completed."); } }
Example 3
Source File: NIOAcceptor.java From Mycat-NIO with Apache License 2.0 | 6 votes |
public NIOAcceptor(String name, String bindIp, int port, ConnectionFactory factory, NIOReactorPool reactorPool) throws IOException { super.setName(name); this.port = port; this.selector = Selector.open(); this.serverChannel = ServerSocketChannel.open(); this.serverChannel.configureBlocking(false); /** 设置TCP属性 */ serverChannel.setOption(StandardSocketOptions.SO_REUSEADDR, true); serverChannel.setOption(StandardSocketOptions.SO_RCVBUF, 1024 * 16 * 2); // backlog=100 serverChannel.bind(new InetSocketAddress(bindIp, port), 100); this.serverChannel.register(selector, SelectionKey.OP_ACCEPT); this.factory = factory; this.reactorPool = reactorPool; }
Example 4
Source File: SelectorImpl.java From jdk1.8-source-analysis with Apache License 2.0 | 6 votes |
private void startSelector() { try { selector = Selector.open(); } catch (IOException e) { if (orb.transportDebugFlag) { dprint(".startSelector: Selector.open: IOException: ", e); } // REVISIT - better handling/reporting RuntimeException rte = new RuntimeException(".startSelector: Selector.open exception"); rte.initCause(e); throw rte; } setDaemon(true); start(); selectorStarted = true; if (orb.transportDebugFlag) { dprint(".startSelector: selector.start completed."); } }
Example 5
Source File: NetworkHandler.java From RipplePower with Apache License 2.0 | 6 votes |
/** * Creates the network listener * * @param maxConnections * The maximum number of connections * @param maxOutbound * The maximum number of outbound connections * @param hostName * The host name for this port or null * @param listenPort * The port to listen on * @param staticAddresses * Static peer address * @param blacklist * Peer blacklist * @throws IOException * I/O error */ public NetworkHandler(int maxConnections, int maxOutbound, String hostName, int listenPort, PeerAddress[] staticAddresses, List<BlacklistEntry> blacklist) throws IOException { this.maxConnections = maxConnections; this.maxOutbound = maxOutbound; this.hostName = hostName; BTCLoader.listenPort = listenPort; peerBlacklist.addAll(blacklist); // // Create the selector for listening for network events // networkSelector = Selector.open(); // // Build the static peer address list // if (staticAddresses != null) { staticConnections = true; this.maxOutbound = Math.min(this.maxOutbound, staticAddresses.length); for (PeerAddress address : staticAddresses) { address.setStatic(true); BTCLoader.peerAddresses.add(0, address); BTCLoader.peerMap.put(address, address); } } }
Example 6
Source File: TCPContinuationProxy.java From loom-fiber with MIT License | 6 votes |
@SuppressWarnings("resource") public static void main(String[] args) throws IOException { var server = ServerSocketChannel.open(); server.bind(new InetSocketAddress(7777)); System.out.println("server bound to " + server.getLocalAddress()); var remote = SocketChannel.open(); remote.connect(new InetSocketAddress(InetAddress.getByName(Host.NAME), 7)); remote.configureBlocking(false); var selector = Selector.open(); var scheduler = new Scheduler(selector); System.out.println("accepting ..."); var client = server.accept(); client.configureBlocking(false); var cont1 = new Continuation(SCOPE, runnable(scheduler, client, remote)); var cont2 = new Continuation(SCOPE, runnable(scheduler, remote, client)); scheduler.inject(cont1); scheduler.inject(cont2); scheduler.loop(); }
Example 7
Source File: NioProcessor.java From neoscada with Eclipse Public License 1.0 | 6 votes |
/** * In the case we are using the java select() method, this method is used to * trash the buggy selector and create a new one, registering all the * sockets on it. */ @Override protected void registerNewSelector() throws IOException { synchronized (selector) { Set<SelectionKey> keys = selector.keys(); // Open a new selector Selector newSelector = Selector.open(); // Loop on all the registered keys, and register them on the new selector for (SelectionKey key : keys) { SelectableChannel ch = key.channel(); // Don't forget to attache the session, and back ! NioSession session = (NioSession) key.attachment(); SelectionKey newKey = ch.register(newSelector, key.interestOps(), session); session.setSelectionKey(newKey); } // Now we can close the old selector and switch it selector.close(); selector = newSelector; } }
Example 8
Source File: SelectorImpl.java From jdk8u60 with GNU General Public License v2.0 | 6 votes |
private void startSelector() { try { selector = Selector.open(); } catch (IOException e) { if (orb.transportDebugFlag) { dprint(".startSelector: Selector.open: IOException: " + e); } // REVISIT - better handling/reporting RuntimeException rte = new RuntimeException(".startSelector: Selector.open exception"); rte.initCause(e); throw rte; } setDaemon(true); start(); selectorStarted = true; if (orb.transportDebugFlag) { dprint(".startSelector: selector.start completed."); } }
Example 9
Source File: SelectorTest.java From cava with Apache License 2.0 | 6 votes |
@Test void selectorRemovesKeysOnChannelCloseWhenSelecting() throws Exception { Pipe pipe = Pipe.open(); Selector selector = Selector.open(); SelectableChannel source = pipe.source(); source.configureBlocking(false); SelectionKey key = source.register(selector, OP_READ); assertTrue(selector.keys().contains(key)); source.close(); assertTrue(selector.keys().contains(key)); selector.selectNow(); assertFalse(selector.keys().contains(key)); }
Example 10
Source File: TcpProxyServer.java From SmartProxy with GNU General Public License v3.0 | 5 votes |
public TcpProxyServer(int port) throws IOException { m_Selector = Selector.open(); m_ServerSocketChannel = ServerSocketChannel.open(); m_ServerSocketChannel.configureBlocking(false); m_ServerSocketChannel.socket().bind(new InetSocketAddress(port)); m_ServerSocketChannel.register(m_Selector, SelectionKey.OP_ACCEPT); this.Port=(short) m_ServerSocketChannel.socket().getLocalPort(); System.out.printf("AsyncTcpServer listen on %d success.\n", this.Port&0xFFFF); }
Example 11
Source File: TestMessageIO.java From tracing-framework with BSD 3-Clause "New" or "Revised" License | 5 votes |
/** Waits for up to 1 second for the server to be acceptable */ private static void awaitAcceptable(ServerSocketChannel channel) throws IOException { Selector selector = Selector.open(); SelectionKey key = channel.register(selector, SelectionKey.OP_ACCEPT); try { assertEquals(true, awaitOp(selector, key, SelectionKey.OP_ACCEPT)); } finally { // Cancel key and close selector key.cancel(); selector.close(); } }
Example 12
Source File: NIOConnector.java From heisenberg with Apache License 2.0 | 4 votes |
public NIOConnector(String name) throws IOException { super.setName(name); this.name = name; this.selector = Selector.open(); this.connectQueue = new LinkedBlockingQueue<BackendConnection>(); }
Example 13
Source File: VhostsService.java From Virtual-Hosts with GNU General Public License v3.0 | 4 votes |
@Override public void onCreate() { // registerNetReceiver(); super.onCreate(); if (isOAndBoot) { //android 8.0 boot if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel("vhosts_channel_id", "System", NotificationManager.IMPORTANCE_NONE); NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); manager.createNotificationChannel(channel); Notification notification = new Notification.Builder(this, "vhosts_channel_id") .setSmallIcon(R.mipmap.ic_launcher) .setContentTitle("Virtual Hosts Running") .build(); startForeground(1, notification); } isOAndBoot=false; } setupHostFile(); setupVPN(); if (vpnInterface == null) { LogUtils.d(TAG, "unknow error"); stopVService(); return; } isRunning = true; try { udpSelector = Selector.open(); tcpSelector = Selector.open(); deviceToNetworkUDPQueue = new ConcurrentLinkedQueue<>(); deviceToNetworkTCPQueue = new ConcurrentLinkedQueue<>(); networkToDeviceQueue = new ConcurrentLinkedQueue<>(); udpSelectorLock = new ReentrantLock(); tcpSelectorLock = new ReentrantLock(); executorService = Executors.newFixedThreadPool(5); executorService.submit(new UDPInput(networkToDeviceQueue, udpSelector, udpSelectorLock)); executorService.submit(new UDPOutput(deviceToNetworkUDPQueue, networkToDeviceQueue, udpSelector, udpSelectorLock, this)); executorService.submit(new TCPInput(networkToDeviceQueue, tcpSelector, tcpSelectorLock)); executorService.submit(new TCPOutput(deviceToNetworkTCPQueue, networkToDeviceQueue, tcpSelector, tcpSelectorLock, this)); executorService.submit(new VPNRunnable(vpnInterface.getFileDescriptor(), deviceToNetworkUDPQueue, deviceToNetworkTCPQueue, networkToDeviceQueue)); LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(BROADCAST_VPN_STATE).putExtra("running", true)); LogUtils.i(TAG, "Started"); } catch (Exception e) { // TODO: Here and elsewhere, we should explicitly notify the user of any errors // and suggest that they stop the service, since we can't do it ourselves LogUtils.e(TAG, "Error starting service", e); stopVService(); } }
Example 14
Source File: LotsOfCancels.java From jdk8u60 with GNU General Public License v2.0 | 4 votes |
ClientThread(SocketAddress address) throws Exception { this.address = address; selector = Selector.open(); setDaemon(true); }
Example 15
Source File: SelectorTest.java From j2objc with Apache License 2.0 | 4 votes |
/** * @tests java.nio.channel.Selector#select(long) */ public void test_selectJ_SelectorClosed() throws IOException { assert_select_SelectorClosed(SelectType.TIMEOUT, 0); selector = Selector.open(); assert_select_SelectorClosed(SelectType.TIMEOUT, WAIT_TIME); }
Example 16
Source File: SelectAfterRead.java From jdk8u-dev-jdk with GNU General Public License v2.0 | 4 votes |
public static void main(String[] argv) throws Exception { // server: accept connection and write one byte try (ByteServer server = new ByteServer(); SocketChannel sc = SocketChannel.open(server.address())) { server.acceptConnection(); server.write(1); try (Selector sel = Selector.open()) { sc.read(ByteBuffer.allocate(1)); sc.configureBlocking(false); sc.register(sel, SelectionKey.OP_READ); // previously on Windows select would select channel here, although there was // nothing to read if (sel.selectNow() != 0) throw new Exception("Select returned nonzero value"); } } // Now we will test a two reads combination // server: accept connection and write two bytes try (ByteServer server = new ByteServer(); SocketChannel sc = SocketChannel.open(server.address())) { server.acceptConnection(); server.write(2); try (Selector sel = Selector.open()) { sc.configureBlocking(false); sc.register(sel, SelectionKey.OP_READ); if (sel.select(TIMEOUT) != 1) throw new Exception("One selected key expected"); sel.selectedKeys().clear(); // previously on Windows a channel would get selected only once if (sel.selectNow() != 1) throw new Exception("One selected key expected"); // Previously on Windows two consequent reads would cause select() // to select a channel, although there was nothing remaining to // read in the channel if (sc.read(ByteBuffer.allocate(1)) != 1) throw new Exception("One byte expected"); if (sc.read(ByteBuffer.allocate(1)) != 1) throw new Exception("One byte expected"); if (sel.selectNow() != 0) throw new Exception("Select returned nonzero value"); } } }
Example 17
Source File: NIOReactor.java From dble with GNU General Public License v2.0 | 4 votes |
private RW() throws IOException { this.selector = Selector.open(); this.registerQueue = new ConcurrentLinkedQueue<>(); }
Example 18
Source File: RacyDeregister.java From hottub with GNU General Public License v2.0 | 4 votes |
public static void main(String[] args) throws Exception { InetAddress addr = InetAddress.getByName(null); ServerSocketChannel sc = ServerSocketChannel.open(); sc.socket().bind(new InetSocketAddress(addr, 0)); SocketChannel.open(new InetSocketAddress(addr, sc.socket().getLocalPort())); SocketChannel accepted = sc.accept(); accepted.configureBlocking(false); SocketChannel.open(new InetSocketAddress(addr, sc.socket().getLocalPort())); SocketChannel accepted2 = sc.accept(); accepted2.configureBlocking(false); final Selector sel = Selector.open(); SelectionKey key2 = accepted2.register(sel, SelectionKey.OP_READ); final SelectionKey[] key = new SelectionKey[]{ accepted.register(sel, SelectionKey.OP_READ)}; // thread that will be changing key[0].interestOps to OP_READ | OP_WRITE new Thread() { public void run() { try { for (int k = 0; k < 15; k++) { for (int i = 0; i < 10000; i++) { synchronized (notifyLock) { synchronized (selectorLock) { sel.wakeup(); key[0].interestOps(SelectionKey.OP_READ | SelectionKey.OP_WRITE); } notified = false; long beginTime = System.currentTimeMillis(); while (true) { notifyLock.wait(5000); if (notified) { break; } long endTime = System.currentTimeMillis(); if (endTime - beginTime > 5000) { succTermination = false; // wake up main thread doing select() sel.wakeup(); return; } } } } } succTermination = true; // wake up main thread doing select() sel.wakeup(); } catch (Exception e) { System.out.println(e); succTermination = true; // wake up main thread doing select() sel.wakeup(); } } }.start(); // main thread will be doing registering/deregistering with the sel while (true) { sel.select(); if (Boolean.TRUE.equals(succTermination)) { System.out.println("Test passed"); sel.close(); sc.close(); break; } else if (Boolean.FALSE.equals(succTermination)) { System.out.println("Failed to pass the test"); sel.close(); sc.close(); throw new RuntimeException("Failed to pass the test"); } synchronized (selectorLock) { } if (sel.selectedKeys().contains(key[0]) && key[0].isWritable()) { synchronized (notifyLock) { notified = true; notifyLock.notify(); key[0].cancel(); sel.selectNow(); key2 = accepted2.register(sel, SelectionKey.OP_READ); key[0] = accepted.register(sel, SelectionKey.OP_READ); } } key2.cancel(); sel.selectedKeys().clear(); } }
Example 19
Source File: NioEchoServer.java From JavaTutorial with Apache License 2.0 | 4 votes |
/** * 启动服务器。 * * @param port 服务监听的端口 * @param selectTimeout {@link Selector}检查通道就绪状态的超时时间(单位:毫秒) */ private static void startServer(int port, int selectTimeout) { ServerSocketChannel serverChannel = null; try { serverChannel = ServerSocketChannel.open(); serverChannel.configureBlocking(false); ServerSocket serverSocket = serverChannel.socket(); serverSocket.bind(new InetSocketAddress(port)); if (logger.isLoggable(Level.INFO)) { logger.info("NIO echo网络服务启动完毕,监听端口:" +port); } Selector selector = Selector.open(); serverChannel.register(selector, SelectionKey.OP_ACCEPT); while (true) { int selectNum = selector.select(selectTimeout); if (0 == selectNum) { continue; } Set<SelectionKey> selectionKeys = selector.selectedKeys(); Iterator<SelectionKey> it = selectionKeys.iterator(); while (it.hasNext()) { SelectionKey selectionKey = (SelectionKey) it.next(); // 接受新的Socket连接 if (selectionKey.isAcceptable()) { acceptNew(selector, selectionKey); } // 读取并处理Socket的数据 if (selectionKey.isReadable()) { readData(selector, selectionKey); } it.remove(); } // end of while iterator } } catch (IOException e) { logger.log(Level.SEVERE, "处理网络连接出错", e); } }
Example 20
Source File: LotsOfCancels.java From jdk8u-dev-jdk with GNU General Public License v2.0 | 4 votes |
ClientThread(SocketAddress address) throws Exception { this.address = address; selector = Selector.open(); setDaemon(true); }