Esempio n. 1
0
        private static void DownloadTorrent(string[] args)
        {
            if (args.Length < 4)
            {
                Console.Error.WriteLine("You must pass at least 3 arguments to create a torrent: torrent path, download path and listen port");
                return;
            }

            string torrentPath    = args[1].Trim();
            string downloadPath   = args[2].Trim();
            string listenPortText = args[3].Trim();

            torrentPath  = Path.GetFullPath(torrentPath);
            downloadPath = Path.GetFullPath(downloadPath);

            int listenPort;

            if (!int.TryParse(listenPortText, out listenPort) || listenPort < 0)
            {
                Console.Error.WriteLine("The listen port is invalid: {0}", listenPortText);
                return;
            }

            if (!File.Exists(torrentPath))
            {
                Console.Error.WriteLine("The torrent doesn't exist: {0}", torrentPath);
                return;
            }

            Prefs.Peer.ListenPort = listenPort;
            Prefs.Torrent.AllocateFullFileSizes = true;

            var torrentMetaData = new TorrentMetaData();

            torrentMetaData.LoadFromFile(torrentPath);

            var consoleLogger = new ConsoleLogger();

            consoleLogger.Level = LogLevel.Info;
            var fileLogger = new FileLogger("swifter.log");

            fileLogger.Level = LogLevel.Debug;
            var groupLogger = new GroupLogger(consoleLogger, fileLogger);

            Log.Logger = groupLogger;

            TorrentEngine.Initialize();
            try
            {
                var torrent = new Torrent(torrentMetaData, downloadPath);
                torrent.Start();

                while (true)
                {
                    var keyInfo = Console.ReadKey(true);
                    if (keyInfo.Key == ConsoleKey.Escape)
                    {
                        break;
                    }

                    if (keyInfo.Key == ConsoleKey.Enter)
                    {
                        string peerEndPointText = Console.ReadLine();
                        var    peerEndPoint     = ParseEndPoint(peerEndPointText);
                        if (peerEndPoint != null)
                        {
                            var peerInfo = new PeerInfo(peerEndPoint);
                            torrent.AddPeer(peerInfo);
                        }
                        else
                        {
                            Log.LogError("[Console] Invalid end-point: {0}", peerEndPointText);
                        }
                    }
                    else if (keyInfo.Key == ConsoleKey.Spacebar)
                    {
                        string downloadRate = Torrent.GetHumanReadableSpeed(torrent.SessionDownloadRate);
                        string uploadRate   = Torrent.GetHumanReadableSpeed(torrent.SessionUploadRate);
                        string downloadLeft = Torrent.GetHumanReadableSize(torrent.BytesLeftToDownload);

                        Log.LogInfo("[Console] Download Speed: {0}  Upload Speed: {1}  Left : {2}", downloadRate, uploadRate, downloadLeft);
                    }
                }

                torrent.Stop();
            }
            finally
            {
                TorrentEngine.Uninitialize();
            }
        }