do it properly!
[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->{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 $origin on $chan_call unknown") if isdbg('chanerr');
90                 return;
91         }
92         $parent->np(1);
93         
94         my @pc19;
95         my @pc21;
96
97         # move the origin over to the user, if required
98         if ($thing->{user}) {
99                 $origin = $thing->{user};
100                 my $ref = Route::Node::get($origin);
101                 if ($ref) {
102                         $parent = $ref;
103                 } else {
104                         # auto vivify a node that has come that we don't know about
105                         push @pc19, $parent->add($origin, 0, 1);
106                         $parent = Route::Node::get($origin); # reparent to me now.
107                 }
108                 $parent->np(1);
109         }
110
111         # do nodes
112         my %in;
113         if ($thing->{n}) {
114                 for (split(/:/, $thing->{n})) {
115                         my ($here, $call) = unpack("A1 A*", $_);
116                         next if $call eq $main::mycall;
117                         $in{$call} = $here;
118                 }
119         }
120         if ($thing->{a}) {
121                 for (split(/:/, $thing->{a})) {
122                         my ($here, $call) = unpack("A1 A*", $_); 
123                         next if $call eq $main::mycall;
124                         $in{$call} = $here;
125                 } 
126         }
127         my ($del, $add) = $parent->diff_nodes(keys %in);
128         if ($del) {
129                 foreach my $call (@$del) {
130                         next if $call eq $main::mycall;
131                         RouteDB::delete($call, $chan_call);
132                         my $ref = Route::Node::get($call);
133                         push @pc21, $ref->del($parent) if $ref;
134                 }
135         }
136         if ($add) {
137                 foreach my $call (@$add) {
138                         next if $call eq $main::mycall;
139                         RouteDB::update($call, $chan_call);
140                         my $here = $in{$call};
141                         push @pc19, $parent->add($call, 0, $here);
142                 }
143         }
144         $thing->{pc21n} = \@pc21 if @pc21;
145         $thing->{pc19n} = \@pc19 if @pc19;
146         
147         # now users
148         if ($thing->{u}) {
149                 %in = (map {my ($here, $call) = unpack "A1 A*", $_; ($call, $here)} split /:/, $thing->{u});
150                 ($del, $add) = $parent->diff_users(keys %in);
151
152                 my $call;
153
154                 my @pc17;
155                 foreach $call (@$del) {
156                         RouteDB::delete($call, $chan_call);
157                         my $ref = Route::User::get($call);
158                         if ($ref) {
159                                 $parent->del_user($ref);
160                                 push @pc17, $ref;
161                         } else {
162                                 dbg("Thingy::Rt::lcf: del user $call not known, ignored") if isdbg('chanerr');
163                                 next;
164                         }
165                 }
166                 if (@pc17) {
167                         $thing->{pc17n} = $parent;
168                         $thing->{pc17u} = \@pc17;
169                 }
170         
171                 my @pc16;
172                 foreach $call (@$add) {
173                         RouteDB::update($call, $chan_call);
174                         push @pc16, _add_user($parent, $call, $in{$call});
175                 }
176                 if (@pc16) {
177                         $thing->{pc16n} = $parent;
178                         $thing->{pc16u} = \@pc16;
179                 }
180         }
181
182         return $thing;
183 }
184
185
186 sub _add_user
187 {
188         my $node = shift;
189         my $user = shift;
190         my $flag = shift;
191         
192         my @out = $node->add_user($user, $flag);
193         my $ur = _upd_user_rec($user, $node);
194         $ur->put;
195         return @out;
196 }
197
198 sub _upd_user_rec
199 {
200         my $call = shift;
201         my $parentcall = shift;
202         
203         # add this station to the user database, if required
204         my $user = DXUser->get_current($call);
205         $user = DXUser->new($call) if !$user;
206         $user->homenode($parentcall) if !$user->homenode;
207         $user->node($parentcall);
208         $user->lastin($main::systime) unless DXChannel::get($call);
209         return $user;
210 }
211
212 #
213 # Generate a configuration for onward broadcast
214
215 # Basically, this creates a thingy with list of nodes and users that
216 # are on this node. This the normal method of spreading this
217 # info whenever a node connects and also periodically.
218 #
219
220 sub new_cf
221 {
222         my $pkg = shift;
223         my $thing = $pkg->SUPER::new(@_);
224         
225         $thing->{'s'} = 'cf';
226
227         my @anodes;
228         my @pnodes;
229         my @users;
230         
231         foreach my $dxchan (DXChannel::get_all()) {
232                 next if $dxchan == $main::me;
233                 if ($dxchan->is_node) {
234                         my $ref = Route::Node::get($dxchan->{call});
235                         push @pnodes, $ref if $ref;
236                 } elsif ($dxchan->is_aranea) {
237                         my $ref = Route::Node::get($dxchan->{call});
238                         push @anodes, $ref if $ref;
239                 } else {
240                         my $ref = Route::User::get($dxchan->{call});
241                         push @users, $ref if $ref;
242                 }
243         }
244         $thing->{anodes} = \@anodes if @anodes;
245         $thing->{pnodes} = \@pnodes if @pnodes;
246         $thing->{ausers} = \@users if @users;
247         return $thing;
248 }
249
250
251 # copy out the PC16 data for a node into the
252 # pc16n and u slots if there are any users 
253 #
254 sub copy_pc16_data
255 {
256         my $thing = shift;
257         my $uref = shift;
258
259         $thing->{'s'} = 'cf';
260
261         my @u = map {Route::User::get($_)} $uref->users;
262         $thing->{ausers} = \@u if @u;
263         return @u;
264 }
265
266
267
268 1;