| 1 | package PITA::Guest::Server; | ||||||||||
| 2 | |||||||||||
| 3 | 4 | ▶ | use 5.008; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 4 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 4 | ▶ | use Params::Util 1.00 (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 4 | ▶ | use POE::Wheel::Run 1.299 (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 4 | ▶ | use POE::Declare::HTTP::Server 0.05 (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 4 | ▶ | use PITA::Guest::Server::HTTP (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 4 | our $VERSION = '0.60'; | |||||||||
| 11 | |||||||||||
| 12 | 4 | ▶ | use POE::Declare 0.58 { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 4 | Hostname => 'Param', | |||||||||
| 14 | Port => 'Param', | ||||||||||
| 15 | Program => 'Param', | ||||||||||
| 16 | Uploads => 'Param', | ||||||||||
| 17 | Mirrors => 'Param', | ||||||||||
| 18 | StartupEvent => 'Message', | ||||||||||
| 19 | ShutdownEvent => 'Message', | ||||||||||
| 20 | pinged => 'Attribute', | ||||||||||
| 21 | uploaded => 'Attribute', | ||||||||||
| 22 | mirrored => 'Attribute', | ||||||||||
| 23 | status => 'Internal', | ||||||||||
| 24 | http => 'Internal', | ||||||||||
| 25 | child => 'Internal', | ||||||||||
| 26 | 4 | }; | |||||||||
| 27 | |||||||||||
| 28 | 4 | ▶ | use constant { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 29 | 4 | STOPPED => 1, | |||||||||
| 30 | STARTING => 1, | ||||||||||
| 31 | RUNNING => 1, | ||||||||||
| 32 | STOPPING => 1, | ||||||||||
| 33 | 4 | }; | |||||||||
| 34 | |||||||||||
| 35 | |||||||||||
| 36 | |||||||||||
| 37 | |||||||||||
| 38 | |||||||||||
| 39 | ###################################################################### | ||||||||||
| 40 | # Constructor and Accessors | ||||||||||
| 41 | |||||||||||
| 42 | 4 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||
| 43 | 4 | my $self = shift->SUPER::new(@_); | |||||||||
| 44 | |||||||||||
| 45 | # Set up tracking variables | ||||||||||
| 46 | 4 | $self->{status} = STOPPED; | |||||||||
| 47 | 4 | $self->{pinged} = 0; | |||||||||
| 48 | 4 | $self->{mirrored} = [ ]; | |||||||||
| 49 | 4 | $self->{uploaded} = [ ]; | |||||||||
| 50 | |||||||||||
| 51 | # Check params | ||||||||||
| 52 | 4 | ▶ | unless ( Params::Util::_ARRAY($self->Program) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 53 | 0 | die "Missing or invalid 'Program' param"; | |||||||||
| 54 | } | ||||||||||
| 55 | |||||||||||
| 56 | # Create the web server | ||||||||||
| 57 | 4 | $self->{http} = PITA::Guest::Server::HTTP->new( | |||||||||
| 58 | Hostname => $self->Hostname, | ||||||||||
| 59 | Port => $self->Port, | ||||||||||
| 60 | Mirrors => $self->Mirrors, | ||||||||||
| 61 | StartupEvent => $self->lookback('http_startup_event'), | ||||||||||
| 62 | StartupError => $self->lookback('http_startup_error'), | ||||||||||
| 63 | ShutdownEvent => $self->lookback('http_shutdown_event'), | ||||||||||
| 64 | PingEvent => $self->lookback('http_ping'), | ||||||||||
| 65 | MirrorEvent => $self->lookback('http_mirror'), | ||||||||||
| 66 | UploadEvent => $self->lookback('http_upload'), | ||||||||||
| 67 | ); | ||||||||||
| 68 | |||||||||||
| 69 | 4 | return $self; | |||||||||
| 70 | } | ||||||||||
| 71 | |||||||||||
| 72 | |||||||||||
| 73 | |||||||||||
| 74 | |||||||||||
| 75 | |||||||||||
| 76 | |||||||||||
| 77 | |||||||||||
| 78 | |||||||||||
| 79 | |||||||||||
| 80 | |||||||||||
| 81 | ###################################################################### | ||||||||||
| 82 | # Main Methods | ||||||||||
| 83 | |||||||||||
| 84 | # Sort of half-assed Process compatibility for testing purposes | ||||||||||
| 85 | 4 | ▶ | sub run { | ||||||||
|
Subroutine
run: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 86 | 4 | $_[0]->start; | |||||||||
| 87 | 4 | POE::Kernel->run; | |||||||||
| 88 | 4 | return 1; | |||||||||
| 89 | } | ||||||||||
| 90 | |||||||||||
| 91 | 8 | ▶ | sub start { | ||||||||
|
Subroutine
start: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 92 | 8 | my $self = shift; | |||||||||
| 93 | 8 | ▶ | unless ( $self->spawned ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 94 | 4 | $self->spawn; | |||||||||
| 95 | 4 | $self->post('startup'); | |||||||||
| 96 | } | ||||||||||
| 97 | 8 | return 1; | |||||||||
| 98 | } | ||||||||||
| 99 | |||||||||||
| 100 | 0 | ▶ | sub stop { | ||||||||
|
Subroutine
stop: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 101 | 0 | my $self = shift; | |||||||||
| 102 | 0 | ▶ | if ( $self->spawned ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 103 | 0 | $self->post('shutdown'); | |||||||||
| 104 | } | ||||||||||
| 105 | 0 | return 1; | |||||||||
| 106 | } | ||||||||||
| 107 | |||||||||||
| 108 | |||||||||||
| 109 | |||||||||||
| 110 | |||||||||||
| 111 | |||||||||||
| 112 | ###################################################################### | ||||||||||
| 113 | # Event Methods | ||||||||||
| 114 | |||||||||||
| 115 | 4 | ▶ | sub startup : Event { | ||||||||
|
Subroutine
startup: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 116 | # Kick off the blanket startup timeout | ||||||||||
| 117 | 4 | $_[SELF]->{status} = STARTING; | |||||||||
| 118 | 4 | $_[SELF]->startup_timeout_start; | |||||||||
| 119 | 4 | $_[SELF]->post('http_startup'); | |||||||||
| 120 | 4 | ▶ | } | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 121 | |||||||||||
| 122 | 4 | ▶ | sub http_startup : Event { | ||||||||
|
Subroutine
http_startup: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 123 | 4 | $_[SELF]->{http}->start; | |||||||||
| 124 | 4 | ▶ | } | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 125 | |||||||||||
| 126 | 4 | ▶ | sub http_startup_event : Event { | ||||||||
|
Subroutine
http_startup_event: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 127 | 4 | $_[SELF]->post('child_startup'); | |||||||||
| 128 | 4 | ▶ | } | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 129 | |||||||||||
| 130 | 0 | ▶ | sub http_startup_error : Event { | ||||||||
|
Subroutine
http_startup_error: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 131 | 0 | die "Failed to start the web server"; | |||||||||
| 132 | 4 | ▶ | } | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 133 | |||||||||||
| 134 | 4 | ▶ | sub http_shutdown_event : Event { | ||||||||
|
Subroutine
http_shutdown_event: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 135 | # Nothing to do? | ||||||||||
| 136 | 4 | ▶ | } | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 137 | |||||||||||
| 138 | 3 | ▶ | sub http_ping : Event { | ||||||||
|
Subroutine
http_ping: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 139 | 3 | $_[SELF]->startup_timeout_stop; | |||||||||
| 140 | 3 | $_[SELF]->activity_timeout_start; | |||||||||
| 141 | 3 | $_[SELF]->{status} = RUNNING; | |||||||||
| 142 | 3 | $_[SELF]->{pinged} = 1; | |||||||||
| 143 | 3 | $_[SELF]->StartupEvent; | |||||||||
| 144 | 4 | ▶ | } | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 145 | |||||||||||
| 146 | 1 | ▶ | sub http_mirror : Event { | ||||||||
|
Subroutine
http_mirror: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 147 | 1 | $_[SELF]->activity_timeout_start; | |||||||||
| 148 | 1 | push @{$_[SELF]->{mirrored}}, [ $_[ARG1], $_[ARG2], $_[ARG3] ]; | |||||||||
| 149 | 4 | ▶ | } | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 150 | |||||||||||
| 151 | 2 | ▶ | sub http_upload : Event { | ||||||||
|
Subroutine
http_upload: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 152 | 2 | $_[SELF]->activity_timeout_start; | |||||||||
| 153 | 2 | push @{$_[SELF]->{uploaded}}, [ $_[ARG1], $_[ARG2] ]; | |||||||||
| 154 | |||||||||||
| 155 | # Do we have everything? | ||||||||||
| 156 | 2 | ▶ | unless ( grep { not defined $_ } values %{$_[SELF]} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 157 | 2 | $_[SELF]->{status} = STOPPING; | |||||||||
| 158 | 2 | $_[SELF]->activity_timeout_stop; | |||||||||
| 159 | 2 | $_[SELF]->shutdown_timeout_start; | |||||||||
| 160 | } | ||||||||||
| 161 | 4 | ▶ | } | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 162 | |||||||||||
| 163 | 4 | ▶ | sub child_startup : Event { | ||||||||
|
Subroutine
child_startup: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 164 | # Spawn the program | ||||||||||
| 165 | 4 | $_[SELF]->{child} = POE::Wheel::Run->new( | |||||||||
| 166 | Program => $_[SELF]->Program, | ||||||||||
| 167 | StdoutEvent => 'child_stdout', | ||||||||||
| 168 | StderrEvent => 'child_stderr', | ||||||||||
| 169 | CloseEvent => 'child_close', | ||||||||||
| 170 | ); | ||||||||||
| 171 | |||||||||||
| 172 | # Trap signals from the child as well. | ||||||||||
| 173 | # NOTE: This needs to be brought under the management of POE::Declare. | ||||||||||
| 174 | 4 | $_[KERNEL]->sig_child( $_[SELF]->{child}->PID => 'child_signal' ); | |||||||||
| 175 | 4 | ▶ | } | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 176 | |||||||||||
| 177 | 0 | ▶ | sub child_stdout : Event { | ||||||||
|
Subroutine
child_stdout: not called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 178 | # Do nothing for now | ||||||||||
| 179 | # print STDERR "# CHILD STDOUT $_[ARG0]\n"; | ||||||||||
| 180 | 4 | ▶ | } | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 181 | |||||||||||
| 182 | 0 | ▶ | sub child_stderr : Event { | ||||||||
|
Subroutine
child_stderr: not called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 183 | # Do nothing for now | ||||||||||
| 184 | # print STDERR "# CHILD STDERR $_[ARG0]\n"; | ||||||||||
| 185 | 4 | ▶ | } | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 186 | |||||||||||
| 187 | 4 | ▶ | sub child_close : Event { | ||||||||
|
Subroutine
child_close: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||
| 188 | # print STDERR "# CHILD CLOSE\n"; | ||||||||||
| 189 | 4 | ▶ | if ( $_[SELF]->{child} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 190 | # Wait for a little to give the child time to SIGCHILD us | ||||||||||
| 191 | 4 | $_[SELF]->child_signal_timeout_start; | |||||||||
| 192 | } | ||||||||||
| 193 | 4 | ▶ | } | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 194 | |||||||||||
| 195 | 4 | ▶ | sub child_signal : Event { | ||||||||
|
Subroutine
child_signal: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||
| 196 | # print STDERR "# CHILD SIGCHILD $_[ARG2]\n"; | ||||||||||
| 197 | 4 | $_[SELF]->child_signal_timeout_stop; | |||||||||
| 198 | 4 | ▶ | if ( $_[SELF]->{child} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 199 | 4 | $_[SELF]->post('shutdown'); | |||||||||
| 200 | } | ||||||||||
| 201 | 4 | ▶ | } | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 202 | |||||||||||
| 203 | 0 | ▶ | sub child_signal_timeout : Timeout(5) { | ||||||||
|
Subroutine
child_signal_timeout: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 204 | 0 | ▶ | if ( $_[SELF]->{child} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 205 | 0 | $_[SELF]->post('shutdown'); | |||||||||
| 206 | } | ||||||||||
| 207 | 4 | ▶ | } | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 208 | |||||||||||
| 209 | 0 | ▶ | sub startup_timeout : Timeout(30) { | ||||||||
|
Subroutine
startup_timeout: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 210 | 0 | $_[SELF]->post('shutdown'); | |||||||||
| 211 | 4 | ▶ | } | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 212 | |||||||||||
| 213 | 0 | ▶ | sub activity_timeout : Timeout(3600) { | ||||||||
|
Subroutine
activity_timeout: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 214 | 0 | $_[SELF]->post('shutdown'); | |||||||||
| 215 | 4 | ▶ | } | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 216 | |||||||||||
| 217 | 0 | ▶ | sub shutdown_timeout : Timeout(60) { | ||||||||
|
Subroutine
shutdown_timeout: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 218 | 0 | $_[SELF]->post('shutdown'); | |||||||||
| 219 | 4 | ▶ | } | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 220 | |||||||||||
| 221 | 4 | ▶ | sub shutdown : Event { | ||||||||
|
Subroutine
shutdown: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 222 | 4 | $_[SELF]->finish; | |||||||||
| 223 | 4 | $_[SELF]->ShutdownEvent( | |||||||||
| 224 | $_[SELF]->pinged, | ||||||||||
| 225 | $_[SELF]->mirrored, | ||||||||||
| 226 | $_[SELF]->uploaded, | ||||||||||
| 227 | ); | ||||||||||
| 228 | 4 | $_[SELF]->{status} = STOPPED; | |||||||||
| 229 | 4 | ▶ | } | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 230 | |||||||||||
| 231 | |||||||||||
| 232 | |||||||||||
| 233 | |||||||||||
| 234 | |||||||||||
| 235 | ###################################################################### | ||||||||||
| 236 | # Support Methods | ||||||||||
| 237 | |||||||||||
| 238 | 4 | ▶ | sub finish { | ||||||||
|
Subroutine
finish: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||
| 239 | 4 | my $self = shift; | |||||||||
| 240 | |||||||||||
| 241 | # Clean up our children | ||||||||||
| 242 | 4 | ▶ | if ( $self->{child} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 243 | 4 | $self->{child}->kill(9); | |||||||||
| 244 | 4 | $self->{child} = undef; | |||||||||
| 245 | } | ||||||||||
| 246 | 4 | ▶ | if ( $self->{http}->spawned ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 247 | 4 | $self->{http}->call('shutdown'); | |||||||||
| 248 | } | ||||||||||
| 249 | |||||||||||
| 250 | # Call parent method to clean out other things | ||||||||||
| 251 | 4 | $self->SUPER::finish(@_); | |||||||||
| 252 | } | ||||||||||
| 253 | |||||||||||
| 254 | 4 | compile; | |||||||||