4 # This is the new fundamental protocol engine handler
6 # This is where all the new things (and eventually all the old things
11 # Copyright (c) 2004 Dirk Koopman G1TLH
18 use vars qw($VERSION $BRANCH @queue @permin @persec);
20 main::mkver($VERSION = q$Revision$);
22 @queue = (); # the input / processing queue
25 # these are set up using the Thingy->add_second_process($addr, $name)
26 # and Thingy->add_minute_process($addr, $name)
28 # They replace the old cycle in cluster.pl
31 @persec = (); # this replaces the cycle in cluster.pl
32 @permin = (); # this is an extra per minute cycle
42 # we expect all thingies to be subclassed
46 my $pkg = ref $class || $class;
49 $thing->{origin} ||= $main::mycall;
55 # send it out in the format asked for, if available
65 } elsif ($dxchan->isa('DXChannel')) {
73 if ($thing->can('out_filter')) {
74 return unless $thing->out_filter($dxchan);
77 # before send (and line generation) things
78 # function must return true to make the send happen
79 $sub = "before_send_$class";
80 if ($thing->can($sub)) {
81 return $thing->$sub($dxchan);
84 # generate the protocol line which may (or not) be cached
86 unless ($ref = $thing->{class}) {
88 $ref = $thing->$sub($dxchan) if $thing->can($sub);
90 $dxchan->send(ref $ref ? @$ref : $ref) if $ref;
93 if ($thing->can('after_send_all')) {
94 $thing->after_send_all($dxchan);
96 $sub = "after_send_$class";
97 $thing->$sub($dxchan) if $thing->can($sub);
102 # This is the main routing engine for the new protocol. Broadcast is a slight
103 # misnomer, because if it thinks it can route it down one or interfaces, it will.
105 # It handles anything it recognises as a callsign, sees if it can find it in a
106 # routing table, and if it does, then routes the message.
108 # If it can't then it will broadcast it.
113 dbg("Thingy::broadcast: " . $thing->ascii) if isdbg('thing');
116 my $to ||= $thing->{touser};
117 $to ||= $thing->{group};
118 if ($to && is_callsign($to) && (my $ref = Route::get($to))) {
119 dbg("Thingy::broadcast: routing for $to") if isdbg('thing');
120 @dxchan = $ref->alldxchan;
122 @dxchan = DXChannel::get_all();
125 dbg("Thingy::broadcast: offered " . join(',', map {$_->call} @dxchan)) if isdbg('thing');
127 foreach my $dxchan (@dxchan) {
128 next if $dxchan == $main::me;
129 next if grep $dxchan == $_, @_;
130 next if $dxchan->{call} eq $thing->{origin};
131 next if $thing->{user} && !$dxchan->is_user && $dxchan->{call} eq $thing->{user};
133 dbg("Thingy::broadcast: sending to $dxchan->{call}") if isdbg('thing');
134 $thing->send($dxchan);
138 # queue this thing for processing
143 $thing->{dxchan} = $dxchan->call;
148 # this is the main commutator loop. In due course it will
149 # become the *only* commutator loop, This can be called in one
150 # of two ways: either with 2 args or with none.
152 # The two arg form is an immediate "queue and handle" and does
153 # a full cycle, immediately
161 $thing->queue(shift);
165 $thing = shift @queue;
166 my $dxchan = DXChannel::get($thing->{dxchan});
168 if ($thing->can('in_filter')) {
169 next unless $thing->in_filter($dxchan);
172 # remember any useful routes
173 RouteDB::update($thing->{origin}, $dxchan->{call}, $thing->{hopsaway});
174 RouteDB::update($thing->{user}, $dxchan->{call}, $thing->{hopsaway}) if exists $thing->{user};
176 $thing->handle($dxchan);
180 # per second and per minute processing
181 if ($main::systime != $lastsec) {
182 if ($main::systime >= $lastmin+60) {
183 foreach my $r (@permin) {
186 $lastmin = $main::systime;
188 foreach my $r (@persec) {
191 $lastsec = $main::systime;
195 sub add_minute_process
200 dbg('Adding $name to Thingy per minute queue');
201 push @permin, [$addr, $name];
204 sub add_second_process
209 dbg('Adding $name to Thingy per second queue');
210 push @persec, [$addr, $name];
217 my $dd = new Data::Dumper([$thing]);
221 $dd->Quotekeys($] < 5.005 ? 1 : 0);
228 my $s = $thing->{'s'} = sprintf "%X", int(rand() * 100000000);
229 my $auth = Verify->new("DXSp,$main::mycall,$s,$thing->{v},$thing->{b}");
230 $thing->{auth} = $auth->challenge($main::me->user->passphrase);
234 # create a generalised reply to a passed thing, if it isn't replyable
235 # to then undef is returned
242 if ($thing->{group} eq $main::mycall) {
244 $out->{touser} = $thing->{user} if $thing->{user};
245 $out->{group} = $thing->{origin};
246 } elsif (DXChannel::get($thing->{group})) {
247 $out = $thing->new(user => $thing->{group});
248 $out->{touser} = $thing->{user} if $thing->{user};
249 $out->{group} = $thing->{origin};
250 } elsif ($thing->{touser} && DXChannel::get($thing->{touser})) {
251 $out = $thing->new(user => $thing->{touser});
252 $out->{group} = $thing->{group};