|
Line 0
Link Here
|
|
|
1 |
package Koha::Icarus::Listener; |
| 2 |
|
| 3 |
use Modern::Perl; |
| 4 |
use parent 'Koha::Icarus::Base'; |
| 5 |
|
| 6 |
use POE qw(Wheel::ReadWrite Wheel::SocketFactory Wheel::Run); |
| 7 |
use IO::Socket qw(AF_UNIX); |
| 8 |
use URI; |
| 9 |
use Koha::Icarus::Task; |
| 10 |
use JSON; #For "on_client_input" |
| 11 |
|
| 12 |
my $null_filter = POE::Filter::Line->new( |
| 13 |
Literal => chr(0), |
| 14 |
); |
| 15 |
|
| 16 |
sub new { |
| 17 |
my ($class, $args) = @_; |
| 18 |
$args = {} unless defined $args; |
| 19 |
$args->{_component} = "server"; |
| 20 |
$args->{_id} = "undefined"; |
| 21 |
return bless ($args, $class); |
| 22 |
} |
| 23 |
|
| 24 |
#NOTE: "spawn" inspired by http://poe.perl.org/?POE_Cookbook/Object_Methods |
| 25 |
sub spawn { |
| 26 |
my ($class, $args) = @_; |
| 27 |
my $self = $class->new($args); |
| 28 |
POE::Session->create( |
| 29 |
object_states => [ |
| 30 |
$self => { |
| 31 |
_start => "on_server_start", |
| 32 |
shutdown => "shutdown", |
| 33 |
set_verbosity => "set_verbosity", |
| 34 |
_child => "on_task_event", |
| 35 |
got_list_tasks => "on_list_tasks", |
| 36 |
graceful_shutdown => "graceful_shutdown", |
| 37 |
got_client_accept => "on_client_accept", |
| 38 |
got_client_error => "on_client_error", |
| 39 |
got_server_error => "on_server_error", |
| 40 |
got_add_task => "on_add_task", |
| 41 |
got_client_input => "on_client_input", |
| 42 |
}, |
| 43 |
], |
| 44 |
); |
| 45 |
} |
| 46 |
|
| 47 |
#Methods for POE::Session |
| 48 |
|
| 49 |
sub on_server_start { |
| 50 |
my ($self, $kernel,$heap,$session) = @_[OBJECT, KERNEL,HEAP,SESSION]; |
| 51 |
my $server_id = $session->ID; |
| 52 |
$self->{_id} = $server_id; #Set internal id for logging purposes |
| 53 |
|
| 54 |
my $bind_address_uri = $self->{Socket}; |
| 55 |
my $max_tasks = $self->{MaxTasks}; |
| 56 |
|
| 57 |
$kernel->sig(INT => "graceful_shutdown"); |
| 58 |
$kernel->sig(TERM => "graceful_shutdown"); |
| 59 |
|
| 60 |
$heap->{max_tasks} = $max_tasks // 25; #Default maximum of 25 unless otherwise specified |
| 61 |
|
| 62 |
$self->log("Maximum number of tasks allowed: $heap->{max_tasks}"); |
| 63 |
$self->log("Starting server..."); |
| 64 |
|
| 65 |
my %server_params = ( |
| 66 |
SuccessEvent => "got_client_accept", |
| 67 |
FailureEvent => "got_server_error", |
| 68 |
); |
| 69 |
|
| 70 |
#TODO: At this time, only "unix" sockets are supported. In future, perhaps TCP/IP sockets could also be supported. |
| 71 |
my $uri = URI->new($bind_address_uri); |
| 72 |
my $scheme = $uri->scheme; |
| 73 |
|
| 74 |
if ($scheme eq 'unix'){ |
| 75 |
my $bind_address = $uri->path; |
| 76 |
$server_params{SocketDomain} = AF_UNIX; |
| 77 |
$server_params{BindAddress} = $bind_address; |
| 78 |
#When starting a unix socket server, you need to remove any existing references to that socket file. |
| 79 |
if ($bind_address && (-e $bind_address) ){ |
| 80 |
unlink $bind_address; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
$heap->{server} = POE::Wheel::SocketFactory->new(%server_params); |
| 85 |
|
| 86 |
if ($scheme eq 'unix'){ |
| 87 |
#FIXME/DEBUGGING: This is a way to force a permission denied error... |
| 88 |
#chmod 0755, $uri->path; |
| 89 |
#Make the socket writeable to other users like Apache |
| 90 |
chmod 0666, $uri->path; |
| 91 |
} |
| 92 |
|
| 93 |
} |
| 94 |
|
| 95 |
sub shutdown { |
| 96 |
my ($self,$heap,$session,$kernel) = @_[OBJECT, HEAP,SESSION,KERNEL]; |
| 97 |
|
| 98 |
if ($heap->{server}){ |
| 99 |
$self->log("Shutting down server..."); |
| 100 |
#Delete the server, so that you can't get any new connections |
| 101 |
delete $heap->{server} if $heap->{server}; |
| 102 |
} |
| 103 |
|
| 104 |
if ($heap->{client}){ |
| 105 |
$self->log("Shutting down any remaining clients..."); |
| 106 |
#Delete the clients, so that you bring down the existing connections |
| 107 |
delete $heap->{client}; #http://www.perlmonks.org/?node_id=176971 |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
sub on_task_event { |
| 112 |
my ($self, $kernel, $heap,$session) = @_[OBJECT,KERNEL, HEAP,SESSION]; |
| 113 |
my ($action,$child_session,$task) = @_[ARG0,ARG1,ARG2]; |
| 114 |
|
| 115 |
my $child_id = $child_session->ID; |
| 116 |
$self->log("$action child $child_id"); |
| 117 |
|
| 118 |
if ($action eq 'create'){ |
| 119 |
#NOTE: The $task variable is returned by the child POE session's _start event |
| 120 |
my $task_id = $child_session->ID; |
| 121 |
$heap->{tasks}->{$task_id}->{task} = $task; |
| 122 |
|
| 123 |
} elsif ($action eq 'lose'){ |
| 124 |
my $task_id = $child_session->ID; |
| 125 |
delete $heap->{tasks}->{$task_id}; |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
#TODO: Put this in a parent class? |
| 130 |
sub set_verbosity { |
| 131 |
my ($self,$session,$kernel,$new_verbosity) = @_[OBJECT,SESSION,KERNEL,ARG0]; |
| 132 |
if (defined $new_verbosity){ |
| 133 |
$self->{Verbosity} = $new_verbosity; |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
sub on_list_tasks { |
| 138 |
my ($self, $kernel, $heap,$session) = @_[OBJECT, KERNEL, HEAP,SESSION]; |
| 139 |
|
| 140 |
#DEBUG: You can access the POE::Kernel's sessions with "$POE::Kernel::poe_kernel->[POE::Kernel::KR_SESSIONS]". |
| 141 |
#While it's black magic you shouldn't touch, it can be helpful when debugging. |
| 142 |
|
| 143 |
my @tasks = (); |
| 144 |
foreach my $task_id (keys %{$heap->{tasks}} ){ |
| 145 |
push(@tasks,{ task_id => $task_id, task => $heap->{tasks}->{$task_id}->{task} }); |
| 146 |
} |
| 147 |
return \@tasks; |
| 148 |
} |
| 149 |
|
| 150 |
sub graceful_shutdown { |
| 151 |
my ($self, $heap,$session,$kernel,$signal) = @_[OBJECT, HEAP,SESSION,KERNEL,ARG0]; |
| 152 |
|
| 153 |
#Tell the kernel that you're handling the signal sent to this session |
| 154 |
$kernel->sig_handled(); |
| 155 |
$kernel->sig($signal); |
| 156 |
|
| 157 |
my $tasks = $kernel->call($session,"got_list_tasks"); |
| 158 |
|
| 159 |
|
| 160 |
if ( $heap->{tasks} && %{$heap->{tasks}} ){ |
| 161 |
$self->log("Waiting for tasks to finish..."); |
| 162 |
foreach my $task_id (keys %{$heap->{tasks}}){ |
| 163 |
$self->log("Task $task_id still exists..."); |
| 164 |
$kernel->post($task_id,"got_task_stop"); |
| 165 |
} |
| 166 |
} else { |
| 167 |
$self->log("All tasks have finished"); |
| 168 |
$kernel->yield("shutdown"); |
| 169 |
return; |
| 170 |
} |
| 171 |
|
| 172 |
$self->log("Attempting graceful shutdown in 1 second..."); |
| 173 |
#NOTE: Basically, we just try another graceful shutdown on the next tick. |
| 174 |
$kernel->delay("graceful_shutdown" => 1); |
| 175 |
} |
| 176 |
|
| 177 |
#Accept client connection to listener |
| 178 |
sub on_client_accept { |
| 179 |
my ($self, $client_socket, $server_wheel_id, $heap, $session) = @_[OBJECT, ARG0, ARG3, HEAP,SESSION]; |
| 180 |
|
| 181 |
my $client_wheel = POE::Wheel::ReadWrite->new( |
| 182 |
Handle => $client_socket, |
| 183 |
InputEvent => "got_client_input", |
| 184 |
ErrorEvent => "got_client_error", |
| 185 |
InputFilter => $null_filter, |
| 186 |
OutputFilter => $null_filter, |
| 187 |
); |
| 188 |
|
| 189 |
$client_wheel->put("HELLO"); |
| 190 |
$heap->{client}->{ $client_wheel->ID() } = $client_wheel; |
| 191 |
$self->log("Connection ".$client_wheel->ID()." started.$server_wheel_id"); |
| 192 |
} |
| 193 |
|
| 194 |
#Handle server error - shutdown server |
| 195 |
sub on_server_error { |
| 196 |
my ($self, $operation, $errnum, $errstr, $heap, $session) = @_[OBJECT, ARG0, ARG1, ARG2,HEAP, SESSION]; |
| 197 |
$self->log("Server $operation error $errnum: $errstr\n"); |
| 198 |
delete $heap->{server}; |
| 199 |
} |
| 200 |
|
| 201 |
#Handle client error - including disconnect |
| 202 |
sub on_client_error { |
| 203 |
my ($self, $wheel_id,$heap,$session) = @_[OBJECT, ARG3,HEAP,SESSION]; |
| 204 |
|
| 205 |
$self->log("Connection $wheel_id failed or ended."); |
| 206 |
delete $heap->{client}->{$wheel_id}; |
| 207 |
|
| 208 |
} |
| 209 |
|
| 210 |
sub on_add_task { |
| 211 |
my ($self, $message, $kernel, $heap, $session) = @_[OBJECT, ARG0, KERNEL, HEAP,SESSION]; |
| 212 |
|
| 213 |
#Fetch a list of all tasks |
| 214 |
my @task_keys = keys %{$heap->{tasks}}; |
| 215 |
|
| 216 |
#If the number in the list is less than the max, add a new task |
| 217 |
#else die. |
| 218 |
if (scalar @task_keys < $heap->{max_tasks}){ |
| 219 |
my $server_id = $session->ID; |
| 220 |
my $task_session = Koha::Icarus::Task->spawn({ message => $message, server_id => $server_id, Verbosity => 1, }); |
| 221 |
return $task_session->ID; |
| 222 |
} else { |
| 223 |
#This die should be caught by the event caller... |
| 224 |
die "Maximum number of tasks already reached.\n"; |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
sub on_client_input { |
| 229 |
my ($self, $input, $wheel_id, $session, $kernel, $heap) = @_[OBJECT, ARG0, ARG1, SESSION, KERNEL, HEAP]; |
| 230 |
|
| 231 |
#Store server id more explicitly |
| 232 |
my $server_id = $session->ID; |
| 233 |
|
| 234 |
#Server listener has received input from client |
| 235 |
my $client = $heap->{client}->{$wheel_id}; |
| 236 |
|
| 237 |
#FIXME: you probably don't want to log this as it can have auth info... |
| 238 |
#$self->log("Input = $input"); |
| 239 |
|
| 240 |
#Parse input from client |
| 241 |
my $message = from_json($input); |
| 242 |
|
| 243 |
if ( ref $message eq 'HASH' ){ |
| 244 |
#Read "command" from client |
| 245 |
if (my $command = $message->{command}){ |
| 246 |
$self->log("Message received with command \"$command\"."); |
| 247 |
if ($command eq 'add task'){ |
| 248 |
my $output = {}; |
| 249 |
|
| 250 |
#Create a task session |
| 251 |
eval { |
| 252 |
#NOTE: The server automatically keeps track of its child tasks |
| 253 |
my $task_id = $kernel->call($server_id,"got_add_task",$message); |
| 254 |
|
| 255 |
$output->{action} = "added"; |
| 256 |
$output->{task_id} = $task_id; |
| 257 |
}; |
| 258 |
if ($@){ |
| 259 |
#FIXME: You might be able to remove this log... |
| 260 |
$self->log("$@"); |
| 261 |
chomp($@); |
| 262 |
$output->{action} = "error"; |
| 263 |
$output->{error_message} = $@; |
| 264 |
} |
| 265 |
my $server_output = to_json($output); |
| 266 |
$client->put($server_output); |
| 267 |
return; |
| 268 |
|
| 269 |
} elsif ( ($command eq 'remove task') || ($command eq 'start task' ) ){ |
| 270 |
|
| 271 |
my $task_id = $message->{task_id}; |
| 272 |
|
| 273 |
my $output = { |
| 274 |
task_id => $task_id, |
| 275 |
}; |
| 276 |
|
| 277 |
if ($command eq 'remove task'){ |
| 278 |
$kernel->call($task_id,"got_task_stop"); |
| 279 |
$output->{action} = "removed"; |
| 280 |
} elsif ($command eq 'start task'){ |
| 281 |
my $response = $kernel->call($task_id, "on_task_init"); |
| 282 |
$output->{action} = $response; |
| 283 |
} |
| 284 |
|
| 285 |
if ($!){ |
| 286 |
$output->{action} = "error"; |
| 287 |
$output->{error_message} = $!; |
| 288 |
} |
| 289 |
|
| 290 |
#FIXME: What do we actually want to send back to the client? |
| 291 |
my $server_output = to_json($output); |
| 292 |
$client->put($server_output); |
| 293 |
return; |
| 294 |
|
| 295 |
} elsif ($command eq 'list tasks'){ |
| 296 |
|
| 297 |
#Get tasks from listener (ie self) |
| 298 |
my $tasks = $kernel->call($server_id, "got_list_tasks"); |
| 299 |
|
| 300 |
#Prepare output for client |
| 301 |
my $server_output = to_json({tasks => $tasks}, {pretty => 1}); |
| 302 |
|
| 303 |
#Send output to client |
| 304 |
$client->put($server_output); |
| 305 |
return; |
| 306 |
|
| 307 |
} elsif ($command eq 'shutdown'){ |
| 308 |
$kernel->post($server_id, "graceful_shutdown"); |
| 309 |
my $server_output = to_json({action => 'shutting down'}); |
| 310 |
$client->put($server_output); |
| 311 |
return; |
| 312 |
} else { |
| 313 |
$self->log("The message contained an invalid command!"); |
| 314 |
$client->put("Sorry! That is an invalid command!"); |
| 315 |
return; |
| 316 |
} |
| 317 |
} else { |
| 318 |
$self->log("The message was missing a command!"); |
| 319 |
} |
| 320 |
} else { |
| 321 |
$self->log("The message was malformed!"); |
| 322 |
} |
| 323 |
$client->put("Sorry! That is an invalid message!"); |
| 324 |
return; |
| 325 |
} |
| 326 |
|
| 327 |
1; |