Exemplo n.º 1
0
        public void Start()
        {
            var converter = new JsonMessageConverter();
            var comm      = new UdpCommunicator(new IPEndPoint(IPAddress.Any, 4000), converter);

            comm.Start();
            var router = new MessageRouter(comm);

            var content = new Content()
            {
                FileName    = "test_file.txt",
                ByteSize    = 1024,
                ContentHash = new byte[] { 0xDE, 0xAD, 0xBE, 0xEF },
                Description = "Test file",
                LocalPath   = "."
            };

            var chunks = new HashSet <int>(new List <int>()
            {
                0, 1, 2, 3
            });

            var worker = new ChunkReceiver(content, chunks, new Guid("11f684d9-53a5-4fd9-948f-5526e881f60c"));

            router.AddConversation(worker, new IPEndPoint(IPAddress.Loopback, 4001));

            Console.ReadLine();
            comm.Stop();
        }
Exemplo n.º 2
0
        private void downloadButton_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(saveTextBox.Text))
            {
                if (Directory.Exists(saveTextBox.Text))
                {
                    if (searchDataGridView.SelectedRows.Count > 0)
                    {
                        List <Content> selectedContent = new List <Content>();
                        foreach (DataGridViewRow row in searchDataGridView.SelectedRows)
                        {
                            selectedContent.Add(row.DataBoundItem as Content);
                        }
                        foreach (Content file in selectedContent)
                        {
                            // Set save path
                            file.LocalPath = saveTextBox.Text;

                            // Read local content and search for the selected hash
                            var contentList = persistenceManager.ReadContent();

                            var listToSeed = contentList.RemoteContent.Where(content => content.ContentHash.SequenceEqual(file.ContentHash)).Take(5);

                            int numTotalChunks = (int)Math.Ceiling(file.ByteSize / 256.0);
                            int numPerSeeder   = numTotalChunks / listToSeed.Count();
                            int leftover       = numTotalChunks % listToSeed.Count();

                            var dict = new ConcurrentDictionary <int, byte[]>();

                            var assembler = new FileAssembler(file, dict);
                            Assemblers.Add(assembler);
                            assembler.Start();

                            Log.Info(string.Format("Looking for {0} chunks from {1} seeders", numTotalChunks, listToSeed.Count()));
                            for (var i = 0; i < listToSeed.Count(); i++)
                            {
                                var c = listToSeed.ElementAt(i);

                                var set      = new HashSet <int>();
                                var endIndex = (i + 1) * numPerSeeder;
                                if (i == listToSeed.Count())
                                {
                                    endIndex += leftover;
                                }
                                for (var j = i * numPerSeeder; j < endIndex; j++)
                                {
                                    set.Add(j);
                                }

                                var seederConvo = new ChunkReceiver(c, set, dict, Guid.NewGuid());
                                router.AddConversation(seederConvo, new IPEndPoint(IPAddress.Parse(c.Host), c.Port));
                            }
                        }
                    }
                    else
                    {
                        Console.WriteLine("Failed");
                    }
                }
                else
                {
                    MessageBox.Show("The save location provided does not exist", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                MessageBox.Show("You must specify a save location", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }