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