private void ListenerReceivedAnnounce(object sender, AnnounceParameters e)
        {
            if (disposed)
            {
                e.Response.Add(RequestParameters.FailureKey, (BEncodedString)"The tracker has been shut down");
                return;
            }

            monitor.AnnounceReceived();
            SimpleTorrentManager manager;

            // Check to see if we're monitoring the requested torrent
            lock (torrents)
            {
                if (!torrents.TryGetValue(e.InfoHash, out manager))
                {
                    if (AllowUnregisteredTorrents)
                    {
                        Add(new InfoHashTrackable(BitConverter.ToString(e.InfoHash.Hash), e.InfoHash));
                        manager = torrents[e.InfoHash];
                    }
                    else
                    {
                        e.Response.Add(RequestParameters.FailureKey, (BEncodedString)"The requested torrent is not registered with this tracker");
                        return;
                    }
                }
            }

            // If a non-compact response is expected and we do not allow non-compact responses
            // bail out
            if (!AllowNonCompact && !e.HasRequestedCompact)
            {
                e.Response.Add(RequestParameters.FailureKey, (BEncodedString)"This tracker does not support non-compact responses");
                return;
            }

            lock (manager)
            {
                // Update the tracker with the peers information. This adds the peer to the tracker,
                // updates it's information or removes it depending on the context
                manager.Update(e);

                // Clear any peers who haven't announced within the allowed timespan and may be inactive
                manager.ClearZombiePeers(DateTime.Now.Add(-TimeoutInterval));

                // Fulfill the announce request
                manager.GetPeers(e.Response, e.NumberWanted, e.HasRequestedCompact);
            }

            e.Response.Add(Tracker.IntervalKey, new BEncodedNumber((int)AnnounceInterval.TotalSeconds));
            e.Response.Add(Tracker.MinIntervalKey, new BEncodedNumber((int)MinAnnounceInterval.TotalSeconds));
            e.Response.Add(Tracker.TrackerIdKey, trackerId); // FIXME: Is this right?
            e.Response.Add(Tracker.CompleteKey, new BEncodedNumber(manager.Complete));
            e.Response.Add(Tracker.Incomplete, new BEncodedNumber(manager.Incomplete));

            //FIXME is this the right behaivour
            //if (par.TrackerId == null)
            //    par.TrackerId = "monotorrent-tracker";
        }
Example #2
0
        public override bool Equals(object obj)
        {
            AnnounceParameters other = obj as AnnounceParameters;

            return(other == null ? false : other.clientAddress.Equals(clientAddress) &&
                   other.Port.Equals(Port));
        }
Example #3
0
        /// <summary>
        /// Updates the peer in the tracker database based on the announce parameters
        /// </summary>
        /// <param name="par"></param>
        internal void Update(AnnounceParameters par)
        {
            Peer   peer;
            object peerKey = comparer.GetKey(par);

            if (!peers.TryGetValue(peerKey, out peer))
            {
                peer = new Peer(par, peerKey);
                Add(peer);
            }
            else
            {
                Debug.WriteLine(string.Format("Updating: {0} with key {1}", peer.ClientAddress, peerKey));
                peer.Update(par);
            }
            if (par.Event == TorrentEvent.Completed)
            {
                System.Threading.Interlocked.Increment(ref downloaded.number);
            }

            else if (par.Event == TorrentEvent.Stopped)
            {
                Remove(peer);
            }

            tracker.RaisePeerAnnounced(new AnnounceEventArgs(peer, this));
            UpdateCounts();
        }
Example #4
0
        internal void Update(AnnounceParameters parameters)
        {
            DateTime now         = DateTime.Now;
            double   elapsedTime = (now - lastAnnounceTime).TotalSeconds;

            if (elapsedTime < 1)
            {
                elapsedTime = 1;
            }

            clientAddress    = parameters.ClientAddress;
            downloadSpeed    = (int)((parameters.Downloaded - downloaded) / elapsedTime);
            uploadSpeed      = (int)((parameters.Uploaded - uploaded) / elapsedTime);
            downloaded       = parameters.Downloaded;
            uploaded         = parameters.Uploaded;
            left             = parameters.Left;
            peerId           = parameters.PeerId;
            lastAnnounceTime = now;
        }
Example #5
0
 public object GetKey(AnnounceParameters parameters)
 {
     return(parameters.ClientAddress);
 }
 public object GetKey(AnnounceParameters parameters)
 {
     return(parameters.Uploaded);
 }
Example #7
0
 internal Peer(AnnounceParameters par, object dictionaryKey)
 {
     this.dictionaryKey = dictionaryKey;
     Update(par);
 }