📄 virtualchannelselectorimpl.java
字号:
/*
* Created on Jul 28, 2004
* Created by Alon Rohter
* Copyright (C) 2004, 2005, 2006 Aelitis, All Rights Reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* AELITIS, SAS au capital de 46,603.30 euros
* 8 Allee Lenotre, La Grille Royale, 78600 Le Mesnil le Roi, France.
*
*/
package com.aelitis.azureus.core.networkmanager.impl;
import java.nio.channels.*;
import java.util.*;
import org.gudy.azureus2.core3.logging.*;
import org.gudy.azureus2.core3.util.*;
import com.aelitis.azureus.core.networkmanager.VirtualChannelSelector;
/**
* Provides a simplified and safe (selectable-channel) socket single-op selector.
*/
public class VirtualChannelSelectorImpl {
protected Selector selector;
private final SelectorGuard selector_guard;
private final LinkedList register_cancel_list = new LinkedList();
private final AEMonitor register_cancel_list_mon = new AEMonitor( "VirtualChannelSelector:RCL");
private final HashMap paused_states = new HashMap();
private final int INTEREST_OP;
private final boolean pause_after_select;
protected final VirtualChannelSelector parent;
private int[] select_counts = new int[ 50 ];
private int round = 0;
private volatile boolean destroyed;
public VirtualChannelSelectorImpl( VirtualChannelSelector _parent, int _interest_op, boolean _pause_after_select ) {
this.parent = _parent;
INTEREST_OP = _interest_op;
pause_after_select = _pause_after_select;
String type;
switch( INTEREST_OP ) {
case VirtualChannelSelector.OP_CONNECT:
type = "OP_CONNECT"; break;
case VirtualChannelSelector.OP_READ:
type = "OP_READ"; break;
default:
type = "OP_WRITE"; break;
}
selector_guard = new SelectorGuard( type, new SelectorGuard.GuardListener() {
public boolean safeModeSelectEnabled() {
return parent.isSafeSelectionModeEnabled();
}
public void spinDetected() {
closeExistingSelector();
try { Thread.sleep( 1000 ); }catch( Throwable x ) {x.printStackTrace();}
parent.enableSafeSelectionMode();
}
public void failureDetected() {
try { Thread.sleep( 10000 ); }catch( Throwable x ) {x.printStackTrace();}
closeExistingSelector();
try { Thread.sleep( 1000 ); }catch( Throwable x ) {x.printStackTrace();}
selector = openNewSelector();
}
});
selector = openNewSelector();
}
protected Selector openNewSelector() {
Selector sel = null;
try {
sel = Selector.open();
}
catch (Throwable t) {
Debug.out( "ERROR: caught exception on Selector.open()", t );
try { Thread.sleep( 3000 ); }catch( Throwable x ) {x.printStackTrace();}
int fail_count = 1;
while( fail_count < 10 ) {
try {
sel = Selector.open();
break;
}
catch( Throwable f ) {
Debug.out( f );
fail_count++;
try { Thread.sleep( 3000 ); }catch( Throwable x ) {x.printStackTrace();}
}
}
if( fail_count < 10 ) { //success !
Debug.out( "NOTICE: socket Selector successfully opened after " +fail_count+ " failures." );
}
else { //failure
Logger.log(new LogAlert(LogAlert.REPEATABLE, LogAlert.AT_ERROR,
"ERROR: socket Selector.open() failed 10 times in a row, aborting."
+ "\nAzureus / Java is likely being firewalled!"));
}
}
return sel;
}
public void
register(
SocketChannel channel,
VirtualChannelSelector.VirtualSelectorListener listener,
Object attachment )
{
addRegOrCancel( new RegistrationData( channel, listener, attachment ));
}
public void pauseSelects( SocketChannel channel ) {
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 resumeSelects( SocketChannel channel ) {
if( channel == null ) {
Debug.printStackTrace( new Exception( "resumeSelects():: channel == null" ) );
return;
}
SelectionKey key = channel.keyFor( selector );
if( key != null && key.isValid() ) {
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 ); }
}
public void
cancel(
SocketChannel channel )
{
pauseSelects( channel );
addRegOrCancel( channel );
}
private void
addRegOrCancel(
Object obj_to_add )
{
if ( destroyed ){
if ( obj_to_add instanceof SocketChannel ){
// don't worry too much about cancels
}else{
Debug.out( "addRegOrCancel called after selector destroyed" );
}
}
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 )
for (Iterator it = register_cancel_list.iterator();it.hasNext();){
Object obj = it.next();
boolean remove_it = false;
if ( obj_to_add instanceof SocketChannel ){
if ( obj_to_add == obj ||
( obj instanceof RegistrationData &&
((RegistrationData)obj).channel == obj_to_add )){
// remove existing cancel or register
remove_it = true;
}
}else{
RegistrationData rd = (RegistrationData)obj_to_add;
if ( rd.channel == obj ||
( obj instanceof RegistrationData &&
((RegistrationData)obj).channel == rd.channel )){
remove_it = true;
}
}
if ( remove_it ){
it.remove();
break;
}
}
register_cancel_list.add( obj_to_add );
}finally{
register_cancel_list_mon.exit();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -