Java Code Examples for java.nio.channels.SocketChannel#isBlocking()

The following examples show how to use java.nio.channels.SocketChannel#isBlocking() . 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: AsynchronousTlsChannel.java    From pgadba with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Initializes a new instance of this class.
 *
 * @param channelGroup group to associate new new channel to
 * @param tlsChannel existing TLS channel to be used asynchronously
 * @param socketChannel underlying socket
 * @throws ClosedChannelException if any of the underlying channels are closed.
 * @throws IllegalArgumentException is the socket is in blocking mode
 */
public AsynchronousTlsChannel(
    AsynchronousTlsChannelGroup channelGroup,
    TlsChannel tlsChannel,
    SocketChannel socketChannel) throws ClosedChannelException, IllegalArgumentException {
  if (!tlsChannel.isOpen() || !socketChannel.isOpen()) {
    throw new ClosedChannelException();
  }
  if (socketChannel.isBlocking()) {
    throw new IllegalArgumentException("socket channel must be in non-blocking mode");
  }
  this.group = channelGroup;
  this.tlsChannel = tlsChannel;
  this.registeredSocket = channelGroup.registerSocket(tlsChannel, socketChannel);
}
 
Example 2
Source File: NioSocket.java    From triplea with GNU General Public License v3.0 5 votes vote down vote up
/** Add this channel. The channel will either be unquarantined, or an error will be reported */
public void add(final SocketChannel channel, final QuarantineConversation conversation) {
  if (channel.isBlocking()) {
    throw new IllegalArgumentException("Channel is blocking");
  }
  // add the decoder first, so it can quarantine the messages!
  decoder.add(channel, conversation);
  reader.add(channel);
}
 
Example 3
Source File: FIXInitiatorTest.java    From philadelphia with Apache License 2.0 5 votes vote down vote up
private void receiveBlocking(FIXConnection connection) throws IOException {
    SocketChannel channel = connection.getChannel();

    if (channel.isBlocking()) {
        connection.receive();
    } else {
        channel.configureBlocking(true);

        try {
            connection.receive();
        } finally {
            channel.configureBlocking(false);
        }
    }
}
 
Example 4
Source File: AsynchronousTlsChannel.java    From tls-channel with MIT License 5 votes vote down vote up
/**
 * Initializes a new instance of this class.
 *
 * @param channelGroup group to associate new new channel to
 * @param tlsChannel existing TLS channel to be used asynchronously
 * @param socketChannel underlying socket
 * @throws ClosedChannelException if any of the underlying channels are closed.
 * @throws IllegalArgumentException is the socket is in blocking mode
 */
public AsynchronousTlsChannel(
    AsynchronousTlsChannelGroup channelGroup, TlsChannel tlsChannel, SocketChannel socketChannel)
    throws ClosedChannelException, IllegalArgumentException {
  if (!tlsChannel.isOpen() || !socketChannel.isOpen()) {
    throw new ClosedChannelException();
  }
  if (socketChannel.isBlocking()) {
    throw new IllegalArgumentException("socket channel must be in non-blocking mode");
  }
  this.group = channelGroup;
  this.tlsChannel = tlsChannel;
  this.registeredSocket = channelGroup.registerSocket(tlsChannel, socketChannel);
}
 
Example 5
Source File: SocketIOWithTimeout.java    From hadoop with Apache License 2.0 4 votes vote down vote up
/**
 * The contract is similar to {@link SocketChannel#connect(SocketAddress)} 
 * with a timeout.
 * 
 * @see SocketChannel#connect(SocketAddress)
 * 
 * @param channel - this should be a {@link SelectableChannel}
 * @param endpoint
 * @throws IOException
 */
static void connect(SocketChannel channel, 
                    SocketAddress endpoint, int timeout) throws IOException {
  
  boolean blockingOn = channel.isBlocking();
  if (blockingOn) {
    channel.configureBlocking(false);
  }
  
  try { 
    if (channel.connect(endpoint)) {
      return;
    }

    long timeoutLeft = timeout;
    long endTime = (timeout > 0) ? (Time.now() + timeout): 0;
    
    while (true) {
      // we might have to call finishConnect() more than once
      // for some channels (with user level protocols)
      
      int ret = selector.select((SelectableChannel)channel, 
                                SelectionKey.OP_CONNECT, timeoutLeft);
      
      if (ret > 0 && channel.finishConnect()) {
        return;
      }
      
      if (ret == 0 ||
          (timeout > 0 &&  
            (timeoutLeft = (endTime - Time.now())) <= 0)) {
        throw new SocketTimeoutException(
                  timeoutExceptionString(channel, timeout, 
                                         SelectionKey.OP_CONNECT));
      }
    }
  } catch (IOException e) {
    // javadoc for SocketChannel.connect() says channel should be closed.
    try {
      channel.close();
    } catch (IOException ignored) {}
    throw e;
  } finally {
    if (blockingOn && channel.isOpen()) {
      channel.configureBlocking(true);
    }
  }
}
 
Example 6
Source File: SocketIOWithTimeout.java    From big-c with Apache License 2.0 4 votes vote down vote up
/**
 * The contract is similar to {@link SocketChannel#connect(SocketAddress)} 
 * with a timeout.
 * 
 * @see SocketChannel#connect(SocketAddress)
 * 
 * @param channel - this should be a {@link SelectableChannel}
 * @param endpoint
 * @throws IOException
 */
static void connect(SocketChannel channel, 
                    SocketAddress endpoint, int timeout) throws IOException {
  
  boolean blockingOn = channel.isBlocking();
  if (blockingOn) {
    channel.configureBlocking(false);
  }
  
  try { 
    if (channel.connect(endpoint)) {
      return;
    }

    long timeoutLeft = timeout;
    long endTime = (timeout > 0) ? (Time.now() + timeout): 0;
    
    while (true) {
      // we might have to call finishConnect() more than once
      // for some channels (with user level protocols)
      
      int ret = selector.select((SelectableChannel)channel, 
                                SelectionKey.OP_CONNECT, timeoutLeft);
      
      if (ret > 0 && channel.finishConnect()) {
        return;
      }
      
      if (ret == 0 ||
          (timeout > 0 &&  
            (timeoutLeft = (endTime - Time.now())) <= 0)) {
        throw new SocketTimeoutException(
                  timeoutExceptionString(channel, timeout, 
                                         SelectionKey.OP_CONNECT));
      }
    }
  } catch (IOException e) {
    // javadoc for SocketChannel.connect() says channel should be closed.
    try {
      channel.close();
    } catch (IOException ignored) {}
    throw e;
  } finally {
    if (blockingOn && channel.isOpen()) {
      channel.configureBlocking(true);
    }
  }
}
 
Example 7
Source File: PackedBufferManager.java    From incubator-retired-htrace with Apache License 2.0 4 votes vote down vote up
private SelectionKey doConnect() throws IOException {
  SocketChannel sock = SocketChannel.open();
  SelectionKey sockKey = null;
  boolean success = false;
  try {
    if (sock.isBlocking()) {
      sock.configureBlocking(false);
    }
    InetSocketAddress resolvedEndpoint =
        new InetSocketAddress(conf.endpoint.getHostString(),
            conf.endpoint.getPort());
    resolvedEndpoint.getHostName(); // trigger DNS resolution
    sock.connect(resolvedEndpoint);
    sockKey = sock.register(selector, SelectionKey.OP_CONNECT, sock);
    long startMs = TimeUtil.nowMs();
    long remainingMs = conf.connectTimeoutMs;
    while (true) {
      selector.select(remainingMs);
      for (SelectionKey key : selector.keys()) {
        if (key.isConnectable()) {
          SocketChannel s = (SocketChannel)key.attachment();
          s.finishConnect();
          if (LOG.isTraceEnabled()) {
            LOG.trace("Successfully connected to " + conf.endpointStr + ".");
          }
          success = true;
          return sockKey;
        }
      }
      remainingMs = updateRemainingMs(startMs, conf.connectTimeoutMs);
      if (remainingMs == 0) {
        throw new IOException("Attempt to connect to " + conf.endpointStr +
            " timed out after " +  TimeUtil.deltaMs(startMs, TimeUtil.nowMs()) +
            " ms.");
      }
    }
  } finally {
    if (!success) {
      if (sockKey != null) {
        sockKey.cancel();
      }
      sock.close();
    }
  }
}
 
Example 8
Source File: SocketIOWithTimeout.java    From RDFS with Apache License 2.0 4 votes vote down vote up
/**
 * The contract is similar to {@link SocketChannel#connect(SocketAddress)} 
 * with a timeout.
 * 
 * @see SocketChannel#connect(SocketAddress)
 * 
 * @param channel - this should be a {@link SelectableChannel}
 * @param endpoint
 * @throws IOException
 */
static void connect(SocketChannel channel, 
                    SocketAddress endpoint, int timeout) throws IOException {
  
  boolean blockingOn = channel.isBlocking();
  if (blockingOn) {
    channel.configureBlocking(false);
  }
  
  try { 
    if (channel.connect(endpoint)) {
      return;
    }

    long timeoutLeft = timeout;
    long endTime = (timeout > 0) ? (System.currentTimeMillis() + timeout): 0;
    
    while (true) {
      // we might have to call finishConnect() more than once
      // for some channels (with user level protocols)
      
      int ret = selector.select((SelectableChannel)channel, 
                                SelectionKey.OP_CONNECT, timeoutLeft);
      
      if (ret > 0 && channel.finishConnect()) {
        return;
      }
      
      if (ret == 0 ||
          (timeout > 0 &&  
            (timeoutLeft = (endTime - System.currentTimeMillis())) <= 0)) {
        throw new SocketTimeoutException(
                  timeoutExceptionString(channel, timeout, 
                                         SelectionKey.OP_CONNECT));
      }
    }
  } catch (IOException e) {
    // javadoc for SocketChannel.connect() says channel should be closed.
    try {
      channel.close();
    } catch (IOException ignored) {}
    throw e;
  } finally {
    if (blockingOn && channel.isOpen()) {
      channel.configureBlocking(true);
    }
  }
}
 
Example 9
Source File: SocketIOWithTimeout.java    From stratosphere with Apache License 2.0 4 votes vote down vote up
/**
 * The contract is similar to {@link SocketChannel#connect(SocketAddress)} with a timeout.
 * 
 * @see SocketChannel#connect(SocketAddress)
 * @param channel
 *        - this should be a {@link SelectableChannel}
 * @param endpoint
 * @throws IOException
 */
static void connect(SocketChannel channel, SocketAddress endpoint, int timeout) throws IOException {

	boolean blockingOn = channel.isBlocking();
	if (blockingOn) {
		channel.configureBlocking(false);
	}

	try {
		if (channel.connect(endpoint)) {
			return;
		}

		long timeoutLeft = timeout;
		long endTime = (timeout > 0) ? (System.currentTimeMillis() + timeout) : 0;

		while (true) {
			// we might have to call finishConnect() more than once
			// for some channels (with user level protocols)

			int ret = selector.select((SelectableChannel) channel, SelectionKey.OP_CONNECT, timeoutLeft);

			if (ret > 0 && channel.finishConnect()) {
				return;
			}

			if (ret == 0 || (timeout > 0 && (timeoutLeft = (endTime - System.currentTimeMillis())) <= 0)) {
				throw new SocketTimeoutException(timeoutExceptionString(channel, timeout, SelectionKey.OP_CONNECT));
			}
		}
	} catch (IOException e) {
		// javadoc for SocketChannel.connect() says channel should be closed.
		try {
			channel.close();
		} catch (IOException ignored) {
		}
		throw e;
	} finally {
		if (blockingOn && channel.isOpen()) {
			channel.configureBlocking(true);
		}
	}
}
 
Example 10
Source File: SocketIOWithTimeout.java    From hadoop-gpu with Apache License 2.0 4 votes vote down vote up
/**
 * The contract is similar to {@link SocketChannel#connect(SocketAddress)} 
 * with a timeout.
 * 
 * @see SocketChannel#connect(SocketAddress)
 * 
 * @param channel - this should be a {@link SelectableChannel}
 * @param endpoint
 * @throws IOException
 */
static void connect(SocketChannel channel, 
                    SocketAddress endpoint, int timeout) throws IOException {
  
  boolean blockingOn = channel.isBlocking();
  if (blockingOn) {
    channel.configureBlocking(false);
  }
  
  try { 
    if (channel.connect(endpoint)) {
      return;
    }

    long timeoutLeft = timeout;
    long endTime = (timeout > 0) ? (System.currentTimeMillis() + timeout): 0;
    
    while (true) {
      // we might have to call finishConnect() more than once
      // for some channels (with user level protocols)
      
      int ret = selector.select((SelectableChannel)channel, 
                                SelectionKey.OP_CONNECT, timeoutLeft);
      
      if (ret > 0 && channel.finishConnect()) {
        return;
      }
      
      if (ret == 0 ||
          (timeout > 0 &&  
            (timeoutLeft = (endTime - System.currentTimeMillis())) <= 0)) {
        throw new SocketTimeoutException(
                  timeoutExceptionString(channel, timeout, 
                                         SelectionKey.OP_CONNECT));
      }
    }
  } catch (IOException e) {
    // javadoc for SocketChannel.connect() says channel should be closed.
    try {
      channel.close();
    } catch (IOException ignored) {}
    throw e;
  } finally {
    if (blockingOn && channel.isOpen()) {
      channel.configureBlocking(true);
    }
  }
}