3 # Database Handler module for DXSpider
5 # Copyright (c) 1999 Dirk Koopman G1TLH
17 use vars qw($opentime $dbbase %avail %valid $lastprocesstime $nextstream %stream);
19 $opentime = 5*60; # length of time a database stays open after last access
20 $dbbase = "$main::root/db"; # where all the databases are kept;
21 %avail = (); # The hash contains a list of all the databases
23 accesst => '9,Last Accs Time,atime',
24 createt => '9,Create Time,atime',
25 lastt => '9,Last Upd Time,atime',
27 db => '9,DB Tied hash',
28 remote => '0,Remote Database',
29 pre => '0,Heading txt',
31 chain => '0,Search these,parray',
32 disable => '0,Disabled?,yesno',
33 nf => '0,Not Found txt',
34 cal => '0,No Key txt',
35 allowread => '9,Allowed read,parray',
36 denyread => '9,Deny read,parray',
37 allowupd => '9,Allow upd,parray',
38 denyupd => '9,Deny upd,parray',
39 fwdupd => '9,Forw upd to,parray',
40 template => '9,Upd Templates,parray',
41 te => '9,End Upd txt',
42 tae => '9,End App txt',
43 atemplate => '9,App Templates,parray',
44 help => '0,Help txt,parray',
47 $lastprocesstime = time;
51 # allocate a new stream for this request
55 my $n = ++$nextstream;
56 $stream{$n} = { n=>$n, call=>$call, t=>$main::systime };
74 # load all the database descriptors
77 my $s = readfilestr($dbbase, "dbs", "pl");
85 # save all the database descriptors
89 writefilestr($dbbase, "dbs", "pl", \%avail);
92 # get the descriptor of the database you want.
95 return undef unless %avail;
98 my $r = $avail{$name};
100 # search for a partial if not found direct
102 for (sort { $a->{name} cmp $b->{name} }values %avail) {
103 if ($_->{name} =~ /^$name/) {
116 $self->{accesst} = $main::systime;
117 return $self->{db} if $self->{db};
119 $self->{db} = tie %hash, 'DB_File', "$dbbase/$self->{name}";
138 for (values %avail) {
144 # get a value from the database
151 # make sure we are open
154 my $s = $self->{db}->get($key, $value);
155 return $s ? undef : $value;
160 # put a value to the database
167 # make sure we are open
170 my $s = $self->{db}->put($key, $value);
171 return $s ? undef : 1;
176 # create a new database params: <name> [<remote node call>]
183 $self->{name} = lc $name;
184 $self->{remote} = uc $remote if $remote;
185 $self->{chain} = $chain if $chain && ref $chain;
186 $self->{accesst} = $self->{createt} = $self->{lastt} = $main::systime;
187 $avail{$self->{name}} = $self;
188 mkdir $dbbase, 02775 unless -e $dbbase;
197 unlink "$dbbase/$self->{name}";
198 delete $avail{$self->{name}};
203 # process intermediate lines for an update
204 # NOTE THAT THIS WILL BE CALLED FROM DXCommandmode and the
205 # object will be a DXChannel (actually DXCommandmode)
213 # periodic maintenance
215 # just close any things that haven't been accessed for the default
221 my ($dxchan, $line) = @_;
223 # this is periodic processing
224 if (!$dxchan || !$line) {
225 if ($main::systime - $lastprocesstime >= 60) {
227 for (values %avail) {
228 if ($main::systime - $_->{accesst} > $opentime) {
233 $lastprocesstime = $main::systime;
238 my @f = split /\^/, $line;
239 my ($pcno) = $f[0] =~ /^PC(\d\d)/; # just get the number
241 # route out ones that are not for us
242 if ($f[1] eq $main::mycall) {
245 $dxchan->route($f[1], $line);
250 if ($pcno == 37) { # probably obsolete
254 if ($pcno == 44) { # incoming DB Request
255 my $db = getdesc($f[4]);
258 sendremote($dxchan, $f[2], $f[3], $dxchan->msg('dx1', $db->{remote}));
260 my $value = $db->getkey($f[5]);
262 my @out = split /\n/, $value;
263 sendremote($dxchan, $f[2], $f[3], @out);
265 sendremote($dxchan, $f[2], $f[3], $dxchan->msg('dx2', $f[5], $db->{name}));
269 sendremote($dxchan, $f[2], $f[3], $dxchan->msg('dx3', $f[4]));
274 if ($pcno == 45) { # incoming DB Information
275 my $n = getstream($f[3]);
277 my $mchan = DXChannel->get($n->{call});
278 $mchan->send($f[2] . ":$f[4]") if $mchan;
283 if ($pcno == 46) { # incoming DB Complete
288 if ($pcno == 47) { # incoming DB Update request
292 if ($pcno == 48) { # incoming DB Update request
298 # send back a trache of data to the remote
299 # remember $dxchan is a dxchannel
307 $dxchan->send(DXProt::pc45($main::mycall, $tonode, $stream, $_));
309 $dxchan->send(DXProt::pc46($main::mycall, $tonode, $stream));
312 # print a value from the db reference
317 return $self->{$s} ? $self->{$s} : undef;
320 # various access routines
323 # return a list of valid elements
332 # return a prompt for a field
337 my ($self, $ele) = @_;
345 my $name = $AUTOLOAD;
346 return if $name =~ /::DESTROY$/;
349 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
350 # this clever line of code creates a subroutine which takes over from autoload
351 # from OO Perl - Conway
352 *{$AUTOLOAD} = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}} ;
353 @_ ? $self->{$name} = shift : $self->{$name} ;