2 # Investigate whether an external node is accessible
4 # If it is, make it believable otherwise mark as not
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.
11 # Copyright (c) 2004 Dirk Koopman, G1TLH
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;
30 use vars qw (%list %valid $pingint $maxpingwait);
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
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',
47 pcxx => '0,Stored PCProt,parray',
58 my $self = $list{"$via,$call"};
63 start=>$main::systime,
67 $list{"$via,$call"} = $self;
69 dbg("Investigate: New $call via $via") if isdbg('investigate');
75 return $list{"$_[1],$_[0]"};
82 dbg("Investigate: $self->{call} via $self->{via} state $self->{state}->$state") if isdbg('investigate');
83 $self->{state} = $state;
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});
95 $user->set_believe($self->{call});
98 my $dxchan = DXChannel::get($self->{via});
100 dbg("Investigate: sending PC19 for $self->{call}") if isdbg('investigate');
101 foreach my $pc (@{$self->{pcxx}}) {
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);
117 dbg("Investigate: Storing (". join(',', @_) . ")") if isdbg('investigate');
118 push @{$self->{pcxx}}, [@_];
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;
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');
137 my $user = DXUser->get_current($v->{via});
139 $user->lastping($v->{via}, $main::systime);
151 my $name = $AUTOLOAD;
152 return if $name =~ /::DESTROY$/;
155 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
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}};