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