force a disconnect down all other interfaces if a node connects directly
[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 # this deletes this node completely by grabbing the parents
115 # and deleting me from them
116 sub delete
117 {
118         my $self = shift;
119         my @out;
120         
121         $self->_del_users;
122         foreach my $call (@{$self->{parent}}) {
123                 my $parent = Route::Node::get($call);
124                 push @out, $parent->del($self) if $parent;
125         }
126         return @out;
127 }
128
129 sub del_nodes
130 {
131         my $parent = shift;
132         my @out;
133         foreach my $rcall (@{$parent->{nodes}}) {
134                 my $r = get($rcall);
135                 push @out, $r->del($parent, $parent->{call}, @_) if $r;
136         }
137         return @out;
138 }
139
140 sub _del_users
141 {
142         my $self = shift;
143         for (@{$self->{users}}) {
144                 my $ref = Route::User::get($_);
145                 $ref->del($self) if $ref;
146         }
147         $self->{users} = [];
148 }
149
150 # add a user to this node
151 sub add_user
152 {
153         my $self = shift;
154         my $ucall = shift;
155
156         confess "Trying to add NULL User call to routing tables" unless $ucall;
157
158         my $uref = Route::User::get($ucall);
159         my @out;
160         if ($uref) {
161                 @out = $uref->addparent($self);
162         } else {
163                 $uref = Route::User->new($ucall, $self->{call}, @_);
164                 @out = $uref;
165         }
166         $self->_adduser($uref);
167         $self->{usercount} = scalar @{$self->{users}};
168
169         return @out;
170 }
171
172 # delete a user from this node
173 sub del_user
174 {
175         my $self = shift;
176         my $ref = shift;
177         my @out;
178         
179         if ($ref) {
180                 @out = $self->_deluser($ref);
181                 $ref->del($self);
182         } else {
183                 confess "tried to delete non-existant $ref->{call} from $self->{call}";
184         }
185         $self->{usercount} = scalar @{$self->{users}};
186         return @out;
187 }
188
189 sub usercount
190 {
191         my $self = shift;
192         if (@_ && @{$self->{users}} == 0) {
193                 $self->{usercount} = shift;
194         }
195         return $self->{usercount};
196 }
197
198 sub users
199 {
200         my $self = shift;
201         return @{$self->{users}};
202 }
203
204 sub nodes
205 {
206         my $self = shift;
207         return @{$self->{nodes}};
208 }
209
210 sub parents
211 {
212         my $self = shift;
213         return @{$self->{parent}};
214 }
215
216 sub rnodes
217 {
218         my $self = shift;
219         my @out;
220         foreach my $call (@{$self->{nodes}}) {
221                 next if grep $call eq $_, @_;
222                 push @out, $call;
223                 my $r = get($call);
224                 push @out, $r->rnodes($call, @_) if $r;
225         }
226         return @out;
227 }
228
229
230 sub new
231 {
232         my $pkg = shift;
233         my $call = uc shift;
234         
235         confess "already have $call in $pkg" if $list{$call};
236         
237         my $self = $pkg->SUPER::new($call);
238         $self->{parent} = ref $pkg ? [ $pkg->{call} ] : [ ];
239         $self->{version} = shift;
240         $self->{flags} = shift;
241         $self->{users} = [];
242         $self->{nodes} = [];
243         
244         $list{$call} = $self;
245         
246         return $self;
247 }
248
249 sub get
250 {
251         my $call = shift;
252         $call = shift if ref $call;
253         my $ref = $list{uc $call};
254         dbg("Failed to get Node $call" ) if !$ref && isdbg('routerr');
255         return $ref;
256 }
257
258 sub get_all
259 {
260         return values %list;
261 }
262
263 sub _addparent
264 {
265         my $self = shift;
266     return $self->_addlist('parent', @_);
267 }
268
269 sub _delparent
270 {
271         my $self = shift;
272     return $self->_dellist('parent', @_);
273 }
274
275
276 sub _addnode
277 {
278         my $self = shift;
279     return $self->_addlist('nodes', @_);
280 }
281
282 sub _delnode
283 {
284         my $self = shift;
285     return $self->_dellist('nodes', @_);
286 }
287
288
289 sub _adduser
290 {
291         my $self = shift;
292     return $self->_addlist('users', @_);
293 }
294
295 sub _deluser
296 {
297         my $self = shift;
298     return $self->_dellist('users', @_);
299 }
300
301 sub DESTROY
302 {
303         my $self = shift;
304         my $pkg = ref $self;
305         my $call = $self->{call} || "Unknown";
306         
307         dbg("destroying $pkg with $call") if isdbg('routelow');
308 }
309
310 #
311 # generic AUTOLOAD for accessors
312 #
313
314 sub AUTOLOAD
315 {
316         no strict;
317         my $name = $AUTOLOAD;
318         return if $name =~ /::DESTROY$/;
319         $name =~ s/^.*:://o;
320   
321         confess "Non-existant field '$AUTOLOAD'" unless $valid{$name} || $Route::valid{$name};
322
323         # this clever line of code creates a subroutine which takes over from autoload
324         # from OO Perl - Conway
325         *$AUTOLOAD = sub {$_[0]->{$name} = $_[1] if @_ > 1; return $_[0]->{$name}};
326         goto &$AUTOLOAD;
327 }
328
329 1;
330