package Tracker;

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

/**
 * 
 * @author Pawel
 *
 */

/**
 Tracker will check torrent files usage and make statistics from it,
 such as how many peers are downloading file, how much of it is being downloaded and so on.
 */

public class Tracker
{
	// timer is needed for calculating operations times
	Timer timer;
	Time time;
	// list of possible tracker states
	public static final String EVENT[] =
		{
		    "Started", "Stopped", "Completed", "Update"
		};
	// how long tracker should wait for response
	public int interval = 5 * 60;
	// this lock will prevent from unnecessary changes of tracker status
	private ReentrantLock trackerLock = new ReentrantLock();
	// list of peers
	ArrayList<Peer> peers;
	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 = time.getMinutes() * time.getSeconds();
		this.peers = new ArrayList<Peer>(fileID);
	}
	
	// managing tracker's lock
	
	public void lock()
	{
		this.trackerLock.lock();
	}
	
	public void unlock()
	{
		this.trackerLock.unlock();
	}
}

@@ -15,30 +15,8 @@
 r;%0A%0A
-import java.sql.Time;%0A
 impo
@@ -117,348 +117,161 @@
 k;%0A%0A
-/**%0A * %0A * @author Pawel%0A *%0A */%0A%0A/**%0A Tracker will check torrent files usage and make statistics from it,%0A such as how many peers are downloading file, how much of it is being downloaded and so on.%0A */%0A%0Apublic class Tracker%0A%7B%0A%09// timer is needed for calculating operations times%0A%09Timer timer;%0A%09Time time;%0A%09// list of possible tracker states
+import TorrentMetadata.TorrentFile;%0A%0A/**%0A * Class used for tracking torrent files and peers%0A * @author Pawel%0A *%0A */%0A%0Apublic class Tracker%0A%7B%0A%09Timer timer;
 %0A%09pu
@@ -299,13 +299,13 @@
 ing 
-EVENT
+state
 %5B%5D =
@@ -369,153 +369,37 @@
 %7D;%0A%09
-// how long tracker should wait for response%0A%09public int interval = 5 * 60;%0A%09// this lock will prevent from unnecessary changes of tracker status
+public int interval = 5 * 60;
 %0A%09pr
@@ -458,26 +458,48 @@
 );%0A%09
-// list of
+private ArrayList%3CPeer%3E
  peers
+;
 %0A%09
+private 
 Arra
@@ -500,35 +500,70 @@
 e ArrayList%3C
-Peer%3E peers
+InfoHash%3E torrents = new ArrayList%3CInfoHash%3E()
 ;%0A%09public in
@@ -745,45 +745,9 @@
 D = 
-time.getMinutes() * time.getSeconds()
+0
 ;%0A%09%09
@@ -799,35 +799,173 @@
 %09%0A%09/
-/ managing tracker's lock%0A%09
+**%0A%09 * Locks tracker to prevent modification%0A%09 */%0A%09public void lock()%0A%09%7B%0A%09%09this.trackerLock.lock();%0A%09%7D%0A%09%0A%09/**%0A%09 * Unlocks tracker to make modifications possible%0A%09 */
 %0A%09pu
@@ -966,32 +966,34 @@
 */%0A%09public void 
+un
 lock()%0A%09%7B%0A%09%09this
@@ -997,32 +997,34 @@
 his.trackerLock.
+un
 lock();%0A%09%7D%0A%09%0A%09pu
@@ -1023,36 +1023,152 @@
 %09%7D%0A%09
-%0A%09public void unlock(
+/**%0A%09 * Adds new torrent to track%0A%09 * @param infoHash encoded in SHA1 hash of torrent metafile%0A%09 */%0A%09public void add(InfoHash infoHash
 )%0A%09%7B%0A%09%09
+if(
 this
@@ -1180,23 +1180,309 @@
 kerLock.
-unlock();%0A%09%7D
+isHeldByCurrentThread() == true)%0A%09%09%7B%0A%09%09%09System.out.println(%22Tracker is locked right now.%22);%0A%09%09%7D%0A%09%09%0A%09%09if(infoHash.toString().length() != 20)%0A%09%09%7B%0A%09%09%09System.out.println(%22InfoHash length is not correct!%22);%0A%09%09%7D%0A%09%09%0A%09%09this.torrents.add(infoHash);%0A%09%7D%0A%09%0A%09//TODO%0A%09//add function for tracking peer and torrent
 %0A%7D%0A

Update Tracker.java