Esempio n. 1
0
        public List<Peer> ResolveByPeerName(string peerName)
        {
            try
            {
                if (string.IsNullOrEmpty(peerName))
                    throw new ArgumentException("Cannot have a null or empty peer name.");

                PeerNameResolver resolver = new PeerNameResolver();
                PeerNameRecordCollection resolvedName = resolver.Resolve(new PeerName(peerName, PeerNameType.Unsecured),
                    Cloud.AllLinkLocal);
                
                List<Peer> foundPeers = new List<Peer>();
                foreach (PeerNameRecord foundItem in resolvedName)
                {
                    Peer peer = new Peer(){
                        PeerName = foundItem.PeerName.Classifier,
                        PeerHostName = foundItem.PeerName.PeerHostName,
                        Comments = foundItem.Comment
                    };

                    foundPeers.Add(peer);
                }

                return foundPeers;
            }
            catch (PeerToPeerException px)
            {
                throw new Exception(px.InnerException.Message);
            }
        }     
Esempio n. 2
0
        public void AddPeer(Peer peer)
        {
            string temp = String.Format("INSERT INTO peers (peerId, peerName, peerHostName, comments) values ('{0}', '{1}', '{2}', '{3}')",
                peer.PeerID, peer.PeerName, peer.PeerHostName, peer.Comments);
            dbConnection.Update(temp);

            // log the event
            logging.LogI(String.Format("Info: *** A new peer '{0}' is connected ***" +
                Environment.NewLine, peer.PeerID.ToString()));
            logging.AddPeer(peer.PeerID.ToString());
        }
Esempio n. 3
0
        public void DoHost(Peer peer)
        {
            PeerServiceHost.peer = peer;

            string address = string.Empty;
            address = string.Format("net.tcp://{0}:{1}/PeerToPeerClient.Services/PeerService",
                peer.PeerHostName, Config.LocalPort);

            Uri uri = new Uri(address);
            NetTcpBinding tcpBinding = new NetTcpBinding(SecurityMode.None);

            ServiceHost serviceHost = new ServiceHost(typeof(PeerService), uri);
            serviceHost.AddServiceEndpoint(typeof(IPeerService), tcpBinding, "");

            serviceHost.Open();

            // add this peer to the server
            SuperPeerServiceClient ssc = new SuperPeerServiceClient();
            ssc.AddPeer(peer);

            // add the files that this peer wants to share to the server
            DirectoryInfo dirInfo = new DirectoryInfo(Config.SharedFolder);

            if (dirInfo.Exists)
            {
                FileInfo[] filesInfo = dirInfo.GetFiles();
                List<ClassLibrary.Entites.File> files = new List<ClassLibrary.Entites.File>();

                foreach (var fileInfo in filesInfo)
                {
                    ClassLibrary.Entites.File file = new ClassLibrary.Entites.File();
                    file.FileName = fileInfo.Name;
                    file.FileType = fileInfo.Extension;
                    file.FileSize = fileInfo.Length;

                    SHA512 sha512 = SHA512.Create(); 
                    using (var stream = fileInfo.Open(FileMode.Open, FileAccess.Read))
                    {
                        file.FileHash = BitConverter.ToString(sha512.ComputeHash(stream), 0);
                    }

                    file.PeerName = peer.PeerName;
                    file.PeerID = peer.PeerID;

                    files.Add(file);
                }

                if (files.Count != 0)
                    ssc.AddFiles(files);
            }

            else 
                dirInfo.Create();
        }
Esempio n. 4
0
        public void RemovePeer(Peer peer) 
        {
            string temp = String.Format("DELETE FROM peers WHERE peerId='{0}'", peer.PeerID);
            dbConnection.Update(temp);

            // log the event
            logging.LogI(String.Format("Info: *** A peer '{0}' is disconnected ***" +
                Environment.NewLine, peer.PeerID));
            logging.RemovePeer(peer.PeerID.ToString());

            temp = String.Format("DELETE FROM files WHERE peerId='{0}'", peer.PeerID.ToString());
            int affectedRows = dbConnection.Update(temp);
            
            // log the event
            logging.LogI(String.Format("Info: *** {0} files are removed due to peer '{1}' disconnection ***" +
                Environment.NewLine, affectedRows, peer.PeerID));
        }
Esempio n. 5
0
        public Peer Register() 
        {
            string timeStamp = string.Format("PeerToPeerClient Created at : {0}", DateTime.Now.ToShortTimeString());
            registration.Comment = timeStamp;

            try
            {
                registration.Start();
                Peer peer = new Peer()
                {
                    PeerID = Guid.NewGuid(),
                    PeerName = registration.PeerName.Classifier,
                    PeerHostName = registration.PeerName.PeerHostName,
                    Comments = registration.Comment
                };

                return peer;
            }
            catch (PeerToPeerException PEX)
            {
                throw new Exception(PEX.InnerException.Message);
            }
        }
Esempio n. 6
0
 public void Leave(Peer peer)
 {
     registration.Stop();
     SuperPeerServiceClient ssc = new SuperPeerServiceClient();
     ssc.RemovePeer(peer);
 }