]> dxcluster.net Git - spider.git/blob - perl/Investigate.pm
add a 36 month limit on sh/log queries.
[spider.git] / perl / Investigate.pm
1 #
2 # Investigate whether an external node is accessible
3 #
4 # If it is, make it believable otherwise mark as not
5 # to be believed. 
6 #
7 # It is possible to store up state for a node to be 
8 # investigated, so that if it is accessible, its details
9 # will be passed on to whomsoever might be interested.
10 #
11 # Copyright (c) 2004 Dirk Koopman, G1TLH
12 #
13 # $Id$
14 #
15
16 use strict;
17
18 package Investigate;
19
20 use DXDebug;
21 use DXUtil;
22
23
24 use vars qw($VERSION $BRANCH);
25 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
26 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/  || (0,0));
27 $main::build += $VERSION;
28 $main::branch += $BRANCH;
29
30 use vars qw (%list %valid $pingint $maxpingwait);
31
32 $pingint = 5;                                   # interval between pings for each investigation
33                                                                 # this is to stop floods of pings
34 $maxpingwait = 120;                             # the maximum time we will wait for a reply to a ping
35 my $lastping = 0;                               # last ping done
36 %list = ();                                             # the list of outstanding investigations
37 %valid = (                                              # valid fields
38                   call => '0,Callsign',
39                   start => '0,Started at,atime',
40                   version => '0,Node Version',
41                   build => '0,Node Build',
42                   here => '0,Here?,yesno',
43                   conf => '0,In Conf?,yesno',
44                   pingsent => '0,Time ping sent,atime',
45                   state => '0,State',
46                   via => '0,Via Node',
47                   pcxx => '0,Stored PCProt,parray',
48                  );
49
50 my %via = ();
51
52 sub new
53 {
54         my $pkg = shift;
55         my $call = shift;
56         my $via = shift;
57         
58         my $self = $list{"$via,$call"};
59         unless ($self) {
60                 $self = bless { 
61                                            call=>$call, 
62                                            via=>$via,
63                                            start=>$main::systime,
64                                            state=>'start',
65                                            pcxx=>[],
66                                           }, ref($pkg) || $pkg;
67                 $list{"$via,$call"} = $self; 
68         } 
69         dbg("Investigate: New $call via $via") if isdbg('investigate');
70         return $self;
71 }
72
73 sub get
74 {
75         return $list{"$_[1],$_[0]"};
76 }
77
78 sub chgstate
79 {
80         my $self = shift;
81         my $state = shift;
82         dbg("Investigate: $self->{call} via $self->{via} state $self->{state}->$state") if isdbg('investigate');
83         $self->{state} = $state;
84 }
85
86 sub handle_ping
87 {
88         my $self = shift;
89         dbg("Investigate: ping received for $self->{call} via $self->{via}") if isdbg('investigate');
90         if ($self->{state} eq 'waitping') {
91                 $via{$self->{via}} = 0;       # cue up next ping on this interface
92                 delete $list{"$self->{via},$self->{call}"};
93                 my $user = DXUser->get_current($self->{via});
94                 if ($user) {
95                         $user->set_believe($self->{call});
96                         $user->put;
97                 }
98                 my $dxchan = DXChannel::get($self->{via});
99                 if ($dxchan) {
100                         dbg("Investigate: sending PC19 for $self->{call}") if isdbg('investigate');
101                         foreach my $pc (@{$self->{pcxx}}) {
102                                 no strict 'refs';
103                                 my $handle = "handle_$pc->[0]";
104                                 dbg("Investigate: sending PC$pc->[0] (" . join(',', @$pc) . ")") if isdbg('investigate');
105                                 my $regex = $pc->[1];
106                                 $regex =~ s/\^/\\^/g;
107                                 DXProt::eph_del_regex($regex);
108                                 $dxchan->$handle(@$pc);
109                         }
110                 }
111         }
112 }
113
114 sub store_pcxx
115 {
116         my $self = shift;
117         dbg("Investigate: Storing (". join(',', @_) . ")") if isdbg('investigate');
118         push @{$self->{pcxx}}, [@_];
119 }
120
121 sub process
122 {
123         while (my ($k, $v) = each %list) {
124                 if ($v->{state} eq 'start') {
125                         my $via = $via{$v->{via}} || 0;
126                         if ($main::systime > $via+$pingint) {
127                                 DXXml::Ping::add($main::me, $v->{call}, $v->{via});
128                                 $v->{start} = $lastping = $main::systime;
129                                 dbg("Investigate: ping sent to $v->{call} via $v->{via}") if isdbg('investigate');
130                                 $v->chgstate('waitping');
131                                 $via{$v->{via}} = $main::systime;
132                         }
133                 } elsif ($v->{state} eq 'waitping') {
134                         if ($main::systime > $v->{start} + $maxpingwait) {
135                                 dbg("Investigate: ping timed out on $v->{call} via $v->{via}") if isdbg('investigate');
136                                 delete $list{$k};
137                                 my $user = DXUser->get_current($v->{via});
138                                 if ($user) {
139                                         $user->lastping($v->{via}, $main::systime);
140                                         $user->put;
141                                 }
142                         }
143                 }
144         }
145 }
146
147
148 sub AUTOLOAD
149 {
150         no strict;
151         my $name = $AUTOLOAD;
152         return if $name =~ /::DESTROY$/;
153         $name =~ s/^.*:://o;
154   
155         confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
156
157         # this clever line of code creates a subroutine which takes over from autoload
158         # from OO Perl - Conway
159         *$AUTOLOAD = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}};
160         goto &$AUTOLOAD;
161 }
162 1;