-
Notifications
You must be signed in to change notification settings - Fork 6
/
uPackagesScanner.pas
396 lines (378 loc) · 15.1 KB
/
uPackagesScanner.pas
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
unit uPackagesScanner;
{******************************************************************************}
{* Packages Scanner Unit *}
{* Revolutionary Confederation of Anarcho Syndicalists *}
{* Written by: black.rabbit 2012 *}
{******************************************************************************}
interface
{$I 'std.inc'}
uses
Windows, SysUtils, Variants, Classes,
Controls, ComCtrls, Gauges,
sListView,
{ utils }
DateUtils, Utils, Strings, VarRecs, Versions, EClasses,
DllThreads,
{ kernel }
Kernel, ProtoClasses, CryptoClasses, MetaClasses, ParaClasses,
HypoClasses, HyperClasses,
{ engine }
Engine,
{ SQLite }
SQLite3, SQLite3DLL, SQLiteTable3;
{ èêîíêè }
const
pckDefault = 0;
pckEmpty = 1;
pckFull = 2;
pckTest = 3;
pckError = 4;
pckSend = 5;
type
PLogRecord = ^TLogRecord;
TLogRecord = packed record
Caption : ShortString;
ImageIndex : Integer;
Sender : ShortString;
Receiver : ShortString;
Message : ShortString;
PackageType : ShortString;
MetaClassID : ShortString;
end;
type
{ îáðàáîò÷èê âõîäÿùèõ ïàêåòîâ }
{$M+}
CPackagesScanner = class of TPackagesScanner;
PPackagesScanner = ^TPackagesScanner;
TPackagesScanner = class (TDllThread)
private
f_DBFileName: String; { ôàéë áàçû äàííûõ }
f_DB: TSQLiteDatabase; { îáúåêò áàçû äàííûõ }
f_ListView: TsListView; { ëîã }
f_Gauge: TGauge; { ïîêàçàòåëü ïðîãðåññà çàãðóçêè }
f_StatusPanel: TStatusPanel; { ñòðîêà ñîñòîÿíèÿ çàãðóçêè }
f_Packages: TPackages; { ñïèñîê ïàêåòîâ }
f_PackageIndex: LongInt; { ïîçèöèÿ â îáõîäå ñïèñêà }
private
f_Log: TItems;
f_Status: String;
f_MaxProgress: WORD;
f_Progress: WORD;
public
constructor Create (anArgs: array of const); override;
destructor Destroy; override;
public
procedure Main; override;
procedure Return; override;
public
procedure ProcessPackage (aPackage: TPackage); virtual;
protected
procedure WriteStatus (const aMessage: String); overload;
procedure WriteStatus (const aMessage: String;
aParams: array of const); overload;
procedure WriteLog (const aSender: String;
const aReceiver: String;
const aMessage: String;
const anImageIndex: Integer = pckDefault;
const aPackageType: String = '';
const aMetaClassID: String = ''); overload;
procedure WriteLog (const aSender: String;
const aReceiver: String;
const aMessage: String;
aParams: array of const;
const anImageIndex: Integer = pckDefault;
const aPackageType: String = '';
const aMetaClassID: String = ''); overload;
public
property DBFileName: String read f_DBFileName;
property DB: TSQLiteDatabase read f_DB write f_DB;
property ListView: TsListView read f_ListView write f_ListView;
property Gauge: TGauge read f_Gauge write f_Gauge;
property StatusPanel: TStatusPanel read f_StatusPanel write f_StatusPanel;
property Packages: TPackages read f_Packages write f_Packages;
property PackageIndex: LongInt read f_PackageIndex write f_PackageIndex;
property Log: TItems read f_Log write f_Log;
property Status: String read f_Status write f_Status;
property MaxProgress: WORD read f_MaxProgress write f_MaxProgress;
property Progress: WORD read f_Progress write f_Progress;
end;
{$M-}
{ TPackagesScanner Errors }
resourcestring
ERR_TPACKAGESCANNER_INCORRECT_DATABASE = 'Íå èíèöèàëèçèðîâàí îáúåêò ÁÄ!';
ERR_TPACKAGESCANNER_INCORRECT_GAUGE = 'Íå èíèöèàëèçèðîâàí ýëåìåíò êîíòðîëÿ ïðîãðåññà!';
ERR_TPACKAGESCANNER_INCORRECT_STATUS_PANEL = 'Íå èíèöèàëèçèðîâàíà ñòðîêà ñîñòîÿíèÿ!';
ERR_TPACKAGESCANNER_INVALID_PACKAGE = 'Ïîëó÷åí íåêîððåêòíûé ïàêåò!';
ERR_TPACKAGESCANNER_CREATE = 'Îøèáêà ñîçäàíèÿ ïîòîêà îáðàáîòêè ïàêåòîâ!';
ERR_TPACKAGESCANNER_DESTROY = 'Îøèáêà óíè÷òîæåíèÿ ïîòîêà îáðàáîòêè ïàêåòîâ!';
ERR_TPACKAGESCANNER_MAIN = 'Îøèáêà ãëàâíîé ôóíêöèè ïîòîêà!';
ERR_TPACKAGESCANNER_RETURN = 'Îøèáêà ôóíêöèè âîçâðàòà ïîòîêà!';
ERR_TPACKAGESCANNER_WRITE_STATUS = 'Îøèáêà îòîáðàæåíèÿ ñòàòóñà!';
ERR_TPACKAGESCANNER_WRITE_LOG = 'Îøèáêà çàïèñè â ëîã!';
{ TPackagesScanner Hints }
resourcestring
MSG_TPACKAGESCANNER_EXECUTING_PACKAGE = 'Îáðàáîòêà ïàêåòà ''%s''...';
MSG_TPACKAGESCANNER_EXECUTED_PACKAGE = 'Ïàêåò ''%s'' îáðàáîòàí.';
MSG_TPACKAGESCANNER_REJECTED_PACKAGE = 'Ïàêåò ''%s'' îòâåðãíóò. Îøèáêà : %s';
implementation
constructor TPackagesScanner.Create (anArgs: array of const);
var
I : Integer;
args : array_of_const;
OBJ : TObject;
begin
try
{ ïåðåäàåì ïàðàìåòðû ñîçäàíèÿ áàçîâîãî ïîòîêà,
íà÷èíàÿ ñ ïÿòîãî àðãóìåíòà }
if ( High (anArgs) >= 4 ) then
begin
SetLength ( Args, High (anArgs)-4 +1 );
for I := 4 to High (anArgs) do
args [I-4] := anArgs [I];
end
else
args := _array_of_const ([]);
inherited Create (args);
{ ÿâíî óêàçûâàåì íà íåîáõîäèìîñòü óíè÷òîæåíèÿ ïîòîêà ïî îêîí÷àíèþ ðàáîòû }
FreeOnTerminate := TRUE;
{ èìÿ ïîòîêà }
Name := ClassName;
{ ïåðâûé ïàðàìåòð - ôàéë ÁÄ }
f_DBFileName := '';
f_DB := NIL;
if notEmpty (0,anArgs) then
begin
f_DBFileName := toString (anArgs [0]);
f_DB := TSQLiteDatabase.Create (f_DBFileName);
end;
if ( not Assigned (f_DB) ) then
raise Exception.Create (ERR_TPACKAGESCANNER_INCORRECT_DATABASE);
{ âòîðîé ïàðàìåòð - ListView
ëîã ïî÷òû }
f_ListView := NIL;
if notEmpty (1,anArgs) then
begin
OBJ := toObject (anArgs [1]);
if ( Assigned (OBJ) and OBJ.InheritsFrom (TsListView) ) then
f_ListView := TsListView (OBJ);
end;
{ òðåòèé ïàðàìåòð - Gauge
ïîêàçàòåëü ïðîãðåññà çàãðóçêè }
f_Gauge := NIL;
if notEmpty (2,anArgs) then
begin
OBJ := toObject (anArgs [2]);
if ( Assigned (OBJ) and OBJ.InheritsFrom (TControl) ) then
f_Gauge := TGauge (OBJ);
end;
{ ÷åòâåðòûé ïàðàìåòð - StatusPanel
ïîêàçàòåëü ïðîãðåññà çàãðóçêè }
f_StatusPanel := NIL;
if notEmpty (3,anArgs) then
begin
OBJ := toObject (anArgs [3]);
if ( Assigned (OBJ) and OBJ.InheritsFrom (TStatusPanel) ) then
f_StatusPanel := TStatusPanel (OBJ);
end;
{ ñïèñîê ïàêåòîâ }
f_Packages := NIL;
f_PackageIndex := -1;
{ ëîã }
f_Log := TItems.Create ([]);
except on E: Exception do
_raise ([ 'Create', ERR_TPACKAGESCANNER_CREATE, E, Exception (FatalException) ],
['{C4C3FCC8-25BE-4DAF-AE8C-ED2B25A61F63}']);
end;
end;
destructor TPackagesScanner.Destroy;
begin
try
FreeAndNil (f_Log);
FreeAndNil (f_Packages);
FreeAndNil (f_DB);
inherited Destroy;
except on E: Exception do
_raise ([ 'Destroy', ERR_TPACKAGESCANNER_DESTROY, E, Exception (FatalException) ],
['{E7C92472-772D-4B7F-83A3-8AEB36BD7EFD}']);
end;
end;
procedure TPackagesScanner.Main;
begin
try
if Terminated then Exit;
inherited Main;
{ çàïðàøèâàåì âñå ïîëó÷åííûå ïàêåòû,
ïðåäíàçíà÷åííûå íàì }
if not Assigned (Packages) then
try
f_Packages := TPackages.Load (DB,[ _([]),
_([]),
_([ USER_KEY_HASH ]),
_([USER_ID]),
_([]),
_([PACKAGE_RECEIVED_STATUS_ID]),
_([ TUser, {TPic,} TMessage, TCategorie, TMetaObject ]) ],
[ _pck_time_stamp_create ]) as TPackages;
MaxProgress := Packages.Count;
Progress := 0;
except
FreeAndNil (f_Packages);
end;
if not Assigned (Packages) or ( Packages.Count <= 0 ) then
Terminate
else if ( PackageIndex >= Packages.Count-1 ) then
Terminate
else
try
PackageIndex := PackageIndex + 1;
{ îáðàáàòûâàåì ïàêåò }
ProcessPackage (Packages.ItemAt [PackageIndex]);
{ îáðàáîòêà ïðîøëà óñïåøíî - ïàêåò îáðàáîòàí }
Packages.ItemAt [PackageIndex].IDStatus := PACKAGE_EXECUTED_STATUS_ID;
Packages.ItemAt [PackageIndex].Save;
{ ïðîãðåññ }
Progress := Progress + 1;
except on ProcessError: Exception do
if Assigned (Packages.ItemAt [PackageIndex]) then
begin
{ ïðè îáðàáîòêå ïðîèçîøëà îøèáêà - ïàêåò îòâåðãíóò }
Packages.ItemAt [PackageIndex].IDStatus := PACKAGE_REJECTED_STATUS_ID;
Packages.ItemAt [PackageIndex].Save;
{ ïèøåì â ëîã ïðè÷èíó îøèáêè }
WriteLog ( '...', User.Login,
Format (MSG_TPACKAGESCANNER_REJECTED_PACKAGE,
[ Packages.ItemAt [PackageIndex].KeyHash,
ProcessError.Message ]),
pckEmpty,
Packages.ItemAt [PackageIndex].PckType.IDExternal,
Packages.ItemAt [PackageIndex].MetaClass.GetClassID );
end
else
WriteStatus (ProcessError.Message);
end;
except on E: Exception do
_raise ([ 'Main', ERR_TPACKAGESCANNER_MAIN, E, Exception (FatalException) ],
['{A99E3E33-1DA6-4180-9C88-8DE52F14E2C5}']);
end;
end;
procedure TPackagesScanner.Return;
var
Itm : TListItem;
begin
try
inherited Return;
if Assigned (ListView) and Assigned (Log) then
begin
while Log.Count > 0 do
begin
Itm := ListView.Items.Add;
Itm.Caption := PLogRecord (Log.Item [0])^.Caption;
Itm.ImageIndex := PLogRecord (Log.Item [0])^.ImageIndex;
Itm.SubItems.Add ( PLogRecord (Log.Item [0])^.Sender );
Itm.SubItems.Add ( PLogRecord (Log.Item [0])^.Receiver );
Itm.SubItems.Add ( PLogRecord (Log.Item [0])^.Message );
if ( ( PLogRecord (Log.Item [0])^.PackageType = 'PUT' ) or
( PLogRecord (Log.Item [0])^.PackageType = 'DEL' ) ) and
( ( PLogRecord (Log.Item [0])^.MetaClassID = TCategorie.GetClassID ) or
( PLogRecord (Log.Item [0])^.MetaClassID = TMessage.GetClassID ) ) then
SetTabStatus (tabForum,tbsEmpty);
Dispose ( PLogRecord (Log.Item [0]) );
Log.Delete (0);
end;
end;
if Assigned (StatusPanel) and notEmpty (Status) then
begin
StatusPanel.Text := Status;
end;
if Assigned (Gauge) then
begin
Gauge.MinValue := 0;
Gauge.MaxValue := MaxProgress;
Gauge.Progress := Progress;
end;
ProcessMessages;
except on E: Exception do
_raise ([ 'Return', ERR_TPACKAGESCANNER_RETURN, E, Exception (FatalException) ],
['{813F60E5-0E43-478E-8A32-B4BC75D4CD34}']);
end;
end;
procedure TPackagesScanner.ProcessPackage (aPackage: TPackage);
begin
if not Assigned (aPackage) then
raise Exception.Create (ERR_TPACKAGESCANNER_INVALID_PACKAGE);
WriteStatus (MSG_TPACKAGESCANNER_EXECUTING_PACKAGE,
[ aPackage.KeyHash ]);
aPackage.ProcessMetaData;
{ ïèøåì â ëîã, ÷òî ïàêåò îáðàáîòàí }
WriteLog ( '...', User.Login,
Format (MSG_TPACKAGESCANNER_EXECUTED_PACKAGE,
[ aPackage.KeyHash ]),
pckFull,
aPackage.PckType.IDExternal,
aPackage.MetaClass.GetClassID );
end;
procedure TPackagesScanner.WriteStatus (const aMessage: String);
begin
try
Status := aMessage;
except on E: Exception do
_raise (['WriteStatus',ERR_TPACKAGESCANNER_WRITE_STATUS,E],
['{38EE1B09-5C82-4D05-A623-E9305352D91B}']);
end;
end;
procedure TPackagesScanner.WriteStatus (const aMessage: String;
aParams: array of const);
begin
try
WriteStatus ( Format (aMessage,aParams) );
except on E: Exception do
_raise (['WriteStatus',ERR_TPACKAGESCANNER_WRITE_STATUS,E],
['{57C86D62-0E46-4D2D-BF5B-DAD6BB746598}']);
end;
end;
procedure TPackagesScanner.WriteLog (const aSender: String;
const aReceiver: String;
const aMessage: String;
const anImageIndex: Integer = pckDefault;
const aPackageType: String = '';
const aMetaClassID: String = '');
var
Rec : PLogRecord;
begin
try
if Assigned (Log) then
begin
Rec := AllocMem ( SizeOf (TLogRecord) + 1 );
Rec^.Caption := _DateTimeToStr (now);
Rec^.ImageIndex := anImageIndex;
Rec^.Sender := aSender;
Rec^.Receiver := aReceiver;
if notEmpty (aMetaClassID) then
Rec^.Message := Format ('%s : %s : %s',[aMetaClassID,aPackageType,aMessage])
else
Rec^.Message := aMessage;
Rec^.PackageType := aPackageType;
Rec^.MetaClassID := aMetaClassID;
Log.Add (Rec);
end;
except on E: Exception do
_raise (['WriteLog',ERR_TPACKAGESCANNER_WRITE_LOG,E],
['{A484C147-114D-49A8-9F55-0CF50D0D3047}']);
end;
end;
procedure TPackagesScanner.WriteLog (const aSender: String;
const aReceiver: String;
const aMessage: String;
aParams: array of const;
const anImageIndex: Integer = pckDefault;
const aPackageType: String = '';
const aMetaClassID: String = '');
begin
try
WriteLog ( aSender, aReceiver, Format (aMessage,aParams), anImageIndex, aPackageType, aMetaClassID );
except on E: Exception do
_raise (['WriteLog',ERR_TPACKAGESCANNER_WRITE_LOG,E],
['{86B0F1A8-F5D5-4ED6-8F6B-E9155771325E}']);
end;
end;
end.