/** * Pause selection operations for the given channel * @param channel to pause */ public void pauseSelects( AbstractSelectableChannel channel ) { if( SAFE_SELECTOR_MODE_ENABLED ) { try{ selectors_mon.enter(); //System.out.println( "pause - " + channel.hashCode() + " - " + Debug.getCompressedStackTrace()); for( Map.Entry<VirtualChannelSelectorImpl, ArrayList<AbstractSelectableChannel>> entry: selectors.entrySet()) { VirtualChannelSelectorImpl sel = entry.getKey(); ArrayList<AbstractSelectableChannel> channels = entry.getValue(); if( channels.contains( channel ) ) { sel.pauseSelects( channel ); return; } } Debug.out( "pauseSelects():: channel not found!" ); } finally{ selectors_mon.exit(); } } else { selector_impl.pauseSelects( channel ); } }
/** * Resume selection operations for the given channel * @param channel to resume */ public void resumeSelects( AbstractSelectableChannel channel ) { if( SAFE_SELECTOR_MODE_ENABLED ) { try{ selectors_mon.enter(); //System.out.println( "resume - " + channel.hashCode() + " - " + Debug.getCompressedStackTrace()); for( Map.Entry<VirtualChannelSelectorImpl, ArrayList<AbstractSelectableChannel>> entry: selectors.entrySet()) { VirtualChannelSelectorImpl sel = entry.getKey(); ArrayList<AbstractSelectableChannel> channels = entry.getValue(); if( channels.contains( channel ) ) { sel.resumeSelects( channel ); return; } } Debug.out( "resumeSelects():: channel not found!" ); } finally{ selectors_mon.exit(); } } else { selector_impl.resumeSelects( channel ); } }
/** * Cancel the selection operations for the given channel. * @param channel channel originally registered */ public void cancel( AbstractSelectableChannel channel ) { if( SAFE_SELECTOR_MODE_ENABLED ) { try{ selectors_mon.enter(); //System.out.println( "cancel - " + channel.hashCode() + " - " + Debug.getCompressedStackTrace()); for( Map.Entry<VirtualChannelSelectorImpl, ArrayList<AbstractSelectableChannel>> entry: selectors.entrySet()) { VirtualChannelSelectorImpl sel = entry.getKey(); ArrayList<AbstractSelectableChannel> channels = entry.getValue(); if( channels.remove( channel ) ) { sel.cancel( channel ); return; } } } finally{ selectors_mon.exit(); } } else { if( selector_impl != null ) selector_impl.cancel( channel ); } }
public void selectFailure( VirtualAbstractSelectorListener listener, AbstractSelectableChannel sc, Object attachment, Throwable msg) { if ( op == OP_ACCEPT ){ ((VirtualAcceptSelectorListener)listener).selectFailure( VirtualChannelSelector.this, (ServerSocketChannel)sc, attachment, msg ); }else{ ((VirtualSelectorListener)listener).selectFailure( VirtualChannelSelector.this, (SocketChannel)sc, attachment, msg ); } }
public void pauseSelects( AbstractSelectableChannel channel ) { //System.out.println( "pauseSelects: " + channel + " - " + Debug.getCompressedStackTrace() ); if( channel == null ) { return; } SelectionKey key = channel.keyFor( selector ); if( key != null && key.isValid() ) { key.interestOps( key.interestOps() & ~INTEREST_OP ); } else { //channel not (yet?) registered if( channel.isOpen() ) { //only bother if channel has not already been closed try{ register_cancel_list_mon.enter(); paused_states.put( channel, Boolean.TRUE); //ensure the op is paused upon reg select-time reg } finally{ register_cancel_list_mon.exit(); } } } }
public void pauseSelects( AbstractSelectableChannel channel ) { //System.out.println( "pauseSelects: " + channel + " - " + Debug.getCompressedStackTrace() ); if( channel == null ) { return; } SelectionKey key = channel.keyFor( selector ); if( key != null && key.isValid() ) { key.interestOps( key.interestOps() & ~INTEREST_OP ); } else { //channel not (yet?) registered if( channel.isOpen() ) { //only bother if channel has not already been closed try{ register_cancel_list_mon.enter(); paused_states.put( channel, new Boolean( true ) ); //ensure the op is paused upon reg select-time reg } finally{ register_cancel_list_mon.exit(); } } } }
public void register(AbstractSelectableChannel channel, GroupPurpose purpose, Object attachment) { int ops = 0; switch (purpose) { case ACCEPT: default: ops = SelectionKey.OP_ACCEPT; break; case IO: ops = SelectionKey.OP_READ | SelectionKey.OP_WRITE; break; } try { if (attachment != null) { channel.register(this.selector, ops, attachment); } else { channel.register(this.selector, ops); } } catch (Exception e) { log.warn("Error during selector registration", e); } }
protected final SelectionKey register (AbstractSelectableChannel ch, int ops, Object att) { SelectionKeyImpl result; if (ch instanceof SocketChannelImpl) result = new SocketChannelSelectionKey (ch, this); else if (ch instanceof DatagramChannelImpl) result = new DatagramChannelSelectionKey (ch, this); else if (ch instanceof ServerSocketChannelImpl) result = new ServerSocketChannelSelectionKey (ch, this); else if (ch instanceof gnu.java.nio.SocketChannelImpl) result = new gnu.java.nio.SocketChannelSelectionKeyImpl((gnu.java.nio.SocketChannelImpl)ch, this); else throw new InternalError ("No known channel type"); synchronized (keys) { keys.add (result); result.interestOps (ops); result.attach (att); } return result; }
/** * @tests AbstractSelector#register(AbstractSelectableChannel,int,Object) */ @TestTargetNew( level = TestLevel.COMPLETE, notes = "Verifies register method from SelectableChannel class.", method = "register", args = {AbstractSelectableChannel.class, int.class, java.lang.Object.class} ) public void test_register_LAbstractSelectableChannelIObject() throws Exception { Selector acceptSelector = new MockSelectorProvider().openSelector(); ServerSocketChannel ssc = ServerSocketChannel.open(); ssc.configureBlocking(false); assertFalse(ssc.isRegistered()); ssc.register(acceptSelector, SelectionKey.OP_ACCEPT); assertTrue(ssc.isRegistered()); assertTrue(((MockAbstractSelector)acceptSelector).isRegisterCalled); }
protected SelectionKey register(AbstractSelectableChannel channel, int operations, Object attachment) { if (!provider().equals(channel.provider())) { throw new IllegalSelectorException(); } synchronized (this) { synchronized (keysSet) { // System.out.println("Registering channel"); // create the key SelectionKey sk = new EpollSelectionKeyImpl(channel, operations, attachment, this); int index = addKey(sk); ((EpollSelectionKeyImpl) sk).setIndex(index); // System.out.println(" channel registered with index = " + // index); return sk; } } }
/** * @see java.nio.channels.spi.AbstractSelector#register(java.nio.channels.spi.AbstractSelectableChannel, * int, java.lang.Object) */ @Override protected SelectionKey register(AbstractSelectableChannel channel, int operations, Object attachment) { if (!provider().equals(channel.provider())) { throw new IllegalSelectorException(); } synchronized (this) { synchronized (unmodifiableKeys) { // create the key SelectionKeyImpl selectionKey = new SelectionKeyImpl( channel, operations, attachment, this); addKey(selectionKey); mutableKeys.add(selectionKey); return selectionKey; } } }
protected void addInterest( AbstractSelectableChannel channel , int interest , Object attachment , Selector selector ) { try { SelectionKey sk = channel.keyFor(selector); if (sk != null) { if (!sk.isValid()) return; int actualInterests = sk.interestOps(); if ((actualInterests & interest) != interest) sk.interestOps(actualInterests | interest); if (attachment != null) sk.attach(attachment); } else channel.register(selector, interest, attachment); } catch (ClosedChannelException e) { log.warn("Cannot add interest to selector channel : channel is closed"); } }
@Override public UUID connect(AbstractSelectableChannel ch) { assert ch != null; if (this.clients.size() == this.maxPlayers) { return null; } Connection next = new HSConnection(this, ch, this.master.getEncryptionBitCount()); this.clients.add(next); return next.getId(); }
public static void writeBlocking(WebSocketImpl ws, ByteChannel channel) throws InterruptedException, IOException { if (!$assertionsDisabled && (channel instanceof AbstractSelectableChannel) && !((AbstractSelectableChannel) channel).isBlocking()) { throw new AssertionError(); } else if ($assertionsDisabled || !(channel instanceof WrappedByteChannel) || ((WrappedByteChannel) channel).isBlocking()) { ByteBuffer buf = (ByteBuffer) ws.outQueue.take(); while (buf.hasRemaining()) { channel.write(buf); } } else { throw new AssertionError(); } }
void doAsyncClose(final AbstractSelectableChannel sc) { AdaptorCloseAndInterrupt.pool.schedule(new Callable<Void>() { public Void call() throws Exception { sc.close(); return null; } }, new Random().nextInt(1000), TimeUnit.MILLISECONDS); }
protected final SelectionKey register(AbstractSelectableChannel ch, int ops, Object attachment) { if (!(ch instanceof SelChImpl)) throw new IllegalSelectorException(); SelectionKeyImpl k = new SelectionKeyImpl((SelChImpl)ch, this); k.attach(attachment); synchronized (publicKeys) { implRegister(k); } k.interestOps(ops); return k; }
void doAsyncClose(final AbstractSelectableChannel sc) { AdaptorCloseAndInterrupt.pool.schedule(new Callable<Void>() { public Void call() throws Exception { sc.close(); isClosed.set(true); return null; } }, new Random().nextInt(1000), TimeUnit.MILLISECONDS); }
private void initSafeMode() { //System.out.println( "***************** SAFE SOCKET SELECTOR MODE ENABLED *****************" ); if (Logger.isEnabled()) { Logger.log(new LogEvent(LOGID, "***************** SAFE SOCKET SELECTOR MODE ENABLED *****************")); } selector_impl = null; selectors = new HashMap<>(); selectors_mon = new AEMonitor( "VirtualChannelSelector:FM" ); selectors.put( new VirtualChannelSelectorImpl( this, op, pause, randomise_keys ), new ArrayList<AbstractSelectableChannel>() ); selectors_keyset_cow = new HashSet<>(selectors.keySet()); }
public boolean selectSuccess( VirtualAbstractSelectorListener listener, AbstractSelectableChannel sc, Object attachment ) { if ( op == OP_ACCEPT ){ return(((VirtualAcceptSelectorListener)listener).selectSuccess( VirtualChannelSelector.this, (ServerSocketChannel)sc, attachment )); }else{ return(((VirtualSelectorListener)listener).selectSuccess( VirtualChannelSelector.this, (SocketChannel)sc, attachment )); } }
public void resumeSelects( AbstractSelectableChannel channel ) { //System.out.println( "resumeSelects: " + channel + " - " + Debug.getCompressedStackTrace() ); if( channel == null ) { Debug.printStackTrace( new Exception( "resumeSelects():: channel == null" ) ); return; } SelectionKey key = channel.keyFor( selector ); if( key != null && key.isValid() ) { // if we're resuming a non-interested key then reset the metrics if (( key.interestOps() & INTEREST_OP ) == 0 ){ RegistrationData data = (RegistrationData)key.attachment(); data.last_select_success_time = SystemTime.getCurrentTime(); data.non_progress_count = 0; } key.interestOps( key.interestOps() | INTEREST_OP ); } else { //channel not (yet?) registered try{ register_cancel_list_mon.enter(); paused_states.remove( channel ); //check if the channel's op has been already paused before select-time reg } finally{ register_cancel_list_mon.exit(); } } //try{ // selector.wakeup(); //} //catch( Throwable t ) { Debug.out( "selector.wakeup():: caught exception: ", t ); } }
RegistrationData( AbstractSelectableChannel _channel, VirtualChannelSelector.VirtualAbstractSelectorListener _listener, Object _attachment ) { channel = _channel; listener = _listener; attachment = _attachment; last_select_success_time = SystemTime.getCurrentTime(); }