Java Code Examples for java.nio.channels.SelectionKey#OP_READ

The following examples show how to use java.nio.channels.SelectionKey#OP_READ . 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: SocketIOWithTimeout.java    From stratosphere with Apache License 2.0 6 votes vote down vote up
private static String timeoutExceptionString(SelectableChannel channel, long timeout, int ops) {

		String waitingFor;
		switch (ops) {

		case SelectionKey.OP_READ:
			waitingFor = "read";
			break;

		case SelectionKey.OP_WRITE:
			waitingFor = "write";
			break;

		case SelectionKey.OP_CONNECT:
			waitingFor = "connect";
			break;

		default:
			waitingFor = "" + ops;
		}

		return timeout + " millis timeout while " + "waiting for channel to be ready for " + waitingFor + ". ch : "
			+ channel;
	}
 
Example 2
Source File: SocketIOWithTimeout.java    From hadoop with Apache License 2.0 6 votes vote down vote up
private static String timeoutExceptionString(SelectableChannel channel,
                                             long timeout, int ops) {
  
  String waitingFor;
  switch(ops) {
  
  case SelectionKey.OP_READ :
    waitingFor = "read"; break;
    
  case SelectionKey.OP_WRITE :
    waitingFor = "write"; break;      
    
  case SelectionKey.OP_CONNECT :
    waitingFor = "connect"; break;
    
  default :
    waitingFor = "" + ops;  
  }
  
  return timeout + " millis timeout while " +
         "waiting for channel to be ready for " + 
         waitingFor + ". ch : " + channel;    
}
 
Example 3
Source File: SocketIOWithTimeout.java    From RDFS with Apache License 2.0 6 votes vote down vote up
private static String timeoutExceptionString(SelectableChannel channel,
                                             long timeout, int ops) {
  
  String waitingFor;
  switch(ops) {
  
  case SelectionKey.OP_READ :
    waitingFor = "read"; break;
    
  case SelectionKey.OP_WRITE :
    waitingFor = "write"; break;      
    
  case SelectionKey.OP_CONNECT :
    waitingFor = "connect"; break;
    
  default :
    waitingFor = "" + ops;  
  }
  
  return timeout + " millis timeout while " +
         "waiting for channel to be ready for " + 
         waitingFor + ". ch : " + channel;    
}
 
Example 4
Source File: SctpMultiChannelImpl.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Translates native poll revent ops into a ready operation ops
 */
private boolean translateReadyOps(int ops, int initialOps,
                                  SelectionKeyImpl sk) {
    int intOps = sk.nioInterestOps();
    int oldOps = sk.nioReadyOps();
    int newOps = initialOps;

    if ((ops & Net.POLLNVAL) != 0) {
        /* This should only happen if this channel is pre-closed while a
         * selection operation is in progress
         * ## Throw an error if this channel has not been pre-closed */
        return false;
    }

    if ((ops & (Net.POLLERR | Net.POLLHUP)) != 0) {
        newOps = intOps;
        sk.nioReadyOps(newOps);
        return (newOps & ~oldOps) != 0;
    }

    if (((ops & Net.POLLIN) != 0) &&
        ((intOps & SelectionKey.OP_READ) != 0))
        newOps |= SelectionKey.OP_READ;

    if (((ops & Net.POLLOUT) != 0) &&
        ((intOps & SelectionKey.OP_WRITE) != 0))
        newOps |= SelectionKey.OP_WRITE;

    sk.nioReadyOps(newOps);
    return (newOps & ~oldOps) != 0;
}
 
Example 5
Source File: SelectionRegistration.java    From DeviceConnect-Android with MIT License 5 votes vote down vote up
/**
 * Returns the name of the given IO operation.
 * 
 * @param operation
 *            The IO operation code.
 * @return The name of the given IO operation.
 */
public static String getName(int operation) {
    StringBuilder result = new StringBuilder();

    if ((operation & SelectionKey.OP_ACCEPT) != 0) {
        result.append("ACCEPT ");
    }

    if ((operation & SelectionKey.OP_CONNECT) != 0) {
        result.append("CONNECT ");
    }

    if ((operation & SelectionKey.OP_READ) != 0) {
        result.append("READ ");
    }

    if ((operation & SelectionKey.OP_WRITE) != 0) {
        result.append("WRITE ");
    }

    if (operation == 0) {
        result.append("NONE ");
    }

    if (result.length() == 0) {
        result.append(operation);
    }

    return result.toString();
}
 
Example 6
Source File: Firehose.java    From mldht with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public int calcInterestOps() {
	int ops = SelectionKey.OP_READ;
	if(!empty)
		ops |= SelectionKey.OP_WRITE;
	return ops;
}
 
Example 7
Source File: AbstractMqttChannel.java    From xenqtt with Apache License 2.0 5 votes vote down vote up
private void connectFinished() {
	int ops = sendMessageInProgress != null ? SelectionKey.OP_READ | SelectionKey.OP_WRITE : SelectionKey.OP_READ;
	selectionKey.interestOps(ops);
	commandComplete(connectionCompleteCommand);
	connectionCompleteCommand = null;
	handler.channelOpened(this);
}
 
Example 8
Source File: SctpChannelImpl.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void translateAndSetInterestOps(int ops, SelectionKeyImpl sk) {
    int newOps = 0;
    if ((ops & SelectionKey.OP_READ) != 0)
        newOps |= Net.POLLIN;
    if ((ops & SelectionKey.OP_WRITE) != 0)
        newOps |= Net.POLLOUT;
    if ((ops & SelectionKey.OP_CONNECT) != 0)
        newOps |= Net.POLLCONN;
    sk.selector.putEventOps(sk, newOps);
}
 
Example 9
Source File: SctpChannelImpl.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void translateAndSetInterestOps(int ops, SelectionKeyImpl sk) {
    int newOps = 0;
    if ((ops & SelectionKey.OP_READ) != 0)
        newOps |= Net.POLLIN;
    if ((ops & SelectionKey.OP_WRITE) != 0)
        newOps |= Net.POLLOUT;
    if ((ops & SelectionKey.OP_CONNECT) != 0)
        newOps |= Net.POLLCONN;
    sk.selector.putEventOps(sk, newOps);
}
 
Example 10
Source File: SctpMultiChannelImpl.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Translates native poll revent ops into a ready operation ops
 */
private boolean translateReadyOps(int ops, int initialOps,
                                  SelectionKeyImpl sk) {
    int intOps = sk.nioInterestOps();
    int oldOps = sk.nioReadyOps();
    int newOps = initialOps;

    if ((ops & Net.POLLNVAL) != 0) {
        /* This should only happen if this channel is pre-closed while a
         * selection operation is in progress
         * ## Throw an error if this channel has not been pre-closed */
        return false;
    }

    if ((ops & (Net.POLLERR | Net.POLLHUP)) != 0) {
        newOps = intOps;
        sk.nioReadyOps(newOps);
        return (newOps & ~oldOps) != 0;
    }

    if (((ops & Net.POLLIN) != 0) &&
        ((intOps & SelectionKey.OP_READ) != 0))
        newOps |= SelectionKey.OP_READ;

    if (((ops & Net.POLLOUT) != 0) &&
        ((intOps & SelectionKey.OP_WRITE) != 0))
        newOps |= SelectionKey.OP_WRITE;

    sk.nioReadyOps(newOps);
    return (newOps & ~oldOps) != 0;
}
 
Example 11
Source File: SctpChannelImpl.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void translateAndSetInterestOps(int ops, SelectionKeyImpl sk) {
    int newOps = 0;
    if ((ops & SelectionKey.OP_READ) != 0)
        newOps |= Net.POLLIN;
    if ((ops & SelectionKey.OP_WRITE) != 0)
        newOps |= Net.POLLOUT;
    if ((ops & SelectionKey.OP_CONNECT) != 0)
        newOps |= Net.POLLCONN;
    sk.selector.putEventOps(sk, newOps);
}
 
Example 12
Source File: Client.java    From gnirehtet with Apache License 2.0 5 votes vote down vote up
private void updateInterests() {
    int interestOps = SelectionKey.OP_READ; // we always want to read
    if (!networkToClient.isEmpty()) {
        interestOps |= SelectionKey.OP_WRITE;
    }
    if (interests != interestOps) {
        // interests must be changed
        interests = interestOps;
        selectionKey.interestOps(interestOps);
    }
}
 
Example 13
Source File: SctpMultiChannelImpl.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void translateAndSetInterestOps(int ops, SelectionKeyImpl sk) {
    int newOps = 0;
    if ((ops & SelectionKey.OP_READ) != 0)
        newOps |= Net.POLLIN;
    if ((ops & SelectionKey.OP_WRITE) != 0)
        newOps |= Net.POLLOUT;
    sk.selector.putEventOps(sk, newOps);
}
 
Example 14
Source File: NioUtils.java    From hasting with MIT License 5 votes vote down vote up
public static void setNioReadOp(SelectionKey key){
	if(checkKey(key)){
        final int interestOps = key.interestOps();
        if ((interestOps & SelectionKey.OP_READ) == 0) {
        	key.interestOps(interestOps | SelectionKey.OP_READ);
        }
	}
}
 
Example 15
Source File: SocketOrChannelConnectionImpl.java    From jdk1.8-source-analysis with Apache License 2.0 4 votes vote down vote up
public int getInterestOps()
{
    return SelectionKey.OP_READ;
}
 
Example 16
Source File: SocketOrChannelConnectionImpl.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
public int getInterestOps()
{
    return SelectionKey.OP_READ;
}
 
Example 17
Source File: UkcpServerChannel.java    From kcp-netty with MIT License 4 votes vote down vote up
public UkcpServerChannel(DatagramChannel socket) {
    super(null, socket, SelectionKey.OP_READ);
    config = new DefaultUkcpServerChannelConfig(this, socket.socket());
}
 
Example 18
Source File: PubSubClient.java    From tracing-framework with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/** The main client loop when we've connected to the server. Selects on the selector and does async read/write */
private void ClientThreadMainLoop(SocketChannel channel) throws IOException {
    // Create subscription message if we have some already
    TopicMessage subscriptions = null;
    synchronized (this) {
        if (subscribers.size() > 0) {
            log.debug("Sending existing subscriptions");
            ControlMessage.Builder msg = ControlMessage.newBuilder();
            for (ByteString topic : subscribers.keySet()) {
                msg.addTopicSubscribe(topic);
            }
            subscriptions = new ProtobufMessage(CONTROL_TOPIC, msg.build());
        }
    }

    // Create a message reader and writer
    log.debug("Creating client reader and writer for channel {}", channel);
    ClientReader reader = new ClientReader(channel);
    ClientWriter writer = new ClientWriter(channel, subscriptions);
    SelectionKey k = channel.register(selector, SelectionKey.OP_READ);

    // Do main loop
    while (!Thread.currentThread().isInterrupted()) {
        // Register for read and write as needed
        int ops = SelectionKey.OP_READ;
        if (writer.canWrite()) {
            ops |= SelectionKey.OP_WRITE;
        }
        k.interestOps(ops);

        // Wait until we can do something
        selector.select(1000);

        // Deal with keys
        Iterator<SelectionKey> it = selector.selectedKeys().iterator();
        while (it.hasNext()) {
            // Get next key, remove from selected
            SelectionKey selected = it.next();
            it.remove();

            // Cancel if necessary
            if (!selected.isValid()) {
                selected.cancel();
            }

            // Do nothing if its not our key... why this can happen, I do
            // not know
            if (selected != k) {
                continue;
            }

            // Check to see whether we can read and write
            if (k.isWritable()) {
                log.debug("Writing");
                boolean hasRemaining = writer.write();
                
                // Signal anybody waiting
                if (!hasRemaining) {
                    notifyLock.lock();
                    try {
                        notifyCondition.signalAll();
                    } finally {
                        notifyLock.unlock();
                    }
                }
            } else if (k.isReadable()) {
                log.debug("Reading");
                if (!reader.read()) {
                    log.debug("Reader reached end of stream");
                    k.cancel();
                    return;
                }
            }
        }
    }
}
 
Example 19
Source File: SctpChannel.java    From jdk8u-dev-jdk with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Returns an operation set identifying this channel's supported operations.
 *
 * <P> SCTP channels support connecting, reading, and writing, so this
 * method returns <tt>(</tt>{@link SelectionKey#OP_CONNECT}
 * <tt>|</tt>&nbsp;{@link SelectionKey#OP_READ} <tt>|</tt>&nbsp;{@link
 * SelectionKey#OP_WRITE}<tt>)</tt>.  </p>
 *
 * @return  The valid-operation set
 */
@Override
public final int validOps() {
    return (SelectionKey.OP_READ |
            SelectionKey.OP_WRITE |
            SelectionKey.OP_CONNECT);
}
 
Example 20
Source File: SctpChannel.java    From openjdk-8 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Returns an operation set identifying this channel's supported operations.
 *
 * <P> SCTP channels support connecting, reading, and writing, so this
 * method returns <tt>(</tt>{@link SelectionKey#OP_CONNECT}
 * <tt>|</tt>&nbsp;{@link SelectionKey#OP_READ} <tt>|</tt>&nbsp;{@link
 * SelectionKey#OP_WRITE}<tt>)</tt>.  </p>
 *
 * @return  The valid-operation set
 */
@Override
public final int validOps() {
    return (SelectionKey.OP_READ |
            SelectionKey.OP_WRITE |
            SelectionKey.OP_CONNECT);
}