10f20f28714f3a73d988fce6812b95cec8462947
[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         if ($thing->can($sub)) {
78                 return $thing->$sub($dxchan);
79         }
80         
81         # generate the protocol line which may (or not) be cached
82         my $ref;
83         unless ($ref = $thing->{class}) {
84                 $sub = "gen_$class";
85                 $ref = $thing->$sub($dxchan) if $thing->can($sub);
86         }
87         $dxchan->send(ref $ref ? @$ref : $ref) if $ref;
88
89         # after send
90         if ($thing->can('after_send_all')) {
91                 $thing->after_send_all($dxchan);
92         } else {
93                 $sub = "after_send_$class";
94                 $thing->$sub($dxchan) if $thing->can($sub);
95         }
96 }
97
98 # broadcast to all except @_
99 sub broadcast
100 {
101         my $thing = shift;
102         dbg("Thingy::broadcast: " . $thing->ascii) if isdbg('thing'); 
103
104         foreach my $dxchan (DXChannel::get_all()) {
105                 next if $dxchan == $main::me;
106                 next if grep $dxchan == $_, @_;
107                 $thing->send($dxchan); 
108         }
109 }
110
111 # queue this thing for processing
112 sub queue
113 {
114         my $thing = shift;
115         my $dxchan = shift;
116         $thing->{dxchan} = $dxchan->call;
117         push @queue, $thing;
118 }
119
120 #
121 # this is the main commutator loop. In due course it will
122 # become the *only* commutator loop, This can be called in one
123 # of two ways: either with 2 args or with none.
124 #
125 # The two arg form is an immediate "queue and handle" and does
126 # a full cycle, immediately
127 #
128 sub process
129 {
130         my $thing;
131         if (@_ == 2) {
132                 $thing = shift;
133                 $thing->queue(shift);
134         }
135         while (@queue) {
136                 $thing = shift @queue;
137                 my $dxchan = DXChannel::get($thing->{dxchan});
138                 if ($dxchan) {
139                         if ($thing->can('in_filter')) {
140                                 next unless $thing->in_filter($dxchan);
141                         }
142
143                         # remember any useful routes
144                         RouteDB::update($thing->{origin}, $dxchan->{call}, $thing->{hopsaway});
145                         RouteDB::update($thing->{user}, $dxchan->{call}, $thing->{hopsaway}) if exists $thing->{user};
146                 
147                         $thing->handle($dxchan);
148                 }
149         }
150
151         # per second and per minute processing
152         if ($main::systime != $lastsec) {
153                 if ($main::systime >= $lastmin+60) {
154                         foreach my $r (@permin) {
155                                 &{$r->[0]}();
156                         }
157                         $lastmin = $main::systime;
158                 }
159                 foreach my $r (@persec) {
160                         &{$r->[0]}();
161                 }
162                 $lastsec = $main::systime;
163         }
164 }
165
166 sub add_minute_process
167 {
168         my $pkg = shift;
169         my $addr = shift;
170         my $name = shift;
171         dbg('Adding $name to Thingy per minute queue');
172         push @permin, [$addr, $name];
173 }
174
175 sub add_second_process
176 {
177         my $pkg = shift;
178         my $addr = shift;
179         my $name = shift;
180         dbg('Adding $name to Thingy per second queue');
181         push @persec, [$addr, $name];
182 }
183
184
185 sub ascii
186 {
187         my $thing = shift;
188         my $dd = new Data::Dumper([$thing]);
189         $dd->Indent(0);
190         $dd->Terse(1);
191         $dd->Sortkeys(1);
192     $dd->Quotekeys($] < 5.005 ? 1 : 0);
193         return $dd->Dumpxs;
194 }
195
196 sub add_auth
197 {
198         my $thing = shift;
199         my $s = $thing->{'s'} = sprintf "%X", int(rand() * 100000000);
200         my $auth = Verify->new("DXSp,$main::mycall,$s,$main::version,$main::build");
201         $thing->{auth} = $auth->challenge($main::me->user->passphrase);
202 }
203
204 1;
205