Esempio n. 1
0
        /// <summary>
        /// Start the next download in line for a specific user
        /// this will search for another queue entry for this user
        /// and if found start it
        /// (will keep the connection open if we download a whole bunch of files from this guy)
        /// </summary>
        /// <param name="username">the user to search for in our download queue entries sources</param>
        private void ContinueWithQueueForUser(string username)
        {
            //check for existing connection in peers for this user

            if (string.IsNullOrEmpty(username))
            {
                return;
            }
            Queue.QueueEntry entry = download_queue.FindFirstUnusedQueueEntryBySourceUser(username);
            if (entry != null)
            {
                StartDownload(entry.FindFirstSourceByUser(username));
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Setup the event handlers for a fresh connected peer
        /// </summary>
        /// <param name="client">an ungrabbed peer</param>
        private void SetupPeerEventHandler(Peer client)
        {
            client.Nick          = nick;
            client.DataReceived += delegate(Peer data_received_client)
            {/*
              *     Queue.QueueEntry entry = download_queue.FindFirstUnusedQueueEntryBySourceUser(data_received_client.PeerNick);
              *     if (entry != null)
              *     {
              *         Queue.QueueEntry.Source source = entry.FindFirstSourceByUser(data_received_client.PeerNick);
              *         if (source != null)
              *         {
              *             entry.IsInUse = true;
              *         }
              *         else
              *         {
              *             Console.WriteLine("no correct source found in queue entry for user: "******"nothing found in queue for user: "******"files.xml.bz2")
                {
                    file_list_request_client.UploadFilename = file_list_request_client.UploadRequestFilename;
                }
                file_list_request_client.UploadFileListData = shares.GetFileListXmlBZ2();
                return(Peer.FileRequestAnswer.LetsGo);
            };

            client.FileRequestReceived += delegate(Peer file_request_client)
            {
                Sharing.SharingEntry entry = shares.GetShareByFileRequest(file_request_client.UploadRequestFilename);
                if (entry != null)
                {
                    file_request_client.UploadFilename = entry.Filename;
                    return(Peer.FileRequestAnswer.LetsGo);
                }
                else
                {
                    return(Peer.FileRequestAnswer.FileNotAvailable);
                }
            };

            client.HandShakeCompleted += delegate(Peer handshake_client)
            {
                if (PeerHandShakeCompleted != null)
                {
                    PeerHandShakeCompleted(handshake_client);
                }
                Queue.QueueEntry entry = download_queue.FindFirstUnusedQueueEntryBySourceUser(handshake_client.PeerNick);
                if (entry != null)
                {
                    Queue.QueueEntry.Source source = entry.FindFirstSourceByUser(handshake_client.PeerNick);
                    if (source != null)
                    {
                        //handshake_client.StartDownload(source.Filename, entry.OutputFilename, entry.Filesize);
                        if (entry.Type == Queue.QueueEntry.EntryType.File && entry.WantTTHL)
                        {
                            handshake_client.GetTTHL(entry);
                        }
                        else if (entry.Type == Queue.QueueEntry.EntryType.File)
                        {
                            handshake_client.StartDownload(source, entry);
                        }
                        else if (entry.Type == Queue.QueueEntry.EntryType.FileList)
                        {
                            handshake_client.GetFileList(entry);
                        }
                    }
                    else
                    {
                        Console.WriteLine("no correct source found in queue entry for user: "******"nothing found in queue for user: " + handshake_client.PeerNick);
                        handshake_client.Disconnect();
                    }
                }
            };


            client.Completed += delegate(Peer completed_client)
            {
                //download_queue.Remove(download_queue.FindQueueEntryByOutputFilename(completed_client.OutputFilename));
                if (auto_remove_downloads)
                {
                    download_queue.Remove(completed_client.QueueEntry);
                }
                ContinueWithQueueForUser(completed_client.PeerNick);
                if (PeerCompleted != null)
                {
                    PeerCompleted(completed_client);
                }
            };

            client.Disconnected += delegate(Peer disconnected_client)
            {
                lock (peers_lock)
                {
                    if (peers.Contains(disconnected_client))
                    {
                        peers.Remove(disconnected_client);
                    }
                }
                //Queue.QueueEntry entry = download_queue.FindQueueEntryByOutputFilename(disconnected_client.OutputFilename);
                //Queue.QueueEntry entry = disconnected_client.QueueEntry;
                //if (entry != null) //TODO this will cause trouble -> fix with disconnect cause change in callback
                //    entry.IsInUse = false;
                //ContinueWithQueueForUser(disconnected_client.PeerNick);//TODO prevent hammering on strange source with a seconds counter
                if (PeerDisconnected != null)
                {
                    PeerDisconnected(disconnected_client);
                }
            };
        }