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