further detail OR->connect arguments, and document other bits
[scpubgit/Object-Remote.git] / lib / Object / Remote / Node.pm
1 package Object::Remote::Node;
2
3 use strictures 1;
4 use Object::Remote::Connector::STDIO;
5 use Object::Remote::Logging qw(:log :dlog);
6 use Object::Remote::WatchDog;
7 use Object::Remote;
8
9 sub run {
10   my ($class, %args) = @_;
11   log_trace { "run() has been invoked on remote node" };
12
13   my $c = Object::Remote::Connector::STDIO->new->connect;
14
15   $c->register_class_call_handler;
16
17   my $loop = Object::Remote->current_loop;
18
19   $c->on_close->on_ready(sub {
20     log_debug { "Node connection with call handler has closed" };
21     $loop->want_stop
22   });
23
24   Dlog_trace { "Node is sending 'Shere' to $_" } $c->send_to_fh;
25   print { $c->send_to_fh } "Shere\n";
26
27   log_debug { "Node is going to start the run loop" };
28   #TODO the alarm should be reset after the run loop starts
29   #at a minimum - the remote side node should probably send
30   #a command that clears the alarm in all instances - even
31   #if the Object::Remote::Watchdog is not being used
32   if ($args{watchdog_timeout}) {
33     Object::Remote::WatchDog->instance(timeout => $args{watchdog_timeout});
34   } else {
35     #reset connection watchdog from the fatnode
36     alarm(0);
37   }
38   $loop->want_run;
39   $loop->run_while_wanted;
40   log_debug { "Run loop invocation in node has completed" };
41 }
42
43 1;
44
45 =head1 NAME
46
47 Object::Remote::Node - A minimum remote OR command processing loop
48
49 =head1 SYNOPSIS
50
51   use Object::Remote::Node;
52   
53   Object::Remote::Node->run(%args);
54
55 =head1 DESCRIPTION
56
57 The minimum amount of code necessary to read OR JSON commands from STDIN and
58 send responses to STDOUT after processing. Uses
59 L<Object::Remote::Connector::STDIO>.
60
61 =head1 ARGUMENTS
62
63 =head2 watchdog_timeout
64
65 If provided sets up a L<Object::Remote::WatchDog> with the timeout set to the
66 value in seconds specified by this argument.
67
68 If not provided or undef/0, attempts to cancel any existing watch dogs. ???
69
70 =cut