51ecedea2bb6575c9d58220cba96011e696cd9e4
[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(%list %valid @ISA $max $filterdef);
18 @ISA = qw(Route);
19
20 %valid = (
21                   parent => '0,Parent Calls,parray',
22                   nodes => '0,Nodes,parray',
23                   users => '0,Users,parray',
24                   usercount => '0,User Count',
25                   version => '0,Version',
26 );
27
28 $filterdef = $Route::filterdef;
29 %list = ();
30 $max = 0;
31
32 sub count
33 {
34         my $n = scalar (keys %list);
35         $max = $n if $n > $max;
36         return $n;
37 }
38
39 sub max
40 {
41         return $max;
42 }
43
44 #
45 # this routine handles the possible adding of an entry in the routing
46 # table. It will only add an entry if it is new. It may have all sorts of
47 # other side effects which may include fixing up other links.
48 #
49 # It will return a node object if (and only if) it is a completely new
50 # object with that callsign. The upper layers are expected to do something
51 # sensible with this!
52 #
53 # called as $parent->add(call, dxchan, version, flags) 
54 #
55
56 sub add
57 {
58         my $parent = shift;
59         my $call = uc shift;
60         confess "Route::add trying to add $call to myself" if $call eq $parent->{call};
61         my $self = get($call);
62         if ($self) {
63                 $self->_addparent($parent->{call});
64                 $parent->_addnode($call);
65                 return undef;
66         }
67         $parent->_addnode($call);
68         $self = $parent->new($call, @_);
69         return $self;
70 }
71
72 #
73 # this routine is the opposite of 'add' above.
74 #
75 # It will return an object if (and only if) this 'del' will remove
76 # this object completely
77 #
78
79 sub del
80 {
81         my $self = shift;
82         my $pref = shift;
83
84         # delete parent from this call's parent list
85         my $pcall = $pref->{call};
86         my $ncall = $self->{call};
87         $pref->_delnode($ncall);;
88         my $ref = $self->_delparent($pcall);
89         my @nodes;
90         
91         # is this the last connection, I have no parents anymore?
92         unless (@$ref) {
93                 foreach my $rcall (@{$self->{nodes}}) {
94                         next if grep $rcall eq $_, @_;
95                         my $r = Route::Node::get($rcall);
96                         push @nodes, $r->del($self, $ncall, @_) if $r;
97                 }
98                 $self->_del_users;
99                 delete $list{$self->{call}};
100                 push @nodes, $self;
101         }
102         return @nodes;
103 }
104
105 sub del_nodes
106 {
107         my $parent = shift;
108         my @out;
109         foreach my $rcall (@{$parent->{nodes}}) {
110                 my $r = get($rcall);
111                 push @out, $r->del($parent, $parent->{call}, @_) if $r;
112         }
113         return @out;
114 }
115
116 sub _del_users
117 {
118         my $self = shift;
119         for (@{$self->{users}}) {
120                 my $ref = Route::User::get($_);
121                 $ref->del($self) if $ref;
122         }
123         $self->{users} = [];
124 }
125
126 # add a user to this node
127 sub add_user
128 {
129         my $self = shift;
130         my $ucall = shift;
131
132         confess "Trying to add NULL User call to routing tables" unless $ucall;
133
134         $self->_adduser($ucall);
135
136         $self->{usercount} = scalar @{$self->{users}};
137         my $uref = Route::User::get($ucall);
138         my @out = (Route::User->new($ucall, $self->{call}, @_)) unless $uref;
139         return @out;
140 }
141
142 # delete a user from this node
143 sub del_user
144 {
145         my $self = shift;
146         my $ucall = shift;
147         my $ref = Route::User::get($ucall);
148         $self->_deluser($ucall);
149         my @out = $ref->del($self) if $ref;
150         return @out;
151 }
152
153 sub usercount
154 {
155         my $self = shift;
156         if (@_ && @{$self->{users}} == 0) {
157                 $self->{usercount} = shift;
158         }
159         return $self->{usercount};
160 }
161
162 sub users
163 {
164         my $self = shift;
165         return @{$self->{users}};
166 }
167
168 sub nodes
169 {
170         my $self = shift;
171         return @{$self->{nodes}};
172 }
173
174 sub rnodes
175 {
176         my $self = shift;
177         my @out;
178         foreach my $call (@{$self->{nodes}}) {
179                 next if grep $call eq $_, @_;
180                 push @out, $call;
181                 my $r = get($call);
182                 push @out, $r->rnodes($call, @_) if $r;
183         }
184         return @out;
185 }
186
187
188 sub new
189 {
190         my $pkg = shift;
191         my $call = uc shift;
192         
193         confess "already have $call in $pkg" if $list{$call};
194         
195         my $self = $pkg->SUPER::new($call);
196         $self->{parent} = ref $pkg ? [ $pkg->{call} ] : [ ];
197         $self->{version} = shift;
198         $self->{flags} = shift;
199         $self->{users} = [];
200         $self->{nodes} = [];
201         
202         $list{$call} = $self;
203         
204         return $self;
205 }
206
207 sub get
208 {
209         my $call = shift;
210         $call = shift if ref $call;
211         my $ref = $list{uc $call};
212         dbg("Failed to get Node $call" ) if !$ref && isdbg('routerr');
213         return $ref;
214 }
215
216 sub get_all
217 {
218         return values %list;
219 }
220
221 sub _addparent
222 {
223         my $self = shift;
224     return $self->_addlist('parent', @_);
225 }
226
227 sub _delparent
228 {
229         my $self = shift;
230     return $self->_dellist('parent', @_);
231 }
232
233
234 sub _addnode
235 {
236         my $self = shift;
237     return $self->_addlist('nodes', @_);
238 }
239
240 sub _delnode
241 {
242         my $self = shift;
243     return $self->_dellist('nodes', @_);
244 }
245
246
247 sub _adduser
248 {
249         my $self = shift;
250     return $self->_addlist('users', @_);
251 }
252
253 sub _deluser
254 {
255         my $self = shift;
256     return $self->_dellist('users', @_);
257 }
258
259 sub DESTROY
260 {
261         my $self = shift;
262         my $pkg = ref $self;
263         my $call = $self->{call} || "Unknown";
264         
265         dbg("destroying $pkg with $call") if isdbg('routelow');
266 }
267
268 #
269 # generic AUTOLOAD for accessors
270 #
271
272 sub AUTOLOAD
273 {
274         no strict;
275
276         my $self = shift;
277         $name = $AUTOLOAD;
278         return if $name =~ /::DESTROY$/;
279         $name =~ s/.*:://o;
280   
281         confess "Non-existant field '$AUTOLOAD'" unless $valid{$name} || $Route::valid{$name};
282
283         # this clever line of code creates a subroutine which takes over from autoload
284         # from OO Perl - Conway
285 #       print "AUTOLOAD: $AUTOLOAD\n";
286 #       *{$AUTOLOAD} = sub {my $self = shift; @_ ? $self->{$name} = shift : $self->{$name}} ;
287     @_ ? $self->{$name} = shift : $self->{$name} ;
288 }
289
290 1;
291