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

📄 test.java

📁 这是一个基于java编写的torrent的P2P源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * Created on 12-Jan-2005
 * Created by Paul Gardner
 * 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.dht.impl;

import com.aelitis.azureus.core.AzureusCoreFactory;
import com.aelitis.azureus.core.dht.*;
import com.aelitis.azureus.core.dht.control.DHTControlContact;
import com.aelitis.azureus.core.dht.nat.DHTNATPuncherAdapter;
import com.aelitis.azureus.core.dht.nat.impl.DHTNATPuncherImpl;
import com.aelitis.azureus.core.dht.transport.*;
import com.aelitis.azureus.core.dht.transport.loopback.DHTTransportLoopbackImpl;
import com.aelitis.azureus.core.dht.transport.udp.DHTTransportUDP;
import com.aelitis.azureus.core.dht.transport.udp.impl.DHTTransportUDPImpl;
import com.aelitis.azureus.plugins.dht.impl.DHTPluginStorageManager;

import java.io.*;
import java.math.BigInteger;
import java.net.InetSocketAddress;
import java.security.KeyFactory;
import java.security.Signature;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.RSAPrivateKeySpec;
import java.security.spec.RSAPublicKeySpec;
import java.util.*;

import org.gudy.azureus2.core3.util.AEThread;
import org.gudy.azureus2.core3.util.HashWrapper;
import org.gudy.azureus2.core3.util.SHA1Simple;
import org.gudy.azureus2.core3.util.Timer;
import org.gudy.azureus2.core3.util.TimerEvent;
import org.gudy.azureus2.core3.util.TimerEventPerformer;
import org.gudy.azureus2.plugins.PluginInterface;
import org.gudy.azureus2.plugins.logging.LoggerChannel;
import org.gudy.azureus2.plugins.logging.LoggerChannelListener;

/**
 * @author parg
 *
 */

public class 
Test 
	implements DHTNATPuncherAdapter
{
	static boolean	AELITIS_TEST	= false;
	static InetSocketAddress	AELITIS_ADDRESS = new InetSocketAddress("213.186.46.164", 6881);
	
	static int DEFAULT_NETWORK = DHT.NW_CVS;
	
	static{
		
		DHTTransportUDPImpl.TEST_EXTERNAL_IP	= true;
	}
	
	int num_dhts			= 3;
	int num_stores			= 2;
	static int MAX_VALUES	= 10000;
	
	boolean	udp_protocol	= true;
	int		udp_timeout		= 1000;
	

	static int		K			= 20;
	static int		B			= 5;
	static int		ID_BYTES	= 20;
	
	int		fail_percentage	= 00;
	
	static Properties	dht_props = new Properties();

	static{
		dht_props.put( DHT.PR_CONTACTS_PER_NODE, new Integer(K));
		dht_props.put( DHT.PR_NODE_SPLIT_FACTOR, new Integer(B));
		dht_props.put( DHT.PR_CACHE_REPUBLISH_INTERVAL, new Integer(30000));
		dht_props.put( DHT.PR_ORIGINAL_REPUBLISH_INTERVAL, new Integer(60000));
	}

	static byte[]	th_key	= new byte[]{ 1,1,1,1 };
	
	static Map	check = new HashMap();


	static DHTLogger	logger;
	
	static{
		
		final LoggerChannel c_logger = AzureusCoreFactory.create().getPluginManager().getDefaultPluginInterface().getLogger().getNullChannel("test");
		
		c_logger.addListener(
			new LoggerChannelListener()
			{
				public void
				messageLogged(
					int		type,
					String	content )
				{
					System.out.println( content );
				}
				
				public void
				messageLogged(
					String		str,
					Throwable	error )
				{
					System.out.println( str );
					
					error.printStackTrace();
				}
			});
		
		logger = 
			new DHTLogger()
			{
				public void
				log(
					String	str )
				{
					c_logger.log( str );
				}
			
				public void
				log(
					Throwable e )
				{
					c_logger.log( e );
				}
				
				public void
				log(
					int		log_type,
					String	str )
				{
					if ( isEnabled( log_type )){
						
						c_logger.log( str );
					}
				}
			
				public boolean
				isEnabled(
					int	log_type )
				{
					return( true );
				}
					
				public PluginInterface
				getPluginInterface()
				{
					return( c_logger.getLogger().getPluginInterface());
				}
			};
	}

	public static DHTLogger
	getLogger()
	{
		return( logger );
	}
	
	public static void
	main(
		String[]		args )
	{
		new Test();
	}
	
	
	Map	port_map = new HashMap();
	
	protected
	Test()
	{
		try{
			DHTLog.setLogging( true );
			
			DHT[]			dhts 		= new DHT[num_dhts*2+30];
			DHTTransport[]	transports 	= new DHTTransport[num_dhts*2+30];
			
			
			for (int i=0;i<num_dhts;i++){
				
				createDHT( dhts, transports, DEFAULT_NETWORK, i );
			}

			for (int i=0;i<num_dhts-1;i++){
			
				if ( AELITIS_TEST ){
					
					((DHTTransportUDP)transports[i]).importContact( AELITIS_ADDRESS, DHTTransportUDP.PROTOCOL_VERSION_MAIN );
					
				}else{
					ByteArrayOutputStream	baos = new ByteArrayOutputStream();
					
					DataOutputStream	daos = new DataOutputStream( baos );
					
					transports[i].getLocalContact().exportContact( daos );
					
					daos.close();
					
					transports[i+1].importContact( new DataInputStream( new ByteArrayInputStream( baos.toByteArray())));
				}
					
				dhts[i].integrate(true);
					
				if ( i > 0 && i%10 == 0 ){
						
					System.out.println( "Integrated " + i + " DHTs" );
				}
			}
			
			if ( AELITIS_TEST ){
				
				((DHTTransportUDP)transports[num_dhts-1]).importContact( AELITIS_ADDRESS, DHTTransportUDP.PROTOCOL_VERSION_MAIN );

			}else{
				
				ByteArrayOutputStream	baos = new ByteArrayOutputStream();
				
				DataOutputStream	daos = new DataOutputStream( baos );
				
				transports[0].getLocalContact().exportContact( daos );
				
				daos.close();
				
				transports[num_dhts-1].importContact( new DataInputStream( new ByteArrayInputStream( baos.toByteArray())));
			}
			
			
			dhts[num_dhts-1].integrate( true );
			
			DHTTransportLoopbackImpl.setFailPercentage(fail_percentage);
			
			//dht1.print();
			
			//DHTTransportLoopbackImpl.setLatency( 500);
			
			/*
			System.out.println( "before put:" + transports[99].getStats().getString());
			dhts[99].put( "fred".getBytes(), new byte[2]);
			System.out.println( "after put:" + transports[99].getStats().getString());

			System.out.println( "get:"  + dhts[0].get( "fred".getBytes()));
			System.out.println( "get:"  + dhts[77].get( "fred".getBytes()));
			*/
			
			Map	store_index = new HashMap();
			
			for (int i=0;i<num_stores;i++){
				
				int	dht_index = (int)(Math.random()*num_dhts);
				
				DHT	dht = dhts[dht_index];

				dht.put( (""+i).getBytes(), "", new byte[4], (byte)0, new DHTOperationAdapter());
			
				store_index.put( ""+i, dht );
				
				if ( i != 0 && i %100 == 0 ){
					
					System.out.println( "Stored " + i + " values" );
				}
			}
			
			Timer	timer = new Timer("");
			
			timer.addPeriodicEvent(
				10000,
				new TimerEventPerformer()
				{
					public void 
					perform(
						TimerEvent event) 
					{
						if ( !udp_protocol ){
							
							DHTTransportStats stats = DHTTransportLoopbackImpl.getOverallStats();
						
							System.out.println( "Overall stats: " + stats.getString());
						}
					}
				});
			
			LineNumberReader	reader = new LineNumberReader( new InputStreamReader( System.in ));
			
			while( true ){
				
				System.out.print( "> " );
				
				try{
					String	str = reader.readLine().trim();
					
					if ( str == null ){
						
						break;
					}
					
					int	pos = str.indexOf(' ');
					
					if ( pos == -1 || pos == 0 ){
						
						usage();
						
						continue;
					}
					
					int	dht_index = (int)(Math.random()*num_dhts);
					
					DHT	dht = dhts[dht_index];
					
					String	lhs = str.substring(0,pos);
					String	rhs = str.substring(pos+1);
					
					DHTTransportStats	stats_before 	= null;
					
					char command = lhs.toLowerCase().charAt(0);
					
					if ( command == 'p' ){
						
						pos = rhs.indexOf('=');
						
						if ( pos == -1 ){
							
							usage();
							
						}else{
						
							System.out.println( "Using dht " + dht_index );
							
							stats_before = dht.getTransport().getStats().snapshot();
	
							String	key = rhs.substring(0,pos);
							String	val = rhs.substring(pos+1);
							
							dht.put( key.getBytes(), "", val.getBytes(), (byte)(Math.random()*255), new DHTOperationAdapter() );
						}
					}else if ( command == 'x' ){
						
						dht = (DHT)store_index.get( rhs );
						
						if ( dht == null ){
							
							System.out.println( "DHT not found" );
							
						}else{
							
							stats_before = dht.getTransport().getStats().snapshot();
							
							byte[]	res = dht.remove( rhs.getBytes(), "", new DHTOperationAdapter());
							
							if ( res != null ){
								
								store_index.remove( rhs );
							}
							
							System.out.println( "-> " + (res==null?"null":new String(res)));
						}
					}else if ( command == 'e' ){
						
						dht = (DHT)store_index.get( rhs );
						
						if ( dht == null ){
							
							System.out.println( "DHT not found" );
							
						}else{
							
							DataOutputStream	daos = new DataOutputStream( new FileOutputStream( "C:\\temp\\dht.state"));
							
							dht.exportState( daos, 0 );
							
							daos.close();
						}
					}else if ( command == 'g' ){
						
						System.out.println( "Using dht " + dht_index );
						
						stats_before = dht.getTransport().getStats().snapshot();
					
						dht.get( 
								rhs.getBytes(), "", (byte)0, 32, 0, false, false,
								new DHTOperationAdapter()
								{
									public void
									read(
										DHTTransportContact	contact,
										DHTTransportValue	value )
									{
										System.out.println( "-> " + new String(value.getValue()));
									}
																	
									public void
									complete(
										boolean				timeout )
									{
										System.out.println( "-> complete" );
									}		
								});
						
					}else if ( command == '?' ){
						
						System.out.println( "Using dht " + dht_index );
						
						stats_before = dht.getTransport().getStats().snapshot();
					
						final DHT	f_dht = dht;
						
						dht.get( 
								rhs.getBytes(), "", DHT.FLAG_STATS, 32, 0, false, false,
								new DHTOperationAdapter()
								{
									public void
									read(
										DHTTransportContact	contact,
										DHTTransportValue	value )
									{
										System.out.println( "-> " + new String(value.getValue()) + ", flags=" + value.getFlags());
										
										try{
											DHTStorageKeyStats	stats = f_dht.getStorageAdapter().deserialiseStats( new DataInputStream( new ByteArrayInputStream( value.getValue())));
											
											System.out.println( "    stats: size = " + stats.getSize() + ", entries=" + stats.getEntryCount() + ", rpm=" + stats.getReadsPerMinute());
										}catch( Throwable e ){
											
											e.printStackTrace();
										}
									}
																	
									public void
									complete(
										boolean				timeout )
									{
										System.out.println( "-> complete" );
									}		
								});
						
						
						
					}else if ( command == 'd' ){
						
						System.out.println( "Using dht " + dht_index );
						
						stats_before = dht.getTransport().getStats().snapshot();
						
						byte[]	res = dht.remove( rhs.getBytes(), "", new DHTOperationAdapter());
						
						System.out.println( "-> " + (res==null?"null":new String(res)));
						
					}else if ( command == 'z' ){
						
						System.out.println( "Using dht " + dht_index );
						
						stats_before = dht.getTransport().getStats().snapshot();
						
						dht.get( rhs.getBytes(), "", (byte)0, 10, 0, false, false,
								new DHTOperationListener()
								{
									public void
									searching(
										DHTTransportContact	contact,
										int					level,
										int					active_searches )
									{
										
									}
									
									public void
									diversified()
									{
									}
									
									public void
									found(
										DHTTransportContact	contact )
									{
									}
									
									public void
									read(
										final DHTTransportContact	contact,
										final DHTTransportValue		value )
									{
										System.out.println( "-> " + value.getString());
	
										new AEThread("blah")
										{
											public void
											runSupport()
											{
												DHTTransportFullStats stats = contact.getStats();
										
												System.out.println( "    stats = " + stats.getString() );
											}
										}.start();
									}
									public void
									wrote(
										final DHTTransportContact	contact,
										DHTTransportValue	value )
									{
									}
									
						
									public void
									complete(
										boolean				timeout )
									{
										System.out.println( "complete");
									}
								});
						
						
					}else if ( command == 'v' ){
				
						try{
							int	index = Integer.parseInt( rhs );
					
							dht = dhts[index];
	
							stats_before = dht.getTransport().getStats().snapshot();
							
							dht.print();
	
							List	l = dht.getControl().getContacts();
							
							for (int i=0;i<l.size();i++){
								
								DHTControlContact	c = (DHTControlContact)l.get(i);
								
								System.out.println( "  contact:" + c.getRouterContact().getString() + "/" + c.getTransportContact().getString());

⌨️ 快捷键说明

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