3 # Database Handler module for DXSpider
5 # Copyright (c) 1999 Dirk Koopman G1TLH
18 use vars qw($opentime $dbbase %avail %valid $lastprocesstime $nextstream %stream);
20 $opentime = 5*60; # length of time a database stays open after last access
21 $dbbase = "$main::root/db"; # where all the databases are kept;
22 %avail = (); # The hash contains a list of all the databases
24 accesst => '9,Last Access Time,atime',
25 createt => '9,Create Time,atime',
26 lastt => '9,Last Update Time,atime',
28 db => '9,DB Tied hash',
29 remote => '0,Remote Database',
30 pre => '0,Heading text',
31 post => '0,Tail text',
32 chain => '0,Search these,parray',
33 disable => '0,Disabled?,yesno',
34 nf => '0,Not Found text',
35 cal => '0,No Key text',
36 allowread => '9,Allowed to read,parray',
37 denyread => '9,Deny to read,parray',
38 allowupd => '9,Allow to update,parray',
39 denyupd => '9,Deny to update,parray',
40 fwdupd => '9,Forward updates to,parray',
41 template => '9,Upd Templates,parray',
42 help => '0,Help txt,parray',
45 $lastprocesstime = time;
49 # allocate a new stream for this request
53 my $n = ++$nextstream;
54 $stream{$n} = { n=>$n, call=>$call, t=>$main::systime };
72 # load all the database descriptors
75 my $s = readfilestr($dbbase, "dbs", "pl");
83 # save all the database descriptors
87 writefilestr($dbbase, "dbs", "pl", \%avail);
90 # get the descriptor of the database you want.
93 return undef unless %avail;
96 my $r = $avail{$name};
98 # search for a partial if not found direct
100 for (values %avail) {
101 if ($_->{name} =~ /^$name/) {
114 $self->{accesst} = $main::systime;
115 return $self->{db} if $self->{db};
117 $self->{db} = tie %hash, 'DB_File', "$dbbase/$self->{name}";
136 for (values %avail) {
142 # get a value from the database
149 # make sure we are open
152 my $s = $self->{db}->get($key, $value);
153 return $s ? undef : $value;
158 # put a value to the database
165 # make sure we are open
168 my $s = $self->{db}->put($key, $value);
169 return $s ? undef : 1;
174 # create a new database params: <name> [<remote node call>]
181 $self->{name} = lc $name;
182 $self->{remote} = uc $remote if $remote;
183 $self->{chain} = $chain if $chain && ref $chain;
184 $self->{accesst} = $self->{createt} = $self->{lastt} = $main::systime;
185 $avail{$self->{name}} = $self;
186 mkdir $dbbase, 02775 unless -e $dbbase;
195 unlink "$dbbase/$self->{name}";
196 delete $avail{$self->{name}};
201 # process intermediate lines for an update
202 # NOTE THAT THIS WILL BE CALLED FROM DXCommandmode and the
203 # object will be a DXChannel (actually DXCommandmode)
211 # periodic maintenance
213 # just close any things that haven't been accessed for the default
219 my ($dxchan, $line) = @_;
221 # this is periodic processing
222 if (!$dxchan || !$line) {
223 if ($main::systime - $lastprocesstime >= 60) {
225 for (values %avail) {
226 if ($main::systime - $_->{accesst} > $opentime) {
231 $lastprocesstime = $main::systime;
236 my @f = split /\^/, $line;
237 my ($pcno) = $f[0] =~ /^PC(\d\d)/; # just get the number
239 # route out ones that are not for us
240 if ($f[1] eq $main::mycall) {
243 $dxchan->route($f[1], $line);
248 if ($pcno == 37) { # probably obsolete
252 if ($pcno == 44) { # incoming DB Request
253 my $db = getdesc($f[4]);
256 sendremote($dxchan, $f[2], $f[3], $dxchan->msg('dx1', $db->{remote}));
258 my $value = $db->getkey($f[5]);
260 my @out = split /\n/, $value;
261 sendremote($dxchan, $f[2], $f[3], @out);
263 sendremote($dxchan, $f[2], $f[3], $dxchan->msg('dx2', $f[5], $db->{name}));
267 sendremote($dxchan, $f[2], $f[3], $dxchan->msg('dx3', $f[4]));
272 if ($pcno == 45) { # incoming DB Information
273 my $n = getstream($f[3]);
275 my $mchan = DXChannel->get($n->{call});
276 $mchan->send($f[2] . ":$f[4]") if $mchan;
281 if ($pcno == 46) { # incoming DB Complete
286 if ($pcno == 47) { # incoming DB Update request
290 if ($pcno == 48) { # incoming DB Update request
296 # send back a trache of data to the remote
297 # remember $dxchan is a dxchannel
305 $dxchan->send(DXProt::pc45($main::mycall, $tonode, $stream, $_));
307 $dxchan->send(DXProt::pc46($main::mycall, $tonode, $stream));
310 # print a value from the db reference
315 return $self->{$s} ? $self->{$s} : undef;
318 # various access routines
321 # return a list of valid elements
330 # return a prompt for a field
335 my ($self, $ele) = @_;
343 my $name = $AUTOLOAD;
344 return if $name =~ /::DESTROY$/;
347 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
348 @_ ? $self->{$name} = shift : $self->{$name} ;