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