67e32a00eac1197a581599b308a935dd7f86f32b
[spider.git] / perl / Route / Node.pm
1 #
2 # Node routing routines
3 #
4 # Copyright (c) 2001 Dirk Koopman G1TLH
5 #
6 # $Id$
7
8
9 package Route::Node;
10
11 use DXDebug;
12 use Route;
13 use Route::User;
14
15 use strict;
16
17 use vars qw($VERSION $BRANCH);
18 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
19 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/  || (0,0));
20 $main::build += $VERSION;
21 $main::branch += $BRANCH;
22
23 use vars qw(%list %valid @ISA $max $filterdef);
24 @ISA = qw(Route);
25
26 %valid = (
27                   parent => '0,Parent Calls,parray',
28                   nodes => '0,Nodes,parray',
29                   users => '0,Users,parray',
30                   usercount => '0,User Count',
31                   version => '0,Version',
32                   handle_xml => '0,Using XML,yesno',
33                   lastmsg => '0,Last Route Msg,atime',
34                   lastid => '0,Last Route MsgID',
35 );
36
37 $filterdef = $Route::filterdef;
38 %list = ();
39 $max = 0;
40
41 sub count
42 {
43         my $n = scalar (keys %list);
44         $max = $n if $n > $max;
45         return $n;
46 }
47
48 sub max
49 {
50         count();
51         return $max;
52 }
53
54 #
55 # this routine handles the possible adding of an entry in the routing
56 # table. It will only add an entry if it is new. It may have all sorts of
57 # other side effects which may include fixing up other links.
58 #
59 # It will return a node object if (and only if) it is a completely new
60 # object with that callsign. The upper layers are expected to do something
61 # sensible with this!
62 #
63 # called as $parent->add(call, dxchan, version, flags) 
64 #
65
66 sub add
67 {
68         my $parent = shift;
69         my $call = uc shift;
70         confess "Route::add trying to add $call to myself" if $call eq $parent->{call};
71         my $self = get($call);
72         if ($self) {
73                 $self->_addparent($parent);
74                 $parent->_addnode($self);
75                 return undef;
76         }
77         $self = $parent->new($call, @_);
78         $parent->_addnode($self);
79         return $self;
80 }
81
82 #
83 # this routine is the opposite of 'add' above.
84 #
85 # It will return an object if (and only if) this 'del' will remove
86 # this object completely
87 #
88
89 sub del
90 {
91         my $self = shift;
92         my $pref = shift;
93
94         # delete parent from this call's parent list
95         $pref->_delnode($self);
96     $self->_delparent($pref);
97         my @nodes;
98         my $ncall = $self->{call};
99         
100         # is this the last connection, I have no parents anymore?
101         unless (@{$self->{parent}}) {
102                 foreach my $rcall (@{$self->{nodes}}) {
103                         next if grep $rcall eq $_, @_;
104                         my $r = Route::Node::get($rcall);
105                         push @nodes, $r->del($self, $ncall, @_) if $r;
106                 }
107                 $self->_del_users;
108                 delete $list{$self->{call}};
109                 push @nodes, $self;
110         }
111         return @nodes;
112 }
113
114 sub del_nodes
115 {
116         my $parent = shift;
117         my @out;
118         foreach my $rcall (@{$parent->{nodes}}) {
119                 my $r = get($rcall);
120                 push @out, $r->del($parent, $parent->{call}, @_) if $r;
121         }
122         return @out;
123 }
124
125 sub _del_users
126 {
127         my $self = shift;
128         for (@{$self->{users}}) {
129                 my $ref = Route::User::get($_);
130                 $ref->del($self) if $ref;
131         }
132         $self->{users} = [];
133 }
134
135 # add a user to this node
136 sub add_user
137 {
138         my $self = shift;
139         my $ucall = shift;
140
141         confess "Trying to add NULL User call to routing tables" unless $ucall;
142
143         my $uref = Route::User::get($ucall);
144         my @out;
145         if ($uref) {
146                 @out = $uref->addparent($self);
147         } else {
148                 $uref = Route::User->new($ucall, $self->{call}, @_);
149                 @out = $uref;
150         }
151         $self->_adduser($uref);
152         $self->{usercount} = scalar @{$self->{users}};
153
154         return @out;
155 }
156
157 # delete a user from this node
158 sub del_user
159 {
160         my $self = shift;
161         my $ref = shift;
162         my @out;
163         
164         if ($ref) {
165                 @out = $self->_deluser($ref);
166                 $ref->del($self);
167         } else {
168                 confess "tried to delete non-existant $ref->{call} from $self->{call}";
169         }
170         $self->{usercount} = scalar @{$self->{users}};
171         return @out;
172 }
173
174 sub usercount
175 {
176         my $self = shift;
177         if (@_ && @{$self->{users}} == 0) {
178                 $self->{usercount} = shift;
179         }
180         return $self->{usercount};
181 }
182
183 sub users
184 {
185         my $self = shift;
186         return @{$self->{users}};
187 }
188
189 sub nodes
190 {
191         my $self = shift;
192         return @{$self->{nodes}};
193 }
194
195 sub parents
196 {
197         my $self = shift;
198         return @{$self->{parent}};
199 }
200
201 sub rnodes
202 {
203         my $self = shift;
204         my @out;
205         foreach my $call (@{$self->{nodes}}) {
206                 next if grep $call eq $_, @_;
207                 push @out, $call;
208                 my $r = get($call);
209                 push @out, $r->rnodes($call, @_) if $r;
210         }
211         return @out;
212 }
213
214 # this takes in a list of node and user calls (not references) from 
215 # a config type update for a node and returns
216 # the differences as lists of things that have gone away
217 # and things that have been added. 
218 sub calc_config_changes
219 {
220         my $self = shift;
221         my %nodes = map {$_ => 1} @{$self->{nodes}};
222         my %users = map {$_ => 1} @{$self->{users}};
223         my $cnodes = shift;
224         my $cusers = shift;
225         my (@dnodes, @dusers, @nnodes, @nusers);
226         push @nnodes, map {my @r = $nodes{$_} ? () : $_; delete $nodes{$_}; @r} @$cnodes;
227         push @dnodes, keys %nodes;
228         push @nusers, map {my @r = $users{$_} ? () : $_; delete $users{$_}; @r} @$cusers;
229         push @dusers, keys %users;
230         return (\@dnodes, \@dusers, \@nnodes, \@nusers);
231 }
232
233 sub new
234 {
235         my $pkg = shift;
236         my $call = uc shift;
237         
238         confess "already have $call in $pkg" if $list{$call};
239         
240         my $self = $pkg->SUPER::new($call);
241         $self->{parent} = ref $pkg ? [ $pkg->{call} ] : [ ];
242         $self->{version} = shift || 5000;
243         $self->{flags} = shift || Route::here(1);
244         $self->{users} = [];
245         $self->{nodes} = [];
246         $self->{lastid} = {};
247         
248         $list{$call} = $self;
249         
250         return $self;
251 }
252
253 sub get
254 {
255         my $call = shift;
256         $call = shift if ref $call;
257         my $ref = $list{uc $call};
258         dbg("Failed to get Node $call" ) if !$ref && isdbg('routerr');
259         return $ref;
260 }
261
262 sub get_all
263 {
264         return values %list;
265 }
266
267 sub _addparent
268 {
269         my $self = shift;
270     return $self->_addlist('parent', @_);
271 }
272
273 sub _delparent
274 {
275         my $self = shift;
276     return $self->_dellist('parent', @_);
277 }
278
279
280 sub _addnode
281 {
282         my $self = shift;
283     return $self->_addlist('nodes', @_);
284 }
285
286 sub _delnode
287 {
288         my $self = shift;
289     return $self->_dellist('nodes', @_);
290 }
291
292
293 sub _adduser
294 {
295         my $self = shift;
296     return $self->_addlist('users', @_);
297 }
298
299 sub _deluser
300 {
301         my $self = shift;
302     return $self->_dellist('users', @_);
303 }
304
305 sub DESTROY
306 {
307         my $self = shift;
308         my $pkg = ref $self;
309         my $call = $self->{call} || "Unknown";
310         
311         dbg("destroying $pkg with $call") if isdbg('routelow');
312 }
313
314 #
315 # generic AUTOLOAD for accessors
316 #
317
318 sub AUTOLOAD
319 {
320         no strict;
321         my $name = $AUTOLOAD;
322         return if $name =~ /::DESTROY$/;
323         $name =~ s/^.*:://o;
324   
325         confess "Non-existant field '$AUTOLOAD'" unless $valid{$name} || $Route::valid{$name};
326
327         # this clever line of code creates a subroutine which takes over from autoload
328         # from OO Perl - Conway
329         *$AUTOLOAD = sub {$_[0]->{$name} = $_[1] if @_ > 1; return $_[0]->{$name}};
330         goto &$AUTOLOAD;
331 }
332
333 1;
334