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
40 # we expect all thingies to be subclassed
46 $thing->{origin} ||= $main::mycall;
52 # send it out in the format asked for, if available
62 } elsif ($dxchan->isa('DXChannel')) {
70 if ($thing->can('out_filter')) {
71 return unless $thing->out_filter($dxchan);
74 # before send (and line generation) things
75 # function must return true to make the send happen
76 $sub = "before_send_$class";
77 if ($thing->can($sub)) {
78 return $thing->$sub($dxchan);
81 # generate the protocol line which may (or not) be cached
83 unless ($ref = $thing->{class}) {
85 $ref = $thing->$sub($dxchan) if $thing->can($sub);
87 $dxchan->send(ref $ref ? @$ref : $ref) if $ref;
90 if ($thing->can('after_send_all')) {
91 $thing->after_send_all($dxchan);
93 $sub = "after_send_$class";
94 $thing->$sub($dxchan) if $thing->can($sub);
98 # broadcast to all except @_
102 dbg("Thingy::broadcast: " . $thing->ascii) if isdbg('thing');
104 foreach my $dxchan (DXChannel::get_all()) {
105 next if $dxchan == $main::me;
106 next if grep $dxchan == $_, @_;
107 next if $dxchan->{call} eq $thing->{origin};
108 next if $thing->{user} && !$dxchan->is_user && $dxchan->{call} eq $thing->{user};
110 dbg("Thingy::broadcast: sending to $dxchan->{call}") if isdbg('thing');
111 $thing->send($dxchan);
115 # queue this thing for processing
120 $thing->{dxchan} = $dxchan->call;
125 # this is the main commutator loop. In due course it will
126 # become the *only* commutator loop, This can be called in one
127 # of two ways: either with 2 args or with none.
129 # The two arg form is an immediate "queue and handle" and does
130 # a full cycle, immediately
138 $thing->queue(shift);
142 $thing = shift @queue;
143 my $dxchan = DXChannel::get($thing->{dxchan});
145 if ($thing->can('in_filter')) {
146 next unless $thing->in_filter($dxchan);
149 # remember any useful routes
150 RouteDB::update($thing->{origin}, $dxchan->{call}, $thing->{hopsaway});
151 RouteDB::update($thing->{user}, $dxchan->{call}, $thing->{hopsaway}) if exists $thing->{user};
153 $thing->handle($dxchan);
157 # per second and per minute processing
158 if ($main::systime != $lastsec) {
159 if ($main::systime >= $lastmin+60) {
160 foreach my $r (@permin) {
163 $lastmin = $main::systime;
165 foreach my $r (@persec) {
168 $lastsec = $main::systime;
172 sub add_minute_process
177 dbg('Adding $name to Thingy per minute queue');
178 push @permin, [$addr, $name];
181 sub add_second_process
186 dbg('Adding $name to Thingy per second queue');
187 push @persec, [$addr, $name];
194 my $dd = new Data::Dumper([$thing]);
198 $dd->Quotekeys($] < 5.005 ? 1 : 0);
205 my $s = $thing->{'s'} = sprintf "%X", int(rand() * 100000000);
206 my $auth = Verify->new("DXSp,$main::mycall,$s,$thing->{v},$thing->{b}");
207 $thing->{auth} = $auth->challenge($main::me->user->passphrase);
211 # create a generalised reply to a passed thing, if it isn't replyable
212 # to then undef is returned
219 if ($thing->{group} eq $main::mycall) {
221 $out->{touser} = $thing->{user} if $thing->{user};
222 } elsif (DXChannel::get($thing->{group})) {
223 $out = $thing->new(user => $thing->{group});
224 $out->{touser} = $thing->{user} if $thing->{user};
225 } elsif ($thing->{touser} && DXChannel->{$thing->{touser}}) {
226 $out = $thing->new(user => $thing->{touser});
227 $out->{group} = $thing->{group};