try and fix routing table errors
[spider.git] / perl / Thingy / Rt.pm
1 #
2 # Route Thingy handling
3 #
4 # Note that this is a generator of pc(16|17|19|21)n and pc(16|17)u
5 # and a consumer of the fpc versions of the above
6 #
7 # $Id$
8 #
9 # Copyright (c) 2005 Dirk Koopman G1TLH
10 #
11
12 use strict;
13
14 package Thingy::Rt;
15
16 use vars qw($VERSION $BRANCH);
17
18 main::mkver($VERSION = q$Revision$);
19
20 use DXChannel;
21 use DXDebug;
22 use DXUtil;
23 use Thingy;
24 use Thingy::RouteFilter;
25 use Spot;
26
27 use vars qw(@ISA);
28 @ISA = qw(Thingy Thingy::RouteFilter);
29
30 sub gen_Aranea
31 {
32         my $thing = shift;
33         my $dxchan = shift;
34         
35         unless ($thing->{Aranea}) {
36                 my $ref;
37                 if ($ref = $thing->{anodes}) {
38                         $thing->{a} = join(':', map {"$_->{flags}$_->{call}"} @$ref) || '';
39                 }
40                 if ($ref = $thing->{pnodes}) {
41                         $thing->{n} = join(':', map {"$_->{flags}$_->{call}"} @$ref) || '';
42                 }
43                 if ($ref = $thing->{ausers}) {
44                         $thing->{u} = join(':', map {"$_->{flags}$_->{call}"} @$ref) || '';
45                 }
46                 $thing->{Aranea} = Aranea::genmsg($thing, [qw(s a n u)]);
47         }
48         
49         return $thing->{Aranea};
50 }
51
52 sub from_Aranea
53 {
54         my $thing = shift;
55         $thing->{u} ||= '';
56         $thing->{n} ||= '';
57         $thing->{a} ||= '';
58         return $thing;
59 }
60
61 sub handle
62 {
63         my $thing = shift;
64         my $dxchan = shift;
65
66         if ($thing->{'s'}) {
67                 my $sub = "handle_$thing->{s}";
68                 if ($thing->can($sub)) {
69                         no strict 'refs';
70                         $thing = $thing->$sub($dxchan);
71                 }
72
73                 $thing->broadcast($dxchan) if $thing;
74         }
75 }
76
77 # this handles the standard local configuration, it 
78 # will reset all the config, make / break links and
79 # will generate pc sentences as required for nodes and users
80 sub handle_cf
81 {
82         my $thing = shift;
83         my $dxchan = shift;
84         my $origin = $thing->{user} || $thing->{origin};
85         my $chan_call = $dxchan->{call};
86         
87         my $parent = Route::Node::get($origin);
88         unless ($parent) {
89                 dbg("Thingy::Rt::cf: received from $thing->{origin}/$origin on $chan_call unknown") if isdbg('chanerr');
90                 return;
91         }
92
93         # do nodes
94         my %in;
95         if ($thing->{n}) {
96                 for (split(/:/, $thing->{n})) {
97                         my ($here, $call) = unpack("A1 A*", $_);
98                         unless ($call eq $main::mycall) {
99                                 $in{$call} = $here;
100                         }
101                 }
102         }
103         if ($thing->{a}) {
104                 for (split(/:/, $thing->{a})) {
105                         my ($here, $call) = unpack("A1 A*", $_); 
106                         unless ($call eq $main::mycall) {
107                                 $in{$call} = $here;
108                         }
109                 } 
110         }
111         my ($del, $add) = $parent->diff_nodes(keys %in);
112         if ($add) {
113                 my @pc21;
114                 foreach my $call (@$del) {
115                         RouteDB::delete($call, $chan_call);
116                         my $ref = Route::Node::get($call);
117                         push @pc21, $ref->del($parent) if $ref;
118                 }
119                 $thing->{pc21n} = \@pc21 if @pc21;
120         }
121         if ($del) {
122                 my @pc19;
123                 foreach my $call (@$add) {
124                         RouteDB::update($call, $chan_call);
125                         my $here = $in{$call};
126                         push @pc19, $parent->add($call, 0, $here);
127                 }
128                 $thing->{pc19n} = \@pc19 if @pc19;
129         }
130         
131         # now users
132         if ($thing->{u}) {
133                 %in = (map {my ($here, $call) = unpack "A1 A*", $_; ($call, $here)} split /:/, $thing->{u});
134                 ($del, $add) = $parent->diff_users(keys %in);
135
136                 my $call;
137
138                 my @pc17;
139                 foreach $call (@$del) {
140                         RouteDB::delete($call, $chan_call);
141                         my $ref = Route::User::get($call);
142                         if ($ref) {
143                                 $parent->del_user($ref);
144                                 push @pc17, $ref;
145                         } else {
146                                 dbg("Thingy::Rt::lcf: del user $call not known, ignored") if isdbg('chanerr');
147                                 next;
148                         }
149                 }
150                 if (@pc17) {
151                         $thing->{pc17n} = $parent;
152                         $thing->{pc17u} = \@pc17;
153                 }
154         
155                 my @pc16;
156                 foreach $call (@$add) {
157                         RouteDB::update($call, $chan_call);
158                         push @pc16, _add_user($parent, $call, $in{$call});
159                 }
160                 if (@pc16) {
161                         $thing->{pc16n} = $parent;
162                         $thing->{pc16u} = \@pc16;
163                 }
164         }
165
166         return $thing;
167 }
168
169
170 sub _add_user
171 {
172         my $node = shift;
173         my $user = shift;
174         my $flag = shift;
175         
176         my @out = $node->add_user($user, $flag);
177         my $ur = _upd_user_rec($user, $node);
178         $ur->put;
179         return @out;
180 }
181
182 sub _upd_user_rec
183 {
184         my $call = shift;
185         my $parentcall = shift;
186         
187         # add this station to the user database, if required
188         my $user = DXUser->get_current($call);
189         $user = DXUser->new($call) if !$user;
190         $user->homenode($parentcall) if !$user->homenode;
191         $user->node($parentcall);
192         $user->lastin($main::systime) unless DXChannel::get($call);
193         return $user;
194 }
195
196 #
197 # Generate a configuration for onward broadcast
198
199 # Basically, this creates a thingy with list of nodes and users that
200 # are on this node. This the normal method of spreading this
201 # info whenever a node connects and also periodically.
202 #
203
204 sub new_cf
205 {
206         my $pkg = shift;
207         my $thing = $pkg->SUPER::new(@_);
208         
209         $thing->{'s'} = 'cf';
210
211         my @anodes;
212         my @pnodes;
213         my @users;
214         
215         foreach my $dxchan (DXChannel::get_all()) {
216                 next if $dxchan == $main::me;
217                 if ($dxchan->is_node) {
218                         my $ref = Route::Node::get($dxchan->{call});
219                         push @pnodes, $ref if $ref;
220                 } elsif ($dxchan->is_aranea) {
221                         my $ref = Route::Node::get($dxchan->{call});
222                         push @anodes, $ref if $ref;
223                 } else {
224                         my $ref = Route::User::get($dxchan->{call});
225                         push @users, $ref if $ref;
226                 }
227         }
228         $thing->{anodes} = \@anodes if @anodes;
229         $thing->{pnodes} = \@pnodes if @pnodes;
230         $thing->{ausers} = \@users if @users;
231         return $thing;
232 }
233
234
235 # copy out the PC16 data for a node into the
236 # pc16n and u slots if there are any users 
237 #
238 sub copy_pc16_data
239 {
240         my $thing = shift;
241         my $uref = shift;
242
243         $thing->{'s'} = 'cf';
244
245         my @u = map {Route::User::get($_)} $uref->users;
246         $thing->{ausers} = \@u if @u;
247         return @u;
248 }
249
250
251
252 1;