-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathFileTracker.cs
More file actions
51 lines (44 loc) · 1.65 KB
/
Copy pathFileTracker.cs
File metadata and controls
51 lines (44 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// This file is part of TorrentCore.
// https://torrentcore.org
// Copyright (c) Samuel Fisher.
//
// Licensed under the GNU Lesser General Public License, version 3. See the
// LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TorrentCore.Application.BitTorrent;
using TorrentCore.Tracker;
namespace CustomTransportProtocol
{
/// <summary>
/// Represents a tracker where peers announce by writing a file into a specified directory.
/// </summary>
class FileTracker : ITracker
{
private readonly string _baseDir;
private readonly PeerId _peerId;
public FileTracker(string baseDir, PeerId peerId)
{
_baseDir = baseDir;
_peerId = peerId;
}
public string Type => "File";
public Task<AnnounceResult> Announce(AnnounceRequest request)
{
File.WriteAllText(Path.Combine(_baseDir, "announce", $"{_peerId}.txt"), _peerId.ToString());
var peers = Directory.GetFiles(Path.Combine(_baseDir, "announce")).Where(x => Path.GetFileName(x) != $"{_peerId}.txt")
.Select(x =>
{
var peerId = File.ReadAllText(x);
return new FileTransportStream(
new DirectoryInfo(Path.Combine(_baseDir, _peerId.ToString(), peerId)),
new DirectoryInfo(Path.Combine(_baseDir, peerId, _peerId.ToString())));
}).ToList();
return Task.FromResult(new AnnounceResult(peers));
}
}
}