-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReportClient.pm
65 lines (49 loc) · 1.51 KB
/
ReportClient.pm
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
# ---- class ReportClient ----
# write connection to server for sending reports
package DDgrey::ReportClient;
use strict;
use integer;
use Data::Dumper; # DEBUG
use DDgrey::Perl6::Parameters;
use DDgrey::Report;
use parent qw(DDgrey::Client);
# ---- constructor ----
# ---- methods ----
sub service($self){
# return: name of subsystem (for logging)
return "report client";
};
sub log_connect{
# return: whether to log connect
return 0;
}
sub report($self,$report){
# effect: sends report to server
$main::debug > 1 and main::lm("scheduling report ".$report->unicode(),"report client");
$self->schedule(sub{
$self->send("report\r\n");
$self->{line_handler}=sub{$self->handle_go(shift(),$report)};
});
};
sub handle_go($self,$line,$report){
# effect: sends report to server if line inidicates possible
if($line=~/^301\D/){
$self->send($report->as_text().".\r\n");
$self->{line_handler}=sub{$self->handle_reported(shift(),$report)};
}
else{
main::lm("got error from server (".$line=~s/[\r\n]+$//r.")",$self->service(),"warning");
delete($self->{line_handler});
};
};
sub handle_reported($self,$line,$report){
# effect: handles line
# pre: line is result of sending report to server
$main::debug > 1 and main::lm("sent report ".$report->unicode(),"report client");
if(not $line=~/^200\D/){
main::lm("got error from server (".$line=~s/[\r\n]+$//r.")",$self->service(),"warning");
};
delete($self->{line_handler});
};
# ---- package init ----
return 1;