Java Code Examples for java.nio.channels.spi.AbstractSelectableChannel#keyFor()

The following examples show how to use java.nio.channels.spi.AbstractSelectableChannel#keyFor() . 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: VirtualChannelSelectorImpl.java    From BiglyBT with GNU General Public License v2.0 6 votes vote down vote up
public boolean
isPaused(
	AbstractSelectableChannel	channel )
{
    SelectionKey key = channel.keyFor( selector );

    if( key != null && key.isValid() ) {
    	
    	return((  key.interestOps() & INTEREST_OP ) == 0 );
    }else{
        try{  
        	register_cancel_list_mon.enter();

        	Boolean b = paused_states.get( channel );

        	return( b != null && b );
        	
      }finally{ 
    	  register_cancel_list_mon.exit();  
      }
    }
}
 
Example 2
Source File: VirtualChannelSelectorImpl.java    From BiglyBT with GNU General Public License v2.0 6 votes vote down vote up
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();  }
        }
      }
    }
 
Example 3
Source File: VirtualChannelSelectorImpl.java    From BiglyBT with GNU General Public License v2.0 6 votes vote down vote up
public boolean
isRegistered( AbstractSelectableChannel channel ){

  SelectionKey key = channel.keyFor( selector );

     if( key != null ){
    	 return( true );
     }else{
	  	try{
    		register_cancel_list_mon.enter();

    			// ensure that there's only one operation outstanding for a given channel
    			// at any one time (the latest operation requested )

    		return( register_cancel_list.containsKey( channel ));

    	}finally{

    		register_cancel_list_mon.exit();
    	} 
     }
}
 
Example 4
Source File: VirtualChannelSelectorImpl.java    From TorrentEngine with GNU General Public License v3.0 6 votes vote down vote up
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();  }
    }
  }
}
 
Example 5
Source File: VirtualChannelSelectorImpl.java    From BiglyBT with GNU General Public License v2.0 5 votes vote down vote up
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 );   }
}
 
Example 6
Source File: VirtualChannelSelectorImpl.java    From TorrentEngine with GNU General Public License v3.0 5 votes vote down vote up
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 );   }
}
 
Example 7
Source File: AbstractIoService.java    From jane with GNU Lesser General Public License v3.0 5 votes vote down vote up
protected void close(AbstractSelectableChannel channel) {
	if (channel == null)
		return;
	try {
		SelectionKey key = channel.keyFor(selector);
		if (key != null)
			key.cancel();
		channel.close();
	} catch (Exception e) {
		ExceptionMonitor.getInstance().exceptionCaught(e);
	}
}