/// <summary> /// Start downloading a queue entry /// this will send connection requests to every source of /// the entry /// </summary> /// <param name="me">the queue entry to start downloading to</param> public void StartDownload(Queue.QueueEntry me) { //TODO change this back to thread safe enumerator in the queue class if (me == null) { return; } me.StartDownload(); /* lock (me.SourcesLock) * {//TODO put this in queue class * foreach (Queue.QueueEntry.Source source in me.Sources) * { * if (source.HasHub && source.IsOnline && !CheckForUserInPeers(source.UserName)) * { * source.Hub.SendConnectToMe(source.UserName); //signal download to hub to start it * } * } * } * //sources strategy * //for selection and skipping if source offline * //source offline detection * * //result.Hub.SendCommand("ConnectToMe", result.UserName); //signal download to hub to start it * //Console.WriteLine("Hub was not resolved from result hub address: " + result.HubAddress); */ }
/// <summary> /// Interpret a received search result /// (active and passive results can be handled with this method) /// this will automatically add sources to already existing download /// queue entries, these will not be shown in the search results /// TODO make this an option /// </summary> /// <param name="result">the result to be interpreted</param> private void InterpretReceivedSearchResult(SearchResults.SearchResult result) { //Console.WriteLine("Adding Result to SearchResults"); result.Hub = ResolveHub(result.HubAddress); if (result.Hub != null) //only add results for hubs still connected { if (result.HasTTH) {//only if a result has a tth it is considered a source for some queue entry Queue.QueueEntry entry = download_queue.FindQueueEntryByTTH(result.TTH); if (entry != null) {//this searchresult is also a source for a queue entry //,instead using of giving it back as result we add it to the source pool of the queue entry entry.AddSource(new Queue.QueueEntry.Source(result.UserName, result.Filename, result.Hub)); } //no queue entry found for this one just hand it over to SearchResults else { search_results.AddResult(result); } } else { search_results.AddResult(result); } } /* * //Console.WriteLine("Adding Result to SearchResults"); * result.Hub = ResolveHub(result.HubAddress); * search_results.AddResult(result); */ }
/// <summary> /// Find more sources for a queue entry /// this will send a tth search to all connected hubs /// </summary> /// <param name="me">the entry for which to search alternates for</param> public void FindAlternateSources(Queue.QueueEntry me) { //search all hubs for tth string if (me != null && me.HasTTH) { Search(me.TTH, true); } }
/// <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)); } }
/// <summary> /// Stop downloading a queue entry /// this will look for a peer using this /// entry and disconnect it /// (TODO add a PauseDownload, because this doesnt stop new /// connections from downloading data for this entry again) /// </summary> /// <param name="me">the queue entry to be stopped</param> public void StopDownload(Queue.QueueEntry me) { lock (peers_lock) { foreach (Peer peer in peers) { if (peer.QueueEntry == me && peer.IsDownloading) { peers.Remove(peer); peer.Disconnect(); break; } } } }
/// <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); } }; }
/// <summary> /// TODO Work in progress /// </summary> /// <param name="me"></param> public void GetTTHL(Queue.QueueEntry me) { //if (me == null) return; me.WantTTHL = true; me.StartDownload(); }