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


Java SelectableChannel类代码示例

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


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

示例1: acceptCallback

import java.nio.channels.SelectableChannel; //导入依赖的package包/类
public void acceptCallback(SelectableChannel channel) {
    // accept the connection
    assert channel == serverSocket;
    SocketChannel client;
    try {
        client = serverSocket.accept();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    assert client != null;

    // wrap it in a message connection and register with event loop
    NIOMessageConnection connection = new NIOMessageConnection(client);
    connection.setBigEndian();

    this.eventLoop.registerRead(client, new ClientConnectionHandler(connection));
    this.numConnections.incrementAndGet();
}
 
开发者ID:s-store,项目名称:sstore-soft,代码行数:19,代码来源:VoltProcedureListener.java

示例2: acceptCallback

import java.nio.channels.SelectableChannel; //导入依赖的package包/类
@Override
    public void acceptCallback(SelectableChannel channel) {
        // accept the connection
        assert channel == serverSocket;
        SocketChannel client;
        try {
            client = serverSocket.accept();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        assert client != null;

        // wrap it in a message connection and register with event loop
        ProtoConnection connection = new ProtoConnection(new NonBlockingConnection(client));

        eventLoop.registerRead(client, new EventCallbackWrapper(connection));
//        SelectionKey clientKey = connection.register(selector);
//        clientKey.attach(connection);
//        eventQueue.add(new Event(connection, null));
    }
 
开发者ID:s-store,项目名称:sstore-soft,代码行数:21,代码来源:ProtoServer.java

示例3: addInterest

import java.nio.channels.SelectableChannel; //导入依赖的package包/类
private void addInterest(SelectableChannel channel, int operation, Handler callback) {
    // TODO: Support multiple handlers?
    SelectionKey key = channel.keyFor(selector);
    if (key != null) {
        assert (key.interestOps() & operation) == 0;
        if (key.attachment() == null) {
            key.attach(callback);
        } else {
            assert callback == key.attachment();
        }
        key.interestOps(key.interestOps() | operation);
        // TODO: This fixes a synchronization issue where one thread changes the interest set
        // of a thread while another thread is blocked in select(), because the Selector
        // documentation states that it waits for events registered "as of the moment that the
        // selection operation began. Is there a better fix?
        selector.wakeup();
    } else {
        register(channel, operation, callback);
    }
}
 
开发者ID:s-store,项目名称:sstore-soft,代码行数:21,代码来源:NIOEventLoop.java

示例4: timeoutExceptionString

import java.nio.channels.SelectableChannel; //导入依赖的package包/类
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;    
}
 
开发者ID:nucypher,项目名称:hadoop-oss,代码行数:24,代码来源:SocketIOWithTimeout.java

示例5: registerNewSelector

import java.nio.channels.SelectableChannel; //导入依赖的package包/类
/**
 * In the case we are using the java select() method, this method is used to
 * trash the buggy selector and create a new one, registering all the
 * sockets on it.
 */
@Override
protected void registerNewSelector() throws IOException {
    synchronized (selector) {
        Set<SelectionKey> keys = selector.keys();

        // Open a new selector
        Selector newSelector = Selector.open();

        // Loop on all the registered keys, and register them on the new selector
        for (SelectionKey key : keys) {
            SelectableChannel ch = key.channel();

            // Don't forget to attache the session, and back !
            NioSession session = (NioSession) key.attachment();
            SelectionKey newKey = ch.register(newSelector, key.interestOps(), session);
            session.setSelectionKey(newKey);
        }

        // Now we can close the old selector and switch it
        selector.close();
        selector = newSelector;
    }
}
 
开发者ID:eclipse,项目名称:neoscada,代码行数:29,代码来源:NioProcessor.java

示例6: handleIOException

import java.nio.channels.SelectableChannel; //导入依赖的package包/类
private void handleIOException( SelectionKey key, WebSocket conn, IOException ex ) {
	// onWebsocketError( conn, ex );// conn may be null here
	if( conn != null ) {
		conn.closeConnection( CloseFrame.ABNORMAL_CLOSE, ex.getMessage() );
	} else if( key != null ) {
		SelectableChannel channel = key.channel();
		if( channel != null && channel.isOpen() ) { // this could be the case if the IOException ex is a SSLException
			try {
				channel.close();
			} catch ( IOException e ) {
				// there is nothing that must be done here
			}
			if( WebSocketImpl.DEBUG )
				System.out.println( "Connection closed because of" + ex );
		}
	}
}
 
开发者ID:LDLN,项目名称:Responder-Android,代码行数:18,代码来源:WebSocketServer.java

示例7: handleIOException

import java.nio.channels.SelectableChannel; //导入依赖的package包/类
private void handleIOException(SelectionKey key, WebSocket conn, IOException ex) {
    // onWebsocketError( conn, ex );// conn may be null here
    if (conn != null) {
        conn.closeConnection(CloseFrame.ABNORMAL_CLOSE, ex.getMessage());
    } else if (key != null) {
        SelectableChannel channel = key.channel();
        if (channel != null && channel.isOpen()) { // this could be the case if the IOException ex is a SSLException
            try {
                channel.close();
            } catch (IOException e) {
                // there is nothing that must be done here
            }
            if (WebSocketImpl.DEBUG) {
                System.out.println("Connection closed because of" + ex);
            }
        }
    }
}
 
开发者ID:GloriousEggroll,项目名称:quorrabot,代码行数:19,代码来源:WebSocketServer.java

示例8: readCallback

import java.nio.channels.SelectableChannel; //导入依赖的package包/类
@Override
public void readCallback(SelectableChannel channel) {
    try {
        read(this);
    } catch (RuntimeException ex) {
        if (ex.getCause() instanceof IOException) {
            // Ignore this
            if (LOG.isDebugEnabled()) LOG.warn("Client connection closed unexpectedly", ex);
        } else {
            throw ex;
        }
    }
}
 
开发者ID:s-store,项目名称:sstore-soft,代码行数:14,代码来源:VoltProcedureListener.java

示例9: registerRead

import java.nio.channels.SelectableChannel; //导入依赖的package包/类
@Override
public void registerRead(SelectableChannel channel, Handler handler) {
    // Disallow both being registered for read events and connection events at the same time.
    // On Linux, when a connect fails, the socket is ready for both events, which causes
    // errors when reads are attempted on the closed socket.
    assert channel.keyFor(selector) == null ||
            (channel.keyFor(selector).interestOps() & SelectionKey.OP_CONNECT) == 0;
    addInterest(channel, SelectionKey.OP_READ, handler);
}
 
开发者ID:s-store,项目名称:sstore-soft,代码行数:10,代码来源:NIOEventLoop.java

示例10: get

import java.nio.channels.SelectableChannel; //导入依赖的package包/类
/**
 * Takes one selector from end of LRU list of free selectors.
 * If there are no selectors awailable, it creates a new selector.
 * Also invokes trimIdleSelectors(). 
 * 
 * @param channel
 * @return 
 * @throws IOException
 */
private synchronized SelectorInfo get(SelectableChannel channel) 
                                                     throws IOException {
  SelectorInfo selInfo = null;
  
  SelectorProvider provider = channel.provider();
  
  // pick the list : rarely there is more than one provider in use.
  ProviderInfo pList = providerList;
  while (pList != null && pList.provider != provider) {
    pList = pList.next;
  }      
  if (pList == null) {
    //LOG.info("Creating new ProviderInfo : " + provider.toString());
    pList = new ProviderInfo();
    pList.provider = provider;
    pList.queue = new LinkedList<SelectorInfo>();
    pList.next = providerList;
    providerList = pList;
  }
  
  LinkedList<SelectorInfo> queue = pList.queue;
  
  if (queue.isEmpty()) {
    Selector selector = provider.openSelector();
    selInfo = new SelectorInfo();
    selInfo.selector = selector;
    selInfo.queue = queue;
  } else {
    selInfo = queue.removeLast();
  }
  
  trimIdleSelectors(Time.now());
  return selInfo;
}
 
开发者ID:naver,项目名称:hadoop,代码行数:44,代码来源:SocketIOWithTimeout.java

示例11: readCallback

import java.nio.channels.SelectableChannel; //导入依赖的package包/类
@Override
public void readCallback(SelectableChannel channel) {
    boolean isOpen = connection.readAllAvailable();
    if (!isOpen) {
        // TODO: Fail any subsequent RPCs
        throw new UnsupportedOperationException("Connection closed: not handled (for now).");
    }

    while (true) {
        RpcResponse.Builder builder = RpcResponse.newBuilder();
        boolean success = connection.readBufferedMessage(builder);
        if (!success) {
            // TODO: Cache the builder object to reduce garbage?
            break;
        }

        // Set the appropriate flags on the RPC object
        // TODO: Handle bad sequence number by ignoring/logging?
        RpcResponse response = builder.build();
        ProtoRpcController rpc = null;
        synchronized (this) {
            rpc = pendingRpcs.remove(response.getSequenceNumber());
            assert response.getStatus() == Protocol.Status.OK;
            assert rpc != null :
                    "No ProtoRpcController for Sequence# " + response.getSequenceNumber();
        }
        rpc.finishRpcSuccess(response.getResponse());
    }
}
 
开发者ID:s-store,项目名称:sstore-soft,代码行数:30,代码来源:ProtoRpcChannel.java

示例12: acceptCallback

import java.nio.channels.SelectableChannel; //导入依赖的package包/类
@Override
public void acceptCallback(SelectableChannel channel) {
    // accept the connection
    assert client == null;
    try {
        client = ((ServerSocketChannel) channel).accept();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    assert client != null;
}
 
开发者ID:s-store,项目名称:sstore-soft,代码行数:12,代码来源:NIOEventLoopTest.java

示例13: registerWrite

import java.nio.channels.SelectableChannel; //导入依赖的package包/类
@Override
public void registerWrite(SelectableChannel channel, Handler handler) {
    if (writeHandler != null) {
        throw new IllegalStateException("Each channel can only call registerWrite() once");
    }
    writeHandler = handler;
}
 
开发者ID:s-store,项目名称:sstore-soft,代码行数:8,代码来源:MockEventLoop.java

示例14: implClose

import java.nio.channels.SelectableChannel; //导入依赖的package包/类
protected void implClose() throws IOException {
    synchronized (closeLock) {
        if (channelArray != null) {
            if (pollWrapper != null) {
                // prevent further wakeup
                synchronized (interruptLock) {
                    interruptTriggered = true;
                }
                wakeupPipe.sink().close();
                wakeupPipe.source().close();
                for(int i = 1; i < totalChannels; i++) { // Deregister channels
                    if (i % MAX_SELECTABLE_FDS != 0) { // skip wakeupEvent
                        deregister(channelArray[i]);
                        SelectableChannel selch = channelArray[i].channel();
                        if (!selch.isOpen() && !selch.isRegistered())
                            ((SelChImpl)selch).kill();
                    }
                }
                pollWrapper.free();
                pollWrapper = null;
                selectedKeys = null;
                channelArray = null;
                // Make all remaining helper threads exit
                for (SelectThread t: threads)
                     t.makeZombie();
                startLock.startThreads();
            }
        }
    }
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:31,代码来源:WindowsSelectorImpl.java

示例15: unregisterChannel

import java.nio.channels.SelectableChannel; //导入依赖的package包/类
private void unregisterChannel(SelectableChannel channel) {

        SelectionKey key = channel.keyFor(selector);
        if (key != null) {
            // not registered with this selector => null returned
            key.cancel();
        }
    }
 
开发者ID:cpppwner,项目名称:NoRiskNoFun,代码行数:9,代码来源:SocketSelectorImpl.java


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