d3b1e955411833567a8b2551c55e19d1616b4136
[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
215 sub new
216 {
217         my $pkg = shift;
218         my $call = uc shift;
219         
220         confess "already have $call in $pkg" if $list{$call};
221         
222         my $self = $pkg->SUPER::new($call);
223         $self->{parent} = ref $pkg ? [ $pkg->{call} ] : [ ];
224         $self->{version} = shift;
225         $self->{flags} = shift;
226         $self->{users} = [];
227         $self->{nodes} = [];
228         
229         $list{$call} = $self;
230         
231         return $self;
232 }
233
234 sub get
235 {
236         my $call = shift;
237         $call = shift if ref $call;
238         my $ref = $list{uc $call};
239         dbg("Failed to get Node $call" ) if !$ref && isdbg('routerr');
240         return $ref;
241 }
242
243 sub get_all
244 {
245         return values %list;
246 }
247
248 sub _addparent
249 {
250         my $self = shift;
251     return $self->_addlist('parent', @_);
252 }
253
254 sub _delparent
255 {
256         my $self = shift;
257     return $self->_dellist('parent', @_);
258 }
259
260
261 sub _addnode
262 {
263         my $self = shift;
264     return $self->_addlist('nodes', @_);
265 }
266
267 sub _delnode
268 {
269         my $self = shift;
270     return $self->_dellist('nodes', @_);
271 }
272
273
274 sub _adduser
275 {
276         my $self = shift;
277     return $self->_addlist('users', @_);
278 }
279
280 sub _deluser
281 {
282         my $self = shift;
283     return $self->_dellist('users', @_);
284 }
285
286 sub DESTROY
287 {
288         my $self = shift;
289         my $pkg = ref $self;
290         my $call = $self->{call} || "Unknown";
291         
292         dbg("destroying $pkg with $call") if isdbg('routelow');
293 }
294
295 #
296 # generic AUTOLOAD for accessors
297 #
298
299 sub AUTOLOAD
300 {
301         no strict;
302         my $name = $AUTOLOAD;
303         return if $name =~ /::DESTROY$/;
304         $name =~ s/^.*:://o;
305   
306         confess "Non-existant field '$AUTOLOAD'" unless $valid{$name} || $Route::valid{$name};
307
308         # this clever line of code creates a subroutine which takes over from autoload
309         # from OO Perl - Conway
310         *$AUTOLOAD = sub {$_[0]->{$name} = $_[1] if @_ > 1; return $_[0]->{$name}};
311         goto &$AUTOLOAD;
312 }
313
314 1;
315