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