Skip to content

Commit

Permalink
Fix up the tracker sample a little
Browse files Browse the repository at this point in the history
Addresses #605

However, it addresses the problem by listening on localhost. Ain't
nobody got time to figure out how to run as admin and unblock incoming
http requests on a regular old computer.
  • Loading branch information
alanmcgovern committed Jul 28, 2024
1 parent 9bf3224 commit f5ab102
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 19 deletions.
36 changes: 22 additions & 14 deletions src/Samples/TrackerApp/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,9 @@ public MySimpleTracker ()
tracker = new TrackerServer ();
tracker.AllowUnregisteredTorrents = true;

var httpEndpoint = new System.Net.IPEndPoint (System.Net.IPAddress.Any, 10000);
var udpEndpoint = new System.Net.IPEndPoint (System.Net.IPAddress.Any, 10001);
// Listen on a private address (localhost) in the sample.
var httpEndpoint = new System.Net.IPEndPoint (System.Net.IPAddress.Loopback, 10000);
var udpEndpoint = new System.Net.IPEndPoint (System.Net.IPAddress.Loopback, 10001);
Console.WriteLine ("Listening for HTTP requests at: {0}", httpEndpoint);
Console.WriteLine ("Listening for UDP requests at: {0}", udpEndpoint);

Expand Down Expand Up @@ -174,18 +175,22 @@ public static void Main (string[] args)
private static void Benchmark ()
{
Console.Clear ();
Console.Write ("How many active torrents will be simulated: ");
int torrents = GetInt ();
Console.Write ("How many active peers per torrent: ");
int peers = GetInt ();
Console.Write ("How many requests per second: ");
int requests = GetInt ();

Console.Write ("What is the tracker address: ");
Console.Write ("How many active torrents will be simulated (default 10): ");
int torrents = GetInt (10);
Console.Write ("How many active peers per torrent (default 1000): ");
int peers = GetInt (1000);
Console.Write ("How many requests per second (default 100): ");
int requests = GetInt (100);

const string defaultAddress = "http://127.0.0.1:35277/announce/";
Console.WriteLine ("Read https://stackoverflow.com/questions/2583347/c-sharp-httplistener-without-using-netsh-to-register-a-uri to see how to listen on public IP addresses");
Console.Write ($"What is the tracker address (default {defaultAddress}): ");
string address = Console.ReadLine ();
if (string.IsNullOrEmpty (address))
address = defaultAddress;

StressTest test = new StressTest (torrents, peers, requests);
test.Start (address);
StressTest test = new StressTest (torrents, peers, requests, address);
test.Start ();

while (true) {
Console.WriteLine ("Measured announces/sec: {0}", test.RequestRate);
Expand All @@ -200,10 +205,13 @@ private static void StartTracker ()
new MySimpleTracker ();
}

private static int GetInt ()
private static int GetInt (int? defaultValue = null)
{
while (true) {
if (int.TryParse (Console.ReadLine (), out int ret))
var data = Console.ReadLine ();
if (string.IsNullOrEmpty (data) && defaultValue.HasValue)
return defaultValue.Value;
else if (int.TryParse (data, out int ret))
return ret;
}
}
Expand Down
27 changes: 22 additions & 5 deletions src/Samples/TrackerApp/StressTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
using System.Threading.Tasks;

using MonoTorrent;
using MonoTorrent.Connections.TrackerServer;
using MonoTorrent.Trackers;
using MonoTorrent.TrackerServer;

namespace TrackerApp
{
Expand All @@ -17,6 +20,11 @@ public class StressTest
readonly Thread[] threads;
private readonly int threadSleepTime;

readonly HttpTrackerListener trackerListener;
readonly string trackerAddress;
readonly TrackerServer trackerServer;


public int RequestRate {
get { return (int) requests.Rate; }
}
Expand All @@ -25,8 +33,10 @@ public long TotalTrackerRequests {
get { return requests.Total; }
}

public StressTest (int torrents, int peers, int requests)
public StressTest (int torrents, int peers, int requests, string trackerAddress)
{
this.trackerAddress = trackerAddress;

for (int i = 0; i < torrents; i++) {
byte[] infoHash = new byte[20];
random.NextBytes (infoHash);
Expand All @@ -35,9 +45,18 @@ public StressTest (int torrents, int peers, int requests)

threadSleepTime = Math.Max ((int) (20000.0 / requests + 0.5), 1);
threads = new Thread[20];

trackerListener = new HttpTrackerListener (trackerAddress);
trackerListener.Start ();

trackerServer = new TrackerServer {
AllowUnregisteredTorrents = true,
AllowScrape = true
};
trackerServer.RegisterListener (trackerListener);
}

public void Start (string trackerAddress)
public void Start ()
{
var client = new HttpClient ();
for (int i = 0; i < threads.Length; i++) {
Expand All @@ -47,11 +66,9 @@ public void Start (string trackerAddress)
while (true) {
sb.Remove (0, sb.Length);

int ipaddress = random.Next (0, hashes.Count);

sb.Append (trackerAddress);
sb.Append ("?info_hash=");
sb.Append (hashes[torrent++]);
sb.Append (hashes[(torrent++) % hashes.Count]);
sb.Append ("&peer_id=");
sb.Append ("12345123451234512345");
sb.Append ("&port=");
Expand Down

0 comments on commit f5ab102

Please sign in to comment.