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);
33 unless ($thing->{Aranea}) {
34 $thing->{Aranea} = Aranea::genmsg($thing, qw(id out o));
36 return $thing->{Aranea};
50 unless ($thing->{DXProt}) {
51 # we need to tease out the nodes out of all of this.
52 # bear in mind that a proxied PC prot node could be in
53 # {user} as well as a true user and also it may not
54 # have originated here.
56 my $from = $thing->{o};
57 $from ||= $thing->{user} if Route::Node::get($thing->{user});
58 $from ||= $thing->{origin};
59 my $to = $thing->{touser} if Route::Node::get($thing->{touser});
60 $to ||= $thing->{group};
63 $thing->{DXProt} = DXProt::pc51($to, $from, $thing->{out});
65 return $thing->{DXProt};
79 my $thing = ref $_[0] ? shift : $_[0]->SUPER::new();
94 if ($thing->{group} eq $main::mycall) {
95 if ($thing->{out} == 1) {
96 my $repthing = $thing->new_reply;
98 $repthing->{id} = $thing->{id};
99 $repthing->{o} = $thing->{o} if $thing->{o};
100 $repthing->send($dxchan) if $repthing;
103 # it's a reply, look in the ping list for this one
104 my $ref = $ping{$thing->{id}} if exists $thing->{id};
105 $ref ||= find(($thing->{user}||$thing->{origin}), ($thing->{touser}||$thing->{group}));
107 my $t = tv_interval($ref->{t}, [ gettimeofday ]);
108 my $tochan = DXChannel::get($ref->{touser} || $ref->{group});
110 my $nopings = $tochan->user->nopings || $DXProt::obscount;
111 push @{$tochan->{pingtime}}, $t;
112 shift @{$tochan->{pingtime}} if @{$tochan->{pingtime}} > 6;
114 # cope with a missed ping, this means you must set the pingint large enough
115 if ($t > $tochan->{pingint} && $t < 2 * $tochan->{pingint} ) {
116 $t -= $tochan->{pingint};
119 # calc smoothed RTT a la TCP
120 if (@{$tochan->{pingtime}} == 1) {
121 $tochan->{pingave} = $t;
123 $tochan->{pingave} = $tochan->{pingave} + (($t - $tochan->{pingave}) / 6);
125 $tochan->{nopings} = $nopings; # pump up the timer
127 if (my $dxc = DXChannel::get($ref->{user} || $ref->{origin})) {
129 my $s = sprintf "%.2f", $t;
130 my $ave = sprintf "%.2f", $tochan ? ($tochan->{pingave} || $t) : $t;
131 $dxc->send($dxc->msg('pingi', ($ref->{touser} || $ref->{group}), $s, $ave))
134 delete $ping{$ref->{id}};
138 $thing->{route} = $thing->{o} if $thing->{o};
139 $thing->broadcast($dxchan);
143 # this just creates a ping for onward transmission
144 # remember it if you want to ping someone from here
148 my $thing = $pkg->SUPER::new(@_);
151 # do this for pings we generate ourselves
155 $thing->{t} = [ gettimeofday ];
157 $thing->{id} = ++$id;
158 my $u = DXUser->get_current($thing->{to});
160 $u->lastping(($thing->{user} || $thing->{group}), $main::systime);
166 # remove any pings outstanding that we have remembered for this
167 # callsign, return the number of forgotten pings
173 foreach my $thing (values %ping) {
174 if (($thing->{user} || $thing->{group}) eq $call) {
176 delete $ping{$thing->{id}};
188 foreach my $thing (values %ping) {
189 if ($thing->{origin} eq $from && $thing->{group} eq $to) {
191 return if $thing->{user} && $thing->{user} eq $user;