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

📄 dhtudputils.java

📁 一个基于JAVA的多torrent下载程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		
		DHTTransportValue value = 
			new DHTTransportValue()
			{
				public boolean
				isLocal()
				{
					return( false );
				}
				
				public long
				getCreationTime()
				{
					return( created );
				}
				
				public byte[]
				getValue()
				{
					return( value_bytes );
				}
				
				public int
				getVersion()
				{
					return( version );
				}
				
				public DHTTransportContact
				getOriginator()
				{
					return( originator );
				}
				
				public int
				getFlags()
				{
					return( flags );
				}
				
				public String
				getString()
				{
					return( new String(getValue()));
				}
			};
			
		return( value );
	}
	
	public static final int DHTTRANSPORTVALUE_SIZE_WITHOUT_VALUE	= 15 + DHTTRANSPORTCONTACT_SIZE;
		
	protected static void
	serialiseTransportValue(
		DHTUDPPacket		packet,
		DataOutputStream	os,
		DHTTransportValue	value,
		long				skew )
	
		throws IOException, DHTTransportException
	{		
		if ( packet.getProtocolVersion() >= DHTTransportUDP.PROTOCOL_VERSION_REMOVE_DIST_ADD_VER ){
			
			int	version = value.getVersion();
			
			//System.out.println( "write: version = " + version );

			os.writeInt( version );
		}else{
			
			//System.out.println( "write: 0" );

			os.writeInt( 0 );
		}
		
			// Don't forget to change the CONSTANT above if you change the size of this!
				
		os.writeLong( value.getCreationTime() + skew );	// 12
		
		serialiseByteArray( os, value.getValue(), DHT.MAX_VALUE_SIZE );	// 12+2+X
		
		serialiseContact( os, value.getOriginator());	// 12 + 2+X + contact
		
		os.writeByte( value.getFlags());	// 13 + 2+ X + contact
	}
	
	protected static void
	serialiseContacts(
		DataOutputStream		os,
		DHTTransportContact[]	contacts )
	
		throws IOException
	{
		serialiseLength( os, contacts.length, 65535 );
		
		for (int i=0;i<contacts.length;i++){
			
			try{
				serialiseContact( os, contacts[i] );
				
			}catch( DHTTransportException e ){
				
				Debug.printStackTrace(e);
				
					// not much we can do here to recover - shouldn't fail anyways
				
				throw( new IOException(e.getMessage()));
			}
		}
	}

	protected static DHTTransportContact[]
	deserialiseContacts(
		DHTTransportUDPImpl		transport,
		DataInputStream			is )
	
		throws IOException
	{
		int	len = deserialiseLength( is, 65535 );
		
		List	l = new ArrayList( len );
		
		for (int i=0;i<len;i++){
			
			try{
				
				l.add( deserialiseContact( transport, is ));
				
			}catch( DHTTransportException e ){
				
				Debug.printStackTrace(e);
			}
		}
				
		DHTTransportContact[]	res = new DHTTransportContact[l.size()];
		
		l.toArray( res );
		
		return( res );
	}
			
	protected static void
	serialiseVivaldi(
		DataOutputStream	os,
		DHTUDPPacketReply	reply )
	
		throws IOException
	{
		float[]	data = reply.getVivaldiData();
	
		if ( data.length != DHTUDPPacketReply.VIVALDI_DATA_LENGTH_V1 ){
		
			Debug.out( "Vivaldi serialisation length changed!!!!" );
			
			throw( new IOException( "argh!!" ));
		}
		
		for (int i=0;i<data.length;i++){
			
			os.writeFloat( data[i] );
		}
	}
	
	protected static void
	deserialiseVivaldi(
		DataInputStream		is,
		DHTUDPPacketReply	reply )
	
		throws IOException
	{
		float[]	data	= new float[DHTUDPPacketReply.VIVALDI_DATA_LENGTH];
		
		if ( data.length != DHTUDPPacketReply.VIVALDI_DATA_LENGTH_V1 ){
			
			Debug.out( "Vivaldi serialisation length changed!!!!" );
			
			throw( new IOException( "argh!!" ));
		}
		
		for (int i=0;i<data.length;i++){
			
			data[i] = is.readFloat();
		}
		
		reply.setVivaldiData( data );
	}
	
	protected static void
	serialiseStats(
		int						version,
		DataOutputStream		os,
		DHTTransportFullStats	stats )
	
		throws IOException
	{
		os.writeLong( stats.getDBValuesStored());
		
		os.writeLong( stats.getRouterNodes());
		os.writeLong( stats.getRouterLeaves());
		os.writeLong( stats.getRouterContacts());
		
		os.writeLong( stats.getTotalBytesReceived());
		os.writeLong( stats.getTotalBytesSent());
		os.writeLong( stats.getTotalPacketsReceived());
		os.writeLong( stats.getTotalPacketsSent());
		os.writeLong( stats.getTotalPingsReceived());
		os.writeLong( stats.getTotalFindNodesReceived());
		os.writeLong( stats.getTotalFindValuesReceived());
		os.writeLong( stats.getTotalStoresReceived());
		os.writeLong( stats.getAverageBytesReceived());
		os.writeLong( stats.getAverageBytesSent());
		os.writeLong( stats.getAveragePacketsReceived());
		os.writeLong( stats.getAveragePacketsSent());
		
		os.writeLong( stats.getIncomingRequests());
		
		String	azversion = stats.getVersion() + "["+version+"]";
		
		serialiseByteArray( os, azversion.getBytes(), 64);
		
		if ( version >= 5 ){
			
			os.writeLong( stats.getRouterUptime());
			os.writeInt( stats.getRouterCount());
		}
	}
	
	protected static DHTTransportFullStats
	deserialiseStats(
		int					version,
		DataInputStream		is )
	
		throws IOException
	{
		final long db_values_stored				= is.readLong();
		
		final long router_nodes					= is.readLong();
		final long router_leaves				= is.readLong();
		final long router_contacts 				= is.readLong();
		
		final long total_bytes_received			= is.readLong();
		final long total_bytes_sent				= is.readLong();
		final long total_packets_received		= is.readLong();
		final long total_packets_sent			= is.readLong();
		final long total_pings_received			= is.readLong();
		final long total_find_nodes_received	= is.readLong();
		final long total_find_values_received	= is.readLong();
		final long total_stores_received		= is.readLong();
		final long average_bytes_received		= is.readLong();
		final long average_bytes_sent			= is.readLong();
		final long average_packets_received		= is.readLong();
		final long average_packets_sent			= is.readLong();
		
		final long incoming_requests			= is.readLong();
		
		final String	az_version = new String( deserialiseByteArray( is, 64 ));
		
		final long	router_uptime;
		final int	router_count;
		
		if ( version >= 5 ){
			
			router_uptime	= is.readLong();
			router_count	= is.readInt();
		}else{
			
			router_uptime	= 0;
			router_count	= 0;
		}
		
		DHTTransportFullStats	res = 
			new DHTTransportFullStats()
			{
				public long
				getDBValuesStored()
				{
					return( db_values_stored );
				}
				
					// Router
				
				public long
				getRouterNodes()
				{
					return( router_nodes );
				}
				
				public long
				getRouterLeaves()
				{
					return( router_leaves );
				}
				
				public long
				getRouterContacts()
				{
					return( router_contacts );
				}
			
				public long
				getRouterUptime()
				{
					return( router_uptime );
				}
				
				public int
				getRouterCount()
				{
					return( router_count );
				}
				public long
				getTotalBytesReceived()
				{
					return( total_bytes_received );
				}
				
				public long
				getTotalBytesSent()
				{
					return( total_bytes_sent );
				}
				
				public long
				getTotalPacketsReceived()
				{
					return( total_packets_received );
				}
				
				public long
				getTotalPacketsSent()
				{
					return( total_packets_sent );
				}
				
				public long
				getTotalPingsReceived()
				{
					return( total_pings_received );
				}
				
				public long
				getTotalFindNodesReceived()
				{
					return( total_find_nodes_received );
				}
				
				public long
				getTotalFindValuesReceived()
				{
					return( total_find_values_received );
				}
				
				public long
				getTotalStoresReceived()
				{
					return( total_stores_received );
				}
				
					// averages
				
				public long
				getAverageBytesReceived()
				{
					return( average_bytes_received );
				}
				
				public long
				getAverageBytesSent()
				{
					return( average_bytes_sent );
				}
				
				public long
				getAveragePacketsReceived()
				{
					return( average_packets_received );
				}
				
				public long
				getAveragePacketsSent()
				{
					return( average_packets_sent );
				}
				
				public long
				getIncomingRequests()
				{
					return( incoming_requests );
				}
				
				public String
				getVersion()
				{
					return( az_version );
				}
				
				public String
				getString()
				{
					return(	"transport:" + 
							getTotalBytesReceived() + "," +
							getTotalBytesSent() + "," +
							getTotalPacketsReceived() + "," +
							getTotalPacketsSent() + "," +
							getTotalPingsReceived() + "," +
							getTotalFindNodesReceived() + "," +
							getTotalFindValuesReceived() + "," +
							getTotalStoresReceived() + "," +
							getAverageBytesReceived() + "," +
							getAverageBytesSent() + "," +
							getAveragePacketsReceived() + "," +
							getAveragePacketsSent() + "," +
							getIncomingRequests() + 
							",router:" +
							getRouterNodes() + "," +
							getRouterLeaves() + "," +
							getRouterContacts() + 
							",database:" +
							getDBValuesStored()+
							",version:" + getVersion()+","+
							getRouterUptime() + ","+
							getRouterCount());
					}
			};
	
		
		return( res );
	}
}

⌨️ 快捷键说明

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