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
48 $thing->{origin} ||= $main::mycall;
54 # send it out in the format asked for, if available
64 } elsif ($dxchan->isa('DXChannel')) {
72 if ($thing->can('out_filter')) {
73 return unless $thing->out_filter($dxchan);
76 # before send (and line generation) things
77 # function must return true to make the send happen
78 $sub = "before_send_$class";
79 if ($thing->can($sub)) {
80 return $thing->$sub($dxchan);
83 # generate the protocol line which may (or not) be cached
85 unless ($ref = $thing->{class}) {
87 $ref = $thing->$sub($dxchan) if $thing->can($sub);
89 $dxchan->send(ref $ref ? @$ref : $ref) if $ref;
92 if ($thing->can('after_send_all')) {
93 $thing->after_send_all($dxchan);
95 $sub = "after_send_$class";
96 $thing->$sub($dxchan) if $thing->can($sub);
101 # This is the main routing engine for the new protocol. Broadcast is a slight
102 # misnomer, because if it thinks it can route it down one or interfaces, it will.
104 # It handles anything it recognises as a callsign, sees if it can find it in a
105 # routing table, and if it does, then routes the message.
107 # If it can't then it will broadcast it.
112 dbg("Thingy::broadcast: " . $thing->ascii) if isdbg('thing');
115 my $to ||= $thing->{touser};
116 $to ||= $thing->{group};
117 if ($to && is_callsign($to) && (my $ref = Route::get($to))) {
118 dbg("Thingy::broadcast: routing for $to") if isdbg('thing');
119 @dxchan = $ref->alldxchan;
121 @dxchan = DXChannel::get_all();
124 dbg("Thingy::broadcast: offered " . join(',', map {$_->call} @dxchan)) if isdbg('thing');
126 foreach my $dxchan (@dxchan) {
127 next if $dxchan == $main::me;
128 next if grep $dxchan == $_, @_;
129 next if $dxchan->{call} eq $thing->{origin};
130 next if $thing->{user} && !$dxchan->is_user && $dxchan->{call} eq $thing->{user};
132 dbg("Thingy::broadcast: sending to $dxchan->{call}") if isdbg('thing');
133 $thing->send($dxchan);
137 # queue this thing for processing
142 $thing->{dxchan} = $dxchan->call;
147 # this is the main commutator loop. In due course it will
148 # become the *only* commutator loop, This can be called in one
149 # of two ways: either with 2 args or with none.
151 # The two arg form is an immediate "queue and handle" and does
152 # a full cycle, immediately
160 $thing->queue(shift);
164 $thing = shift @queue;
165 my $dxchan = DXChannel::get($thing->{dxchan});
167 if ($thing->can('in_filter')) {
168 next unless $thing->in_filter($dxchan);
171 # remember any useful routes
172 RouteDB::update($thing->{origin}, $dxchan->{call}, $thing->{hopsaway});
173 RouteDB::update($thing->{user}, $dxchan->{call}, $thing->{hopsaway}) if exists $thing->{user};
175 $thing->handle($dxchan);
179 # per second and per minute processing
180 if ($main::systime != $lastsec) {
181 if ($main::systime >= $lastmin+60) {
182 foreach my $r (@permin) {
185 $lastmin = $main::systime;
187 foreach my $r (@persec) {
190 $lastsec = $main::systime;
194 sub add_minute_process
199 dbg('Adding $name to Thingy per minute queue');
200 push @permin, [$addr, $name];
203 sub add_second_process
208 dbg('Adding $name to Thingy per second queue');
209 push @persec, [$addr, $name];
216 my $dd = new Data::Dumper([$thing]);
220 $dd->Quotekeys($] < 5.005 ? 1 : 0);
227 my $s = $thing->{'s'} = sprintf "%X", int(rand() * 100000000);
228 my $auth = Verify->new("DXSp,$main::mycall,$s,$thing->{v},$thing->{b}");
229 $thing->{auth} = $auth->challenge($main::me->user->passphrase);
233 # create a generalised reply to a passed thing, if it isn't replyable
234 # to then undef is returned
241 if ($thing->{group} eq $main::mycall) {
243 $out->{touser} = $thing->{user} if $thing->{user};
244 } elsif (DXChannel::get($thing->{group})) {
245 $out = $thing->new(user => $thing->{group});
246 $out->{touser} = $thing->{user} if $thing->{user};
247 } elsif ($thing->{touser} && DXChannel::get($thing->{touser})) {
248 $out = $thing->new(user => $thing->{touser});
249 $out->{group} = $thing->{group};