java.nio.channels.spi.AbstractSelectableChannel Java Examples

The following examples show how to use java.nio.channels.spi.AbstractSelectableChannel. 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: UDPSampler.java    From jmeter-plugins with Apache License 2.0 6 votes vote down vote up
@Override
protected AbstractSelectableChannel getChannel() throws IOException {
    DatagramChannel c;
    if (isWaitResponse()) {
        c = DatagramChannelWithTimeouts.open();
        ((DatagramChannelWithTimeouts) c).setReadTimeout(getTimeoutAsInt());
    } else {
        c = DatagramChannel.open();
    }

    String bindAddress = getBindAddress();
    if (bindAddress.isEmpty()) {
        bindAddress = "0.0.0.0";
    }
    int adr = getBindPortAsInt();
    c.bind(new InetSocketAddress(bindAddress, adr));

    int port = Integer.parseInt(getPort());
    c.connect(new InetSocketAddress(getHostName(), port));
    return c;
}
 
Example #2
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 #3
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 #4
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 #5
Source File: VirtualChannelSelector.java    From BiglyBT with GNU General Public License v2.0 6 votes vote down vote up
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 );

  }
 }
 
Example #6
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 #7
Source File: VirtualChannelSelector.java    From TorrentEngine with GNU General Public License v3.0 6 votes vote down vote up
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 );

  }
 }
 
Example #8
Source File: VirtualChannelSelector.java    From TorrentEngine with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 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 );
  }
}
 
Example #9
Source File: VirtualChannelSelector.java    From TorrentEngine with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 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 );
  }
}
 
Example #10
Source File: VirtualChannelSelector.java    From TorrentEngine with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 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 );
  }
}
 
Example #11
Source File: AdaptorCloseAndInterrupt.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example #12
Source File: HTTPRawSamplerTest.java    From jmeter-plugins with Apache License 2.0 5 votes vote down vote up
/**
 * Test of getChannel method, of class HTTPRawSampler.
 */
@Test
public void testGetChannel() throws Exception {
    System.out.println("getChannel");
    AbstractSelectableChannel result = instance.getChannel();
    assertNotNull(result);
}
 
Example #13
Source File: AdaptorCloseAndInterrupt.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example #14
Source File: NioUtils.java    From netcrusher-java with Apache License 2.0 5 votes vote down vote up
public static void close(AbstractSelectableChannel channel) {
    if (channel != null && channel.isOpen()) {
        try {
            channel.close();
        } catch (IOException e) {
            LOGGER.error("Fail to close channel", e);
        }
    }
}
 
Example #15
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 #16
Source File: VirtualChannelSelectorImpl.java    From TorrentEngine with GNU General Public License v3.0 5 votes vote down vote up
private RegistrationData( AbstractSelectableChannel _channel, VirtualChannelSelector.VirtualAbstractSelectorListener _listener, Object _attachment ) {
	channel 		= _channel;
	listener		= _listener;
	attachment 		= _attachment;
	
	last_select_success_time	= SystemTime.getCurrentTime();
}
 
Example #17
Source File: VirtualChannelSelector.java    From TorrentEngine with GNU General Public License v3.0 5 votes vote down vote up
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<VirtualChannelSelectorImpl,ArrayList<AbstractSelectableChannel>>();
   selectors_mon = new AEMonitor( "VirtualChannelSelector:FM" );
   selectors.put( new VirtualChannelSelectorImpl( this, op, pause, randomise_keys ), new ArrayList<AbstractSelectableChannel>() );
   selectors_keyset_cow = new HashSet<VirtualChannelSelectorImpl>( selectors.keySet());
 }
 
Example #18
Source File: VirtualChannelSelector.java    From TorrentEngine with GNU General Public License v3.0 5 votes vote down vote up
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 ));
  }
 }
 
Example #19
Source File: AdaptorCloseAndInterrupt.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example #20
Source File: AdaptorCloseAndInterrupt.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example #21
Source File: AdaptorCloseAndInterrupt.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example #22
Source File: AssociationImpl.java    From sctp with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * @param socketChannel
 *            the socketChannel to set
 */
protected void setSocketChannel(AbstractSelectableChannel socketChannel) {
	if (this.ipChannelType == IpChannelType.SCTP)
		this.socketChannelSctp = (SctpChannel) socketChannel;
	else
		this.socketChannelTcp = (SocketChannel) socketChannel;
}
 
Example #23
Source File: AdaptorCloseAndInterrupt.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example #24
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);
	}
}
 
Example #25
Source File: AbstractIPSamplerTest.java    From jmeter-plugins with Apache License 2.0 5 votes vote down vote up
/**
 * Test of getChannel method, of class AbstractIPSampler.
 */
@Test
public void testGetChannel() throws Exception {
    System.out.println("getChannel");
    AbstractIPSampler instance = new AbstractIPSamplerImpl();
    AbstractSelectableChannel expResult = null;
    AbstractSelectableChannel result = instance.getChannel();
    assertEquals(expResult, result);
}
 
Example #26
Source File: HTTPRawSampler.java    From jmeter-plugins with Apache License 2.0 5 votes vote down vote up
@Override
protected AbstractSelectableChannel getChannel() throws IOException {
    int t = getTimeoutAsInt();
    if (t > 0) {
        SocketChannelWithTimeouts res = (SocketChannelWithTimeouts) SocketChannelWithTimeouts.open();
        res.setConnectTimeout(t);
        res.setReadTimeout(t);
        return res;
    } else {
        return SocketChannel.open();
    }
}
 
Example #27
Source File: VirtualChannelSelectorImpl.java    From BiglyBT with GNU General Public License v2.0 5 votes vote down vote up
public void
register(
	AbstractSelectableChannel 								channel,
	VirtualChannelSelector.VirtualAbstractSelectorListener 	listener,
	Object 													attachment )
   {
   	if ( destroyed ){

  			Debug.out( "register called after selector destroyed" );
   	}

   	if ( channel == null ){

   		Debug.out( "Attempt to register selects for null channel" );

   		return;
   	}

   	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 )

   		register_cancel_list.remove( channel );

		paused_states.remove( channel );

 			register_cancel_list.put( channel, new RegistrationData( channel, listener, attachment ));

   	}finally{

   		register_cancel_list_mon.exit();
   	}
   }
 
Example #28
Source File: AdaptorCloseAndInterrupt.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example #29
Source File: AdaptorCloseAndInterrupt.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example #30
Source File: AdaptorCloseAndInterrupt.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
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);
}