d59a57b5a1ba17f976d2fe47660366076510e400
[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
51 sub new
52 {
53         my $pkg = shift;
54         my $call = shift;
55         my $via = shift;
56         
57         my $self = $list{"$via,$call"};
58         unless ($self) {
59                 $self = bless { 
60                                            call=>$call, 
61                                            via=>$via,
62                                            start=>$main::systime,
63                                            state=>'start',
64                                            pcxx=>[],
65                                           }, ref($pkg) || $pkg;
66                 $list{"$via,$call"} = $self; 
67         } 
68         dbg("Investigate: New $call via $via") if isdbg('investigate');
69         return $self;
70 }
71
72 sub get
73 {
74         return $list{"$_[1],$_[0]"};
75 }
76
77 sub chgstate
78 {
79         my $self = shift;
80         my $state = shift;
81         dbg("Investigate: $self->{call} via $self->{via} state $self->{state}->$state") if isdbg('investigate');
82         $self->{state} = $state;
83 }
84
85 sub handle_ping
86 {
87         my $self = shift;
88         dbg("Investigate: ping received for $self->{call} via $self->{via}") if isdbg('investigate');
89         if ($self->{state} eq 'waitping') {
90                 delete $list{"$self->{via},$self->{call}"};
91                 my $user = DXUser->get_current($self->{via});
92                 if ($user) {
93                         $user->set_believe($self->{call});
94                         $user->put;
95                 }
96                 my $dxchan = DXChannel->get($self->{via});
97                 if ($dxchan) {
98                         dbg("Investigate: sending PC19 for $self->{call}") if isdbg('investigate');
99                         foreach my $pc (@{$self->{pcxx}}) {
100                                 no strict 'refs';
101                                 my $handle = "handle_$pc->[0]";
102                                 dbg("Investigate: sending PC$pc->[0] (" . join(',', @$pc) . ")") if isdbg('investigate');
103                                 my $regex = $pc->[1];
104                                 $regex =~ s/\^/\\^/g;
105                                 DXProt::eph_del_regex($regex);
106                                 $dxchan->$handle(@$pc);
107                         }
108                 }
109         }
110 }
111
112 sub store_pcxx
113 {
114         my $self = shift;
115         dbg("Investigate: Storing (". join(',', @_) . ")") if isdbg('investigate');
116         push @{$self->{pcxx}}, [@_];
117 }
118
119 sub process
120 {
121         while (my ($k, $v) = each %list) {
122                 if ($v->{state} eq 'start') {
123                         if ($main::systime > $lastping+$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                         }
129                 } elsif ($v->{state} eq 'waitping') {
130                         if ($main::systime > $v->{start} + $maxpingwait) {
131                                 dbg("Investigate: ping timed out on $v->{call} via $v->{via}") if isdbg('investigate');
132                                 delete $list{$k};
133                                 my $user = DXUser->get_current($v->{via});
134                                 if ($user) {
135                                         $user->lastping($v->{via}, $main::systime);
136                                         $user->put;
137                                 }
138                         }
139                 }
140         }
141 }
142
143
144 sub AUTOLOAD
145 {
146         no strict;
147         my $name = $AUTOLOAD;
148         return if $name =~ /::DESTROY$/;
149         $name =~ s/^.*:://o;
150   
151         confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
152
153         # this clever line of code creates a subroutine which takes over from autoload
154         # from OO Perl - Conway
155         *$AUTOLOAD = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}};
156         goto &$AUTOLOAD;
157 }
158 1;