more log lines - found deadlock where controller blocks on read seemingly outside...
[scpubgit/Object-Remote.git] / lib / Object / Remote.pm
CommitLineData
9e72f0cf 1package Object::Remote;
2
3use Object::Remote::MiniLoop;
676438a1 4use Object::Remote::Handle;
4a9fa1a5 5use Object::Remote::Logging qw( :log );
e144d525 6use Module::Runtime qw(use_module);
9e72f0cf 7
936fd146 8our $VERSION = '0.002003'; # 0.2.3
52ea6942 9
4a9fa1a5 10BEGIN {
11 Object::Remote::Logging->init_logging;
12}
13
84b04178 14sub new::on {
15 my ($class, $on, @args) = @_;
4c17fea5 16 my $conn = __PACKAGE__->connect($on);
5d59cb98 17 log_trace { sprintf("constructing instance of $class on connection for child pid of %i", $conn->child_pid) };
11dbd4a0 18 return $conn->remote_object(class => $class, args => \@args);
84b04178 19}
20
624b459b 21sub can::on {
22 my ($class, $on, $name) = @_;
23 my $conn = __PACKAGE__->connect($on);
5d59cb98 24 log_trace { "Invoking remote \$class->can('$name')" };
624b459b 25 return $conn->remote_sub(join('::', $class, $name));
26}
27
676438a1 28sub new {
29 shift;
30 Object::Remote::Handle->new(@_)->proxy;
9e72f0cf 31}
32
4c17fea5 33sub connect {
34 my ($class, $to) = @_;
fbd3b8ec 35 use_module('Object::Remote::Connection')->maybe::start::new_from_spec($to);
4c17fea5 36}
37
9e72f0cf 38sub current_loop {
39 our $Current_Loop ||= Object::Remote::MiniLoop->new
40}
41
9e72f0cf 421;
b9a9982d 43
44=head1 NAME
45
46Object::Remote - Call methods on objects in other processes or on other hosts
47
48=head1 SYNOPSIS
49
50Creating a connection:
51
52 use Object::Remote;
53
54 my $conn = Object::Remote->connect('myserver'); # invokes ssh
55
56Calling a subroutine:
57
58 my $capture = IPC::System::Simple->can::on($conn, 'capture');
59
60 warn $capture->('uptime');
61
62Using an object:
63
64 my $eval = Eval::WithLexicals->new::on($conn);
65
66 $eval->eval(q{my $x = `uptime`});
67
68 warn $eval->eval(q{$x});
69
70Importantly: 'myserver' only requires perl 5.8+ - no non-core modules need to
71be installed on the far side, Object::Remote takes care of it for you!
72
73=head1 DESCRIPTION
74
75Object::Remote allows you to create an object in another process - usually
76one running on another machine you can connect to via ssh, although there
77are other connection mechanisms available.
78
79The idea here is that in many cases one wants to be able to run a piece of
80code on another machine, or perhaps many other machines - but without having
81to install anything on the far side.
82
83=head1 COMPONENTS
84
85=head2 Object::Remote
86
87The "main" API, which provides the L</connect> method to create a connection
88to a remote process/host, L</new::on> to create an object on a connection,
89and L</can::on> to retrieve a subref over a connection.
90
91=head2 Object::Remote::Connection
92
93The object representing a connection, which provides the
94L<Object::Remote::Connection/remote_object> and
95L<Object::Remote::Connection/remote_sub> methods that are used by
96L</new::on> and L</can::on> to return proxies for objects and subroutines
97on the far side.
98
99=head2 Object::Remote::Future
100
101Code for dealing with asynchronous operations, which provides the
102L<Object::Remote::Future/start::method> syntax for calling a possibly
103asynchronous method without blocking, and
104L<Object::Remote::Future/await_future> and L<Object::Remote::Future/await_all>
105to block until an asynchronous call completes or fails.
106
107=head1 METHODS
108
109=head2 connect
110
111 my $conn = Object::Remote->connect('-'); # fork()ed connection
112
113 my $conn = Object::Remote->connect('myserver'); # connection over ssh
114
115 my $conn = Object::Remote->connect('user@myserver'); # connection over ssh
116
117 my $conn = Object::Remote->connect('root@'); # connection over sudo
118
119=head2 new::on
120
121 my $eval = Eval::WithLexicals->new::on($conn);
122
123 my $eval = Eval::WithLexicals->new::on('myserver'); # implicit connect
124
125 my $obj = Some::Class->new::on($conn, %args); # with constructor arguments
126
127=head2 can::on
128
129 my $hostname = Sys::Hostname->can::on($conn, 'hostname');
130
131 my $hostname = Sys::Hostname->can::on('myserver', 'hostname');
132
133=head1 SUPPORT
134
135IRC: #web-simple on irc.perl.org
136
137=head1 AUTHOR
138
139mst - Matt S. Trout (cpan:MSTROUT) <mst@shadowcat.co.uk>
140
141=head1 CONTRIBUTORS
142
143phaylon - Robert Sedlacek (cpan:PHAYLON) <r.sedlacek@shadowcat.co.uk>
144
145=head1 SPONSORS
146
147Parts of this code were paid for by
148
149 Socialflow L<http://www.socialflow.com>
150
151 Shadowcat Systems L<http://www.shadow.cat>
152
153=head1 COPYRIGHT
154
155Copyright (c) 2012 the Object::Remote L</AUTHOR>, L</CONTRIBUTORS> and
156L</SPONSORS> as listed above.
157
158=head1 LICENSE
159
160This library is free software and may be distributed under the same terms
161as perl itself.
162
163=cut