<commit_before>//
// Peer.cs
//
// Authors:
//   <NAME> <EMAIL>
//
// Copyright (C) 2006 <NAME>
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

using System;
using System.Net;
using System.Threading;
using System.Diagnostics;
using System.Collections.Generic;

using MonoTorrent.Common;

namespace MonoTorrent.Tracker
{
    ///<summary>This class holds informations about Peers downloading Files</summary>
    public class Peer
    {
        private Timer timer;
        private AnnounceParameters parameters;
        
        internal Peer(AnnounceParameters par, TimerCallback removePeerCallback)       
        {                        
            long timeout = Tracker.Instance.IntervalAlgorithm.PeerTimeout;
            timer = new Timer(new TimerCallback(removePeerCallback), this, timeout, timeout);
            Debug.WriteLine("new peer: " + par.ip + ":" + par.port);
            Update(par);
        }
        
        ///<summary>The ip Address of the Peer</summary>        
        public string Address
        {
            get {
                return parameters.ip;
            }
        }       
        
        ///<summary>The tcp port the Peer is listening on</summary>
        public ushort Port
        {
            get {
                return parameters.port;
            }
        }       
        
        ///<summary>The Id of the client software</summary>
        public string PeerId
        {
            get { 
                return parameters.peerId;
            }
        }
        
        ///<summary>Calculates a key to identify the Peer. Used as Index in Dictionaries</summary>
        public string Key
        {
            get {
                return Peer.GetKey(parameters);                
            }
        }
        
        ///<summary>Calculates a key to identify the Peer. Used as Index in Dictionaries</summary>
        ///<param name=par>the peer represented by the parameters</param>
        public static string GetKey(AnnounceParameters par)
        {
            
            if (par.key != null) {
                if (!par.key.Equals(string.Empty))
                    return par.key;
            }
            
            return MonoTorrent.Common.ToolBox.GetHex(GenerateCompactPeersEntry(par.ip, par.port));
        }
        
        ///<summary>
        ///calculates the 6 byte ip + port compact compo 
        ///</summary>
        public byte[] CompactPeersEntry
        {
            get {
                return compact_peers_entry;
            }
        }
        private byte[] compact_peers_entry;
        
        ///<summary>returns wheather the Peer has finished downloading.</summary>
        public bool IsCompleted
        {
            get {
                return completed;
            }
            set {
                completed = value;
            }
        }
        private bool completed;
        
        ///<summary>The peer entry in non compact format.</summary> 
        public BEncodedDictionary PeersEntry
        {
            get {
                return peers_entry;
            }
        }
        private BEncodedDictionary peers_entry;
        
        ///<summary>The Peer entry in compact format.</summary>
        public static byte[] GenerateCompactPeersEntry(string address, ushort sport)
        {
            if (address == null)
                throw new ArgumentNullException("address");
            
            if (sport == 0)
                throw new ArgumentException("sport");
                            
            byte[] port = BitConverter.GetBytes(IPAddress.HostToNetworkOrder((ushort)sport));
            byte[] addr = BitConverter.GetBytes((int)IPAddress.Parse(address).Address);
            byte[] entry = new byte[addr.Length + port.Length];
            
            PrintArray(port);
            PrintArray(addr);
            
            Array.Copy(addr, entry, addr.Length);
            Array.Copy(port, 0, entry, addr.Length, port.Length);
            
            PrintArray(entry);
            return entry;
        }
        
        [Conditional("DEBUG")]<commit_msg>Bugfix for the creating compact entries.<commit_after>//
// Peer.cs
//
// Authors:
//   <NAME> <EMAIL>
//
// Copyright (C) 2006 <NAME>
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

using System;
using System.Net;
using System.Threading;
using System.Diagnostics;
using System.Collections.Generic;

using MonoTorrent.Common;

namespace MonoTorrent.Tracker
{
    ///<summary>This class holds informations about Peers downloading Files</summary>
    public class Peer
    {
        private Timer timer;
        private AnnounceParameters parameters;
        
        internal Peer(AnnounceParameters par, TimerCallback removePeerCallback)       
        {                        
            long timeout = Tracker.Instance.IntervalAlgorithm.PeerTimeout;
            timer = new Timer(new TimerCallback(removePeerCallback), this, timeout, timeout);
            Debug.WriteLine("new peer: " + par.ip + ":" + par.port);
            Update(par);
        }
        
        ///<summary>The ip Address of the Peer</summary>        
        public string Address
        {
            get {
                return parameters.ip;
            }
        }       
        
        ///<summary>The tcp port the Peer is listening on</summary>
        public ushort Port
        {
            get {
                return parameters.port;
            }
        }       
        
        ///<summary>The Id of the client software</summary>
        public string PeerId
        {
            get { 
                return parameters.peerId;
            }
        }
        
        ///<summary>Calculates a key to identify the Peer. Used as Index in Dictionaries</summary>
        public string Key
        {
            get {
                return Peer.GetKey(parameters);                
            }
        }
        
        ///<summary>Calculates a key to identify the Peer. Used as Index in Dictionaries</summary>
        ///<param name=par>the peer represented by the parameters</param>
        public static string GetKey(AnnounceParameters par)
        {
            
            if (par.key != null) {
                if (!par.key.Equals(string.Empty))
                    return par.key;
            }
            
            return MonoTorrent.Common.ToolBox.GetHex(GenerateCompactPeersEntry(par.ip, par.port));
        }
        
        ///<summary>
        ///calculates the 6 byte ip + port compact compo 
        ///</summary>
        public byte[] CompactPeersEntry
        {
            get {
                return compact_peers_entry;
            }
        }
        private byte[] compact_peers_entry;
        
        ///<summary>returns wheather the Peer has finished downloading.</summary>
        public bool IsCompleted
        {
            get {
                return completed;
            }
            set {
                completed = value;
            }
        }
        private bool completed;
        
        ///<summary>The peer entry in non compact format.</summary> 
        public BEncodedDictionary PeersEntry
        {
            get {
                return peers_entry;
            }
        }
        private BEncodedDictionary peers_entry;
        
        ///<summary>The Peer entry in compact format.</summary>
        public static byte[] GenerateCompactPeersEntry(string address, ushort sport)
        {
            if (address == null)
                throw new ArgumentNullException("address");
            
            if (sport == 0)
                throw new ArgumentException("sport");
                            
            byte[] port = BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)sport));
            byte[] addr = BitConverter.GetBytes((int)IPAddress.Parse(address).Address);
            byte[] entry = new byte[addr.Length + port.Length];
            Debug.Assert(entry.Length == 6, "This should be 6 bytes");
            
            PrintArray(port);
            PrintArray(addr);
            
            Array.Copy(addr, entry, addr.Length);
            Array.Copy(port, 0, entry, addr.Length, port.Length);
            
            PrintArray(entry);
            return entry;
        }
        
        [Conditional("DEBUG")]
{'fraction_non_alphanumeric': 0.08412836079791847, 'fraction_numerical': 0.0015418714464681506, 'mean_word_length': 1.661708130289818, 'pattern_counts': {'":': 2, '<': 53, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7368234', 'n_tokens_mistral': 2572, 'n_tokens_neox': 2456, 'n_words': 843}