SelectableChannel register - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for SelectableChannel register method.

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: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: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:connect, socket, register, configureBlocking
    } 
 
    private void connect() throws IOException, ClosedChannelException { 
         
        channel = DatagramChannel.open();
        channel.configureBlocking(false);
        channel.socket().bind(new InetSocketAddress(data_port));
        channel.connect(new InetSocketAddress(drone_addr, data_port));
 
        selector = Selector.open();
        channel.register(selector, SelectionKey.OP_READ | SelectionKey.OP_WRITE);
    } 
 
    private void disconnect() { 
        try { 
            if (selector.isOpen())
                selector.close();
        } catch (IOException iox) 
        { 
            // ignore 
6
CodeRank
This code example shows how to use the following methods:configureBlocking, register
                                   SelectableChannel channel,
                                   int ops,
                                   Object attach) throws Exception {
        if (channel == null)return; // could happen
        // set the new channel non-blocking 
        channel.configureBlocking(false);
        // register it with the selector 
        channel.register(selector, ops, attach);
    } 
 
    /** 
     * Start thread and listen 
     */ 
    @Override 
    public void run() { 
        running = true;
        try { 
            listen(); 
        } catch (Exception x) {
            log.error("Unable to run replication listener.", x);
5
CodeRank
This code example shows how to use the following methods:register, socket, configureBlocking
     
    public Listener() throws IOException { 
      address = new InetSocketAddress(bindAddress, port);
      // Create a new server socket and set to non blocking mode 
      acceptChannel = ServerSocketChannel.open();
      acceptChannel.configureBlocking(false);
 
      // Bind the server socket to the local host and port 
      bind(acceptChannel.socket(), address, backlogLength);
      port = acceptChannel.socket().getLocalPort(); //Could be an ephemeral port
      // create a selector; 
      selector= Selector.open();
 
      // Register accepts on the server socket with the selector. 
      acceptChannel.register(selector, SelectionKey.OP_ACCEPT);
      this.setName("IPC Server listener on " + port);
      this.setDaemon(true);
    } 
    /** cleanup connections from connectionList. Choose a random range 
     * to scan and also have a limit on the number of the connections 
5
CodeRank
This code example shows how to use the following methods:register, configureBlocking
this.server = server;
		this.bindEndpoint = bindEndpoint;
 
		socketChannel = ServerSocketChannel.open();
		socketChannel.configureBlocking(false);
 
		acceptSelector = Selector.open();
		socketChannel.register(acceptSelector, SelectionKey.OP_ACCEPT);
 
		keepRunning = true;
	} 
 
	/** 
	 * Runs in a loop, accepting connections and forwarding them to the server 
	 */ 
	@Override 
	public void run() 
	{ 
		try { 
5
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()) {

5
CodeRank

Related SelectableChannel register Questions & Answers:

register Method Overview
Registers this channel with the given selector, returning a selection key.
See Code Examples for other Java 8 SelectableChannel Methods: