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