add interval processing
[spider.git] / perl / Thingy.pm
1 #
2 # Thingy handling
3 #
4 # This is the new fundamental protocol engine handler
5
6 # This is where all the new things (and eventually all the old things
7 # as well) happen.
8 #
9 # $Id$
10 #
11 # Copyright (c) 2004 Dirk Koopman G1TLH
12 #
13
14 use strict;
15
16 package Thingy;
17
18 use vars qw($VERSION $BRANCH @queue @permin @persec);
19
20 main::mkver($VERSION = q$Revision$);
21
22 @queue = ();                                    # the input / processing queue
23
24 #
25 # these are set up using the Thingy->add_second_process($addr, $name)
26 # and Thingy->add_minute_process($addr, $name)
27 #
28 # They replace the old cycle in cluster.pl
29 #
30
31 @persec = ();                                   # this replaces the cycle in cluster.pl
32 @permin = ();                                   # this is an extra per minute cycle
33
34 my $lastsec = time;
35 my $lastmin = time;
36
37 use DXChannel;
38 use DXDebug;
39
40 # we expect all thingies to be subclassed
41 sub new
42 {
43         my $class = shift;
44         my $thing = {@_};
45
46         $thing->{origin} ||= $main::mycall;
47         
48         bless $thing, $class;
49         return $thing;
50 }
51
52 # send it out in the format asked for, if available
53 sub send
54 {
55         my $thing = shift;
56         my $dxchan = shift;
57         my $class;
58         my $sub;
59         
60         if (@_) {
61                 $class = shift;
62         } elsif ($dxchan->isa('DXChannel')) {
63                 $class = ref $dxchan;
64         }
65
66         # BEWARE!!!!!
67         no strict 'refs';
68
69         # do output filtering
70         if ($thing->can('out_filter')) {
71                 return unless $thing->out_filter($dxchan);
72         }
73
74         # before send (and line generation) things
75         # function must return true to make the send happen
76         $sub = "before_send_$class";
77         return unless $thing->can($sub) && $thing->$sub($dxchan);
78         
79         # generate the protocol line which may (or not) be cached
80         my $ref;
81         unless ($ref = $thing->{class}) {
82                 $sub = "gen_$class";
83                 $ref = $thing->$sub($dxchan) if $thing->can($sub);
84         }
85         $dxchan->send(ref $ref ? @$ref : $ref) if $ref;
86
87         # after send
88         if ($thing->can('after_send_all')) {
89                 $thing->after_send_all($dxchan);
90         } else {
91                 $sub = "after_send_$class";
92                 $thing->$sub($dxchan) if $thing->can($sub);
93         }
94 }
95
96 # broadcast to all except @_
97 sub broadcast
98 {
99         my $thing = shift;
100         dbg("Thingy::broadcast: " . $thing->ascii) if isdbg('thing'); 
101
102         foreach my $dxchan (DXChannel::get_all()) {
103                 next if $dxchan == $main::me;
104                 next if grep $dxchan == $_, @_;
105                 $thing->send($dxchan); 
106         }
107 }
108
109 # queue this thing for processing
110 sub queue
111 {
112         my $thing = shift;
113         my $dxchan = shift;
114         $thing->{dxchan} = $dxchan->call;
115         push @queue, $thing;
116 }
117
118 #
119 # this is the main commutator loop. In due course it will
120 # become the *only* commutator loop, This can be called in one
121 # of two ways: either with 2 args or with none.
122 #
123 # The two arg form is an immediate "queue and handle" and does
124 # a full cycle, immediately
125 #
126 sub process
127 {
128         my $thing;
129         if (@_ == 2) {
130                 $thing = shift;
131                 $thing->queue(shift);
132         }
133         while (@queue) {
134                 $thing = shift @queue;
135                 my $dxchan = DXChannel::get($thing->{dxchan});
136                 if ($dxchan) {
137                         if ($thing->can('in_filter')) {
138                                 next unless $thing->in_filter($dxchan);
139                         }
140
141                         # remember any useful routes
142                         RouteDB::update($thing->{origin}, $dxchan->{call}, $thing->{hopsaway});
143                         RouteDB::update($thing->{user}, $dxchan->{call}, $thing->{hopsaway}) if exists $thing->{user};
144                 
145                         $thing->handle($dxchan);
146                 }
147         }
148
149         # per second and per minute processing
150         if ($main::systime != $lastsec) {
151                 if ($main::systime >= $lastmin+60) {
152                         foreach my $r (@permin) {
153                                 &{$r->[0]}();
154                         }
155                         $lastmin = $main::systime;
156                 }
157                 foreach my $r (@persec) {
158                         &{$r->[0]}();
159                 }
160                 $lastsec = $main::systime;
161         }
162 }
163
164 sub add_minute_process
165 {
166         my $pkg = shift;
167         my $addr = shift;
168         my $name = shift;
169         dbg('Adding $name to Thingy per minute queue');
170         push @permin, [$addr, $name];
171 }
172
173 sub add_second_process
174 {
175         my $pkg = shift;
176         my $addr = shift;
177         my $name = shift;
178         dbg('Adding $name to Thingy per second queue');
179         push @persec, [$addr, $name];
180 }
181
182
183 sub ascii
184 {
185         my $thing = shift;
186         my $dd = new Data::Dumper([$thing]);
187         $dd->Indent(0);
188         $dd->Terse(1);
189         $dd->Sortkeys(1);
190     $dd->Quotekeys($] < 5.005 ? 1 : 0);
191         return $dd->Dumpxs;
192 }
193
194 sub add_auth
195 {
196         my $thing = shift;
197         my $s = $thing->{'s'} = sprintf "%X", int(rand() * 100000000);
198         my $auth = Verify->new("DXSp,$main::mycall,$s,$main::version,$main::build");
199         $thing->{auth} = $auth->challenge($main::me->user->passphrase);
200 }
201
202 1;
203