blib/lib/Video/DVDRip/GUI/Cluster/Control.pm untested

filter: Statementstmt 0.0% 0 / 510 Branchbran 0.0% 0 / 144 Conditioncond 0.0% 0 / 65 MC/DCmcdc 0.0% 0 / 35 Subroutinesub 0.0% 0 / 80 Podpod 0.0% 0 / 71 totaltotal 0.0% 0 / 905 CC 73 SCAR 48.2
CC
73
Cov
0%
process_master_event_queue
650.0
connect_master
132.0
disconnect_master
56.0
CRAP 123.8
1 # $Id: Control.pm 2303 2007-04-13 11:23:39Z joern $
2  
3 #-----------------------------------------------------------------------
4 # Copyright (C) 2001-2006 Jörn Reder <joern AT zyn.de>.
5 # All Rights Reserved. See file COPYRIGHT for details.
6 #
7 # This module is part of Video::DVDRip, which is free software; you can
8 # redistribute it and/or modify it under the same terms as Perl itself.
9 #-----------------------------------------------------------------------
10  
11 package Video::DVDRip::GUI::Cluster::Control;
12 use Locale::TextDomain qw (video.dvdrip);
13  
14 use base Video::DVDRip::GUI::Base;
15 use Gtk2::Helper;
16  
17 use Event::RPC 0.89;
18 use Event::RPC::Client;
19  
20 use Video::DVDRip::GUI::Cluster::Node;
21 use Video::DVDRip::GUI::Cluster::Title;
22  
23 use strict;
24 use Carp;
25  
26 sub rpc_client { shift->{rpc_client} }
27 sub master { shift->{master} }
28 sub log_socket { shift->{log_socket} }
29 sub gtk_log_view { shift->{gtk_log_view} }
30 sub gtk_log_buffer { shift->{gtk_log_buffer} }
31 sub exit_on_close { shift->{exit_on_close} }
32 sub selected_project_id { shift->{selected_project_id} }
33 sub selected_job_id { shift->{selected_job_id} }
34 sub selected_node_name { shift->{selected_node_name} }
35 sub master_event_queue { shift->{master_event_queue} }
36 sub log_watcher { shift->{log_watcher} }
37 sub event_timeout { shift->{event_timeout} }
38 sub node_gui { shift->{node_gui} }
39 sub cluster_ff { shift->{cluster_ff} }
40 sub pane1_pos { shift->{pane1_pos} }
41 sub pane2_pos { shift->{pane2_pos} }
42 sub pane3_pos { shift->{pane3_pos} }
43 sub window_height { shift->{window_height} }
44 sub window_width { shift->{window_width} }
45  
46 sub set_rpc_client { shift->{rpc_client} = $_[1] }
47 sub set_master { shift->{master} = $_[1] }
48 sub set_log_socket { shift->{log_socket} = $_[1] }
49 sub set_gtk_log_view { shift->{gtk_log_view} = $_[1] }
50 sub set_gtk_log_buffer { shift->{gtk_log_buffer} = $_[1] }
51 sub set_exit_on_close { shift->{exit_on_close} = $_[1] }
52 sub set_selected_project_id { shift->{selected_project_id} = $_[1] }
53 sub set_selected_job_id { shift->{selected_job_id} = $_[1] }
54 sub set_selected_node_name { shift->{selected_node_name} = $_[1] }
55 sub set_master_event_queue { shift->{master_event_queue} = $_[1] }
56 sub set_log_watcher { shift->{log_watcher} = $_[1] }
57 sub set_event_timeout { shift->{event_timeout} = $_[1] }
58 sub set_node_gui { shift->{node_gui} = $_[1] }
59 sub set_cluster_ff { shift->{cluster_ff} = $_[1] }
60 sub set_pane1_pos { shift->{pane1_pos} = $_[1] }
61 sub set_pane2_pos { shift->{pane2_pos} = $_[1] }
62 sub set_pane3_pos { shift->{pane3_pos} = $_[1] }
63 sub set_window_height { shift->{window_height} = $_[1] }
64 sub set_window_width { shift->{window_width} = $_[1] }
65  
66 sub selected_node {
67 my $self = shift;
68 my $name = $self->selected_node_name;
69 return unless defined $name;
70 $name = $name->[0];
71 return unless $self->master;
72 return $self->master->get_node_by_name($name);
73 }
74  
75 sub selected_project {
76 my $self = shift;
77 my $id = $self->selected_project_id;
78 return unless defined $id;
79 $id = $id->[0];
80 return unless defined $id;
81 return unless $self->master;
82 return $self->master->get_project_by_id($id);
83 }
84  
85 sub selected_job {
86 my $self = shift;
87 my $id = $self->selected_job_id;
88 return unless defined $id;
89 $id = $id->[0];
90 return unless defined $id;
91 return $self->selected_project->get_job_by_id($id);
92 }
93  
94 # GUI Stuff ----------------------------------------------------------
95  
96 sub get_gui_state_file {
97 return $ENV{HOME}."/.dvdrip/cluster-gui.state";
98 }
99  
100 sub save_gui_state {
101 my $self = shift;
102  
103 my $file = $self->get_gui_state_file;
104 open (my $fh, "> $file") or die "can't write $file";
105 print $fh $self->pane1_pos."\t".
106 $self->pane2_pos."\t".
107 $self->pane3_pos."\t".
108 $self->window_width."\t".
109 $self->window_height."\n";
110 close $fh;
111
112 1;
113 }
114  
115 sub load_gui_state {
116 my $self = shift;
117  
118 my $file = $self->get_gui_state_file;
119 if ( ! -f $file ) {
120 $self->set_pane1_pos(170);
121 $self->set_pane2_pos(180);
122 $self->set_pane2_pos(220);
123 $self->set_window_height(700);
124 $self->set_window_width(700);
125 $self->save_gui_state;
126 return 1;
127 }
128  
129 open (my $fh, $file) or die "can't read $file";
130 my $line = <$fh>;
131 close $fh;
132  
133 chomp $line;
134 my @pos = split("\t", $line);
135  
136 $self->set_pane1_pos($pos[0]||170);
137 $self->set_pane2_pos($pos[1]||180);
138 $self->set_pane3_pos($pos[2]||220);
139 $self->set_window_width($pos[3]||700);
140 $self->set_window_height($pos[4]||700);
141 1;
142 }
143  
144 sub open_window {
145 my $self = shift;
146  
147 $self->load_gui_state;
148  
149 my $context = $self->get_context;
150 $context->set_object( cluster_gui => $self );
151  
152 my $cluster_ff = Gtk2::Ex::FormFactory->new(
153 context => $context,
154 sync => 1,
155 content => [
156 Gtk2::Ex::FormFactory::Window->new(
157 name => "cluster_window",
158 title => __ "dvd::rip - Cluster Control",
159 customize_hook => sub {
160 my ($gtk_window) = @_;
161 $_[0]->parent->set(
162 default_width => $self->window_width,
163 default_height => $self->window_height,
164 );
165 1;
166 },
167 closed_hook => sub {
168 $self->close_window;
169 },
170 content => [
171 $self->build_menu,
172 Gtk2::Ex::FormFactory::VBox->new (
173 object => "cluster",
174 expand => 1,
175 spacing => 0,
176 content => [
177 Gtk2::Ex::FormFactory::VPaned->new (
178 name => "cluster_vpane1",
179 attr => "cluster_gui.pane1_pos",
180 expand => 1,
181 content => [
182 $self->build_nodes_box,
183 Gtk2::Ex::FormFactory::VPaned->new (
184 name => "cluster_vpane2",
185 attr => "cluster_gui.pane2_pos",
186 content => [
187 $self->build_projects_box,
188 Gtk2::Ex::FormFactory::VPaned->new (
189 name => "cluster_vpane3",
190 attr => "cluster_gui.pane3_pos",
191 content => [
192 $self->build_jobs_box,
193 $self->build_log_box,
194 ],
195 ),
196 ],
197 ),
198 ],
199 ),
200 ],
201 ),
202 ],
203 ),
204 ],
205 );
206  
207 $self->set_cluster_ff($cluster_ff);
208 $self->set_form_factory($cluster_ff);
209  
210 $cluster_ff->build;
211 $cluster_ff->update;
212 $cluster_ff->show;
213  
214 1;
215 }
216  
217 sub close_window {
218 my $self = shift;
219  
220 $self->disconnect_master;
221  
222 my $cluster_ff = $self->cluster_ff;
223  
224 $cluster_ff->get_widget("cluster_vpane1")->widget_to_object;
225 $cluster_ff->get_widget("cluster_vpane2")->widget_to_object;
226 $cluster_ff->get_widget("cluster_vpane3")->widget_to_object;
227  
228 my ($win_width, $win_height) =
229 $cluster_ff->get_widget("cluster_window")->get_gtk_parent_widget->get_size;
230 $self->set_window_height($win_height);
231 $self->set_window_width($win_width);
232  
233 $self->save_gui_state;
234  
235 $self->get_context->set_object( cluster_gui => undef );
236  
237 $cluster_ff->close;
238 $self->set_cluster_ff(undef);
239  
240 Gtk2->main_quit if $self->exit_on_close;
241  
242 1;
243 }
244  
245 sub build_menu {
246 my $self = shift;
247
248 return Gtk2::Ex::FormFactory::Menu->new (
249 menu_tree => [
250 __"_File" => {
251 item_type => '<Branch>',
252 children => [
253 __"_Close window" => {
254 item_type => '<StockItem>',
255 extra_data => 'gtk-close',
256 callback => sub { $self->close_window },
257 accelerator => '<ctrl>w',
258 },
259 ],
260 },
261 __"_Master" => {
262 item_type => '<Branch>',
263 children => [
264 __"_Connect Master Daemon" => {
265 callback => sub { $self->connect_master },
266 item_type => '<StockItem>',
267 extra_data => 'gtk-connect',
268 accelerator => '<ctrl>M',
269 object => "!cluster",
270 },
271 __"_Disconnect from Master Daemon" => {
272 callback => sub { $self->disconnect_master },
273 item_type => '<StockItem>',
274 extra_data => 'gtk-disconnect',
275 accelerator => '<ctrl>D',
276 object => "cluster",
277 },
278 "sep" => {
279 item_type => "<Separator>",
280 },
281 __"Shutdown Master Daemon" => {
282 callback => sub { $self->shutdown_master },
283 item_type => '<StockItem>',
284 extra_data => 'gtk-media-stop',
285 object => "cluster",
286 },
287 ],
288 },
289 __"_Node" => {
290 item_type => '<Branch>',
291 children => [
292 __"_Add node" => {
293 callback => sub { $self->add_node },
294 item_type => '<StockItem>',
295 extra_data => 'gtk-add',
296 object => "cluster",
297 },
298 __"_Edit node" => {
299 callback => sub { $self->edit_node },
300 item_type => '<StockItem>',
301 extra_data => 'gtk-edit',
302 accelerator => '<ctrl>N',
303 active_cond => sub {
304 my $node = $self->selected_node or return;
305 my $state = $node->state;
306 $node->state ne 'running';
307 },
308 active_depends => "cluster_node",
309 },
310 __"_Start node" => {
311 callback => sub { $self->start_node },
312 item_type => '<StockItem>',
313 extra_data => 'gtk-execute',
314 active_cond => sub {
315 my $node = $self->selected_node or return;
316 $node->state eq 'stopped';
317 },
318 active_depends => "cluster_node",
319 },
320 __"Sto_p node" => {
321 callback => sub { $self->stop_node },
322 item_type => '<StockItem>',
323 extra_data => 'gtk-cancel',
324 active_cond => sub {
325 my $node = $self->selected_node or return;
326 $node->state ne 'stopped';
327 },
328 active_depends => "cluster_node",
329 },
330 __"_Remove node" => {
331 callback => sub { $self->remove_node },
332 item_type => '<StockItem>',
333 extra_data => 'gtk-delete',
334 active_cond => sub {
335 my $node = $self->selected_node or return;
336 my $state = $node->state;
337 $node->state ne 'running';
338 },
339 active_depends => "cluster_node",
340 },
341 ],
342 },
343 __"_Project" => {
344 item_type => '<Branch>',
345 children => [
346 __"_Edit project" => {
347 callback => sub { $self->edit_project },
348 item_type => '<StockItem>',
349 extra_data => 'gtk-edit',
350 active_cond => sub {
351 my $project = $self->selected_project or return;
352 $project->state eq 'not scheduled';
353 },
354 active_depends => "cluster_project",
355 },
356 __"_Start project" => {
357 callback => sub { $self->start_project },
358 item_type => '<StockItem>',
359 extra_data => 'gtk-execute',
360 active_cond => sub {
361 my $project = $self->selected_project or return;
362 $project->state eq 'not scheduled';
363 },
364 active_depends => "cluster_project",
365 },
366 __"_Cancel project" => {
367 callback => sub { $self->cancel_project },
368 item_type => '<StockItem>',
369 extra_data => 'gtk-cancel',
370 active_cond => sub {
371 my $project = $self->selected_project or return;
372 $project->state eq 'running';
373 },
374 active_depends => "cluster_project",
375 },
376 __"Res_tart project" => {
377 callback => sub { $self->restart_project },
378 item_type => '<StockItem>',
379 extra_data => 'gtk-redo',
380 active_cond => sub {
381 my $project = $self->selected_project or return;
382 $project->state eq 'cancelled';
383 },
384 active_depends => "cluster_project",
385 },
386 __"_Remove project" => {
387 callback => sub { $self->remove_project },
388 item_type => '<StockItem>',
389 extra_data => 'gtk-delete',
390 active_cond => sub {
391 my $project = $self->selected_project or return;
392 $project->state ne 'running';
393 },
394 active_depends => "cluster_project",
395 },
396 ],
397 },
398 ],
399 );
400 }
401  
402 sub build_projects_box {
403 my $self = shift;
404  
405 Gtk2::SimpleList->add_column_type(
406 'cluster_project_text',
407 type => "Glib::Scalar",
408 renderer => "Gtk2::CellRendererText",
409 attr => sub {
410 my ( $treecol, $cell, $model, $iter, $col_num ) = @_;
411 my $text = $model->get( $iter, $col_num );
412 my $state = $model->get( $iter, 3 );
413 my $run = $state eq 'running';
414 $cell->set( text => $text );
415 $cell->set( weight => $run ? 700 : 500 );
416 1;
417 },
418 );
419  
420 return Gtk2::Ex::FormFactory::VBox->new(
421 title => __ "Project queue",
422 expand => 1,
423 content => [
424 Gtk2::Ex::FormFactory::List->new(
425 attr => "cluster.projects_list",
426 attr_select => "cluster_gui.selected_project_id",
427 attr_select_column => 0,
428 scrollbars => [ "automatic", "automatic" ],
429 expand => 1,
430 columns => [
431 "id",
432 __ "Number",
433 __ "Project",
434 __ "State",
435 __ "Progress",
436 ],
437 types => [ "int", ("cluster_project_text") x 4 ],
438 selection_mode => "single",
439 customize_hook => sub {
440 my ($gtk_simple_list) = @_;
441 ( $gtk_simple_list->get_columns )[0]->set( visible => 0 );
442 1;
443 },
444 ),
445 Gtk2::Ex::FormFactory::HBox->new(
446 content => [
447 Gtk2::Ex::FormFactory::Button->new(
448 label => __ "Edit project",
449 stock => "gtk-edit",
450 clicked_hook => sub { $self->edit_project },
451 active_cond => sub {
452 my $project = $self->selected_project or return;
453 $project->state eq 'not scheduled';
454 },
455 active_depends => "cluster_project",
456 ),
457 Gtk2::Ex::FormFactory::Button->new(
458 label => __ "Start project",
459 stock => "gtk-execute",
460 clicked_hook => sub { $self->start_project },
461 active_cond => sub {
462 my $project = $self->selected_project or return;
463 $project->state eq 'not scheduled';
464 },
465 active_depends => "cluster_project",
466 ),
467 Gtk2::Ex::FormFactory::Button->new(
468 label => __"Cancel project",
469 stock => "gtk-cancel",
470 clicked_hook => sub { $self->cancel_project },
471 active_cond => sub {
472 my $project = $self->selected_project or return;
473 $project->state eq 'running';
474 },
475 active_depends => "cluster_project",
476 ),
477 Gtk2::Ex::FormFactory::Button->new(
478 label => __"Restart project",
479 stock => "gtk-redo",
480 clicked_hook => sub { $self->restart_project },
481 active_cond => sub {
482 my $project = $self->selected_project or return;
483 $project->state eq 'cancelled' ||
484 $project->state eq 'error';
485 },
486 active_depends => "cluster_project",
487 ),
488 Gtk2::Ex::FormFactory::Button->new(
489 label => __ "Remove project",
490 stock => "gtk-delete",
491 clicked_hook => sub { $self->remove_project },
492 active_cond => sub {
493 my $project = $self->selected_project or return;
494 $project->state ne 'running';
495 },
496 active_depends => "cluster_project",
497 ),
498 ],
499 ),
500 ],
501 );
502 }
503  
504 sub build_jobs_box {
505 my $self = shift;
506  
507 return Gtk2::Ex::FormFactory::VBox->new(
508 title => __"Job running status",
509 expand => 1,
510 content => [
511 Gtk2::Ex::FormFactory::ExecFlow->new(
512 name => "cluster_exec_flow",
513 attr => "cluster.exec_flow_job",
514 scrollbars => [ 'automatic', 'automatic' ],
515 expand => 1,
516 customize_hook => sub {
517 my ($tree_view) = @_;
518 $tree_view->signal_connect (
519 row_activated => sub {
520 my ($gtk_tree_view, $path) = @_;
521 my $model = $gtk_tree_view->get_model;
522 my $iter = $model->get_iter($path);
523 my $job_id = $model->get($iter, 2);
524 my $job = $self->master->get_job_from_id($job_id);
525 return 1 unless $job->get_error_message;
526 $self->long_message_window (
527 message => $job->get_error_message,
528 );
529 1;
530 }
531 );
532 },
533 ),
534 ],
535 );
536 }
537  
538 sub jobs_list {
539 my $self = shift;
540 my $project = $self->selected_project or return;
541 return $project->jobs_list;
542 }
543  
544 sub build_nodes_box {
545 my $self = shift;
546  
547 Gtk2::SimpleList->add_column_type(
548 'cluster_node_text',
549 type => "Glib::Scalar",
550 renderer => "Gtk2::CellRendererText",
551 attr => sub {
552 my ( $treecol, $cell, $model, $iter, $col_num ) = @_;
553 my $text = $model->get( $iter, $col_num );
554 my $state = $model->get( $iter, 4 );
555 my $run = $state !~ /stopped|idle|offline/;
556 $cell->set( text => $text );
557 $cell->set( weight => $run ? 700 : 500 );
558 1;
559 },
560 );
561  
562 return Gtk2::Ex::FormFactory::VBox->new(
563 title => __ "Registered Nodes",
564 expand => 1,
565 content => [
566 Gtk2::Ex::FormFactory::List->new(
567 attr => "cluster.nodes_list",
568 attr_select => "cluster_gui.selected_node_name",
569 attr_select_column => 0,
570 expand => 1,
571 height => 100,
572 scrollbars => [ "automatic", "automatic" ],
573 columns => [
574 "name",
575 __ "Number",
576 __ "Name",
577 __ "Job",
578 __ "Progress"
579 ],
580  
581 # types => [
582 # "int", ("cluster_node_text") x 4
583 # ],
584 selection_mode => "single",
585 customize_hook => sub {
586 my ($gtk_simple_list) = @_;
587 ( $gtk_simple_list->get_columns )[0]->set( visible => 0 );
588 1;
589 },
590 ),
591 Gtk2::Ex::FormFactory::HBox->new(
592 content => [
593 Gtk2::Ex::FormFactory::Button->new(
594 object => "cluster",
595 label => __ "Add node",
596 stock => "gtk-add",
597 clicked_hook => sub { $self->add_node },
598 ),
599 Gtk2::Ex::FormFactory::Button->new(
600 object => "cluster_node",
601 label => __ "Edit node",
602 stock => "gtk-edit",
603 clicked_hook => sub { $self->edit_node },
604 active_cond => sub {
605 my $node = $self->selected_node or return;
606 my $state = $node->state;
607 $node->state ne 'running';
608 },
609 active_depends => "cluster_node",
610 ),
611 Gtk2::Ex::FormFactory::Button->new(
612 object => "cluster_node",
613 label => __ "Start node",
614 stock => "gtk-execute",
615 clicked_hook => sub { $self->start_node },
616 active_cond => sub {
617 my $node = $self->selected_node or return;
618 $node->state eq 'stopped';
619 },
620 active_depends => "cluster_node",
621 ),
622 Gtk2::Ex::FormFactory::Button->new(
623 object => "cluster_node",
624 label => __ "Stop node",
625 stock => "gtk-cancel",
626 clicked_hook => sub { $self->stop_node },
627 active_cond => sub {
628 my $node = $self->selected_node or return;
629 $node->state ne 'stopped';
630 },
631 active_depends => "cluster_node",
632 ),
633 ],
634 ),
635 ],
636 );
637 }
638  
639 sub build_log_box {
640 my $self = shift;
641  
642 return Gtk2::Ex::FormFactory::VBox->new(
643 title => __ "Cluster control daemon log output",
644 height => 80,
645 expand => 1,
646 content => [
647 Gtk2::Ex::FormFactory::TextView->new(
648 scrollbars => [ "never", "always" ],
649 expand => 1,
650 properties => {
651 editable => 0,
652 cursor_visible => 0,
653 wrap_mode => "word",
654 },
655 customize_hook => sub {
656 my ($gtk_text_view) = @_;
657 my $font = Gtk2::Pango::FontDescription->from_string(
658 "mono 7.2");
659 $gtk_text_view->modify_font($font);
660 my $tag_table = Gtk2::TextTagTable->new;
661 $tag_table->add(
662 $self->create_text_tag(
663 "date", foreground => "#666666",
664 )
665 );
666 my $buffer = Gtk2::TextBuffer->new($tag_table);
667 $gtk_text_view->set_buffer($buffer);
668 $self->set_gtk_log_buffer($buffer);
669 $self->set_gtk_log_view($gtk_text_view);
670 1;
671 },
672 ),
673 ],
674 );
675 }
676  
677 sub connect_master {
678 my $self = shift;
679  
680 return if $self->master;
681  
682 if ( !$self->config('cluster_master_local') &&
683 !($self->config('cluster_master_server') &&
684 $self->config('cluster_master_port') ) ) {
685 $self->message_window (
686 message => __("You must first configure a ".
687 "cluster control daemon\n".
688 "in the Preferences dialog."),
689 );
690 return;
691 }
692  
693 my $server = $self->config('cluster_master_local')
694 ? 'localhost'
695 : $self->config('cluster_master_server');
696  
697 my $port = $self->config('cluster_master_port');
698  
699 my $rpc_client = Event::RPC::Client->new(
700 host => $server,
701 port => $port,
702 error_cb => sub { $self->client_server_error },
703 class_map => {
704 'Event::ExecFlow::Job' => 'dvd_rip::Event::ExecFlow::Job',
705 'Event::ExecFlow::Job::Group' => 'dvd_rip::Event::ExecFlow::Job::Group',
706 'Event::ExecFlow::Job::Command' => 'dvd_rip::Event::ExecFlow::Job::Command',
707 'Event::ExecFlow::Job::Code' => 'dvd_rip::Event::ExecFlow::Job::Code',
708 },
709 );
710  
711 eval { $rpc_client->connect };
712  
713 if ( not $rpc_client->get_connected ) {
714 if ( not $self->config('cluster_master_local') ) {
715 $self->error_window(
716 message => __x(
717 "Can't connect to master daemon on {server}:{port}.",
718 server => $server,
719 port => $port
720 )
721 );
722 return;
723 }
724  
725 # Ok, we try to start a local master daemon
726 system("dvdrip-master 3 >/dev/null 2>&1 &");
727  
728 sleep 1;
729  
730 eval { $rpc_client->connect };
731  
732 # give up, if we still have no connection
733 if ( not $rpc_client->get_connected ) {
734 $self->error_window(
735 message => __x(
736 "Can't start local master daemon on port {port}.\n".
737 "Execute the dvdrip-master program by hand to\n".
738 "see why it doesn't run.",
739 port => $port
740 )
741 );
742 return;
743 }
744 }
745  
746 my $master = Video::DVDRip::Cluster::Master->get_master();
747 $master->hello;
748  
749 $self->set_rpc_client($rpc_client);
750 $self->set_master($master);
751 $self->get_context->set_object( cluster => $master );
752 $self->get_context->set_object( "!cluster" => undef );
753 $self->get_context->update_object_widgets("cluster_node");
754 $self->get_context->update_object_widgets("cluster_project");
755  
756 my $sock = Event::RPC::Client->log_connect(
757 server => $server,
758 port => $port + 1,
759 );
760  
761 $self->set_log_socket($sock);
762  
763 $self->set_master_event_queue( [] );
764
765 my $log_buffer = "";
766 my $log_watcher = Gtk2::Helper->add_watch(
767 $sock->fileno,
768 'in',
769 sub {
770 return 1 unless $self->master;
771 if ( !sysread( $sock, $log_buffer, 4096, length($log_buffer) ) ) {
772 $self->client_server_error;
773 return 1;
774 }
775 my $buffer_offset;
776 while ( $log_buffer =~ /(.*\n)/mg ) {
777 my $line = $1;
778 chomp($line);
779 $buffer_offset = pos($log_buffer);
780 if ( $line =~ /EVENT\t(.*)/ ) {
781 $self->enqueue_master_event( split( "\t", $1 ) );
782 }
783 else {
784 my $buffer = $self->gtk_log_buffer;
785 $buffer->insert( $buffer->get_end_iter, $line . "\n" );
786 }
787 }
788 $log_buffer = substr($log_buffer, $buffer_offset);
789 1;
790 }
791 );
792  
793 my $event_timeout = Glib::Timeout->add(
794 200,
795 sub {
796 return unless $self->master;
797 $self->process_master_event_queue;
798 1;
799 }
800 );
801  
802 $self->set_log_watcher($log_watcher);
803 $self->set_event_timeout($event_timeout);
804  
805 1;
806 }
807  
808 sub disconnect_master {
809 my $self = shift;
810  
811 #-- remove all timeouts, sockets and watchers
812 Glib::Source->remove( $self->event_timeout )
813 if $self->event_timeout;
814  
815 close($self->log_socket)
816 if $self->log_socket;
817  
818 Gtk2::Helper->remove_watch( $self->log_watcher )
819 if $self->log_watcher;
820  
821 #-- disconnect client
822 my $rpc_client = $self->rpc_client;
823 $rpc_client->disconnect if $rpc_client;
824  
825 #-- destroy all objects which are connected to the master
826 $self->set_master(undef);
827 $self->set_log_watcher(undef);
828 $self->set_event_timeout(undef);
829 $self->set_log_socket(undef);
830 $self->set_rpc_client(undef);
831 $self->set_selected_project_id(undef);
832 $self->set_selected_node_name(undef);
833  
834 #-- now (and not earlier!) destroy the objects in the context.
835 #-- if that would happen bevor undef'ing the attributes above
836 #-- FormFactory will trigger actions on the line again, which
837 #-- is broken already!
838 $self->get_context->set_object( cluster_project => undef );
839 $self->get_context->set_object( cluster_node => undef );
840 $self->get_context->set_object( cluster => undef );
841 $self->get_context->set_object( "!cluster" => 1 );
842  
843 #-- close windows which are probably open
844 my $cluster_node_gui =
845 $self->get_context->get_object("cluster_node_gui");
846  
847 my $cluster_title_gui =
848 $self->get_context->get_object("cluster_title_gui");
849  
850 $cluster_node_gui->close_window() if $cluster_node_gui;
851 $cluster_title_gui->close_window() if $cluster_title_gui;
852  
853 1;
854 }
855  
856 sub enqueue_master_event {
857 my $self = shift;
858 my ( $event, @args ) = @_;
859  
860 my $queue = $self->master_event_queue;
861 push @{$queue}, [ $event, \@args ];
862  
863 return;
864 }
865  
866 {
867 my %event2action = (
868 PROJECT_UPDATE => "update_projects_list",
869 PROJECT_LIST_UPDATE => "update_projects_list",
870 PROJECT_DELETED => "update_projects_list",
871 JOB_PLAN_UPDATE => "update_jobs_list[ARG0]",
872 JOB_UPDATE => "update_job[ARG0]",
873 JOB_ADDED => "update_job_add[ARG0]",
874 JOB_REMOVED => "update_job_removed[ARG0]",
875 NODE_UPDATE => "update_nodes_list",
876 NODE_DELETED => "update_nodes_list",
877 JOB_PROGRESS_UPDATE => "update_job_progress[ARG0,ARG1,ARG2,ARG3]",
878 NODE_PROGRESS_UPDATE => "update_node_progress[ARG0,ARG1,ARG2]",
879 NODE_TEST_FINISHED => "node_test_finished",
880 NO_MASTER_NODE_FOUND => "no_master_node_found",
881 );
882  
883 sub process_master_event_queue {
884 my $self = shift;
885 my $queue = $self->master_event_queue;
886 return unless $self->master;
887 return if @{$queue} == 0;
888  
889 my $cluster_ff = $self->cluster_ff;
890  
891 my %actions_seen;
892 my @actions;
893 for ( my $i = @{$queue} - 1; $i >= 0; --$i ) {
894 my $action = $event2action{ $queue->[$i]->[0] };
895 if ( !$action ) {
896 warn "Unknown master event $queue->[$i]->[0]";
897 next;
898 }
899 if ( $action =~ /ARG/ ) {
900 $action =~ s/ARG0/$queue->[$i]->[1]->[0]/;
901 $action =~ s/ARG1/$queue->[$i]->[1]->[1]/;
902 $action =~ s/ARG2/$queue->[$i]->[1]->[2]/;
903 $action =~ s/ARG3/$queue->[$i]->[1]->[3]/;
904 }
905 next if $actions_seen{$action};
906 $actions_seen{$action} = 1;
907 unshift @actions, [ $action, $queue->[$i] ];
908 }
909  
910 # print "Queue items: ".@{$queue}." => actions ".@actions."\n";
911  
912 @{$queue} = ();
913  
914 my $context = $self->get_context;
915  
916 my $node_slist
917 = $self->cluster_ff->get_widget("cluster.nodes_list")
918 ->get_gtk_widget;
919  
920 foreach my $action_item (@actions) {
921 my $action = $action_item->[0];
922 my $event = $action_item->[1]->[0];
923 my $args = $action_item->[1]->[1];
924  
925 if ( $action eq 'update_projects_list' ) {
926 $context->update_object_attr_widgets("cluster.projects_list");
927 $context->update_object_widgets("cluster_project");
928  
929 }
930 elsif ( $action =~ /^update_jobs_list/ ) {
931 my ($project_id) = @{$args};
932 next unless $self->selected_project_id;
933  
934 # print "update_jobs_list: $project_id <> ".$self->selected_project_id->[0]."\n";
935 $context->update_object_attr_widgets("cluster_gui.jobs_list")
936 if $project_id == $self->selected_project_id->[0];
937  
938 }
939 elsif ( $action =~ /^update_job_progress/ ) {
940 next unless $self->selected_project_id;
941 my ( $project_id, $job_nr, $state, $progress ) = @{$args};
942 next if $project_id != $self->selected_project_id->[0];
943 # $job_slist->{data}->[ $job_nr - 1 ]->[4] = $state;
944 # $job_slist->{data}->[ $job_nr - 1 ]->[5] = $progress;
945  
946 }
947 elsif ( $action =~ /^update_node_progress/ ) {
948 my ( $name, $job, $progress ) = @{$args};
949 for ( my $i = 0; $i < @{ $node_slist->{data} }; ++$i ) {
950 if ( $node_slist->{data}->[$i]->[0] eq $name ) {
951 $node_slist->{data}->[$i]->[3] = $job;
952 $node_slist->{data}->[$i]->[4] = $progress;
953 last;
954 }
955 }
956  
957 }
958 elsif ( $action =~ /^update_job_add/ ) {
959 my ($job_id) = @{$args};
960 my $job = $self->master->get_job_from_id($job_id);
961 $cluster_ff->get_widget("cluster_exec_flow")->add_job($job)
962 if $job;
963  
964 }
965 elsif ( $action =~ /^update_job_removed/ ) {
966 my ($job_id) = @{$args};
967 $cluster_ff->get_widget("cluster_exec_flow")->remove_job_with_id($job_id);
968  
969 }
970 elsif ( $action =~ /^update_job/ ) {
971 my ($job_id) = @{$args};
972 my $job = $self->master->get_job_from_id($job_id);
973 $cluster_ff->get_widget("cluster_exec_flow")->update_job($job)
974 if $job;
975  
976 }
977 elsif ( $action eq 'update_nodes_list' ) {
978 $context->update_object_attr_widgets("cluster.nodes_list");
979 $context->update_object_attr_widgets("cluster_node");
980  
981 }
982 elsif ( $action eq 'no_master_node_found' ) {
983 $self->error_window(
984 message => __ "Please configure the master node first",
985 );
986 my $node_gui = $self->node_gui;
987 $node_gui->stop_node_test_progress if $node_gui;
988 }
989 elsif ( $action eq 'node_test_finished' ) {
990 my $node_gui = $self->node_gui;
991 $node_gui->node_test_finished if $node_gui;
992 }
993 }
994  
995 1;
996 }
997 }
998  
999 sub client_server_error {
1000 my $self = shift;
1001  
1002 return unless $self->master;
1003  
1004 $self->error_window (
1005 message => __"Connection to master daemon lost!",
1006 );
1007  
1008 $self->disconnect_master;
1009  
1010 1;
1011 }
1012  
1013 sub add_project {
1014 my $self = shift;
1015 my %par = @_;
1016 my ( $project, $title ) = @par{ 'project', 'title' };
1017  
1018 my $cluster_project = Video::DVDRip::Cluster::Project->new(
1019 project => $project,
1020 title_nr => $title->nr,
1021 );
1022  
1023 $self->master->add_project( project => $cluster_project, );
1024  
1025 $self->edit_project(
1026 project => $cluster_project,
1027 just_added => 1,
1028 );
1029  
1030 1;
1031 }
1032  
1033 sub edit_project {
1034 my $self = shift;
1035 my %par = @_;
1036 my ($project, $just_added) = @par{'project','just_added'};
1037  
1038 $project ||= $self->selected_project;
1039 return 1 if not $project;
1040  
1041 Video::DVDRip::GUI::Cluster::Title->new(
1042 cluster_ff => $self->cluster_ff,
1043 master => $self->master,
1044 title => $project->title,
1045 just_added => $just_added,
1046 )->open_window;
1047  
1048 1;
1049 }
1050  
1051 sub remove_project {
1052 my $self = shift;
1053  
1054 my $project = $self->selected_project;
1055 return if not $project;
1056  
1057 $self->confirm_window(
1058 message => __"Do you want to remove the selected project?",
1059 yes_callback => sub {
1060 return unless $self->master;
1061 return if $project->state eq 'running';
1062 $self->master->remove_project( project => $project );
1063 },
1064 );
1065  
1066 1;
1067 }
1068  
1069 sub add_node {
1070 my $self = shift;
1071  
1072 my $node_gui = Video::DVDRip::GUI::Cluster::Node->new(
1073 cluster_ff => $self->cluster_ff,
1074 master => $self->master,
1075 node => Video::DVDRip::Cluster::Node->new,
1076 just_added => 1,
1077 );
1078  
1079 $node_gui->open_window;
1080  
1081 $self->set_node_gui($node_gui);
1082  
1083 1;
1084 }
1085  
1086 sub edit_node {
1087 my $self = shift;
1088  
1089 my $node = $self->selected_node;
1090 return 1 if not $node;
1091 return 1 if $node->state eq 'running';
1092  
1093 my $node_gui = Video::DVDRip::GUI::Cluster::Node->new(
1094 cluster_ff => $self->cluster_ff,
1095 master => $self->master,
1096 node => $node,
1097 );
1098  
1099 $node_gui->open_window;
1100  
1101 $self->set_node_gui($node_gui);
1102  
1103 1;
1104 }
1105  
1106 sub stop_node {
1107 my $self = shift;
1108  
1109 my $node = $self->selected_node;
1110 return 1 if not $node;
1111 return 1 if $node->state eq 'stopped';
1112  
1113 $node->stop;
1114  
1115 1;
1116 }
1117  
1118 sub start_node {
1119 my $self = shift;
1120  
1121 my $node = $self->selected_node;
1122 return 1 if not $node;
1123 return 1
1124 if $node->state ne 'stopped'
1125 and $node->state ne 'aborted';
1126  
1127 $node->start;
1128  
1129 1;
1130 }
1131  
1132 sub remove_node {
1133 my $self = shift;
1134  
1135 my $node = $self->selected_node;
1136 return 1 if not $node;
1137 return 1 if $node->state eq 'running';
1138  
1139 $self->confirm_window(
1140 message => __ "Do you want to remove the selected node?",
1141 yes_callback => sub {
1142 return unless $self->master;
1143 $self->master->remove_node( node => $self->selected_node );
1144 },
1145 );
1146  
1147 1;
1148 }
1149  
1150 sub select_job {
1151 my $self = shift;
1152 my ( $widget, $row ) = @_;
1153  
1154 $self->set_selected_job_row($row);
1155 $self->set_selected_job_id( $self->gtk_widgets->{job_clist_ids}->[$row] );
1156  
1157 1;
1158 }
1159  
1160 sub select_node {
1161 my $self = shift;
1162 my ( $widget, $row ) = @_;
1163  
1164 $self->set_selected_node_row($row);
1165 $self->set_selected_node( $self->master->nodes->[$row] );
1166  
1167 1;
1168 }
1169  
1170 sub start_project {
1171 my $self = shift;
1172  
1173 my $project = $self->selected_project;
1174 return if not $project;
1175 return if $project->state ne 'not scheduled';
1176  
1177 $self->master->schedule_project( project => $project );
1178  
1179 1;
1180 }
1181  
1182 sub cancel_project {
1183 my $self = shift;
1184  
1185 my $project = $self->selected_project;
1186 return if not $project;
1187 return if $project->state ne 'running';
1188  
1189 $self->master->cancel_project( project => $project );
1190  
1191 1;
1192 }
1193  
1194 sub restart_project {
1195 my $self = shift;
1196  
1197 my $project = $self->selected_project;
1198 return if not $project;
1199  
1200 $self->master->restart_project( project => $project );
1201  
1202 1;
1203 }
1204  
1205 sub shutdown_master {
1206 my $self = shift;
1207  
1208 $self->confirm_window(
1209 message => __
1210 "Do you really want to shutdown\nthe Cluster Control Daemon?",
1211 yes_callback => sub {
1212 return unless $self->master;
1213 $self->master->shutdown;
1214 $self->disconnect_master;
1215 1;
1216 },
1217 );
1218  
1219 1;
1220 }
1221  
1222 1;