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(0)->allow_blessed(1)->convert_blessed(1);
108 my $ta = [ gettimeofday ];
109 $json->indent(1)->canonical(1) if isdbg('routecache');
110 my $s = eval {$json->encode(\%list)};
112 my $fh = IO::File->new(">$cachefn") or confess("writing $cachefn $!");
116 dbg("Route::User:Write_cache error '$@'");
119 $json->indent(0)->canonical(0);
120 my $diff = _diffms($ta);
121 my $size = sprintf('%.3fKB', (length($s) / 1000));
122 dbg("Route::User:WRITE_CACHE size: $size time to write: $diff mS");
126 # generic AUTOLOAD for accessors
132 my ($pkg,$name) = $AUTOLOAD =~ /^(.*)::(\w+)$/;
133 return if $name eq 'DESTROY';
135 confess "Non-existant field '$AUTOLOAD'" unless $valid{$name} || $Route::valid{$name};
137 # this clever line of code creates a subroutine which takes over from autoload
138 # from OO Perl - Conway
139 *$AUTOLOAD = sub {$_[0]->{$name} = $_[1] if @_ > 1; return $_[0]->{$name}};
141 # *{"${pkg}::$name"} = sub {$_[0]->{$name} = $_[1] if @_ > 1; return $_[0]->{$name}};
142 # goto &{"${pkg}::$name"};