Daemon/NetServer.pm untested

filter: Statementstmt 0.0% 0 / 89 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 15 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 20 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 148 SCAR 40.3
CC
7
Cov
0%
post_configure
12.0
new
6.0
options
6.0
CRAP 56.0
1 # Copyrights 2007-2018 by [Mark Overmeer <markov@cpan.org>].
2 # For other contributors see ChangeLog.
3 # See the manual pages for details on the licensing terms.
4 # Pod stripped from pm file by OODoc 2.02.
5 # This code is part of distribution XML-Compile-SOAP-Daemon. Meta-POD
6 # processed with OODoc into POD and HTML manual-pages. See README.md
7 # Copyright Mark Overmeer. Licensed under the same terms as Perl itself.
8  
9 package XML::Compile::SOAP::Daemon::NetServer;
10 use vars '$VERSION';
11 $VERSION = '3.14';
12  
13  
14 use warnings;
15 use strict;
16  
17 # The selected type of netserver gets added to the @ISA during new(),
18 # so there are two base-classes!
19 use parent 'XML::Compile::SOAP::Daemon';
20 our @ISA;
21  
22 use Log::Report 'xml-compile-soap-daemon';
23  
24 use Time::HiRes qw/time alarm/;
25 use XML::Compile::SOAP::Util qw/:daemon/;
26 use XML::Compile::SOAP::Daemon::LWPutil;
27  
28 # Net::Server error levels to Log::Report levels
29 my @levelToReason = qw/ERROR WARNING NOTICE INFO TRACE/;
30  
31  
32 sub new($%)
33 { my ($class, %args) = @_;
34 my $daemon = $args{based_on} || 'Net::Server::PreFork';
35  
36 my $self;
37 if(ref $daemon)
38 { $self = $daemon;
39 }
40 else
41 { eval "require $daemon";
42 $@ and error __x"failed to compile Net::Server class {class}, {error}"
43 , class => $daemon, error => $@;
44 $self = $daemon->new(%args);
45 }
46  
47 $self->{based_on} = ref $daemon || $daemon;
48 $daemon->isa('Net::Server')
49 or error __x"The daemon is not a Net::Server, but {class}"
50 , class => $self->{based_on};
51  
52 # Beautiful Perl
53 push @ISA, $self->{based_on};
54 (bless $self, $class)->init(\%args); # $ISA[0] branch only
55 }
56  
57 sub options()
58 { my ($self, $ref) = @_;
59 my $prop = $self->{server};
60 $self->SUPER::options($ref);
61 foreach ( qw/client_timeout client_maxreq client_reqbonus name/ )
62 { $prop->{$_} = undef unless exists $prop->{$_};
63 $ref->{$_} = \$prop->{$_};
64 }
65 }
66  
67 sub default_values()
68 { my $self = shift;
69 my $def = $self->SUPER::default_values;
70 my %mydef =
71 ( # changed defaults
72 setsid => 1, background => 1, log_file => 'Log::Report'
73  
74 # make in-code defaults explicit, Net::Server 0.97
75 # see http://rt.cpan.org//Ticket/Display.html?id=32226
76 , log_level => 2, syslog_ident => 'net_server', syslog_logsock => 'unix'
77 , syslog_facility => 'daemon', syslog_logopt => 'pid'
78  
79 , client_timeout => 30, client_maxreq => 100
80 , client_reqbonus => 0, name => 'soap daemon'
81 );
82 @$def{keys %mydef} = values %mydef;
83 $def;
84 }
85  
86 sub post_configure()
87 { my $self = shift;
88 my $prop = $self->{server};
89  
90 # Change the way messages are logged
91  
92 my $loglevel = $prop->{log_level};
93 my $reasons = ($levelToReason[$loglevel] || 'NOTICE') . '-';
94  
95 my $logger = delete $prop->{log_file};
96 if($logger eq 'Log::Report')
97 { # dispatching already initialized
98 }
99 elsif($logger eq 'Sys::Syslog')
100 { dispatcher SYSLOG => 'default'
101 , accept => $reasons
102 , identity => $prop->{syslog_ident}
103 , logsocket => $prop->{syslog_logsock}
104 , facility => $prop->{syslog_facility}
105 , flags => $prop->{syslog_logopt}
106 }
107 else
108 { dispatcher FILE => 'default', to => $logger;
109 }
110  
111 $self->SUPER::post_configure;
112 }
113  
114 sub post_bind_hook()
115 { my $self = shift;
116 my $prop = $self->{server};
117 lwp_socket_init $_ for @{$prop->{sock}};
118 }
119  
120 sub setWsdlResponse($;$)
121 { my ($self, $fn, $ft) = @_;
122 trace "setting wsdl response to $fn";
123 lwp_wsdl_response $fn, $ft;
124 }
125  
126 # Overrule Net::Server's log() to translate it into Log::Report calls
127 sub log($$@)
128 { my ($self, $level, $msg) = (shift, shift, shift);
129 $msg = sprintf $msg, @_ if @_;
130 chomp $msg; # some log lines have a trailing newline
131  
132 my $reason = $levelToReason[$level] or return;
133 report $reason => $msg;
134 }
135  
136 # use Log::Report for hooks
137 sub write_to_log_hook { panic "write_to_log_hook cannot be used" }
138  
139  
140 sub _run($)
141 { my ($self, $args) = @_;
142 delete $args->{log_file}; # Net::Server should not mess with my preps
143 $args->{no_client_stdout} = 1; # it's a daemon, you know
144 lwp_add_header Server => $self->{prop}{name};
145 $self->{XCSDN_pp} = delete $args->{postprocess};
146  
147 $ISA[1]->can('run')->($self, $args); # never returns
148 }
149  
150 sub process_request()
151 { my $self = shift;
152 my $prop = $self->{server};
153  
154 # Now, our connection will become a HTTP::Daemon connection
155 my $old_class = ref $prop->{client};
156 my $connection = lwp_http11_connection $self, $prop->{client};
157  
158 eval {
159 lwp_handle_connection $connection
160 , expires => time() + $prop->{client_timeout}
161 , maxmsgs => $prop->{client_maxreq}
162 , reqbonus => $prop->{client_reqbonus}
163 , handler => sub {$self->process(@_)}
164 , postprocess => $self->{XCSDN_pp};
165 };
166  
167 info __x"connection ended with force; {error}", error => $@
168 if $@;
169  
170 # Our connection becomes as Net::Server::Proto::TCP again
171 bless $prop->{client}, $old_class;
172 1;
173 }
174  
175 sub url() { "url replacement not yet implemented" }
176 sub product_tokens() { shift->{prop}{name} }
177  
178 #-----------------------------
179  
180  
181 1;