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 use vars qw($VERSION $BRANCH);
52 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
53 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/ || (0,0));
54 $main::build += $VERSION;
55 $main::branch += $BRANCH;
57 # allocate a new stream for this request
61 my $n = ++$nextstream;
62 $stream{$n} = { n=>$n, call=>$call, t=>$main::systime };
80 # load all the database descriptors
83 my $s = readfilestr($dbbase, "dbs", "pl");
88 %avail = ( %$a ) if ref $a;
92 # save all the database descriptors
96 writefilestr($dbbase, "dbs", "pl", \%avail);
99 # get the descriptor of the database you want.
102 return undef unless %avail;
105 my $r = $avail{$name};
107 # search for a partial if not found direct
109 for (sort { $a->{name} cmp $b->{name} }values %avail) {
110 if ($_->{name} =~ /^$name/) {
123 $self->{accesst} = $main::systime;
124 return $self->{db} if $self->{db};
126 $self->{db} = tie %hash, 'DB_File', "$dbbase/$self->{name}";
145 for (values %avail) {
151 # get a value from the database
158 # make sure we are open
161 my $s = $self->{db}->get($key, $value);
162 return $s ? undef : $value;
167 # put a value to the database
174 # make sure we are open
177 my $s = $self->{db}->put($key, $value);
178 return $s ? undef : 1;
183 # create a new database params: <name> [<remote node call>]
190 $self->{name} = lc $name;
191 $self->{remote} = uc $remote if $remote;
192 $self->{chain} = $chain if $chain && ref $chain;
193 $self->{accesst} = $self->{createt} = $self->{lastt} = $main::systime;
194 $avail{$self->{name}} = $self;
195 mkdir $dbbase, 02775 unless -e $dbbase;
204 unlink "$dbbase/$self->{name}";
205 delete $avail{$self->{name}};
210 # process intermediate lines for an update
211 # NOTE THAT THIS WILL BE CALLED FROM DXCommandmode and the
212 # object will be a DXChannel (actually DXCommandmode)
220 # periodic maintenance
222 # just close any things that haven't been accessed for the default
228 my ($dxchan, $line) = @_;
230 # this is periodic processing
231 if (!$dxchan || !$line) {
232 if ($main::systime - $lastprocesstime >= 60) {
234 for (values %avail) {
235 if ($main::systime - $_->{accesst} > $opentime) {
240 $lastprocesstime = $main::systime;
245 my @f = split /\^/, $line;
246 my ($pcno) = $f[0] =~ /^PC(\d\d)/; # just get the number
248 # route out ones that are not for us
249 if ($f[1] eq $main::mycall) {
252 $dxchan->route($f[1], $line);
257 if ($pcno == 37) { # probably obsolete
261 if ($pcno == 44) { # incoming DB Request
262 my $db = getdesc($f[4]);
265 sendremote($dxchan, $f[2], $f[3], $dxchan->msg('db1', $db->{remote}));
267 my $value = $db->getkey($f[5]);
269 my @out = split /\n/, $value;
270 sendremote($dxchan, $f[2], $f[3], @out);
272 sendremote($dxchan, $f[2], $f[3], $dxchan->msg('db2', $f[5], $db->{name}));
276 sendremote($dxchan, $f[2], $f[3], $dxchan->msg('db3', $f[4]));
281 if ($pcno == 45) { # incoming DB Information
282 my $n = getstream($f[3]);
284 my $mchan = DXChannel->get($n->{call});
285 $mchan->send($f[2] . ":$f[4]") if $mchan;
290 if ($pcno == 46) { # incoming DB Complete
295 if ($pcno == 47) { # incoming DB Update request
299 if ($pcno == 48) { # incoming DB Update request
305 # send back a trache of data to the remote
306 # remember $dxchan is a dxchannel
314 $dxchan->send(DXProt::pc45($main::mycall, $tonode, $stream, $_));
316 $dxchan->send(DXProt::pc46($main::mycall, $tonode, $stream));
319 # print a value from the db reference
324 return $self->{$s} ? $self->{$s} : undef;
327 # various access routines
330 # return a list of valid elements
339 # return a prompt for a field
344 my ($self, $ele) = @_;
352 my $name = $AUTOLOAD;
353 return if $name =~ /::DESTROY$/;
356 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
357 # this clever line of code creates a subroutine which takes over from autoload
358 # from OO Perl - Conway
359 *$AUTOLOAD = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}};