Skip to content

Commit 6a507ab

Browse files
committed
refactor: Replay thread now long running
1 parent 6c1d650 commit 6a507ab

File tree

5 files changed

+125
-56
lines changed

5 files changed

+125
-56
lines changed

src/bot/irclogbot.bot.pas

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ interface
1111
, IdIRC
1212
, IRCLogBot.Config
1313
, IRCLogBot.Database
14+
, IRCLogBot.Replay
1415
;
1516

1617
type
@@ -24,6 +25,8 @@ TIRCLogBot = class(TObject)
2425

2526
FDB: TDatabase;
2627

28+
FReplay: TReplayThread;
29+
2730
procedure OnConnected(Sender: TObject);
2831
procedure OnDisconnected(Sender: TObject);
2932
procedure OnNotice(ASender: TIdContext; const ANickname, AHost,
@@ -51,26 +54,25 @@ implementation
5154

5255
uses
5356
IRCLogBot.Common
54-
, IRCLogBot.Replay
5557
;
5658

5759

5860
{ TIRCLogBot }
5961

6062
procedure TIRCLogBot.OnConnected(Sender: TObject);
6163
begin
62-
debug('Connected to server');
64+
debug('Connected to server.');
6365
end;
6466

6567
procedure TIRCLogBot.OnDisconnected(Sender: TObject);
6668
begin
67-
debug('Disconnected from server');
69+
debug('Disconnected from server.');
6870
end;
6971

7072
procedure TIRCLogBot.OnNotice(ASender: TIdContext; const ANickname, AHost,
7173
ATarget, ANotice: String);
7274
begin
73-
debug('>> NOTICE: <%s:%s> (%s) "%s"', [
75+
debug('>> NOTICE: <%s:%s> (%s) "%s".', [
7476
ANickname,
7577
AHost,
7678
ATarget,
@@ -81,7 +83,7 @@ procedure TIRCLogBot.OnNotice(ASender: TIdContext; const ANickname, AHost,
8183
procedure TIRCLogBot.OnServerQuit(ASender: TIdContext; const ANickname, AHost,
8284
AServer, AReason: String);
8385
begin
84-
debug('>> QUIT: <%s:%s> %s "%s"',[
86+
debug('>> QUIT: <%s:%s> %s "%s".',[
8587
ANickname,
8688
AHost,
8789
AServer,
@@ -92,14 +94,14 @@ procedure TIRCLogBot.OnServerQuit(ASender: TIdContext; const ANickname, AHost,
9294
procedure TIRCLogBot.OnJoin(ASender: TIdContext; const ANickname, AHost,
9395
AChannel: String);
9496
begin
95-
debug('>> JOIN: <%s:%s> %s', [
97+
debug('>> JOIN: <%s:%s> %s.', [
9698
ANickname,
9799
AHost,
98100
AChannel
99101
]);
100102
if (ANickname = FConfig.NickName) and (AChannel = FConfig.Channel) then
101103
begin
102-
debug('Successfully joined my channel');
104+
debug('Successfully joined my channel.');
103105
FJoinedChannel:= True;
104106
FIRC.Say(AChannel, 'I have arrived!!! TADAAAAA!!! Send me a private message with ".help" to see what I can do for you.');
105107
end;
@@ -111,15 +113,15 @@ procedure TIRCLogBot.OnPrivateMessage(ASender: TIdContext; const ANickname,
111113
strings: TStringArray;
112114
count: Integer;
113115
begin
114-
debug('>> PRIVMSG: <%s:%s>(%s) "%s"', [
116+
debug('>> PRIVMSG: <%s:%s>(%s) "%s".', [
115117
ANickname,
116118
AHost,
117119
ATarget,
118120
AMessage
119121
]);
120122
if ATarget = FConfig.Channel then
121123
begin
122-
debug('Inserting: %s, %s, %s, %s', [
124+
debug('Inserting: %s, %s, %s, %s.', [
123125
ANickname,
124126
AHost,
125127
ATarget,
@@ -175,46 +177,51 @@ procedure TIRCLogBot.Help(const ATarget: String);
175177
begin
176178
debug('Help command.');
177179
FIRC.Say(ATarget, 'Commands:');
178-
FIRC.Say(ATarget, '.help - This help information.');
180+
FIRC.Say(ATarget, '.help - This help information.');
179181
FIRC.Say(ATarget, '.replay [count] - Raplays last <count> lines. Default is last 10 lines.');
180182
end;
181183

182184
procedure TIRCLogBot.Replay(const ATarget: String; ACount: Integer);
183185
var
184-
replayThread: TReplayThread;
185186
lines: TStringList;
186187
begin
187188
debug('Replay command(%d).', [ACount]);
188189
lines:= FDB.Get(ACount);
189-
debug('Lines: %d', [lines.Count]);
190-
replayThread:= TReplayThread.Create(FIRC, ATarget, lines);
190+
debug('Lines: %d.', [lines.Count]);
191+
FReplay.Add(ATarget, lines);
191192
lines.Free;
192193
end;
193194

194195
procedure TIRCLogBot.Run;
195196
begin
196-
debug('Connecting...');
197197
try
198+
debug('Connecting...');
198199
FIRC.Connect;
199200
except
200201
on e:Exception do
201202
begin
202-
debug('Error connecting: %s', [e.Message]);
203+
debug('Error connecting: "%s".', [e.Message]);
203204
end;
204205
end;
205-
debug('Joining channel: "%s"...', [FConfig.Channel]);
206206
try
207+
debug('Joining channel: "%s"...', [FConfig.Channel]);
207208
FIRC.Join(FConfig.Channel);
208209
except
209210
on e:Exception do
210211
begin
211-
debug('Error joining: %s', [e.Message]);
212+
debug('Error joining: "%s".', [e.Message]);
212213
end;
213214
end;
215+
debug('Starting Replay Thread.');
216+
FReplay:= TReplayThread.Create(FIRC);
214217
end;
215218

216219
procedure TIRCLogBot.Shutdown;
217220
begin
221+
debug('Terminating Replay Thread.');
222+
FReplay.Terminate;
223+
debug('Waiting for Replay Thread to terminate...');
224+
FReplay.WaitFor;
218225
if FIRC.Connected then
219226
begin
220227
debug('Disconnecting...');
@@ -224,7 +231,7 @@ procedure TIRCLogBot.Shutdown;
224231
except
225232
on e:Exception do
226233
begin
227-
debug('Error: %s', [e.Message]);
234+
debug('Error disconnecting: "%s".', [e.Message]);
228235
end;
229236
end;
230237
end;
@@ -257,7 +264,7 @@ constructor TIRCLogBot.Create(const AConfig: TBotConfig);
257264
except
258265
on e:Exception do
259266
begin
260-
debug('Error creating db: ', [e.Message]);
267+
debug('Error creating db: "%s".', [e.Message]);
261268
end;
262269
end;
263270
end;

src/common/irclogbot.common.pas

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ procedure debug(const AMessage: String);
2323
begin
2424
if DebugOn then
2525
begin
26-
dateTimeStr:= FormatDateTime('yyyy-mm-dd hh:nn:ss.zzz:', Now);
26+
dateTimeStr:= FormatDateTime('yyyy/mm/dd hh:nn:ss.zzz: ', Now);
2727
WriteLn(Format('%s %s', [dateTimeStr, AMessage]));
2828
end;
2929
end;
@@ -32,7 +32,7 @@ procedure debug(const AFormat: String; AValues: array of const);
3232
begin
3333
if DebugOn then
3434
begin
35-
dateTimeStr:= FormatDateTime('yyyy-mm-dd hh:nn:ss.zzz: ', Now);
35+
dateTimeStr:= FormatDateTime('yyyy/mm/dd hh:nn:ss.zzz: ', Now);
3636
WriteLn(Format(dateTimeStr+AFormat, AValues));
3737
end;
3838
end;

src/database/irclogbot.database.pas

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ procedure TDatabase.SetupTables;
5555
on e:Exception do
5656
begin
5757
FTransaction.Rollback;
58-
debug('Error setting tables: %s', [e.Message]);
58+
debug('Error setting tables: "%s".', [e.Message]);
5959
end;
6060
end;
6161
end;
@@ -68,7 +68,7 @@ procedure TDatabase.Insert(ANickName, AChannel, AMessage: String);
6868
debug('Starting Transaction...');
6969
FTransaction.StartTransaction;
7070
try
71-
debug('Inserting: <%s> [%s] "%s"', [ANickName, AChannel, AMessage]);
71+
debug('Inserting: <%s> [%s] "%s".', [ANickName, AChannel, AMessage]);
7272
FConnection.ExecuteDirect(Format(
7373
'INSERT INTO logs (nick, channel, message) VALUES (%s, %s, %s)',
7474
[QuotedStr(ANickName), QuotedStr(AChannel), QuotedStr(AMessage)]));
@@ -80,7 +80,7 @@ procedure TDatabase.Insert(ANickName, AChannel, AMessage: String);
8080
on e:Exception do
8181
begin
8282
FTransaction.Rollback;
83-
debug('Error inserting message: %s', [e.Message]);
83+
debug('Error inserting message: "%s".', [e.Message]);
8484
end;
8585
end;
8686
finally
@@ -112,7 +112,7 @@ function TDatabase.Get(ACount: Integer): TStringList;
112112
channel:= FQuery.FieldByName('channel').AsString;
113113
nick:= FQuery.FieldByName('nick').AsString;
114114
message:= FQuery.FieldByName('message').AsString;
115-
debug('Retrieving: %s [%s] %s: %s', [
115+
debug('Retrieving: %s [%s] %s: %s.', [
116116
date,
117117
channel,
118118
nick,
@@ -136,7 +136,7 @@ function TDatabase.Get(ACount: Integer): TStringList;
136136
on e:Exception do
137137
begin
138138
FTransaction.Rollback;
139-
debug('Error retrieving lines: %s', [e.Message]);
139+
debug('Error retrieving lines: "%s".', [e.Message]);
140140
end;
141141
end;
142142
finally

src/paslogbot.lpr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ procedure TPasLogBot.DoRun;
132132
//debug('Short Date: ' + DefaultFormatSettings.ShortDateFormat);
133133
//debug('Short Time: ' + DefaultFormatSettings.ShortTimeFormat);
134134
//debug('Date Sep: ' + DefaultFormatSettings.DateSeparator);
135-
debug('Setting Date and Time formats');
135+
debug('Setting Date and Time formats.');
136136
DefaultFormatSettings.ShortDateFormat:= 'yyyy/mm/dd';
137137
DefaultFormatSettings.DateSeparator:= '/';
138138
debug(Format('Attempting to read config from: "%s"...', [FConfigFile]));

0 commit comments

Comments
 (0)