private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            //get config from app.config
            string port       = ConfigurationManager.AppSettings["port"];
            string username   = ConfigurationManager.AppSettings["username"];
            string serviceUrl = null;

            // set form title
            this.Title = string.Format($"P2P node - {username}");

            //get url-address of service using Ipv4 and port from app config
            foreach (IPAddress address in Dns.GetHostAddresses(Dns.GetHostName()))
            {
                if (address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                {
                    serviceUrl = string.Format($"net.tcp://{address}:{port}/P2PService");
                    break;
                }
            }

            // check if address is not null
            if (serviceUrl == null)
            {
                MessageBox.Show(this, "Can not define service address WCF.", "Networking Error",
                                MessageBoxButton.OK, MessageBoxImage.Stop);
                Application.Current.Shutdown();
            }

            // registration and run WCF
            _localService = new P2PService(this, username);
            _host         = new ServiceHost(_localService, new Uri(serviceUrl ?? throw new InvalidOperationException()));
            NetTcpBinding binding = new NetTcpBinding();

            binding.Security.Mode = SecurityMode.None;
            _host.AddServiceEndpoint(typeof(IP2PService), binding, serviceUrl);
            try
            {
                _host.Open();
            }
            catch (AddressAlreadyInUseException)
            {
                MessageBox.Show(this, "Can not listen, the port is busy", "WCF Error",
                                MessageBoxButton.OK, MessageBoxImage.Stop);
                Application.Current.Shutdown();
            }

            // Create peer name
            peerName = new PeerName("P2P Sample", PeerNameType.Unsecured);

            // get ready to register participant in the local cloud
            _peerNameRegistration = new PeerNameRegistration(peerName, int.Parse(port))
            {
                Cloud = Cloud.AllLinkLocal
            };
            // run registration
            _peerNameRegistration.Start();
        }
Exemple #2
0
        static void Main(string[] args)
        {
            P2PService service = new P2PService();

            service.Start();


            Console.WriteLine("按回车键结束!");
            Console.ReadLine();
        }
Exemple #3
0
        protected void btnFind_Click_1(object sender, System.EventArgs e)
        {
            Cursor = Cursors.WaitCursor;
            Pinger ping = new Pinger();

            if (txtSearch.Text != "")
            {
                lstResults.ListItems.Clear();
                P2PService    p  = new P2PService();
                OptionsLoader ol = new OptionsLoader();
                p.Url = ol.ServiceUrl;
                PeerFile[] FoundFiles = p.FindPeersWithFiles(txtSearch.Text);
                this.LogOnToService();                 // Update the file info
                int    PingTime  = 0;
                string pingEntry = null;
                if (null != FoundFiles)
                {
                    foreach (PeerFile file in FoundFiles)
                    {
                        PingTime = ping.GetPingTime(file.IpAddress);
                        if (PingTime <= 10)
                        {
                            pingEntry = "< 10 ms";
                        }
                        else
                        {
                            pingEntry = Convert.ToString(PingTime) + " ms";
                        }
                        string[] Entry = new string[5];
                        Entry[0] = file.Path;
                        Entry[1] = file.Size;
                        Entry[2] = file.Connection;
                        Entry[3] = file.IpAddress;
                        Entry[4] = pingEntry;
                        lstResults.InsertItem(0, file.Name, 0, Entry);
                    }
                }
                else                 //prepare default entry
                {
                    string[] Entry = new string[5];
                    Entry[0] = "n/a";
                    Entry[1] = "n/a";
                    Entry[2] = "n/a";
                    Entry[3] = "n/a";
                    Entry[4] = "n/a";
                    lstResults.InsertItem(0, "No Results Found", 0, Entry);
                }
            }
            Cursor = Cursors.Default;
        }
Exemple #4
0
        protected void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (ServiceAvailable)
            {
                P2PService    p  = new P2PService();
                OptionsLoader ol = new OptionsLoader();
                p.Url = ol.ServiceUrl;
                p.ClearEntriesForPeer(GlobalIPAddress);
            }

            GlobalThread.Abort();
            GlobalTransfer.TransferShutdown();
            SaveValues();
            EnableListen = false;
        }
Exemple #5
0
        public void LogOnToService()
        {
            this.pnlNumUsers.Text = " Logging on to the ShareBaby service...";
            if (ServiceAvailable)
            {
                OptionsLoader ol = new OptionsLoader();
                P2PService    p  = new P2PService();
                p.Url = ol.ServiceUrl;
                int NumUsers = p.GetNumUsers();
                this.pnlNumUsers.Text = " You are now logged into the ShareBaby service - " + NumUsers + " Users sharing files";
                int NumFiles = p.GetNumFiles();
                this.pnlTotalData.Text = " A total of " + NumFiles + " files are currently being shared on the service";
            }
            else
            {
                this.pnlNumUsers.Text  = " Unable to log in to the ShareBaby service";
                this.pnlTotalData.Text = " No files are being shared on the ShareBaby service at this time";
            }

            return;
        }
Exemple #6
0
        public void PublishFiles()
        {
            OptionsLoader ol         = new OptionsLoader();
            string        Connection = "LAN";

            Connection = ol.Connection;

            // Make sure the user wants these files published and the service is available
            if (ServiceAvailable)
            {
                this.pnlNumUsers.Text = " Publishing content list to the ShareBaby service...";
                string[,] z           = FileLister.GetFileList(this.lstLibrary);
                P2PService p = new P2PService();
                p.Url = ol.ServiceUrl;
                p.ClearEntriesForPeer(GlobalIPAddress);

                for (int i = 0; i <= z.GetUpperBound(1); i++)
                {
                    p.RegisterFile(GlobalIPAddress, z[0, i], Connection, z[1, i], z[2, i]);
                }
            }
        }
Exemple #7
0
        protected void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if(ServiceAvailable)
            {
                P2PService p = new P2PService();
                OptionsLoader ol = new OptionsLoader();
                p.Url = ol.ServiceUrl;
                p.ClearEntriesForPeer(GlobalIPAddress);
            }

            GlobalThread.Abort();
            GlobalTransfer.TransferShutdown();
            SaveValues();
            EnableListen = false;
        }
Exemple #8
0
        protected void btnFind_Click_1(object sender, System.EventArgs e)
        {
            Cursor = Cursors.WaitCursor;
            Pinger ping = new Pinger();

            if( txtSearch.Text != "")
            {
                lstResults.ListItems.Clear();
                P2PService p = new P2PService();
                OptionsLoader ol = new OptionsLoader();
                p.Url = ol.ServiceUrl;
                PeerFile[] FoundFiles = p.FindPeersWithFiles(txtSearch.Text);
                this.LogOnToService(); // Update the file info
                int PingTime = 0;
                string pingEntry = null;
                if (null != FoundFiles)
                {
                    foreach (PeerFile file in FoundFiles)
                    {
                        PingTime = ping.GetPingTime(file.IpAddress);
                        if(PingTime <=10)
                        {
                            pingEntry = "< 10 ms";
                        }
                        else
                        {
                            pingEntry = Convert.ToString(PingTime) + " ms";
                        }
                        string[] Entry = new string[5];
                        Entry[0] = file.Path;
                        Entry[1] = file.Size;
                        Entry[2] = file.Connection;
                        Entry[3] = file.IpAddress;
                        Entry[4] = pingEntry;
                        lstResults.InsertItem(0,file.Name,0,Entry);
                    }
                }
                else //prepare default entry
                {
                    string[] Entry = new string[5];
                    Entry[0] = "n/a";
                    Entry[1] = "n/a";
                    Entry[2] = "n/a";
                    Entry[3] = "n/a";
                    Entry[4] = "n/a";
                    lstResults.InsertItem(0,"No Results Found",0,Entry);
                }
            }
            Cursor = Cursors.Default;
        }
Exemple #9
0
        public void PublishFiles()
        {
            OptionsLoader ol = new OptionsLoader();
            string Connection = "LAN";
            Connection = ol.Connection;

            // Make sure the user wants these files published and the service is available
            if(ServiceAvailable)
            {
                this.pnlNumUsers.Text = " Publishing content list to the ShareBaby service...";
                string[,] z = FileLister.GetFileList(this.lstLibrary);
                P2PService p = new P2PService();
                p.Url = ol.ServiceUrl;
                p.ClearEntriesForPeer(GlobalIPAddress);

                for (int i =0 ; i <= z.GetUpperBound(1) ; i++)
                {
                    p.RegisterFile(GlobalIPAddress, z[0, i], Connection, z[1, i], z[2, i]);
                }
            }
        }
Exemple #10
0
        public void LogOnToService()
        {
            this.pnlNumUsers.Text = " Logging on to the ShareBaby service...";
            if(ServiceAvailable)
            {
                OptionsLoader ol = new OptionsLoader();
                P2PService p = new P2PService();
                p.Url = ol.ServiceUrl;
                int NumUsers = p.GetNumUsers();
                this.pnlNumUsers.Text = " You are now logged into the ShareBaby service - " + NumUsers + " Users sharing files";
                int NumFiles = p.GetNumFiles();
                this.pnlTotalData.Text = " A total of " + NumFiles + " files are currently being shared on the service";
            }
            else
            {
                this.pnlNumUsers.Text = " Unable to log in to the ShareBaby service";
                this.pnlTotalData.Text = " No files are being shared on the ShareBaby service at this time";
            }

            return;
        }
 public HttpServer(P2PService p2PService)
 {
     m_p2PService = p2PService;
 }
Exemple #12
0
 public BlockController(BlockService blockChain, P2PService p2PService)
 {
     _blockChain = blockChain;
     _p2PService = p2PService;
 }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            // Получение конфигурационной информации из app.config
            string port        = ConfigurationManager.AppSettings["port"];
            string username    = ConfigurationManager.AppSettings["username"];
            string machineName = Environment.MachineName;
            string serviceUrl  = null;

            // Установка заголовка окна
            this.Title = string.Format("P2P приложение - {0}", username);

            //  Получение URL-адреса службы с использованием адресаIPv4
            //  и порта из конфигурационного файла
            foreach (IPAddress address in Dns.GetHostAddresses(Dns.GetHostName()))
            {
                if (address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                {
                    serviceUrl = string.Format("net.tcp://{0}:{1}/P2PService", address, port);
                    break;
                }
            }

            // Выполнение проверки, не является ли адрес null
            if (serviceUrl == null)
            {
                // Отображение ошибки и завершение работы приложения
                MessageBox.Show(this, "Не удается определить адрес конечной точки WCF.", "Networking Error",
                                MessageBoxButton.OK, MessageBoxImage.Stop);
                Application.Current.Shutdown();
            }

            // Регистрация и запуск службы WCF
            localService = new P2PService(this, username);
            host         = new ServiceHost(localService, new Uri(serviceUrl));
            NetTcpBinding binding = new NetTcpBinding();

            binding.Security.Mode = SecurityMode.None;
            host.AddServiceEndpoint(typeof(IP2PService), binding, serviceUrl);
            try
            {
                host.Open();
            }
            catch (AddressAlreadyInUseException)
            {
                // Отображение ошибки и завершение работы приложения
                MessageBox.Show(this, "Не удается начать прослушивание, порт занят.", "WCF Error",
                                MessageBoxButton.OK, MessageBoxImage.Stop);
                Application.Current.Shutdown();
            }

            // Создание имени равноправного участника (пира)
            peerName = new PeerName("P2P Sample", PeerNameType.Unsecured);

            // Подготовка процесса регистрации имени равноправного участника в локальном облаке
            if (port != null)
            {
                peerNameRegistration       = new PeerNameRegistration(peerName, int.Parse(port));
                peerNameRegistration.Cloud = Cloud.AllLinkLocal;

                // Запуск процесса регистрации
                peerNameRegistration.Start();
            }
        }
 public PeerController(P2PService p2PService)
 {
     _p2PService = p2PService;
 }