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