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));
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->{user} if Route::Node::get($thing->{user});
57 $from ||= $thing->{origin};
58 my $to = $thing->{touser} if Route::Node::get($thing->{touser});
59 $to ||= $thing->{group};
61 $thing->{DXProt} = DXProt::pc51($to, $from, $thing->{out});
63 return $thing->{DXProt};
77 my $thing = ref $_[0] ? shift : $_[0]->SUPER::new();
92 if ($thing->{group} eq $main::mycall) {
93 if ($thing->{out} == 1) {
94 my $repthing = $thing->new_reply;
96 $repthing->{id} = $thing->{id};
97 $repthing->send($dxchan) if $repthing;
100 # it's a reply, look in the ping list for this one
101 my $ref = $ping{$thing->{id}} if exists $thing->{id};
102 $ref ||= find(($thing->{user}||$thing->{origin}), ($thing->{touser}||$thing->{group}));
104 my $t = tv_interval($ref->{t}, [ gettimeofday ]);
105 my $tochan = DXChannel::get($ref->{touser} || $ref->{group});
107 my $nopings = $tochan->user->nopings || $DXProt::obscount;
108 push @{$tochan->{pingtime}}, $t;
109 shift @{$tochan->{pingtime}} if @{$tochan->{pingtime}} > 6;
111 # cope with a missed ping, this means you must set the pingint large enough
112 if ($t > $tochan->{pingint} && $t < 2 * $tochan->{pingint} ) {
113 $t -= $tochan->{pingint};
116 # calc smoothed RTT a la TCP
117 if (@{$tochan->{pingtime}} == 1) {
118 $tochan->{pingave} = $t;
120 $tochan->{pingave} = $tochan->{pingave} + (($t - $tochan->{pingave}) / 6);
122 $tochan->{nopings} = $nopings; # pump up the timer
124 if (my $dxc = DXChannel::get($ref->{user} || $ref->{origin})) {
126 my $s = sprintf "%.2f", $t;
127 my $ave = sprintf "%.2f", $tochan ? ($tochan->{pingave} || $t) : $t;
128 $dxc->send($dxc->msg('pingi', ($ref->{touser} || $ref->{group}), $s, $ave))
131 delete $ping{$ref->{id}};
135 $thing->broadcast($dxchan);
139 # this just creates a ping for onward transmission
140 # remember it if you want to ping someone from here
144 my $thing = $pkg->SUPER::new(@_);
147 # do this for pings we generate ourselves
151 $thing->{t} = [ gettimeofday ];
153 $thing->{id} = ++$id;
154 my $u = DXUser->get_current($thing->{to});
156 $u->lastping(($thing->{user} || $thing->{group}), $main::systime);
162 # remove any pings outstanding that we have remembered for this
163 # callsign, return the number of forgotten pings
169 foreach my $thing (values %ping) {
170 if (($thing->{user} || $thing->{group}) eq $call) {
172 delete $ping{$thing->{id}};
184 foreach my $thing (values %ping) {
185 if ($thing->{origin} eq $from && $thing->{group} eq $to) {
187 return if $thing->{user} && $thing->{user} eq $user;