public RepositorySession Start() { Metainfo metainfo; RepositoryData data = new RepositoryData(20000); using (FileSandbox temp = new FileSandbox(new EmptyFileLocator())) { MetainfoBuilder builder = new MetainfoBuilder(temp.Directory); string path = temp.CreateFile("debian-8.5.0-amd64-CD-1.iso"); File.WriteAllBytes(path, data.ToBytes()); builder.AddFile(path); metainfo = builder.ToMetainfo(); } FileSandbox sandbox = new FileSandbox(new EmptyFileLocator()); string destination = Path.Combine(sandbox.Directory, metainfo.Hash.ToString()); RepositoryService service = new RepositoryBuilder() .WithHash(metainfo.Hash) .WithDestination(destination) .WithFiles(files) .WithPipeline(pipeline) .WithMemory(new RepositoryMemoryImplementation()) .Build(); return(new RepositorySession(metainfo, service, sandbox, data)); }
public OmnibusSession Start() { Metainfo metainfo; byte[] data = Bytes.Random(20000); using (FileSandbox temp = new FileSandbox(new EmptyFileLocator())) { MetainfoBuilder builder = new MetainfoBuilder(temp.Directory); string path = temp.CreateFile("debian-8.5.0-amd64-CD-1.iso"); File.WriteAllBytes(path, data); builder.AddFile(path); metainfo = builder.ToMetainfo(); } OmnibusService service = new OmnibusBuilder() .WithHash(metainfo.Hash) .WithPipeline(new PipelineSimulator()) .Build(); return(new OmnibusSession(metainfo, service)); }
public MetashareSession Start() { Metainfo metainfo; byte[] bytes = Bytes.Random(20000); using (FileSandbox temp = new FileSandbox(new EmptyFileLocator())) { MetainfoBuilder builder = new MetainfoBuilder(temp.Directory); string path = temp.CreateFile("debian-8.5.0-amd64-CD-1.iso"); File.WriteAllBytes(path, bytes); builder.AddFile(path); metainfo = builder.ToMetainfo(out bytes); } FileSandbox sandbox = new FileSandbox(new EmptyFileLocator()); string destination = Path.Combine(sandbox.Directory, metainfo.Hash.ToString()); File.WriteAllBytes(destination + ".metainfo", bytes); CoordinatorService glue = new CoordinatorBuilder() .WithHash(metainfo.Hash) .WithMemory(new MemoryBuilder().Build()) .WithPlugin(new MetadataPlugin(new MetadataHooks())) .Build(); MetafileService metafile = new MetafileBuilder() .WithHash(metainfo.Hash) .WithDestination(destination + ".metainfo") .WithFiles(files) .WithPipeline(pipeline) .Build(); MetashareService service = new MetashareBuilder() .WithHash(metainfo.Hash) .WithPipeline(pipeline) .WithGlue(glue) .WithMetafile(metafile) .Build(); TaskCompletionSource <bool> onVerified = new TaskCompletionSource <bool>(); metafile.Hooks.OnMetafileVerified += _ => onVerified.SetResult(true); metafile.Start(); metafile.Verify(); onVerified.Task.Wait(5000); return(new MetashareSession(service)); }
static async Task Main(string[] args) { var transportDir = Path.Combine(Environment.CurrentDirectory, "transport"); var seederDir = Path.Combine(Environment.CurrentDirectory, "seeder"); var downloaderDir = Path.Combine(Environment.CurrentDirectory, "downloader"); // Clear old data if (Directory.Exists(transportDir)) { Directory.Delete(transportDir, true); } if (Directory.Exists(seederDir)) { Directory.Delete(seederDir, true); } if (Directory.Exists(downloaderDir)) { Directory.Delete(downloaderDir, true); } // Create directories Directory.CreateDirectory(transportDir); Directory.CreateDirectory(Path.Combine(transportDir, "announce")); Directory.CreateDirectory(seederDir); Directory.CreateDirectory(downloaderDir); File.Copy(Path.Combine(Environment.CurrentDirectory, "TorrentContent.txt"), Path.Combine(seederDir, "TorrentContent.txt"), true); // Share a main loop between both peers to make this easier to follow as an example var mainLoop = new MainLoop(); var metainfo = new MetainfoBuilder("CustomTransportProtocol") .AddFile("TorrentContent.txt", File.ReadAllBytes("TorrentContent.txt")) .WithTracker(new Uri("http://example.com/announce")) // Tracker URI is not used in this example .Build(); var cancelSeeder = new CancellationTokenSource(); var seeder = RunSeederAsync(mainLoop, transportDir, seederDir, metainfo, cancelSeeder.Token); // Run downloader and awit until finished downloading await RunDownloaderAsync(mainLoop, transportDir, downloaderDir, metainfo); // Stop seeding cancelSeeder.Cancel(); mainLoop.Stop(); Console.WriteLine("Finished..."); Console.ReadKey(); }
public async Task Test() { // Create a 256kB file var fileData = Enumerable.Repeat(Enumerable.Range(0, 255).Select(x => (byte)x), 1000) .SelectMany(x => x).ToArray(); var metainfo = new MetainfoBuilder("Test") .AddFile("file.dat", fileData) .Build(); var tracker = new MockTracker(); var sourceFiles = new MemoryFileHandler("file.dat", fileData); var seed = new TorrentClient(0); var seedDownload = seed.Add(metainfo, tracker.CreateTrackerClient(null), sourceFiles); tracker.RegisterPeer(seed.Transport.Port); var peer = new TorrentClient(0); var peerFileHandler = new MemoryFileHandler(); var peerDownload = peer.Add(metainfo, tracker.CreateTrackerClient(null), peerFileHandler); seedDownload.Start(); peerDownload.Start(); try { await peerDownload.WaitForDownloadCompletionAsync(); // Verify downloaded data var resultFile = peerFileHandler.GetFileStream("file.dat") as MemoryStream; Assert.That(resultFile, Is.Not.Null, "Result file does not exist."); Assert.That(resultFile.ToArray().SequenceEqual(fileData), "Downloaded data is not correct."); } finally { seed.Dispose(); peer.Dispose(); } }
public MetafileSession Start(bool completed = false) { Metainfo metainfo = null; byte[] data = null; using (IFileSandbox temp = new FileSandbox(new EmptyFileLocator())) { MetainfoBuilder builder = new MetainfoBuilder(temp.Directory); string path = temp.CreateFile("debian-8.5.0-amd64-CD-1.iso"); File.WriteAllBytes(path, Bytes.Random(20000)); builder.AddFile(path); metainfo = builder.ToMetainfo(out data); } IFileSandbox sandbox = new FileSandbox(new EmptyFileLocator()); string destination = Path.Combine(sandbox.Directory, metainfo.Hash.ToString()); if (completed) { File.WriteAllBytes(destination, data); } MetafileService service = new MetafileBuilder() .WithHash(metainfo.Hash) .WithDestination(destination) .WithFiles(files) .WithPipeline(pipeline) .Build(); TaskCompletionSource <bool> onVerified = new TaskCompletionSource <bool>(); service.Hooks.OnMetafileVerified += _ => onVerified.SetResult(true); return(new MetafileSession(sandbox, metainfo, destination, data, service, onVerified.Task)); }
public MetagetSession Start(bool completed = false) { Metainfo metainfo; byte[] bytes = Bytes.Random(20000); using (FileSandbox temp = new FileSandbox(new EmptyFileLocator())) { MetainfoBuilder builder = new MetainfoBuilder(temp.Directory); string path = temp.CreateFile("debian-8.5.0-amd64-CD-1.iso"); File.WriteAllBytes(path, bytes); builder.AddFile(path); metainfo = builder.ToMetainfo(out bytes); } FileSandbox sandbox = new FileSandbox(new EmptyFileLocator()); string destination = Path.Combine(sandbox.Directory, metainfo.Hash.ToString()); MetagetData data = new MetagetData(bytes); if (completed) { File.WriteAllBytes(destination + ".metainfo", data.ToBytes()); } MetagetService metaget = new MetagetBuilder() .WithHash(metainfo.Hash) .WithPipeline(new PipelineSimulator()) .WithGlue(A.Fake <MetagetGlue>()) .WithMetafile(A.Fake <MetagetMetafile>()) .Build(); return(new MetagetSession(sandbox, metainfo.Hash, data, metaget)); }