⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dhtspeedtesterimpl.java

📁 这是一个基于java编写的torrent的P2P源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		activePing(
			List	candidates )
		{
			String	str  = "";
			
			pingInstanceSet	ping_set = new pingInstanceSet( false );
			
			synchronized( this ){
				
				for (int i=0;i<candidates.size();i++){
					
					potentialPing	pp = (potentialPing)candidates.get(i);
				
					str += (i==0?"":",") + pp.getContact().getString() + "/" + pp.getRTT();
					
					ping( ping_set, pp.getContact());
				}
			}
		}
		
		protected boolean
		update(
			pingInstanceSet		ping_set,
			int					tick_count )
		{
			synchronized( this ){
				
				if ( dead || !running || outstanding > 0 ){
					
					return( false );
				}
				
				if ( best_pingee == null ){
					
					dead	= true;
					
					return( false );
				}					
			}
			
			if ( tick_count % period == 0 ){
				
				ping( ping_set, best_pingee );
			}
			
			return( true );
		}
		
		protected void
		ping(
			pingInstanceSet		ping_set,
			DHTTransportContact	contact )
		{
			final pingInstance	pi = new pingInstance( ping_set );
			
			outstanding++;

			try{
				contact.sendImmediatePing(
					new DHTTransportReplyHandlerAdapter()
					{
						public void
						pingReply(
							DHTTransportContact contact )
						{
							int	rtt = getElapsed();
							
							try{
								synchronized( activePing.this ){
																
									outstanding--;
									
									if ( !running ){									
										
										if ( rtt < best_ping ){
											
											best_pingee = contact;
											best_ping	= rtt;
										}
									
										if ( outstanding == 0 ){
											
											running = true;
										}
									}else{
										
										total_ok++;
										
										consec_fails	= 0;
									}
								}
								
								Iterator	it = listeners.iterator();
								
								while( it.hasNext()){
									
									try{
										((DHTSpeedTesterContactListener)it.next()).ping( activePing.this, getElapsed());
										
									}catch( Throwable e ){
										
										Debug.printStackTrace(e);
									}
								}
							}finally{
								
								pi.setResult( activePing.this, rtt );
							}
							// System.out.println( "    " + contact.getString() + ": " + getElapsed() + ", " + contact.getVivaldiPosition().estimateRTT( dht.getTransport().getLocalContact().getVivaldiPosition().getCoordinates()));					
						}
						
						public void
						failed(
							DHTTransportContact 	contact,
							Throwable				error )
						{
							try{
								synchronized( activePing.this ){
									
									outstanding--;
									
									if ( !running ){
		
										if ( outstanding == 0 ){
											
											running = true;
										}
									}else{
										
										consec_fails++;
										total_fails++;
										
										if ( consec_fails == 3 ){
											
											dead	= true;
											
										}else if ( 	total_ok > 10 && total_fails > 0 && 
													total_ok / total_fails < 1 ){
											
												// failing too often
											
											dead	= true;
											
										}else if ( total_ok > 100 ){
											
											total_ok	= 0;
											total_fails	= 0;
										}
									}
								}
								
								if ( !dead ){
									
									Iterator	it = listeners.iterator();
									
									while( it.hasNext()){
										
										try{
											((DHTSpeedTesterContactListener)it.next()).pingFailed( activePing.this );
											
										}catch( Throwable e ){
											
											Debug.printStackTrace(e);
										}
									}
								}
								// System.out.println( "    " + contact.getString() + ": failed" );
							}finally{
								
								pi.setResult( activePing.this, -1 );
							}
						}
					},
				PING_TIMEOUT );
				
			}catch( Throwable e ){
			
				pi.setResult( this, -1 );
				
				dead	= true;
				
				outstanding--;
				
				Debug.printStackTrace(e);
			}
		}
		
		public void
		destroy()
		{
			dead = true;
		}
		
		protected boolean
		isDead()
		{
			return( dead );
		}
		
		protected boolean
		isInformedAlive()
		{
			return( informed_alive );
		}
		
		protected void
		setInformedAlive()
		{
			informed_alive	= true;
		}
		
		protected void
		informDead()
		{
			if ( informed_alive ){
				
				Iterator	it = listeners.iterator();
				
				while( it.hasNext()){
					
					try{
						((DHTSpeedTesterContactListener)it.next()).contactDied( this );
						
					}catch( Throwable e ){
						
						Debug.printStackTrace(e);
					}
				}
			}
		}
		
		public DHTTransportContact
		getContact()
		{
			return( best_pingee );
		}
		
		public int
		getPingPeriod()
		{
			return( period );
		}
		
		public void
		setPingPeriod(
			int		_period )
		{
			period	= _period;
		}
		
		public void
		addListener(
			DHTSpeedTesterContactListener	listener )
		{
			listeners.add( listener );
		}
		
		public void
		removeListener(
			DHTSpeedTesterContactListener	listener )
		{
			listeners.remove( listener );
		}
	}
	
	protected class
	pingInstance
	{
		private activePing			contact;
		private pingInstanceSet		set;
		private int					result;
		
		protected
		pingInstance(
			pingInstanceSet		_set )
		{
			set	= _set;
			
			set.add( this );
		}
		
		protected activePing
		getContact()
		{
			return( contact );
		}
		
		protected int
		getResult()
		{
			return( result );
		}
		
		protected void
		setResult(
			activePing	_contact,
			int			_result )
		{
			contact	= _contact;
			result	= _result;
			
			set.complete( this );
		}
	}
	
	protected class
	pingInstanceSet
	{
		private boolean		active;
		private int			instances;
		private boolean		full;
		
		List	results = new ArrayList();
		
		protected 
		pingInstanceSet(
			boolean	_active )
		{
			active	= _active;
		}
		
		protected void
		add(
			pingInstance	instance )
		{
			synchronized( this ){
				
				instances++;
			}
		}
		
		protected void
		setFull()
		{
			synchronized( this ){

				full	= true;
				
				if ( results.size() == instances ){
					
					sendResult();
				}
			}
		}
		
		protected void
		complete(
			pingInstance	instance )
		{
			synchronized( this ){
							
				results.add( instance );
				
				if ( results.size() == instances && full ){
					
					sendResult();
				}
			}
		}
		
		protected void
		sendResult()
		{
			if ( active && results.size() > 0 ){
				
				DHTSpeedTesterContact[]	contacts 	= new DHTSpeedTesterContact[results.size()];
				int[]					rtts		= new int[contacts.length];
				
				for (int i=0;i<contacts.length;i++){
					
					pingInstance	pi = (pingInstance)results.get(i);
					
					contacts[i] = pi.getContact();
					rtts[i]		= pi.getResult();
				}
				
				DHTSpeedTesterImpl.this.informResults( contacts, rtts );
			}
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -