package Tracker;

import java.util.ArrayList;
import java.util.Timer;
import java.util.concurrent.locks.ReentrantLock;

import TorrentMetadata.TorrentFile;

/**
 * Class used for tracking torrent files and peers
 * @author Pawel
 *
 */

public class Tracker
{
	Timer timer;
	public static final String state[] =
		{
		    "Started", "Stopped", "Completed", "Update"
		};
	public int interval = 5 * 60;
	private ReentrantLock trackerLock = new ReentrantLock();
	private ArrayList<Peer> peers;
	private ArrayList<InfoHash> torrents = new ArrayList<InfoHash>();
	public int fileID, userID;
	public String url;
	public InfoHash infoHash;
	
	public Tracker(int fileID)
	{
		this.fileID = fileID;
		// infoHash value should be given here
		this.userID = 0;
		this.peers = new ArrayList<Peer>(fileID);
	}
	
	/**
	 * Locks tracker to prevent modification
	 */
	public void lock()
	{
		this.trackerLock.lock();
	}
	
	/**
	 * Unlocks tracker to make modifications possible
	 */
	public void unlock()
	{
		this.trackerLock.unlock();
	}
	/**
	 * Adds new torrent to track
	 * @param infoHash encoded in SHA1 hash of torrent metafile
	 */
	public void add(InfoHash infoHash)
	{
		if(this.trackerLock.isHeldByCurrentThread() == true)
		{
			System.out.println("Tracker is locked right now.");
		}
		
		if(infoHash.toString().length() != 20)
		{
			System.out.println("InfoHash length is not correct!");
		}
		
		this.torrents.add(infoHash);
	}
	
	//TODO
	//add function for tracking peer and torrent
}

@@ -67,92 +67,308 @@
 er;%0A
+%0A
 import 
-java.util.concurrent.locks.ReentrantLock;%0A%0Aimport TorrentMetadata.TorrentFile
+TorrentMetadata.TorrentFile;%0Aimport java.net.InetAddress;%0Aimport java.net.InetSocketAddress;%0Aimport java.net.MalformedURLException;%0Aimport java.net.NetworkInterface;%0Aimport java.net.SocketException;%0Aimport java.util.Enumeration;%0Aimport java.util.logging.Level;%0Aimport java.util.logging.Logger
 ;%0A%0A/
@@ -612,16 +612,18 @@
  * 60;%0A%09
+//
 private 
@@ -802,53 +802,159 @@
 ID;%0A
-%09public String url;%0A%09public InfoHash infoHash
+        public int port = 6961;%0A%09public String announceUrl = %22/announce%22;%0A%09public InfoHash infoHash;%0A        %0A        private InetSocketAddress address
 ;%0A%09%0A
@@ -1012,49 +1012,8 @@
 ID;%0A
-%09%09// infoHash value should be given here%0A
 %09%09th
@@ -1125,24 +1125,26 @@
 ation%0A%09 */%0A%09
+//
 public void 
@@ -1143,35 +1143,39 @@
 ic void lock()%0A%09
+//
 %7B%0A%09
+//
 %09this.trackerLoc
@@ -1181,24 +1181,26 @@
 ck.lock();%0A%09
+//
 %7D%0A%09%0A%09/**%0A%09 *
@@ -1245,32 +1245,34 @@
  possible%0A%09 */%0A%09
+//
 public void unlo
@@ -1277,19 +1277,23 @@
 lock()%0A%09
+//
 %7B%0A%09
+//
 %09this.tr
@@ -1313,16 +1313,18 @@
 ock();%0A%09
+//
 %7D%0A%09/**%0A%09
@@ -1459,16 +1459,18 @@
 h)%0A%09%7B%0A%09%09
+//
 if(this.
@@ -1512,28 +1512,32 @@
  == true)%0A%09%09
+//
 %7B%0A%09%09
+//
 %09System.out.
@@ -1575,24 +1575,26 @@
 t now.%22);%0A%09%09
+//
 %7D%0A%09%09%0A%09%09if(in
@@ -1726,24 +1726,764 @@
 foHash);%0A%09%7D%0A
+        %0A        /**%0A         * Gets an ip address of host%0A         * @return IP address of host%0A         */%0A        public String getAnnounceIP() throws MalformedURLException %0A        %7B%0A            Enumeration e;%0A            try %0A            %7B%0A                e = NetworkInterface.getNetworkInterfaces();%0A                NetworkInterface n = (NetworkInterface) e.nextElement();%0A                Enumeration ee = n.getInetAddresses();%0A                InetAddress i = (InetAddress) ee.nextElement();%0A                return i.getHostAddress();%0A            %7D %0A            catch (SocketException ex) %0A            %7B%0A                Logger.getLogger(Tracker.class.getName()).log(Level.SEVERE, null, ex);%0A            %7D%0A%0A            return null;%0A%09%7D%0A
 %09%0A%09//TODO%0A%09/

Update Tracker.java