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