本文整理汇总了Java中java.nio.channels.SelectionKey.readyOps方法的典型用法代码示例。如果您正苦于以下问题:Java SelectionKey.readyOps方法的具体用法?Java SelectionKey.readyOps怎么用?Java SelectionKey.readyOps使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.nio.channels.SelectionKey
的用法示例。
在下文中一共展示了SelectionKey.readyOps方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: doTransport
import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
@Override
void doTransport(int waitTimeOut, List<Packet> pendingQueue, LinkedList<Packet> outgoingQueue,
ClientCnxn cnxn)
throws IOException, InterruptedException {
selector.select(waitTimeOut);
Set<SelectionKey> selected;
synchronized (this) {
selected = selector.selectedKeys();
}
// Everything below and until we get back to the select is
// non blocking, so time is effectively a constant. That is
// Why we just have to do this once, here
updateNow();
for (SelectionKey k : selected) {
SocketChannel sc = ((SocketChannel) k.channel());
if ((k.readyOps() & SelectionKey.OP_CONNECT) != 0) {
if (sc.finishConnect()) {
updateLastSendAndHeard();
sendThread.primeConnection();
}
} else if ((k.readyOps() & (SelectionKey.OP_READ | SelectionKey.OP_WRITE)) != 0) {
doIO(pendingQueue, outgoingQueue, cnxn);
}
}
if (sendThread.getZkState().isConnected()) {
synchronized(outgoingQueue) {
if (findSendablePacket(outgoingQueue,
cnxn.sendThread.clientTunneledAuthenticationInProgress()) != null) {
enableWrite();
}
}
}
selected.clear();
}
示例2: doTransport
import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
@Override
void doTransport(int waitTimeOut, List<Packet> pendingQueue, ClientCnxn cnxn)
throws IOException, InterruptedException {
selector.select(waitTimeOut);
Set<SelectionKey> selected;
synchronized (this) {
selected = selector.selectedKeys();
}
// Everything below and until we get back to the select is
// non blocking, so time is effectively a constant. That is
// Why we just have to do this once, here
updateNow();
for (SelectionKey k : selected) {
SocketChannel sc = ((SocketChannel) k.channel());
if ((k.readyOps() & SelectionKey.OP_CONNECT) != 0) {
if (sc.finishConnect()) {
updateLastSendAndHeard();
updateSocketAddresses();
sendThread.primeConnection();
}
} else if ((k.readyOps() & (SelectionKey.OP_READ | SelectionKey.OP_WRITE)) != 0) {
doIO(pendingQueue, cnxn);
}
}
if (sendThread.getZkState().isConnected()) {
if (findSendablePacket(outgoingQueue,
sendThread.tunnelAuthInProgress()) != null) {
enableWrite();
}
}
selected.clear();
}
示例3: process
import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
/**
* State machine to send data
* @param key SelectionKey
* @return boolean
* @throws IOException
*/
public boolean process(SelectionKey key, boolean waitForAck) throws IOException {
int ops = key.readyOps();
key.interestOps(key.interestOps() & ~ops);
//in case disconnect has been called
if ((!isConnected()) && (!connecting)) throw new IOException("Sender has been disconnected, can't selection key.");
if ( !key.isValid() ) throw new IOException("Key is not valid, it must have been cancelled.");
if ( key.isConnectable() ) {
if ( socketChannel.finishConnect() ) {
completeConnect();
if ( current != null ) key.interestOps(key.interestOps() | SelectionKey.OP_WRITE);
return false;
} else {
//wait for the connection to finish
key.interestOps(key.interestOps() | SelectionKey.OP_CONNECT);
return false;
}//end if
} else if ( key.isWritable() ) {
boolean writecomplete = write(key);
if ( writecomplete ) {
//we are completed, should we read an ack?
if ( waitForAck ) {
//register to read the ack
key.interestOps(key.interestOps() | SelectionKey.OP_READ);
} else {
//if not, we are ready, setMessage will reregister us for another write interest
//do a health check, we have no way of verify a disconnected
//socket since we don't register for OP_READ on waitForAck=false
read(key);//this causes overhead
setRequestCount(getRequestCount()+1);
return true;
}
} else {
//we are not complete, lets write some more
key.interestOps(key.interestOps()|SelectionKey.OP_WRITE);
}//end if
} else if ( key.isReadable() ) {
boolean readcomplete = read(key);
if ( readcomplete ) {
setRequestCount(getRequestCount()+1);
return true;
} else {
key.interestOps(key.interestOps() | SelectionKey.OP_READ);
}//end if
} else {
//unknown state, should never happen
log.warn("Data is in unknown state. readyOps="+ops);
throw new IOException("Data is in unknown state. readyOps="+ops);
}//end if
return false;
}
示例4: main
import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
Selector selector;
synchronized (Selector.class) {
// Selector.open() isn't thread safe
// http://bugs.sun.com/view_bug.do?bug_id=6427854
// Affects 1.6.0_29, fixed in 1.7.0_01
selector = Selector.open();
}
Member mbr = new MemberImpl("localhost", 9999, 0);
byte seq = 0;
byte[] buf = new byte[50000];
Arrays.fill(buf,seq);
int len = buf.length;
BigDecimal total = new BigDecimal((double)0);
BigDecimal bytes = new BigDecimal((double)len);
NioSender sender = new NioSender();
sender.setDestination(mbr);
sender.setDirectBuffer(true);
sender.setSelector(selector);
sender.connect();
sender.setMessage(buf);
System.out.println("Writing to 9999");
long start = 0;
double mb = 0;
boolean first = true;
int count = 0;
DecimalFormat df = new DecimalFormat("##.00");
while (count<100000) {
if (first) {
first = false;
start = System.currentTimeMillis();
}
sender.setMessage(buf);
int selectedKeys = 0;
try {
selectedKeys = selector.select(0);
} catch (Exception e) {
e.printStackTrace();
continue;
}
if (selectedKeys == 0) {
continue;
}
Iterator<SelectionKey> it = selector.selectedKeys().iterator();
while (it.hasNext()) {
SelectionKey sk = it.next();
it.remove();
try {
int readyOps = sk.readyOps();
sk.interestOps(sk.interestOps() & ~readyOps);
if (sender.process(sk, false)) {
total = total.add(bytes);
sender.reset();
seq++;
Arrays.fill(buf,seq);
sender.setMessage(buf);
mb += ( (double) len) / 1024 / 1024;
if ( ( (++count) % 10000) == 0) {
long time = System.currentTimeMillis();
double seconds = ( (double) (time - start)) / 1000;
System.out.println("Throughput " + df.format(mb / seconds) + " MB/seconds, total "+mb+" MB, total "+total+" bytes.");
}
}
} catch (Throwable t) {
t.printStackTrace();
return;
}
}
}
System.out.println("Complete, sleeping 15 seconds");
Thread.sleep(15000);
}
示例5: main
import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
Selector selector;
synchronized (Selector.class) {
// Selector.open() isn't thread safe
// http://bugs.sun.com/view_bug.do?bug_id=6427854
// Affects 1.6.0_29, fixed in 1.7.0_01
selector = Selector.open();
}
Member mbr = new MemberImpl("localhost", 9999, 0);
ChannelData data = new ChannelData();
data.setOptions(Channel.SEND_OPTIONS_BYTE_MESSAGE);
data.setAddress(mbr);
byte[] buf = new byte[8192 * 4];
data.setMessage(new XByteBuffer(buf,false));
buf = XByteBuffer.createDataPackage(data);
int len = buf.length;
BigDecimal total = new BigDecimal((double)0);
BigDecimal bytes = new BigDecimal((double)len);
NioSender sender = new NioSender();
sender.setDestination(mbr);
sender.setDirectBuffer(true);
sender.setSelector(selector);
sender.setTxBufSize(1024*1024);
sender.connect();
sender.setMessage(buf);
System.out.println("Writing to 9999");
long start = 0;
double mb = 0;
boolean first = true;
int count = 0;
DecimalFormat df = new DecimalFormat("##.00");
while (count<100000) {
if (first) {
first = false;
start = System.currentTimeMillis();
}
sender.setMessage(buf);
int selectedKeys = 0;
try {
selectedKeys = selector.select(0);
} catch (Exception e) {
e.printStackTrace();
continue;
}
if (selectedKeys == 0) {
continue;
}
Iterator<SelectionKey> it = selector.selectedKeys().iterator();
while (it.hasNext()) {
SelectionKey sk = it.next();
it.remove();
try {
int readyOps = sk.readyOps();
sk.interestOps(sk.interestOps() & ~readyOps);
if (sender.process(sk, false)) {
total = total.add(bytes);
sender.reset();
sender.setMessage(buf);
mb += ( (double) len) / 1024 / 1024;
if ( ( (++count) % 10000) == 0) {
long time = System.currentTimeMillis();
double seconds = ( (double) (time - start)) / 1000;
System.out.println("Throughput " + df.format(mb / seconds) + " MB/seconds, total "+mb+" MB, total "+total+" bytes.");
}
}
} catch (Throwable t) {
t.printStackTrace();
return;
}
}
selector.selectedKeys().clear();
}
System.out.println("Complete, sleeping 15 seconds");
Thread.sleep(15000);
}
示例6: run
import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
public void run() {
while (true) {
int selectedKeys = 0;
try {
selectedKeys = selector.select(100);
// if ( selectedKeys == 0 ) {
// System.out.println("No registered interests. Sleeping for a second.");
// Thread.sleep(100);
} catch (Exception e) {
e.printStackTrace();
continue;
}
if (selectedKeys == 0) {
continue;
}
Iterator<SelectionKey> it = selector.selectedKeys().iterator();
while (it.hasNext()) {
SelectionKey sk = it.next();
it.remove();
try {
int readyOps = sk.readyOps();
sk.interestOps(sk.interestOps() & ~readyOps);
NioSender sender = (NioSender) sk.attachment();
if ( sender.process(sk, (testOptions&Channel.SEND_OPTIONS_USE_ACK)==Channel.SEND_OPTIONS_USE_ACK) ) {
System.out.println("Message completed for handler:"+sender);
Thread.sleep(2000);
sender.reset();
sender.setMessage(XByteBuffer.createDataPackage(getMessage(mbr)));
}
} catch (Throwable t) {
t.printStackTrace();
return;
}
}
}
}
示例7: handleClients
import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
private void handleClients() throws Exception {
int selectCount = 0;
while (true) {
int createdCount = 0;
synchronized (this) {
if (connectionsNeeded > 0) {
while (connectionsNeeded > 0 && createdCount < 20) {
connectionsNeeded--;
createdCount++;
totalCreated++;
SocketChannel channel = SocketChannel.open();
channel.configureBlocking(false);
channel.connect(address);
if (!channel.finishConnect()) {
channel.register(selector,
SelectionKey.OP_CONNECT);
}
}
log("Started total of " +
totalCreated + " client connections");
Thread.sleep(200);
}
}
if (createdCount > 0) {
selector.selectNow();
} else {
selectCount++;
long startTime = System.nanoTime();
selector.select();
long duration = durationMillis(startTime);
log("Exited clientSelector.select(), loop #"
+ selectCount + ", duration = " + duration + "ms");
}
int keyCount = -1;
Iterator<SelectionKey> keys =
selector.selectedKeys().iterator();
while (keys.hasNext()) {
SelectionKey key = keys.next();
synchronized (key) {
keyCount++;
keys.remove();
if (!key.isValid()) {
log("Ignoring client key #" + keyCount);
continue;
}
int readyOps = key.readyOps();
if (readyOps == SelectionKey.OP_CONNECT) {
key.interestOps(0);
((SocketChannel) key.channel()).finishConnect();
} else {
log("readyOps() on client key #" + keyCount +
" returned " + readyOps);
}
}
}
}
}
示例8: process
import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
/**
* State machine to send data
*
* @param key
* SelectionKey
* @return boolean
* @throws IOException
*/
public boolean process(SelectionKey key, boolean waitForAck) throws IOException {
int ops = key.readyOps();
key.interestOps(key.interestOps() & ~ops);
// in case disconnect has been called
if ((!isConnected()) && (!connecting))
throw new IOException("Sender has been disconnected, can't selection key.");
if (!key.isValid())
throw new IOException("Key is not valid, it must have been cancelled.");
if (key.isConnectable()) {
if (socketChannel.finishConnect()) {
completeConnect();
if (current != null)
key.interestOps(key.interestOps() | SelectionKey.OP_WRITE);
return false;
} else {
// wait for the connection to finish
key.interestOps(key.interestOps() | SelectionKey.OP_CONNECT);
return false;
} // end if
} else if (key.isWritable()) {
boolean writecomplete = write(key);
if (writecomplete) {
// we are completed, should we read an ack?
if (waitForAck) {
// register to read the ack
key.interestOps(key.interestOps() | SelectionKey.OP_READ);
} else {
// if not, we are ready, setMessage will reregister us for
// another write interest
// do a health check, we have no way of verify a
// disconnected
// socket since we don't register for OP_READ on
// waitForAck=false
read(key);// this causes overhead
setRequestCount(getRequestCount() + 1);
return true;
}
} else {
// we are not complete, lets write some more
key.interestOps(key.interestOps() | SelectionKey.OP_WRITE);
} // end if
} else if (key.isReadable()) {
boolean readcomplete = read(key);
if (readcomplete) {
setRequestCount(getRequestCount() + 1);
return true;
} else {
key.interestOps(key.interestOps() | SelectionKey.OP_READ);
} // end if
} else {
// unknown state, should never happen
log.warn("Data is in unknown state. readyOps=" + ops);
throw new IOException("Data is in unknown state. readyOps=" + ops);
} // end if
return false;
}