FileDocCategorySizeDatePackage
CLCacheDiscovery.javaAPI DocAzureus 3.0.3.48528Fri May 11 13:55:34 BST 2007com.aelitis.azureus.core.peer.cache.cachelogic

CLCacheDiscovery

public class CLCacheDiscovery extends Object implements com.aelitis.azureus.core.peer.cache.CacheDiscoverer
This class handles communication with the CDP server. Original code from
author
Matthias Scheler

Fields Summary
public static final String
CDPDomainName
public static final String
CDPServerName
public static final int
CDPPort
public static final int
CDPVersion
public static final int
CDPTimeout
static CDPResponse
Response
Constructors Summary
Methods Summary
private java.lang.StringbyteArrayToHex(byte[] Bytes, int Max)


	/* Convert an array of bytes into a its hexadecimal representation. */
	
	  
	    
		int		Length, Index, Value;
		String	Result;

		Length = Bytes.length;
		if (Length > Max)
			Length = Max;

		Result = new String();
		for (Index = 0; Index < Length; Index++) {
			Value = Bytes[Index] & 0xff;
			if (Value < 16)
				Result += "0";
			Result += Integer.toHexString(Value);
		}
		return Result;
	
public java.net.InetAddress[]findCache(java.net.URL announce_url, java.lang.String hex_hash)

		String		Hostname;
		InetAddress[]	Caches;

		/*
		 * Build the hostname for the DNS query:
		 * bt-<short hash>.bt-<announce hash>-<farm>.find-cache.com
		 *
		 * short hash:	first four hexadecimal digits of the BitTorrent hash
		 * announce hash:	see hashAnnounceURL()
		 * farm:		farm name returned by CDP query.
		 */
		Hostname = "bt-" + hex_hash.substring(0, 4) +
		".bt-" + hashAnnounceURL(announce_url) +
		"-" + lookupFarm() + CDPDomainName;
		// System.out.println("findCache(): " + announce_url + " " + hex_hash + " --> " +	Hostname);
		try {
			Caches = InetAddress.getAllByName(Hostname);
		} catch (UnknownHostException NoCache) {
			Caches = new InetAddress[0];
		}
		return Caches;
	
public java.net.InetAddress[]findCache(java.net.URL announce_url, byte[] hash)

		return findCache(announce_url, byteArrayToHex(hash, 4));
	
private java.lang.StringhashAnnounceURL(java.net.URL announce_url)

		/* Calculate SHA1 hash of the hostname. */
		
		byte[] Digest = new SHA1Hasher().calculateHash( announce_url.getHost().getBytes());

		/*
		 * Return first 32 characters of the hexadecimal representation of the
		 * SHA1 hash.
		 */
		
		return byteArrayToHex(Digest, 16);
	
public com.aelitis.azureus.core.peer.cache.CachePeer[]lookup(org.gudy.azureus2.core3.torrent.TOTorrent torrent)

		try{
			InetAddress[]	addresses = findCache( torrent.getAnnounceURL(), torrent.getHash());
			
			CachePeer[] result = new CachePeer[addresses.length];
			
			for (int i=0;i<addresses.length;i++){
				
				result[i] = new CacheDiscovery.CachePeerImpl( CachePeer.PT_CACHE_LOGIC, addresses[i], 6881 );
			}
			
			return( result );
			
		}catch( TOTorrentException e ){
			
			Debug.printStackTrace( e );
			
			return( new CachePeer[0] );
		}
	
public com.aelitis.azureus.core.peer.cache.CachePeerlookup(byte[] peer_id, java.net.InetAddress ip, int port)

		if ( PeerClassifier.getClientDescription( peer_id ).startsWith( PeerClassifier.CACHE_LOGIC )){
			
			return( new CacheDiscovery.CachePeerImpl( CachePeer.PT_CACHE_LOGIC, ip, port ));
		}
		
		return( null );
	
private java.lang.StringlookupFarm()

		if (Response != null) {
			
			if (Response.isStillValid()){
			
				return Response.getFarmID();
			}
			
			Response = null;
		}

		try {
			InetAddress	CDPServer;
			DatagramSocket	Socket;
			CDPQuery		Query;
			byte[]		Buffer;
			DatagramPacket	Packet;

			/* Get the IP address of the CDP servers. */
			CDPServer = InetAddress.getByName(CDPServerName);

			/* Create a UDP socket for the CDP query. */
			Socket = new DatagramSocket();
			Socket.setSoTimeout(CDPTimeout);

			/* Build the CDP query. */
			Query = new CDPQuery(Constants.AZUREUS_NAME + " " +	Constants.AZUREUS_VERSION);
			Buffer = Query.getBytes();
			Packet = new DatagramPacket(Buffer, Buffer.length, CDPServer, CDPPort);

			/* Send the query to the CDP server. */
			Socket.send(Packet);

			/* Receive the CDP response. */
			Buffer = new byte[CDPResponse.MaxSize];
			Packet.setData(Buffer);
			Socket.receive(Packet);
			if (Packet.getAddress() != CDPServer || Packet.getPort() != CDPPort)
				throw(new Exception("CDP server address mismatch on response"));

			/* Parse the CDP response. */
			Response = new CDPResponse(Packet.getData());

			/* Return the farmID from the CDP response. */
			return Response.getFarmID();
		} catch (Throwable Excpt) {
			
			if ( Excpt instanceof UnknownHostException ){
				
			}else{
				
				Excpt.printStackTrace();
			}
			
			return "default";
		}
	
public static voidmain(java.lang.String[] args)

		try{
			TOTorrent torrent = TOTorrentFactory.deserialiseFromBEncodedFile( new File( "C:\\temp\\test.torrent" ));
			
			CachePeer[]	peers = new CLCacheDiscovery().lookup( torrent );
			
			System.out.println( "peers=" + peers.length );
			
			for (int i=0;i<peers.length;i++){
				
				System.out.println( "    cache: " + peers[i].getAddress() + ":" + peers[i].getPort() );
			}
		}catch( Throwable e ){
			
			e.printStackTrace();
		}