Java 8 SelectableChannel - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 SelectableChannel class.

This code example shows how to use the following methods:sleep, register, socket, configureBlocking
 
	public static void main(String[] args) {
		try { VocabServer v = new VocabServer(4444);
		Thread t = new Thread(v);
		t.start();
		try {Thread.sleep(10000); } catch (Exception e ) {}
		v.stopServer();
		} catch (IOException e) { e.printStackTrace();}
	} 
	 
	ServerSocketChannel serverChannel;
	public VocabServer(int port) throws IOException {
		serverChannel = ServerSocketChannel.open();
		selector = Selector.open();
		serverChannel.socket().bind (new InetSocketAddress(port));
		serverChannel.configureBlocking (false);
		serverChannel.register (selector, SelectionKey.OP_ACCEPT);		
		System.err.println("Vocab server initialized on port " + port);
	} 
	 

7
CodeRank
This code example shows how to use the following methods:register, bind, isOpen, configureBlocking
This code example shows how to use the following methods:connect, socket, configureBlocking, AbstractSelectableChannel.register
        return false; 
    } 
 
    private void configureSocket() throws IOException {
        if (socketChannel!=null) {
            socketChannel.configureBlocking(false);
            socketChannel.socket().setSendBufferSize(getTxBufSize());
            socketChannel.socket().setReceiveBufferSize(getRxBufSize());
            socketChannel.socket().setSoTimeout((int)getTimeout());
            socketChannel.socket().setSoLinger(getSoLingerOn(),getSoLingerOn()?getSoLingerTime():0);
            socketChannel.socket().setTcpNoDelay(getTcpNoDelay());
            socketChannel.socket().setKeepAlive(getSoKeepAlive());
            socketChannel.socket().setReuseAddress(getSoReuseAddress());
            socketChannel.socket().setOOBInline(getOoBInline());
            socketChannel.socket().setSoLinger(getSoLingerOn(),getSoLingerTime());
            socketChannel.socket().setTrafficClass(getSoTrafficClass());
        } else if (dataChannel!=null) {
            dataChannel.configureBlocking(false);
            dataChannel.socket().setSendBufferSize(getUdpTxBufSize());
            dataChannel.socket().setReceiveBufferSize(getUdpRxBufSize());

6
CodeRank
This code example shows how to use the following methods:isOpen
   * is zero, it will be treated as infinite timeout. The socket's 
   * channel will be configured to be non-blocking. 
   *  
   * @param channel  
   *        Channel for writing, should also be a {@link SelectableChannel}.   
   *        The channel will be configured to be non-blocking. 
   * @param timeout timeout in milliseconds. must not be negative. 
   * @throws IOException 
   */ 
  public SocketOutputStream(WritableByteChannel channel, long timeout) 
                                                         throws IOException { 
    SocketIOWithTimeout.checkChannelValidity(channel);
    writer = new Writer(channel, timeout);
  } 
   
  /** 
   * Same as SocketOutputStream(socket.getChannel(), timeout):<br><br> 
   *  
   * Create a new ouput stream with the given timeout. If the timeout 
   * is zero, it will be treated as infinite timeout. The socket's 
6
CodeRank
This code example shows how to use the following methods:configureBlocking, isRegistered, keyFor, register, isOpen
     */ 
    public void registerChannel(SelectableChannel channel, int operations, Object handler)
            throws IOException {
        assert this == Thread.currentThread() : "Method not called from selector thread";
 
        if (!channel.isOpen()) {
            throw new IOException("Channel is closed");
        } 
 
        if (channel.isRegistered()) {
            SelectionKey key = channel.keyFor(selector);
            assert key != null : "The channel is not registered to selector?";
            key.interestOps(operations);
            key.attach(handler);
        } else { 
            channel.configureBlocking(false);
            channel.register(selector, operations, handler);
        } 
    } 
 

6
CodeRank
This code example shows how to use the following methods:register, connect, finishConnect, isConnected
    @Override 
    public boolean connect(SocketAddress remote) throws IOException {
 
        long start = System.currentTimeMillis();
        //log.debug("trying to connect"); 
        socketChannel.connect(remote);
        while (selector.select(connectTimeout) > 0) {
            selector.selectedKeys().remove(channelKey);
            //log.debug("selected connect"); 
            //log.debug("Spent " + (System.currentTimeMillis() - start)); 
            if (!channelKey.isConnectable()) {
                throw new IllegalStateException("Socket channel is in not connectable state");
            } 
 
            socketChannel.finishConnect();
            channelKey = socketChannel.register(selector, SelectionKey.OP_READ);
            if (log.isDebugEnabled()) { 
                log.debug("Connected socket in " + (System.currentTimeMillis() - start));
            } 
            if (!socketChannel.isConnected()) {

6
CodeRank
This code example shows how to use the following methods:connect, socket, register, configureBlocking
 
		//trigger_buf.put(trigger_bytes); 
	    //trigger_buf.flip(); 
		 
		channel = DatagramChannel.open();
		channel.configureBlocking(false);
		channel.socket().bind(new InetSocketAddress(video_port));
		channel.connect(new InetSocketAddress(drone_addr, video_port));
		 
		 
		selector = Selector.open();
		channel.register(selector, SelectionKey.OP_READ | SelectionKey.OP_WRITE);
	} 
 
	private void disconnect() { 
		Log.v("Drone Control", "Video Reader: Told to disconnect");
		try { 
			selector.close();
		} catch (IOException iox) {
			// Ignore 
6
CodeRank
This code example shows how to use the following methods:configureBlocking, register
	boolean done = false;
	Selector selector = null;
	this.endTime = endTime;
	try { 
		selector = Selector.open();
		channel.configureBlocking(false);
		key = channel.register(selector, SelectionKey.OP_READ);
		done = true;
	} 
	finally { 
		if (!done && selector != null)
			selector.close();
		if (!done)
			channel.close();
	} 
} 
 
static protected void 
blockUntil(SelectionKey key, long endTime) throws IOException {
	long timeout = endTime - System.currentTimeMillis();
6
CodeRank
This code example shows how to use the following methods:register, write, configureBlocking
 
	/***/ 
	private void writeToCommonChannel(SelectionKey key, Object message)
			throws IOException { 
		SocketChannel client = (SocketChannel) key.channel();
		client.configureBlocking(false);
		client.register(selector, SelectionKey.OP_READ | SelectionKey.OP_WRITE);
		ByteBuffer dst = ByteBuffer.allocate(1024);
		dst.put(message.toString().getBytes());
		dst.flip();
		client.write(dst);
	} 
} 
6
CodeRank
This code example shows how to use the following methods:register, socket, configureBlocking
	public PServer(int port, FileSystem fs, Path ttablePath) throws IOException {
		ttable = new TTable_monolithic_IFAs(fs, ttablePath, true);
		 
		serverChannel = ServerSocketChannel.open();
		selector = Selector.open();
		serverChannel.socket().bind (new InetSocketAddress(port));
		serverChannel.configureBlocking (false);
		serverChannel.register (selector, SelectionKey.OP_ACCEPT);		
		System.err.println("PServer initialized on " + InetAddress.getLocalHost() + ":" + port);
	} 
	 
	public void stopServer() { 
		System.err.println("Stopping PServer...");
		try { selector.close(); serverChannel.close(); } catch (Exception e) { System.err.println("Caught " + e); }
	} 
	 
	Selector selector = null;
	long reqs=0;
	 
	public void run() { 
6
CodeRank
See Code Examples for Java 8 SelectableChannel Methods: