当前位置: 首页>>代码示例>>Java>>正文


Java SelectionKey.isAcceptable方法代码示例

本文整理汇总了Java中java.nio.channels.SelectionKey.isAcceptable方法的典型用法代码示例。如果您正苦于以下问题:Java SelectionKey.isAcceptable方法的具体用法?Java SelectionKey.isAcceptable怎么用?Java SelectionKey.isAcceptable使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.nio.channels.SelectionKey的用法示例。


在下文中一共展示了SelectionKey.isAcceptable方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: run

import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
@Override
public void run() {
    final Selector tSelector = this.selector;
    for (; ; ) {
        try {
            tSelector.select(1000L);
            Set<SelectionKey> keys = tSelector.selectedKeys();
            try {
                for (SelectionKey key : keys) {
                    if (key.isValid() && key.isAcceptable()) {
                        accept();
                    } else {
                        key.cancel();
                    }
                }
            } finally {
                keys.clear();
            }
        } catch (Exception e) {
            LOGGER.info(getName(), e);
        }
    }
}
 
开发者ID:actiontech,项目名称:dble,代码行数:24,代码来源:NIOAcceptor.java

示例2: accept

import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
/**
 * {@inheritDoc}
 */
@Override
protected NioSession accept(IoProcessor<NioSession> processor, ServerSocketChannel handle) throws Exception {

    SelectionKey key = handle.keyFor(selector);

    if ((key == null) || (!key.isValid()) || (!key.isAcceptable())) {
        return null;
    }

    // accept the connection from the client
    SocketChannel ch = handle.accept();

    if (ch == null) {
        return null;
    }

    return new NioSocketSession(this, processor, ch);
}
 
开发者ID:eclipse,项目名称:neoscada,代码行数:22,代码来源:NioSocketAcceptor.java

示例3: run

import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
@Override
public void run() {
	final Selector selector = this.selector;
	for (;;) {
		++acceptCount;
		try {
			selector.select( 1000L );
			Set<SelectionKey> keys = selector.selectedKeys();
			try {
				for (SelectionKey key : keys) {
					if (key.isValid() && key.isAcceptable()) {
						accept();							
					} else {
						key.cancel();
					}
				}
			} finally {
				keys.clear();
			}
		} catch (Throwable e) {
			LOGGER.warn(getName(), e);
		}
	}
}
 
开发者ID:variflight,项目名称:feeyo-redisproxy,代码行数:25,代码来源:NIOAcceptor.java

示例4: handleKey

import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
public void handleKey(SelectionKey key) {

        if (key.isValid() && key.isConnectable()) {
            //out node
            addOutNode(key);
        } else if (key.isValid() && key.isAcceptable()) {
            // in Node
            addInNode(key);
        } else {
            // read or write
            ConnectionHandler handler = (ConnectionHandler) key.attachment();
            if (handler != null) {
                ConnectionHandler.handleKey(key);
            }
        }
    }
 
开发者ID:nuls-io,项目名称:nuls,代码行数:17,代码来源:ConnectionManager.java

示例5: run

import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
public void run() {
    try {
        java.nio.channels.Selector acceptSelector = java.nio.channels.Selector.open();
        serverSocketChannel.register(acceptSelector, SelectionKey.OP_ACCEPT);
        while (serverSocketChannel.isOpen()) {
            if (acceptSelector.select(1000) > 0) {
                Iterator<SelectionKey> it = acceptSelector.selectedKeys().iterator();
                while (it.hasNext()) {
                    SelectionKey key = it.next();
                    if (key.isAcceptable()) {
                        SocketChannel socketChannel = ((ServerSocketChannel) key.channel()).accept();
                        socketChannel.configureBlocking(false);
                        newChannels.add(socketChannel);
                        selector.wakeup();
                    }
                    it.remove();
                }
            }
        }
    } catch (IOException e) {
        // ignore
    }
}
 
开发者ID:YMCoding,项目名称:kafka-0.11.0.0-src-with-comment,代码行数:24,代码来源:NioEchoServer.java

示例6: main

import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
//            创建ServerSocketChannel,监听8080端口
        ServerSocketChannel ssc = ServerSocketChannel.open();
        ssc.socket().bind(new InetSocketAddress(8080));
//            设置为非阻塞模式
        ssc.configureBlocking(false);
//            为ssc注册选择器
        Selector selector = Selector.open();
        ssc.register(selector, SelectionKey.OP_ACCEPT);
//            创建处理器
        Handler handler = new Handler(1024);
        while (true) {
//            等待请求,每次等待阻塞3s,超过3s后线程继续向下运行,如果传入0或者不传参数将一直阻塞
            if (selector.select(3000) == 0) {
                System.out.println("等待请求超时……");
                continue;
            }
            System.out.println("处理请求……");
//            获取待处理的SelectionKey
            Iterator<SelectionKey> keyIter = selector.selectedKeys().iterator();
            while (keyIter.hasNext()) {
                SelectionKey key = keyIter.next();
                try {
                    // 接收到连接请求时
                    if (key.isAcceptable()) {
                        handler.handleAccept(key);
                    }
//            读数据
                    if (key.isReadable()) {
                        handler.handleRead(key);
                    }
                } catch (IOException ex) {
                    keyIter.remove();
                    continue;
                }
//            处理完后,从待处理的SelectionKey迭代器中移除当前所使用的key
                keyIter.remove();
            }
        }
    }
 
开发者ID:tomoncle,项目名称:JavaStudy,代码行数:41,代码来源:NIOSocketTest.java

示例7: handleEvent

import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
public void handleEvent(SelectionKey key) {
    if (key.isAcceptable())
        buildConnection(key);

    else if (key.isReadable())
        transferData(key);
}
 
开发者ID:ZhangJiupeng,项目名称:AgentX,代码行数:8,代码来源:SocketTunnel.java

示例8: initializeSelectionSets

import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
private void initializeSelectionSets(Set<SelectionKey> selectionKeys) {
    for (SelectionKey selectionKey : selectionKeys) {
        if (selectionKey.isAcceptable()) {
            acceptableSockets.put((TCPServerSocket)selectionKey.attachment(), false);
            continue; // acceptable sockets are neither readable nor writable
        }
        if (selectionKey.isReadable()) {
            readableSockets.put((TCPClientSocket)selectionKey.attachment(), false);
        }
        if (selectionKey.isWritable()) {
            writableSockets.put((TCPClientSocket)selectionKey.attachment(), false);
        }
    }
}
 
开发者ID:cpppwner,项目名称:NoRiskNoFun,代码行数:15,代码来源:SelectionResultImpl.java

示例9: next

import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
/**
 * Get the next SocketChannel in the operator we have built from
 * the selected-key et for this selector.
 * 
 * @return The next SocketChannel in the iterator
 */
public ServerSocketChannel next() {
    SelectionKey key = iterator.next();

    if (key.isValid() && key.isAcceptable()) {
        return (ServerSocketChannel) key.channel();
    }

    return null;
}
 
开发者ID:eclipse,项目名称:neoscada,代码行数:16,代码来源:NioSocketAcceptor.java

示例10: run

import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
public void run() {
	try {
		do {
			int n = selector.select();
			if (n>0) {
				Iterator<SelectionKey> i = selector.selectedKeys().iterator();
				while ( i.hasNext() ) {
					SelectionKey key = i.next();
					i.remove();
					if ( key.isReadable() ) {
						SCTPMessageChannel channel = (SCTPMessageChannel) key.attachment();
						channel.readMessages();
					} else if (key.isAcceptable()) {
						SctpChannel ch = sctpServerChannel.accept();
						SCTPMessageChannel c = new SCTPMessageChannel( this, ch );
						channels.add( c );
					}
				}
			}
			
			synchronized (this) {
				if (doClose) {
					selector.close();
					return;
				}
			}				
		} while ( selector.isOpen() );
	} catch (IOException ioe) {
		ioe.printStackTrace();
		try {
			selector.close();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			this.stop();
		}
	}
}
 
开发者ID:YunlongYang,项目名称:LightSIP,代码行数:39,代码来源:SCTPMessageProcessor.java

示例11: processKey

import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
private void processKey(SelectionKey key) throws IOException {
	if (key.isAcceptable()) {
		processConnection(key);
	} else if (key.isReadable()) {
		processMessage(key);
	}
}
 
开发者ID:roscisz,项目名称:KernelHive,代码行数:8,代码来源:TCPServer.java

示例12: run

import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
@Override
public void run() {
    while (true) {
        try {
            if (atomicInteger.get() >= 3) {
                System.out.println("end server");
                break;
            }
            selector.select();
            Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
            while (iterator.hasNext()) {
                SelectionKey key = iterator.next();
                iterator.remove();
                if (key.isAcceptable()) {
                    handAccept(key);
                } else if (key.isWritable()) {
                    handWrite(key);
                    atomicInteger.incrementAndGet();
                } else if (key.isReadable()) {
                    handRead(key);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
 
开发者ID:justice-code,项目名称:QiuQiu,代码行数:28,代码来源:Server.java

示例13: select

import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
private void select() {
    try {
        selector.select();

        Iterator<SelectionKey> selectedKeys =
            selector.selectedKeys().iterator();
        while (!stopped && selectedKeys.hasNext()) {
            SelectionKey key = selectedKeys.next();
            selectedKeys.remove();

            if (!key.isValid()) {
                continue;
            }
            if (key.isAcceptable()) {
                if (!doAccept()) {
                    // If unable to pull a new connection off the accept
                    // queue, pause accepting to give us time to free
                    // up file descriptors and so the accept thread
                    // doesn't spin in a tight loop.
                    pauseAccept(10);
                }
            } else {
                LOG.warn("Unexpected ops in accept select "
                         + key.readyOps());
            }
        }
    } catch (IOException e) {
        LOG.warn("Ignoring IOException while selecting", e);
    }
}
 
开发者ID:didichuxing2,项目名称:https-github.com-apache-zookeeper,代码行数:31,代码来源:NIOServerCnxnFactory.java

示例14: run

import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
@Override
public void run() {

    while (running) {

        try {
            select();

            Set<SelectionKey> selectionKeys = selector.selectedKeys();

            if (selectionKeys.isEmpty()) {
                continue;
            }

            Iterator<SelectionKey> iterator = selectionKeys.iterator();
            while (iterator.hasNext()) {

                final SelectionKey key = iterator.next();
                iterator.remove();

                if (!key.isValid()) {
                    continue;
                }

                if (key.isAcceptable()) {
                    doAccept(key);
                }

                if (key.isConnectable()) {
                    doConnect(key);
                }

                if (key.isValid() && key.isReadable()) {
                    doRead(key);
                }

                if (key.isValid() && key.isWritable()) {
                    doWrite(key);
                }
            }

        } catch (Throwable t) {
            LOGGER.warn("Unexpected exception in the selector loop.", t);

            // 睡眠1S, 防止连续的异常导致cpu消耗
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ignore) {
            }
        }
    }
}
 
开发者ID:lemonJun,项目名称:TakinRPC,代码行数:53,代码来源:NioSelectorLoop.java

示例15: startServer

import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
private void startServer() throws Exception{
        //声明一个selector
        selector= SelectorProvider.provider().openSelector();

        //声明一个server socket channel,而且是非阻塞的。
        ServerSocketChannel ssc=ServerSocketChannel.open();
        ssc.configureBlocking(false);

//        InetSocketAddress isa=new InetSocketAddress(InetAddress.getLocalHost(),8000);
        //声明服务器端的端口
        InetSocketAddress isa=new InetSocketAddress(8000);
        //服务器端的socket channel绑定在这个端口。
        ssc.socket().bind(isa);
        //把一个socketchannel注册到一个selector上,同时选择监听的事件,SelectionKey.OP_ACCEPT表示对selector如果
        //监听到注册在它上面的server socket channel准备去接受一个连接,或 有个错误挂起,selector将把OP_ACCEPT加到
        //key ready set 并把key加到selected-key set.
        SelectionKey acceptKey=ssc.register(selector,SelectionKey.OP_ACCEPT);

        for(;;){
            selector.select();
            Set readyKeys=selector.selectedKeys();
            Iterator i=readyKeys.iterator();
            long e=0;
            while (i.hasNext()){
                SelectionKey sk=(SelectionKey)i.next();
                i.remove();

                if(sk.isAcceptable()){
                    doAccept(sk);
                }else if(sk.isValid()&&sk.isReadable()){
                    if(!geym_time_stat.containsKey(((SocketChannel)sk.channel()).socket())){
                        geym_time_stat.put(((SocketChannel)sk.channel()).socket(),System.currentTimeMillis());
                        doRead(sk);
                    }
                }else if(sk.isValid()&&sk.isWritable()){
                    doWrite(sk);
                    e=System.currentTimeMillis();
                    long b=geym_time_stat.remove(((SocketChannel)sk.channel()).socket());
                    System.out.println("spend"+(e-b)+"ms");
                }
            }
        }
    }
 
开发者ID:sean417,项目名称:LearningOfThinkInJava,代码行数:44,代码来源:MultiThreadNIOEchoServer.java


注:本文中的java.nio.channels.SelectionKey.isAcceptable方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。