package Tracker;

import java.net.Socket;

/**
 * 
 * @author Pawel
 *
 */

/**
 Every user downloading a file should be identified as a peer,
 which will help tracking his use of torrent files.
 */

public class Peer 
{
	public static final String state[] =
		{
		    "Unknown", "Started", "Stopped", "Completed"
		};
	public int port, fileID;
	private String id, ip, peerState;
	public Socket peerSocket = null;
	public long downloaded, left;
	
	// Constructor
	
	public Peer(String id, String ip, int port, int fileID, Socket peerSocket)
	{
		this.ip = ip;
		this.id = id;
		this.port = port;
		this.fileID = fileID;
		this.peerSocket = peerSocket;
		this.peerState = "Unknown";
		this.downloaded = 0;
		this.left = 0;
	}
	
	/**
	 * Updates a state of tracked peer
	 * @param downloaded how much of a file this peer has downloaded
	 * @param left how much of a file is left to complete download
	 */
	public void update(long downloaded, long left)
	{
		if(left == 0)
			this.peerState = "Completed";
	}
	
	/**
	 * Return peer's id
	 * @return id
	 */
	public String getPeerId()
	{
		return this.id;
	}
	
	/**
	 * Return peer's ip
	 * @return ip address
	 */
	public String getPeerIp()
	{
		return this.ip;
	}
	
	//TODO
	//updating peer's status, updating peer's information about download
}

@@ -34,16 +34,79 @@
 .Socket;
+%0Aimport java.util.Date;%0A%0Aimport BEncoding.BElement.BDictionary;
 %0A%0A/**%0A *
@@ -501,16 +501,85 @@
 , left;%0A
+        private Date lastAnnounce;%0A        private InfoHash torrent;%0A
 %09%0A%09// Co
@@ -664,16 +664,35 @@
 erSocket
+, InfoHash infoHash
 )%0A%09%7B%0A%09%09t
@@ -866,16 +866,99 @@
 ft = 0;%0A
+                this.lastAnnounce = null;%0A                this.torrent = infoHash;%0A
 %09%7D%0A%09%0A%09/*
@@ -1125,16 +1125,77 @@
 ownload%0A
+         * @param state represents the state of tracked peer%0A
 %09 */%0A%09pu
@@ -1237,16 +1237,30 @@
 ong left
+, String state
 )%0A%09%7B%0A%09%09i
@@ -1276,328 +1276,903 @@
  0)%0A
-%09%09%09this.peerState = %22Completed%22;%0A%09%7D%0A%09%0A%09/**%0A%09 * Return peer's id%0A%09 * @return id%0A%09 */%0A%09public String getPeerId()%0A%09%7B%0A%09%09return this.id;%0A%09%7D%0A%09%0A%09/**%0A%09 * Return peer's ip%0A%09 * @return ip address%0A%09 */%0A%09public String getPeerIp()%0A%09%7B%0A%09%09return this.ip;%0A%09%7D%0A%09%0A%09//TODO%0A%09//updating peer's status, updating peer's information about download
+                %7B%0A                    this.peerState = %22Completed%22;%0A                %7D%0A                else%0A                %7B%0A                    this.lastAnnounce = new Date();%0A                    this.peerState = state;%0A                %7D%0A%09%7D%0A%09%0A%09/**%0A%09 * Return peer's id%0A%09 * @return id%0A%09 */%0A%09public String getPeerId()%0A%09%7B%0A%09%09return this.id;%0A%09%7D%0A%09%0A%09/**%0A%09 * Return peer's ip%0A%09 * @return ip address%0A%09 */%0A%09public String getPeerIp()%0A%09%7B%0A%09%09return this.ip;%0A%09%7D%0A        %0A        public Integer getPeerPort()%0A%09%7B%0A%09%09return this.port;%0A%09%7D%0A        %0A        public InfoHash getInfoHash()%0A%09%7B%0A%09%09return this.torrent;%0A%09%7D%0A        %0A        public BDictionary toDictionary()%0A        %7B%0A            BDictionary peer = new BDictionary();%0A            peer.Add(%22ip%22, this.getPeerIp());%0A            peer.Add(%22port%22, this.getPeerPort());%0A            peer.Add(%22id%22, this.getPeerId());%0A            %0A            return peer;%0A        %7D
 %0A%7D%0A

Update Peer.java