Fallback/Daemon.pm untested

filter: Statementstmt 0.0% 0 / 98 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 168 CC 16 SCAR 34.9
CC
16
Cov
0%
process_request
90.0
handle_cookies
12.0
out
12.0
CRAP 32.7
1 #!/usr/bin/perl -w
2  
3 package Data::Fallback::Daemon;
4  
5  
6 use strict;
7  
8 use vars qw(@ISA);
9 use Net::Server::PreForkSimple;
10 use Data::Dumper;
11 use Data::Fallback;
12 use Time::HiRes qw(gettimeofday);
13  
14 @ISA = qw(Net::Server::PreForkSimple);
15  
16 ### over-ridden subs below
17  
18 sub new {
19 my $type = shift;
20 my $hash_ref = $_[0];
21 my @PASSED_ARGS = (ref $hash_ref eq 'HASH') ? %{$_[0]} : @_;
22 my $cache_object;
23 my @DEFAULT_ARGS = (
24 );
25 my %ARGS = (@DEFAULT_ARGS, @PASSED_ARGS);
26 my $self = bless \%ARGS, $type;
27 return $self;
28 }
29  
30 sub run {
31 my $self = shift;
32 $self->SUPER::run(max_servers => 1,
33 max_requests => 100000,
34 reverse_lookups => 0,
35 );
36
37 }
38  
39  
40 sub __configure_hook {
41 my $self = shift;
42 $self->{fallback} ||= Data::Fallback->new({
43 });
44 $self->{fallback}{loaded_list} ||= {};
45 }
46  
47 sub process_request {
48 my $self = shift;
49 $self->{start} = gettimeofday;
50 eval {
51  
52 $self->{cookies} = {
53 elapsed => 1,
54 history => 1,
55 };
56 $self->{output} = "";
57  
58 local $SIG{ALRM} = sub { die "Timed Out!\n" };
59 my $timeout = 30;
60 my $previous_alarm = alarm($timeout);
61 my $first_line = <STDIN>;
62  
63 # GET /$list/$primary_key/$item
64  
65  
66 # GET /test/14/foo HTTP/1.1
67  
68 # method
69 $first_line =~ m@^(\w+)\s+
70
71 # list name
72 /(\w+)
73 # primary_key
74 /(\w+)
75 # column
76 /([\w,]+)@x;
77  
78 my ($method, $list_name, $primary_key, $item) = ($1, $2, $3, $4);
79 if($method eq 'GET') {
80 unless($self->{fallback}{loaded_list}{$list_name}) {
81 $self->{output} .= "Unknown list name: $list_name\n";
82 $self->{output} .= "Known lists: " . join(", ", keys %{$self->{fallback}{loaded_list}}) . "\n";
83 }
84 #$self->{output} .= Dumper $self;
85 #$self->{output} .= Dumper $first_line;
86 #$self->{output} .= Dumper $method;
87 #$self->{output} .= Dumper $list_name;
88 #$self->{output} .= Dumper $primary_key;
89 #$self->{output} .= Dumper $item;
90  
91 $self->{fallback}{list} = $self->{fallback}{loaded_list}{$list_name};
92 $self->{fallback}{list_name} = $list_name;
93 $self->{fallback}{hash}{primary_key} = $primary_key if(defined $primary_key);
94 #$self->{output} .= Dumper $self->{fallback}{hash}{primary_key};
95 my $get = $self->{fallback}->get("/$primary_key/$item");
96 if(ref $get && ref $get eq 'ARRAY') {
97 $self->{output} .= "<type>array</type>\n";
98 foreach(@{$get}) {
99 $self->{output} .= "<array>$_</array>\n";
100 }
101 } else {
102 $self->{output} .= "<type>scalar</type>\n";
103 $self->{output} .= "<scalar>$get</scalar>\n";
104 }
105 } else {
106 $self->{output} .= "Unknown method: $method, please use GET ($first_line)\n";
107 }
108  
109 alarm($timeout);
110 while( <STDIN> ){
111 s/\r?\n$//;
112 last if(!$_ || $_ eq "");
113 if(/^(?:Set-)?Cookie: (.+?)\s*=\s*(.+?)(?:;|$)/g) {
114 $self->{cookies}{$1} = $2;
115 } else {
116 #$self->{output} .= "You said \"$_\"\r\n";
117 }
118 alarm($timeout);
119 }
120 alarm($previous_alarm);
121  
122 };
123  
124 if( $@ ){
125 if($@=~/timed out/i ){
126 $self->{output} .= "Timed Out.\n";
127 } else {
128 $self->{output} .= "<error>$@</error>\n";
129 }
130 }
131 $self->out;
132 return;
133 }
134  
135 sub print_header {
136 my $self = shift;
137 print "HTTP/1.0 200\r\n";
138 print "Content-type: text/plain\r\n\r\n";
139 }
140  
141 sub handle_cookies {
142 my $self = shift;
143 foreach(keys %{$self->{cookies}}) {
144 next unless($self->{cookies}{$_});
145 if($_ =~ /history/) {
146 $self->{output} .= "<history>\n" . Dumper($self->{fallback}{history}) . "</history>\n";
147 }
148 }
149 }
150  
151 sub out {
152 my $self = shift;
153 $self->print_header;
154 $self->handle_cookies if(scalar keys %{$self->{cookies}});
155 $self->{output} .= "<elapsed>" . (gettimeofday - $self->{start}) . "</elapsed>\n" if($self->{cookies}{elapsed});
156 $self->{output} =~ s/([^\r\n]?)\n/$1\r\n/g;
157 print $self->{output};
158 }
159  
160 sub GET_VALUES {
161 my $values=shift;
162 return () unless defined $values;
163 if (ref $values eq "ARRAY") {
164 return @$values;
165 }
166 return ($values);
167 }
168  
169 =head1 NAME
170  
171 Data::Fallback::Daemon - a daemon for Data::Fallback
172  
173 =head1 DESCRIPTION
174  
175 Data::Fallback works great at finding data at the fastest place you tell it, but what happens when you keep
176 asking for the same data repeatedly? Well, Data::Fallback::Daemon allow to cache in memory the data, and a simple
177 protocol for retrieving it.
178  
179 =head1 TYPICAL USAGE
180  
181 Having one database box and n client boxes doesn't scale too hot for n very large. However, one database box and n
182 client boxes each running their own Data::Fallback::Daemon scales well for large n. So, typical usage in my view is
183 to have local daemons running on each client box.
184  
185 =head1 DBI EXAMPLE
186  
187 This example requires DBI and DBD::mysql to be installed on the box.
188  
189 #!/usr/bin/perl -w
190  
191 use strict;
192 use Data::Fallback;
193 use Data::Fallback::Daemon;
194  
195 # first we set up a simple Data::Fallback::Daemon object
196 my $self = Data::Fallback::Daemon->new({
197  
198 # you can change the port if you like
199 port => '20203',
200  
201 # reverse_lookups can take awhile when you aren't connected to the web,
202 # so I just turn them off for testing purposees
203 reverse_lookups => '',
204  
205 # we need to include a Data::Fallback object
206 fallback => Data::Fallback->new(),
207 });
208  
209 # this is just a dumb test server that I can't guarantee will be up
210 # generally, db is an array ref of your connection info
211 my $db = ["DBI:mysql:database=fallback;host=spack.net;port=7777", "fallback", ""];
212  
213 # loaded_list is a hash ref of all the lists you want to have the daemon maintain
214 # the hash key state is how the list is referenced
215 $self->{fallback}{loaded_list} = {
216 state => [
217 {
218 content => '/tmp/fallback/state_$primary_key',
219 package => 'ConfFile',
220 accept_update => 'group',
221 },
222 {
223 db => $db,
224 content => 'SELECT * FROM state WHERE id = ?',
225 package => 'DBI',
226 },
227 ],
228 };
229  
230 # all set up, just need to run
231 $self->run();
232  
233 =head1 THANKS
234  
235 Thanks to Paul Seamons for Net::Server and for helping me set up this simple daemon.
236  
237 =head1 AUTHOR
238  
239 Copyright 2001-2002, Earl J. Cahill. All rights reserved.
240  
241 This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
242  
243 Address bug reports and comments to: cpan@spack.net.
244  
245 When sending bug reports, please provide the version of Data::Fallback, the version of Perl, and the name and version of the operating
246 system you are using.
247  
248 =cut
249 1;