2 # User routing routines
4 # Copyright (c) 2001 Dirk Koopman G1TLH
15 use Time::HiRes qw(gettimeofday);
19 use vars qw(%list %valid @ISA $max $filterdef);
22 $filterdef = $Route::filterdef;
26 our $cachefn = localdata('route_user_cache');
30 my $n = scalar(keys %list);
31 $max = $n if $n > $max;
49 confess "already have $call in $pkg" if $list{$call};
51 my $self = $pkg->SUPER::new($call);
52 $self->{parent} = [ $ncall ];
53 $self->{flags} = $flags || Route::here(1);
54 $self->{ip} = $ip if defined $ip;
56 dbg("CLUSTER: user $call added") if isdbg('cluster');
70 my $call = $self->{call};
71 $self->delparent($pref);
72 unless (@{$self->{parent}}) {
74 dbg("CLUSTER: user $call deleted") if isdbg('cluster');
83 $call = shift if ref $call;
84 my $ref = $list{uc $call};
85 dbg("Failed to get User $call" ) if !$ref && isdbg('routerr');
92 return $self->_addlist('parent', @_);
98 return $self->_dellist('parent', @_);
101 sub TO_JSON { return { %{ shift() } }; }
105 my $json = DXJSON->new;
106 $json->canonical(isdbg('routecache')||0);
108 my $ta = [ gettimeofday ];
111 while (my ($k, $v) = each %list) {
112 push @s, "$k:" . $json->encode($v) . "\n";
116 my $fh = IO::File->new(">$cachefn") or dbg("Route::User: ERROR writing $cachefn $!"), return;
117 print $fh $_ for (sort @s);
120 dbg("Route::User::write_cache error '$@'");
123 my $diff = _diffms($ta);
124 dbg("Route::User::write_cache time to write: $diff mS");
129 my $json = DXJSON->new;
130 $json->canonical(isdbg('routecache'));
132 my $ta = [ gettimeofday ];
135 my $fh = IO::File->new("$cachefn") or dbg("Route::User: ERROR reading $cachefn $!"), return;
136 while (my $l = <$fh>) {
138 my ($k, $v) = split /:/, $l, 2;
139 $list{$k} = bless $json->decode($v) or dbg("Route::User: Error json error $! decoding '$v'"), next;
144 my $diff = _diffms($ta);
145 dbg("Route::User::read_cache time to read $count records from $cachefn : $diff mS");
149 # generic AUTOLOAD for accessors
155 my ($pkg,$name) = $AUTOLOAD =~ /^(.*)::(\w+)$/;
156 return if $name eq 'DESTROY';
158 confess "Non-existant field '$AUTOLOAD'" unless $valid{$name} || $Route::valid{$name};
160 # this clever line of code creates a subroutine which takes over from autoload
161 # from OO Perl - Conway
162 *$AUTOLOAD = sub {$_[0]->{$name} = $_[1] if @_ > 1; return $_[0]->{$name}};
164 # *{"${pkg}::$name"} = sub {$_[0]->{$name} = $_[1] if @_ > 1; return $_[0]->{$name}};
165 # goto &{"${pkg}::$name"};