/
Main.cs
418 lines (348 loc) · 9.8 KB
/
Main.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
//
// Main.cs
//
// Author:
// Lars Formella <ich@larsformella.de>
//
// Copyright (c) 2012 Lars Formella
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using XG.Core;
using XG.Server.Helper;
using XG.Server.Irc;
using XG.Server.Plugin;
using XG.Server.Worker;
using log4net;
using File = XG.Core.File;
namespace XG.Server
{
public class Main : AWorker
{
#region VARIABLES
static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
readonly Parser _ircParser;
readonly Servers _servers;
readonly FileActions _fileActions;
readonly Workers _workers;
#endregion
#region FUNCTIONS
public Main()
{
_fileActions = new FileActions();
_ircParser = new Parser {FileActions = _fileActions};
_ircParser.ParsingError += IrcParserParsingError;
_servers = new Servers {FileActions = _fileActions, IrcParser = _ircParser};
_workers = new Workers();
}
#endregion
#region AWorker
protected override void StartRun()
{
#region DUPE CHECK
// check if there are some dupes in our database
foreach (Core.Server serv in Servers.All)
{
foreach (Core.Server s in Servers.All)
{
if (s.Name == serv.Name && s.Guid != serv.Guid)
{
Log.Error("Run() removing dupe server " + s.Name);
Servers.Remove(s);
}
}
foreach (Channel chan in serv.Channels)
{
foreach (Channel c in serv.Channels)
{
if (c.Name == chan.Name && c.Guid != chan.Guid)
{
Log.Error("Run() removing dupe channel " + c.Name);
serv.RemoveChannel(c);
}
}
foreach (Bot bot in chan.Bots)
{
foreach (Bot b in chan.Bots)
{
if (b.Name == bot.Name && b.Guid != bot.Guid)
{
Log.Error("Run() removing dupe bot " + b.Name);
chan.RemoveBot(b);
}
}
foreach (Packet pack in bot.Packets)
{
foreach (Packet p in bot.Packets)
{
if (p.Id == pack.Id && p.Guid != pack.Guid)
{
Log.Error("Run() removing dupe Packet " + p.Name);
bot.RemovePacket(p);
}
}
}
}
}
}
#endregion
#region RESET
// reset all objects if the server crashed
foreach (Core.Server tServer in Servers.All)
{
tServer.Connected = false;
tServer.ErrorCode = SocketErrorCode.None;
foreach (Channel tChannel in tServer.Channels)
{
tChannel.Connected = false;
tChannel.ErrorCode = 0;
foreach (Bot tBot in tChannel.Bots)
{
tBot.Connected = false;
tBot.State = Bot.States.Idle;
foreach (Packet pack in tBot.Packets)
{
pack.Connected = false;
}
}
}
}
#endregion
#region CLEAR OLD DL
if (Files.All.Any() && Settings.Instance.ClearReadyDownloads)
{
foreach (File file in Files.All)
{
if (file.Enabled)
{
Files.Remove(file);
Log.Info("Run() removing ready file " + file.Name);
}
}
}
#endregion
#region CRASH RECOVERY
if (Files.All.Any())
{
foreach (File file in Files.All)
{
// lets check if the directory is still on the harddisk
if (!Directory.Exists(Settings.Instance.TempPath + file.TmpPath))
{
Log.Warn("Run() crash recovery directory " + file.TmpPath + " is missing ");
_fileActions.RemoveFile(file);
continue;
}
file.Lock = new object();
if (!file.Enabled)
{
bool complete = true;
string tmpPath = Settings.Instance.TempPath + file.TmpPath;
foreach (FilePart part in file.Parts)
{
// first part is always checked!
if (part.StartSize == 0)
{
part.Checked = true;
}
// check if the real file and the part is actual the same
var info = new FileInfo(tmpPath + part.StartSize);
if (info.Exists)
{
// TODO uhm, should we do smt here ?! maybe check the size and set the state to ready?
if (part.CurrentSize != part.StartSize + info.Length)
{
Log.Warn("Run() crash recovery size mismatch of part " + part.StartSize + " from file " + file.TmpPath + " - db:" + part.CurrentSize +
" real:" + info.Length);
part.CurrentSize = part.StartSize + info.Length;
complete = false;
}
}
else
{
Log.Error("Run() crash recovery part " + part.StartSize + " of file " + file.TmpPath + " is missing");
_fileActions.RemovePart(file, part);
complete = false;
}
// uhh, this is bad - close it and hope it works again
if (part.State == FilePart.States.Open)
{
part.State = FilePart.States.Closed;
complete = false;
}
// the file is closed, so do smt
else
{
// check the file for safety
if (part.Checked && part.State == FilePart.States.Ready)
{
var next = file.Next(part) as FilePart;
if (next != null && !next.Checked && next.CurrentSize - next.StartSize >= Settings.Instance.FileRollbackCheckBytes)
{
complete = false;
try
{
Log.Fatal("Run() crash recovery checking " + next.Name);
FileStream fileStream = System.IO.File.Open(_fileActions.CompletePath(part), FileMode.Open, FileAccess.ReadWrite);
var fileReader = new BinaryReader(fileStream);
// extract the needed refernce bytes
fileStream.Seek(-Settings.Instance.FileRollbackCheckBytes, SeekOrigin.End);
byte[] bytes = fileReader.ReadBytes(Settings.Instance.FileRollbackCheckBytes);
fileReader.Close();
_fileActions.CheckNextReferenceBytes(part, bytes);
}
catch (Exception ex)
{
Log.Fatal("Run() crash recovery", ex);
}
}
}
else
{
complete = false;
}
}
}
// check and maybee join the files if something happend the last run
// for exaple the disk was full or the rights were not there
if (complete && file.Parts.Any())
{
_fileActions.CheckFile(file);
}
}
}
}
#endregion
#region CONNECT ALL ENABLED SERVERS
foreach (Core.Server serv in Servers.All)
{
// TODO check this
serv.Parent = null;
serv.Parent = Servers;
if (serv.Enabled)
{
_servers.ServerConnect(serv);
}
}
#endregion
#region WORKERS
ALoopWorker worker = new SnapshotWorker {SecondsToSleep = Settings.Instance.TakeSnapshotTime};
AddWorker(worker);
worker = new BotWatchdogWorker {SecondsToSleep = Settings.Instance.BotOfflineCheckTime};
AddWorker(worker);
#endregion
}
protected override void StopRun()
{
_servers.AllowRunning = false;
foreach (Core.Server serv in Servers.All)
{
_servers.ServerDisconnect(serv);
}
_workers.StopAll();
}
#endregion
#region PLUGINS / WORKERS
public void AddBackendPlugin(ABackendPlugin aPlugin)
{
Servers = aPlugin.LoadServers();
_fileActions.Servers = Servers;
Files = aPlugin.LoadFiles();
_fileActions.Files = Files;
Searches = aPlugin.LoadSearches();
Snapshots = aPlugin.LoadStatistics();
if (Snapshots.All.Count > 0)
{
// put a empty snapshot on top to nullify values
var lasttime = (from snapshot in Snapshots.All orderby snapshot.Get(SnapshotValue.Timestamp) select snapshot).Last().Get(SnapshotValue.Timestamp);
var lastSnapshot = new Snapshot();
lastSnapshot.Set(SnapshotValue.Timestamp, lasttime + 1);
Snapshots.Add(lastSnapshot);
var firstSnapshot = new Snapshot();
firstSnapshot.Set(SnapshotValue.Timestamp, DateTime.Now.ToTimestamp());
Snapshots.Add(firstSnapshot);
}
AddWorker(aPlugin);
}
public void AddWorker(AWorker aWorker)
{
aWorker.Servers = Servers;
aWorker.Files = Files;
aWorker.Searches = Searches;
aWorker.Snapshots = Snapshots;
_workers.Add(aWorker);
aWorker.Start();
}
#endregion
#region EVENTHANDLER
protected override void ObjectAdded(AObject aParent, AObject aObj)
{
if (aObj is Core.Server)
{
var aServer = aObj as Core.Server;
Log.Info("ServerObjectAdded(" + aServer.Name + ")");
_servers.ServerConnect(aServer);
}
}
protected override void ObjectRemoved(AObject aParent, AObject aObj)
{
if (aObj is Core.Server)
{
var aServer = aObj as Core.Server;
aServer.Enabled = false;
aServer.Commit();
Log.Info("ServerObjectRemoved(" + aServer.Name + ")");
_servers.ServerDisconnect(aServer);
}
}
protected override void ObjectEnabledChanged(AObject aObj)
{
if (aObj is Core.Server)
{
var aServer = aObj as Core.Server;
if (aObj.Enabled)
{
_servers.ServerConnect(aServer);
}
else
{
_servers.ServerDisconnect(aServer);
}
}
}
void IrcParserParsingError(string aData)
{
lock (this)
{
try
{
var sw = new StreamWriter(System.IO.File.OpenWrite(Settings.Instance.ParsingErrorFile));
sw.BaseStream.Seek(0, SeekOrigin.End);
sw.WriteLine(aData.Normalize());
sw.Close();
}
catch (Exception)
{
// just ignore
}
}
}
#endregion
}
}