Java 8 AbstractSelectableChannel - 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 AbstractSelectableChannel class.

This code example shows how to use the following methods:socket, configureBlocking
    public SocketConnection(SocketChannel socket, MessageSerializer<T> serializer) {
        this.socket = socket;
        try { 
            // NOTE: we use non-blocking IO as there is no reliable way when using blocking IO to shutdown reads while 
            // keeping writes active. For example, Socket.shutdownInput() does not work on Windows. 
            socket.configureBlocking(false);
            outstr = new SocketOutputStream(socket);
            instr = new SocketInputStream(socket);
        } catch (IOException e) {
            throw UncheckedException.throwAsUncheckedException(e);
        } 
        InetSocketAddress localSocketAddress = (InetSocketAddress) socket.socket().getLocalSocketAddress();
        localAddress = new SocketInetAddress(localSocketAddress.getAddress(), localSocketAddress.getPort());
        InetSocketAddress remoteSocketAddress = (InetSocketAddress) socket.socket().getRemoteSocketAddress();
        remoteAddress = new SocketInetAddress(remoteSocketAddress.getAddress(), remoteSocketAddress.getPort());
        objectReader = serializer.newReader(instr, localAddress, remoteAddress);
        objectWriter = serializer.newWriter(outstr);
    } 
 
    @Override 
8
CodeRank
This code example shows how to use the following methods:connect, finishConnect, isConnected, isConnectionPending, close
        this.channel1.close();
        assertFalse(this.channel1.isOpen());
    } 
 
    public void testIsConnected() throws Exception {
        assertFalse(this.channel1.isConnected());// not connected
        this.channel1.configureBlocking(false);
        assertFalse(this.channel1.connect(localAddr1));
        assertFalse(this.channel1.isConnected());
        assertTrue(this.channel1.isConnectionPending());
        assertTrue(tryFinish());
        assertTrue(this.channel1.isConnected());
        this.channel1.close();
        assertFalse(this.channel1.isConnected());
    } 
 
    public void testIsConnectionPending() throws Exception {
        // ensure 
        ensureServerClosed(); 
        this.channel1.configureBlocking(false);

7
CodeRank
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:configureBlocking
	} 
 
	public void connect() { 
		try { 
			channel = SocketChannel.open(new InetSocketAddress(host, port));
			channel.configureBlocking(false);
		} catch (IOException e) { e.printStackTrace(); }
		socket = new AsynchronousSocket(channel);
		IOLoop.INSTANCE.addHandler(channel, socket, SelectionKey.OP_READ, ByteBuffer.allocate(1024));
	} 
	 
	public void get(String value, AsyncResult<String> cb) {
		socket.write("GET deft\r\n", new WriteCallback(cb));
	} 
	 
	private class WriteCallback implements AsyncCallback { 
 
		private final AsyncResult<String> cb;
		 
		public WriteCallback(AsyncResult<String> cb) { 
6
CodeRank
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:socket, validOps, isBlocking, isRegistered, provider
            // Android doesn't cache the timeout, so the getsockopt(2) fails and throws. 
        } 
    } 
 
    public void testChannelBasicStatus() { 
        ServerSocket gotSocket = this.serverChannel.socket();
        assertFalse(gotSocket.isClosed());
        assertTrue(this.serverChannel.isBlocking());
        assertFalse(this.serverChannel.isRegistered());
        assertEquals(SelectionKey.OP_ACCEPT, this.serverChannel.validOps());
        assertEquals(SelectorProvider.provider(), this.serverChannel.provider());
    } 
 
    // ------------------------------------------------------------------- 
    // Test for accept() 
    // ------------------------------------------------------------------- 
 
    /* 
     * Test method for 'java.nio.channels.ServerSocketChannel.accept()' 
     */ 
6
CodeRank
This code example shows how to use the following methods:socket, close, configureBlocking
	protected void doAccept(final Selector selector, ServerSocketChannel server, long now) throws IOException {
		logger.debug("New accept"); 
		SocketChannel channel = server.accept();
		if (this.isShuttingDown()) {
			if (logger.isInfoEnabled()) { 
				logger.info("New connection from " + channel.socket().getInetAddress().getHostAddress()
						+ " rejected; the server is in the process of shutting down."); 
			} 
			channel.close();
		} 
		else { 
			try { 
				channel.configureBlocking(false);
				Socket socket = channel.socket();
				setSocketAttributes(socket);
				TcpNioConnection connection = createTcpNioConnection(channel);
				if (connection == null) {
					return; 
				} 
				connection.setTaskExecutor(this.getTaskExecutor());

6
CodeRank
This code example shows how to use the following methods:isConnected, socket, configureBlocking
   * @param socketChannel Already created SocketChannel object 
   * @throws IOException if there is an error setting up the streams 
   */ 
  public TNonblockingSocket(SocketChannel socketChannel) throws IOException {
    this(socketChannel, 0, null);
    if (!socketChannel.isConnected()) throw new IOException("Socket must already be connected");
  } 
 
  private TNonblockingSocket(SocketChannel socketChannel, int timeout, SocketAddress socketAddress)
      throws IOException {
    socketChannel_ = socketChannel;
    socketAddress_ = socketAddress;
 
    // make it a nonblocking channel 
    socketChannel.configureBlocking(false);
 
    // set options 
    Socket socket = socketChannel.socket();
    socket.setSoLinger(false, 0);
    socket.setTcpNoDelay(true);

6
CodeRank
This code example shows how to use the following methods:configureBlocking
	public void setTrafficClass(int ival) throws IOException {
		ch.socket().setTrafficClass(ival);
	} 
 
	public void setNonBlocking() throws IOException {
		ch.configureBlocking(false);
	} 
	 
	@Override 
	public InetSocketAddress getRemoteAddress() {
		return (InetSocketAddress) ch.socket().getRemoteSocketAddress();
	} 
	 
	@Override 
	public String toString() {
		 
		return "InetClientSocket[" + ch.toString()  
			+ " open="+ch.isOpen()
			+ " addr="+ch.socket().getLocalSocketAddress()
			+ " remote="+ch.socket().getRemoteSocketAddress()
6
CodeRank
See Code Examples for Java 8 AbstractSelectableChannel Methods: