-
Notifications
You must be signed in to change notification settings - Fork 9
/
Program.cs
223 lines (184 loc) · 8.72 KB
/
Program.cs
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
using System;
using System.Collections.Generic;
using CommandLine;
using CommandLine.Text;
using SuRGeoNix.BitSwarmLib;
using SuRGeoNix.BitSwarmLib.BEP;
using BitSwarmOptions = SuRGeoNix.BitSwarmLib.Options;
namespace SuRGeoNix.BitSwarmConsole
{
class Program
{
static BitSwarm bitSwarm;
static BitSwarmOptions bitSwarmOptions;
static Torrent torrent;
static bool sessionFinished;
static int prevHeight;
static object lockRefresh = new object();
static View view = View.Stats;
enum View
{
Peers,
Stats,
Torrent
}
private static void Main(string[] args)
{
var parser = new Parser(with => with.HelpWriter = null);
var parserResult = parser.ParseArguments<Options>(args);
parserResult.WithParsed<Options>(options => Run(options)).WithNotParsed(errs => PrintHelp(parserResult, errs));
}
private static void PrintHelp<T>(ParserResult<T> result, IEnumerable<Error> errs)
{
var helpText = HelpText.AutoBuild(result, h => { return HelpText.DefaultParsingErrorsHandler(result, h); }, e => e, false, 160);
helpText.Heading = $"[BitSwarm v{BitSwarm.Version}]";
helpText.AddPostOptionsText("\r\n" + "CONFIG: \t\t\t\t Creates config file with default options (Place it under -> . | $HOME/.bitswarm | /etc/bitswarm | %APPDATA%/BitSwarm)\r\n. \t\t\t\t If [OPTIONS] are defined will override config values (Notice: you can use env variables even on unix %var%)\r\n\r\n\t" + $"bitswarm config\r\n");
helpText.AddPostOptionsText("\r\n" + "USAGE: \r\n\r\n\t" + $"bitswarm [OPTIONS] Torrent|Magnet|Hash|Session");
Console.WriteLine(helpText);
}
private static void PrintMenu() { Console.WriteLine($"[1: Stats] [2: Torrent] [3: Peers] [4: Peers (w/Refresh)] [5: {(bitSwarm.isRunning ? "Pause" : "Continue")}] [Ctrl-C: Exit]".PadLeft(100, ' ')); }
private static void Run(Options userOptions)
{
try
{
Console.WriteLine($"[BitSwarm v{BitSwarm.Version}] Initializing ...");
if (userOptions.Input == "config")
{
BitSwarmOptions.CreateConfig(new BitSwarmOptions());
Console.WriteLine($"[BitSwarm v{BitSwarm.Version}] Config {BitSwarmOptions.ConfigFile} created.");
return;
}
else if ((bitSwarmOptions = BitSwarmOptions.LoadConfig()) != null)
Console.WriteLine($"[BitSwarm v{BitSwarm.Version}] Config {BitSwarmOptions.ConfigFile} loaded.");
if (bitSwarmOptions == null) bitSwarmOptions = new BitSwarmOptions();
if (!Options.ParseOptionsToBitSwarm(userOptions, ref bitSwarmOptions)) return;
// BitSwarm [Create | Subscribe | Open | Start]
bitSwarm = new BitSwarm(bitSwarmOptions);
bitSwarm.MetadataReceived += BitSwarm_MetadataReceived; // Receives torrent data [on torrent file will fire directly, on magnetlink will fire on metadata received]
bitSwarm.StatsUpdated += BitSwarm_StatsUpdated; // Stats refresh every 2 seconds
bitSwarm.StatusChanged += BitSwarm_StatusChanged; // Paused/Stopped or Finished
bitSwarm.Open(userOptions.Input);
bitSwarm.Start();
// Stats | Torrent | Peers Views [Until Stop or Finish]
ConsoleKeyInfo cki;
Console.TreatControlCAsInput = true;
prevHeight = Console.WindowHeight;
while (!sessionFinished)
{
try
{
cki = Console.ReadKey();
if (sessionFinished) break;
if ((cki.Modifiers & ConsoleModifiers.Control) != 0 && cki.Key == ConsoleKey.C) break;
lock (lockRefresh)
switch (cki.Key)
{
case ConsoleKey.D1:
view = View.Stats;
Console.Clear();
Console.WriteLine(bitSwarm.DumpStats());
PrintMenu();
break;
case ConsoleKey.D2:
view = View.Torrent;
Console.Clear();
Console.WriteLine(bitSwarm.DumpTorrent());
PrintMenu();
break;
case ConsoleKey.D3:
view = View.Torrent;
Console.Clear();
Console.WriteLine(bitSwarm.DumpPeers());
PrintMenu();
break;
case ConsoleKey.D4:
view = View.Peers;
Console.Clear();
Console.WriteLine(bitSwarm.DumpPeers());
PrintMenu();
break;
case ConsoleKey.D5:
if (bitSwarm.isRunning)
bitSwarm.Pause();
else
bitSwarm.Start();
view = View.Stats;
Console.Clear();
Console.WriteLine(bitSwarm.DumpStats());
PrintMenu();
break;
default:
break;
}
} catch (Exception) { }
}
// Dispose (force) BitSwarm
if (bitSwarm != null) bitSwarm.Dispose(true);
} catch (Exception e) { Console.WriteLine($"[ERROR] {e.Message}"); }
}
private static void BitSwarm_StatusChanged(object source, BitSwarm.StatusChangedArgs e)
{
if (bitSwarm != null && bitSwarm.isPaused && e.Status == 1)
{
Console.WriteLine("Paused");
return;
}
if (e.Status == 0 && torrent != null && torrent.file.name != null)
{
Console.WriteLine($"\r\nDownload of {torrent.file.name} success!\r\n\r\n");
Console.WriteLine(bitSwarm.DumpTorrent());
Console.WriteLine($"\r\nDownload of {torrent.file.name} success!\r\n\r\n");
}
else if (e.Status == 2)
Console.WriteLine("An error has been occured :( \r\n" + e.ErrorMsg);
bitSwarm?.Dispose();
bitSwarm = null;
sessionFinished = true;
}
private static void BitSwarm_MetadataReceived(object source, BitSwarm.MetadataReceivedArgs e)
{
lock (lockRefresh)
{
torrent = e.Torrent;
view = View.Torrent;
Console.Clear();
Console.WriteLine(bitSwarm.DumpTorrent());
PrintMenu();
}
System.Threading.Thread tmp = new System.Threading.Thread(() =>
{
lock (lockRefresh)
{
System.Threading.Thread.Sleep(3000);
Console.Clear();
Console.WriteLine(bitSwarm.DumpStats());
PrintMenu();
view = View.Stats;
}
});
tmp.IsBackground = true;
tmp.Start();
}
private static void BitSwarm_StatsUpdated(object source, BitSwarm.StatsUpdatedArgs e)
{
try
{
if (view != View.Stats && view != View.Peers) return;
if (Console.WindowHeight != prevHeight) { prevHeight = Console.WindowHeight; Console.Clear(); }
lock (lockRefresh)
if (view == View.Peers)
{
Console.Clear();
Console.SetCursorPosition(0, 0);
Console.WriteLine(bitSwarm.DumpPeers());
}
else if (view == View.Stats)
{
Console.SetCursorPosition(0, 0);
Console.WriteLine(bitSwarm.DumpStats());
}
PrintMenu();
} catch (Exception) { }
}
}
}