*** empty log message ***
[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                   np => '0,Using New Prot,yesno',
33                   lid => '0,Last Msgid',
34 );
35
36 $filterdef = $Route::filterdef;
37 %list = ();
38 $max = 0;
39
40 sub count
41 {
42         my $n = scalar (keys %list);
43         $max = $n if $n > $max;
44         return $n;
45 }
46
47 sub max
48 {
49         count();
50         return $max;
51 }
52
53 #
54 # this routine handles the possible adding of an entry in the routing
55 # table. It will only add an entry if it is new. It may have all sorts of
56 # other side effects which may include fixing up other links.
57 #
58 # It will return a node object if (and only if) it is a completely new
59 # object with that callsign. The upper layers are expected to do something
60 # sensible with this!
61 #
62 # called as $parent->add(call, dxchan, version, flags) 
63 #
64
65 sub add
66 {
67         my $parent = shift;
68         my $call = uc shift;
69         confess "Route::add trying to add $call to myself" if $call eq $parent->{call};
70         my $self = get($call);
71         if ($self) {
72                 $self->_addparent($parent);
73                 $parent->_addnode($self);
74                 return undef;
75         }
76         $self = $parent->new($call, @_);
77         $self->register;
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                 $uref->register;
150                 @out = $uref;
151         }
152         $self->_adduser($uref);
153         $self->{usercount} = scalar @{$self->{users}};
154
155         return @out;
156 }
157
158 # delete a user from this node
159 sub del_user
160 {
161         my $self = shift;
162         my $ref = shift;
163         my @out;
164         
165         if ($ref) {
166                 @out = $self->_deluser($ref);
167                 $ref->del($self);
168         } else {
169                 confess "tried to delete non-existant $ref->{call} from $self->{call}";
170         }
171         $self->{usercount} = scalar @{$self->{users}};
172         return @out;
173 }
174
175 sub usercount
176 {
177         my $self = shift;
178         if (@_ && @{$self->{users}} == 0) {
179                 $self->{usercount} = shift;
180         }
181         return $self->{usercount};
182 }
183
184 sub users
185 {
186         my $self = shift;
187         return @{$self->{users}};
188 }
189
190 sub nodes
191 {
192         my $self = shift;
193         return @{$self->{nodes}};
194 }
195
196 sub parents
197 {
198         my $self = shift;
199         return @{$self->{parent}};
200 }
201
202 sub rnodes
203 {
204         my $self = shift;
205         my @out;
206         foreach my $call (@{$self->{nodes}}) {
207                 next if grep $call eq $_, @_;
208                 push @out, $call;
209                 my $r = get($call);
210                 push @out, $r->rnodes($call, @_) if $r;
211         }
212         return @out;
213 }
214
215
216 sub new
217 {
218         my $pkg = shift;
219         my $call = uc shift;
220         my $self = $pkg->SUPER::new($call);
221         $self->{parent} = ref $pkg ? [ $pkg->{call} ] : [ ];
222         $self->{version} = shift;
223         $self->{flags} = shift;
224         $self->{users} = [];
225         $self->{nodes} = [];
226         $self->{lid} = 0;
227         
228         return $self;
229 }
230
231 sub register
232 {
233         my $self = shift;
234         confess "already have $call in $pkg" if $list{$self->{call}};
235         $list{$call} = $self;
236 }
237
238 sub get
239 {
240         my $call = shift;
241         $call = shift if ref $call;
242         my $ref = $list{uc $call};
243         dbg("Failed to get Node $call" ) if !$ref && isdbg('routerr');
244         return $ref;
245 }
246
247 sub get_all
248 {
249         return values %list;
250 }
251
252 sub newid
253 {
254         my $self = shift;
255         my $id = shift;
256         
257         return 0 if $id == $self->{lid};
258         if ($id > $self->{lid}) {
259                 $self->{lid} = $id;
260                 return 1;
261         } elsif ($self->{lid} - $id > 60000) {
262                 $self->{id} = $id;
263                 return 1;
264         }
265         return 0;
266 }
267
268 sub _addparent
269 {
270         my $self = shift;
271     return $self->_addlist('parent', @_);
272 }
273
274 sub _delparent
275 {
276         my $self = shift;
277     return $self->_dellist('parent', @_);
278 }
279
280
281 sub _addnode
282 {
283         my $self = shift;
284     return $self->_addlist('nodes', @_);
285 }
286
287 sub _delnode
288 {
289         my $self = shift;
290     return $self->_dellist('nodes', @_);
291 }
292
293
294 sub _adduser
295 {
296         my $self = shift;
297     return $self->_addlist('users', @_);
298 }
299
300 sub _deluser
301 {
302         my $self = shift;
303     return $self->_dellist('users', @_);
304 }
305
306 sub DESTROY
307 {
308         my $self = shift;
309         my $pkg = ref $self;
310         my $call = $self->{call} || "Unknown";
311         
312         dbg("destroying $pkg with $call") if isdbg('routelow');
313 }
314
315 #
316 # generic AUTOLOAD for accessors
317 #
318
319 sub AUTOLOAD
320 {
321         no strict;
322
323         my $self = shift;
324         $name = $AUTOLOAD;
325         return if $name =~ /::DESTROY$/;
326         $name =~ s/.*:://o;
327   
328         confess "Non-existant field '$AUTOLOAD'" unless $valid{$name} || $Route::valid{$name};
329
330         *$AUTOLOAD = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}};
331         &$AUTOLOAD($self, @_);
332 }
333
334 1;
335