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.
61 $from ||= $thing->{user} unless Route::User::get($thing->{user});
62 $from ||= $thing->{origin};
63 $to = $thing->{touser} unless Route::User::get($thing->{touser});
64 $to ||= $thing->{group};
66 $from ||= $thing->{user} unless Route::User::get($thing->{user});
67 $from ||= $thing->{origin};
69 $to ||= $thing->{touser} unless Route::User::get($thing->{touser});
70 $to ||= $thing->{group};
72 $thing->{DXProt} = DXProt::pc51($to, $from, $thing->{out});
74 return $thing->{DXProt};
82 if ($dxchan->{call} eq $thing->{touser}) {
83 $buf = $dxchan->msg('pingi', ($thing->{user} || $thing->{origin}), $thing->{ft}, $thing->{fave});
88 # called with the dxchan, line and the split out arguments
91 my $thing = ref $_[0] ? shift : $_[0]->SUPER::new(origin=>$main::mycall);
93 $thing->{DXProt} = shift;
95 $thing->{group} = shift; # to call
97 $thing->{out} = shift; # 1 = ping, 0 = pong;
98 $thing->{user} = $dxchan->{call};
99 $thing->{o} = $from unless $from eq $dxchan->{call};
100 $thing->remember if $thing->{out} && $thing->{group} ne $main::mycall;
110 if ($thing->{group} eq $main::mycall) {
111 if ($thing->{out} == 1) {
112 my $repthing = $thing->new_reply;
113 $repthing->{out} = 0;
114 $repthing->{id} = $thing->{id};
115 $repthing->{o} = $thing->{o} if $thing->{o};
116 $repthing->send($dxchan) if $repthing;
119 # it's a reply, look in the ping list for this one
121 $ref = $ping{$thing->{id}} if exists $thing->{id};
122 $ref = find(($thing->{user}||$thing->{origin}), ($thing->{touser}||$thing->{group})) unless $ref;
124 my $t = tv_interval($ref->{t}, [ gettimeofday ]);
125 my $tochan = DXChannel::get($ref->{touser} || $ref->{group});
127 my $nopings = $tochan->user->nopings || $DXProt::obscount;
128 push @{$tochan->{pingtime}}, $t;
129 shift @{$tochan->{pingtime}} if @{$tochan->{pingtime}} > 6;
131 # cope with a missed ping, this means you must set the pingint large enough
132 if ($t > $tochan->{pingint} && $t < 2 * $tochan->{pingint} ) {
133 $t -= $tochan->{pingint};
136 # calc smoothed RTT a la TCP
137 if (@{$tochan->{pingtime}} == 1) {
138 $tochan->{pingave} = $t;
140 $tochan->{pingave} = $tochan->{pingave} + (($t - $tochan->{pingave}) / 6);
142 $tochan->{nopings} = $nopings; # pump up the timer
144 if (my $dxc = DXChannel::get($ref->{user} || $ref->{origin})) {
145 $thing->{ft} = sprintf "%.2f", $t;
146 $thing->{fave} = sprintf "%.2f", $tochan ? ($tochan->{pingave} || $t) : $t;
149 delete $ping{$ref->{id}};
153 $thing->broadcast($dxchan);
157 # this just creates a ping for onward transmission
158 # remember it if you want to ping someone from here
162 my $thing = $pkg->SUPER::new(@_);
165 # do this for pings we generate ourselves
169 $thing->{t} = [ gettimeofday ];
171 $thing->{id} = ++$id;
172 my $u = DXUser->get_current($thing->{to});
174 $u->lastping(($thing->{user} || $thing->{group}), $main::systime);
180 # remove any pings outstanding that we have remembered for this
181 # callsign, return the number of forgotten pings
187 foreach my $thing (values %ping) {
188 if (($thing->{user} || $thing->{group}) eq $call) {
190 delete $ping{$thing->{id}};
202 foreach my $thing (values %ping) {
203 if ($thing->{origin} eq $from && $thing->{group} eq $to) {
205 return if $thing->{user} && $thing->{user} eq $user;