Ejemplo n.º 1
0
        // Starts a new file transfer, or adds a new peer if one
        // already exists.
        public IFileTransfer StartTransfer(Network network, Node node, IFile file)
        {
            if (node.NodeID == Core.MyNodeID)
            {
                throw new ArgumentException("You cannot start a file transfer with yourself.");
            }

            // Don't download files if it already exists in the completed downloads directory.
            // If the remote file is different, but has the same filename, it'll globber your copy.
            if (!(file is LocalFile))
            {
                if (IO.File.Exists(IO.Path.Combine(Core.Settings.CompletedDownloadDir, file.Name)))
                {
                    throw new Exception("A file by that name already exists in your download directory.");
                }
            }

            IFileTransfer transfer = null;

            lock (this.transfers)
            {
                transfer = this.transfers.SingleOrDefault(t => t.File == file);
            }

            if (transfer == null)
            {
                transfer = Provider.CreateFileTransfer(file);
                lock (this.transfers)
                {
                    this.transfers.Add(transfer);
                }
                RaiseNewTransfer(transfer);
            }

            transfer.AddPeer(network, node);
            transfer.Start();

            return(transfer);
        }