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->send($dxchan) if $repthing;
102 # it's a reply, look in the ping list for this one
103 my $ref = $ping{$thing->{id}} if exists $thing->{id};
104 $ref ||= find(($thing->{user}||$thing->{origin}), ($thing->{touser}||$thing->{group}));
106 my $t = tv_interval($ref->{t}, [ gettimeofday ]);
107 my $tochan = DXChannel::get($ref->{touser} || $ref->{group});
109 my $nopings = $tochan->user->nopings || $DXProt::obscount;
110 push @{$tochan->{pingtime}}, $t;
111 shift @{$tochan->{pingtime}} if @{$tochan->{pingtime}} > 6;
113 # cope with a missed ping, this means you must set the pingint large enough
114 if ($t > $tochan->{pingint} && $t < 2 * $tochan->{pingint} ) {
115 $t -= $tochan->{pingint};
118 # calc smoothed RTT a la TCP
119 if (@{$tochan->{pingtime}} == 1) {
120 $tochan->{pingave} = $t;
122 $tochan->{pingave} = $tochan->{pingave} + (($t - $tochan->{pingave}) / 6);
124 $tochan->{nopings} = $nopings; # pump up the timer
126 if (my $dxc = DXChannel::get($ref->{user} || $ref->{origin})) {
128 my $s = sprintf "%.2f", $t;
129 my $ave = sprintf "%.2f", $tochan ? ($tochan->{pingave} || $t) : $t;
130 $dxc->send($dxc->msg('pingi', ($ref->{touser} || $ref->{group}), $s, $ave))
133 delete $ping{$ref->{id}};
137 $thing->{route} = $thing->{o} if $thing->{o};
138 $thing->broadcast($dxchan);
142 # this just creates a ping for onward transmission
143 # remember it if you want to ping someone from here
147 my $thing = $pkg->SUPER::new(@_);
150 # do this for pings we generate ourselves
154 $thing->{t} = [ gettimeofday ];
156 $thing->{id} = ++$id;
157 my $u = DXUser->get_current($thing->{to});
159 $u->lastping(($thing->{user} || $thing->{group}), $main::systime);
165 # remove any pings outstanding that we have remembered for this
166 # callsign, return the number of forgotten pings
172 foreach my $thing (values %ping) {
173 if (($thing->{user} || $thing->{group}) eq $call) {
175 delete $ping{$thing->{id}};
187 foreach my $thing (values %ping) {
188 if ($thing->{origin} eq $from && $thing->{group} eq $to) {
190 return if $thing->{user} && $thing->{user} eq $user;