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',
57 my $self = $list{"$via,$call"};
62 start=>$main::systime,
66 $list{"$via,$call"} = $self;
68 dbg("Investigate: New $call via $via") if isdbg('investigate');
74 return $list{"$_[1],$_[0]"};
81 dbg("Investigate: $self->{call} via $self->{via} state $self->{state}->$state") if isdbg('investigate');
82 $self->{state} = $state;
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});
93 $user->set_believe($self->{call});
96 my $dxchan = DXChannel->get($self->{via});
98 dbg("Investigate: sending PC19 for $self->{call}") if isdbg('investigate');
99 foreach my $pc (@{$self->{pcxx}}) {
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);
115 dbg("Investigate: Storing (". join(',', @_) . ")") if isdbg('investigate');
116 push @{$self->{pcxx}}, [@_];
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');
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');
133 my $user = DXUser->get_current($v->{via});
135 $user->lastping($v->{via}, $main::systime);
147 my $name = $AUTOLOAD;
148 return if $name =~ /::DESTROY$/;
151 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
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}};