Yenta/Config.pm untested

filter: Statementstmt 0.0% 0 / 67 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 102 CC 7 SCAR 27.1
CC
7
Cov
0%
parse_map
42.0
handle_config
6.0
parse_monitor
2.0
CRAP 15.1
1 # -*- perl -*-
2  
3 # Copyright (c) 2009 AdCopy
4 # Author: Jeff Weisberg
5 # Created: 2009-Mar-27 17:31 (EDT)
6 # Function:
7 #
8 # $Id$
9  
10 package AC::Yenta::Config;
11 use AC::Misc;
12 use AC::Import;
13 use AC::DC::Debug;
14 use AC::ConfigFile::Simple;
15 use Socket;
16 use strict;
17  
18 our @ISA = 'AC::ConfigFile::Simple';
19 our @EXPORT = qw(conf_value conf_map);
20  
21  
22 my %CONFIG = (
23  
24 include => \&AC::ConfigFile::Simple::include_file,
25 debug => \&AC::ConfigFile::Simple::parse_debug,
26 allow => \&AC::ConfigFile::Simple::parse_allow,
27 port => \&AC::ConfigFile::Simple::parse_keyvalue,
28 environment => \&AC::ConfigFile::Simple::parse_keyvalue,
29 secret => \&AC::ConfigFile::Simple::parse_keyvalue,
30 seedpeer => \&AC::ConfigFile::Simple::parse_keyarray,
31  
32 ae_maxload => \&AC::ConfigFile::Simple::parse_keyvalue,
33 distrib_max => \&AC::ConfigFile::Simple::parse_keyvalue,
34  
35 savestatus => \&parse_savefile,
36 monitor => \&parse_monitor,
37 map => \&parse_map,
38  
39 );
40  
41 my @MAP = qw(dbfile basedir keepold history expire backend sharded);
42  
43  
44 ################################################################
45  
46 sub handle_config {
47 my $me = shift;
48 my $key = shift;
49 my $rest = shift;
50  
51 my $fnc = $CONFIG{$key};
52 return unless $fnc;
53 $fnc->($me, $key, $rest);
54 return 1;
55 }
56  
57 ################################################################
58  
59 sub parse_map {
60 my $me = shift;
61 my $key = shift;
62 my $value = shift;
63  
64 my($map) = $value =~ /(\S+)\s+\{\s*/;
65 die "invalid map spec\n" unless $map;
66  
67 my $md = {};
68 problem("map '$map' redefined") if $me->{_pending}{map}{$map};
69  
70 while( defined(my $l = $me->_nextline()) ){
71 last if $l eq '}';
72 my($k, $v) = split /\s+/, $l, 2;
73  
74 if( grep {$_ eq $k} @MAP ){
75 $v = cvt_timespec($v) if $k eq 'expire';
76 $md->{$k} = $v;
77 }else{
78 problem("unknown map option '$k'");
79 }
80 }
81  
82 $me->{_pending}{map}{$map} = $md;
83 }
84  
85 sub parse_monitor {
86 my $me = shift;
87 my $key = shift;
88 my $mon = shift;
89  
90 my($ip, $port) = split /:/, $mon;
91 push @{$me->{_pending}{monitor}}, {
92 monitor => $mon,
93 ipa => $ip,
94 ipn => inet_aton($ip),
95 ipi => inet_atoi($ip),
96 port => $port,
97 };
98 }
99  
100 sub parse_savefile {
101 my $me = shift;
102 my $key = shift;
103 my $save = shift;
104  
105 my($file, @type) = split /\s+/, $save;
106 push @{$me->{_pending}{savestatus}}, {
107 type => \@type,
108 file => $file,
109 };
110 }
111  
112 sub cvt_timespec {
113 my $t = shift;
114  
115 my %f = ( m => 60, h => 3600, d => 86400 );
116 my($n, $f) = $t =~ /(\d+)(\D?)/;
117  
118 $f = $f{$f} || 1;
119 return $n * $f;
120 }
121  
122  
123 ################################################################
124  
125 sub conf_value {
126 my $key = shift;
127  
128 return $AC::Yenta::CONF->{config}{$key};
129 }
130  
131 sub conf_map {
132 my $map = shift;
133  
134 return $AC::Yenta::CONF->{config}{map}{$map};
135 }
136  
137  
138 1;