5242259c63f82ae5f19d3c5f93376cdbfb457b23
[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->{origin};
80         my $chan_call = $dxchan->{call};
81         
82         my $parent = Route::Node::get($origin);
83         unless ($parent) {
84                 dbg("Thingy::Rt::lcf: received from $origin on $chan_call unknown") if isdbg('chanerr');
85                 return;
86         }
87
88         # do nodes
89         my ($del, $add)
90         if ($thing->{n}) {
91                 my %in = (map {my ($here, $call) = unpack "A1 A*", $_; ($call, $here)} split /:/, $thing->{n});
92                 my ($tdel, $tadd) = $parent->diff_nodes(keys %in);
93                 $add = $tadd;
94                 $del = $tdel;
95         }
96         if ($thing->{a}) {
97                 my %in = (map {my ($here, $call) = unpack "A1 A*", $_; ($call, $here)} split /:/, $thing->{a});
98                 my ($tdel, $tadd) = $parent->diff_nodes(keys %in);
99                 push @$add, @$tadd;
100                 push @$del, @$tdel;
101         }
102         if (@$add || @$del) {
103                 my $call;
104
105                 my @pc21;
106                 foreach $call (@$del) {
107                         RouteDB::delete($call, $chan_call);
108                         my $ref = Route::Node::get($call);
109                         push @pc21, $ref->del($parent) if $ref;
110                 }
111                 $thing->{pc21n} = \@pc21 if @pc21;
112                 
113                 my @pc19;
114                 foreach $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                 my %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 sub _add_user
161 {
162         my $node = shift;
163         my $user = shift;
164         my $flag = shift;
165         
166         my @out = $node->add_user($user, $flag);
167         my $ur = _upd_user_rec($user, $node);
168         $ur->put;
169         return @out;
170 }
171
172 sub _upd_user_rec
173 {
174         my $call = shift;
175         my $parentcall = shift;
176         
177         # add this station to the user database, if required
178         my $user = DXUser->get_current($call);
179         $user = DXUser->new($call) if !$user;
180         $user->homenode($parentcall) if !$user->homenode;
181         $user->node($parentcall);
182         $user->lastin($main::systime) unless DXChannel::get($call);
183         return $user;
184 }
185
186 #
187 # Generate a configuration for onward broadcast
188
189 # Basically, this creates a thingy with list of nodes and users that
190 # are on this node. This the normal method of spreading this
191 # info whenever a node connects and also periodically.
192 #
193
194 sub new_lcf
195 {
196         my $pkg = shift;
197         my $thing = $pkg->SUPER::new(@_);
198         
199         $thing->{'s'} = 'cf';
200
201         my @anodes;
202         my @pnodes;
203         my @users;
204         
205         foreach my $dxchan (DXChannel::get_all()) {
206                 next if $dxchan == $main::me;
207                 if ($dxchan->is_node) {
208                         my $ref = Route::Node::get($dxchan->{call});
209                         push @pnodes, $ref if $ref;
210                 } elsif ($dxchan->is_aranea) {
211                         my $ref = Route::Node::get($dxchan->{call});
212                         push @anodes, $ref if $ref;
213                 } else {
214                         my $ref = Route::User::get($dxchan->{call});
215                         push @users, $ref if $ref;
216                 }
217         }
218         $thing->{anodes} = \@anodes if @anodes;
219         $thing->{pnodes} = \@pnodes if @pnodes;
220         $thing->{ausers} = \@users if @users;
221         return $thing;
222 }
223
224
225
226
227 1;