6 # Copyright (c) 2005 Dirk Koopman G1TLH
13 use vars qw($VERSION $BRANCH);
15 main::mkver($VERSION = q$Revision$);
22 use Time::HiRes qw(gettimeofday tv_interval);
25 use vars qw(@ISA %ping $ping_ttl);
29 $ping_ttl = 300; # default ping ttl
35 unless ($thing->{Aranea}) {
36 $thing->{Aranea} = Aranea::genmsg($thing, qw(id out o));
38 return $thing->{Aranea};
52 unless ($thing->{DXProt}) {
53 # we need to tease out the nodes out of all of this.
54 # bear in mind that a proxied PC prot node could be in
55 # {user} as well as a true user and also it may not
56 # have originated here.
58 my $from = $thing->{o};
59 $from ||= $thing->{user} if Route::Node::get($thing->{user});
60 $from ||= $thing->{origin};
61 my $to = $thing->{touser} if Route::Node::get($thing->{touser});
62 $to ||= $thing->{group};
65 $thing->{DXProt} = DXProt::pc51($to, $from, $thing->{out});
67 return $thing->{DXProt};
79 # called with the dxchan, line and the split out arguments
82 my $thing = ref $_[0] ? shift : $_[0]->SUPER::new(origin=>$main::mycall);
84 $thing->{DXProt} = shift;
85 $thing->{group} = shift; # to call
87 $thing->{out} = shift; # 1 = ping, 0 = pong;
88 $thing->{user} = $dxchan->{call};
89 $thing->{o} = $from unless $from eq $dxchan->{call};
90 $thing->remember if $thing->{out};
100 if ($thing->{group} eq $main::mycall) {
101 if ($thing->{out} == 1) {
102 my $repthing = $thing->new_reply;
103 $repthing->{out} = 0;
104 $repthing->{id} = $thing->{id};
105 $repthing->{o} = $thing->{o} if $thing->{o};
106 $repthing->send($dxchan) if $repthing;
109 # it's a reply, look in the ping list for this one
110 my $ref = $ping{$thing->{id}} if exists $thing->{id};
111 $ref ||= find(($thing->{user}||$thing->{origin}), ($thing->{touser}||$thing->{group}));
113 my $t = tv_interval($ref->{t}, [ gettimeofday ]);
114 my $tochan = DXChannel::get($ref->{touser} || $ref->{group});
116 my $nopings = $tochan->user->nopings || $DXProt::obscount;
117 push @{$tochan->{pingtime}}, $t;
118 shift @{$tochan->{pingtime}} if @{$tochan->{pingtime}} > 6;
120 # cope with a missed ping, this means you must set the pingint large enough
121 if ($t > $tochan->{pingint} && $t < 2 * $tochan->{pingint} ) {
122 $t -= $tochan->{pingint};
125 # calc smoothed RTT a la TCP
126 if (@{$tochan->{pingtime}} == 1) {
127 $tochan->{pingave} = $t;
129 $tochan->{pingave} = $tochan->{pingave} + (($t - $tochan->{pingave}) / 6);
131 $tochan->{nopings} = $nopings; # pump up the timer
133 if (my $dxc = DXChannel::get($ref->{user} || $ref->{origin})) {
135 my $s = sprintf "%.2f", $t;
136 my $ave = sprintf "%.2f", $tochan ? ($tochan->{pingave} || $t) : $t;
137 $dxc->send($dxc->msg('pingi', ($ref->{touser} || $ref->{group}), $s, $ave))
140 delete $ping{$ref->{id}};
144 $thing->{route} = $thing->{o} if $thing->{o};
145 $thing->broadcast($dxchan);
149 # this just creates a ping for onward transmission
150 # remember it if you want to ping someone from here
154 my $thing = $pkg->SUPER::new(@_);
157 # do this for pings we generate ourselves
161 $thing->{t} = [ gettimeofday ];
163 $thing->{id} = ++$id;
164 my $u = DXUser->get_current($thing->{to});
166 $u->lastping(($thing->{user} || $thing->{group}), $main::systime);
172 # remove any pings outstanding that we have remembered for this
173 # callsign, return the number of forgotten pings
179 foreach my $thing (values %ping) {
180 if (($thing->{user} || $thing->{group}) eq $call) {
182 delete $ping{$thing->{id}};
194 foreach my $thing (values %ping) {
195 if ($thing->{origin} eq $from && $thing->{group} eq $to) {
197 return if $thing->{user} && $thing->{user} eq $user;