/usr/share/perl5/Event/RPC/Client.pm is in libevent-rpc-perl 1.04-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 | # $Id: Client.pm,v 1.18 2013-02-02 11:24:31 joern Exp $
#-----------------------------------------------------------------------
# Copyright (C) 2002-2006 Jörn Reder <joern AT zyn.de>.
# All Rights Reserved. See file COPYRIGHT for details.
#
# This module is part of Event::RPC, which is free software; you can
# redistribute it and/or modify it under the same terms as Perl itself.
#-----------------------------------------------------------------------
package Event::RPC::Client;
use Event::RPC;
use Event::RPC::Message;
use Carp;
use strict;
use IO::Socket::INET;
sub get_client_version { $Event::RPC::VERSION }
sub get_client_protocol { $Event::RPC::PROTOCOL }
sub get_host { shift->{host} }
sub get_port { shift->{port} }
sub get_sock { shift->{sock} }
sub get_timeout { shift->{timeout} }
sub get_classes { shift->{classes} }
sub get_class_map { shift->{class_map} }
sub get_loaded_classes { shift->{loaded_classes} }
sub get_error_cb { shift->{error_cb} }
sub get_ssl { shift->{ssl} }
sub get_ssl_ca_file { shift->{ssl_ca_file} }
sub get_ssl_ca_path { shift->{ssl_ca_path} }
sub get_auth_user { shift->{auth_user} }
sub get_auth_pass { shift->{auth_pass} }
sub get_connected { shift->{connected} }
sub get_server { shift->{server} }
sub get_server_version { shift->{server_version} }
sub get_server_protocol { shift->{server_protocol} }
sub set_host { shift->{host} = $_[1] }
sub set_port { shift->{port} = $_[1] }
sub set_sock { shift->{sock} = $_[1] }
sub set_timeout { shift->{timeout} = $_[1] }
sub set_classes { shift->{classes} = $_[1] }
sub set_class_map { shift->{class_map} = $_[1] }
sub set_loaded_classes { shift->{loaded_classes} = $_[1] }
sub set_error_cb { shift->{error_cb} = $_[1] }
sub set_ssl { shift->{ssl} = $_[1] }
sub set_ssl_ca_file { shift->{ssl_ca_file} = $_[1] }
sub set_ssl_ca_path { shift->{ssl_ca_path} = $_[1] }
sub set_auth_user { shift->{auth_user} = $_[1] }
sub set_auth_pass { shift->{auth_pass} = $_[1] }
sub set_connected { shift->{connected} = $_[1] }
sub set_server { shift->{server} = $_[1] }
sub set_server_version { shift->{server_version} = $_[1] }
sub set_server_protocol { shift->{server_protocol} = $_[1] }
sub new {
my $class = shift;
my %par = @_;
my ($server, $host, $port, $classes, $class_map, $error_cb, $timeout) =
@par{'server','host','port','classes','class_map','error_cb','timeout'};
my ($ssl, $ssl_ca_file, $auth_user, $auth_pass) =
@par{'ssl','ssl_ca_file','auth_user','auth_pass'};
$server ||= '';
$host ||= '';
if ( $server ne '' and $host eq '' ) {
warn "Option 'server' is deprecated. Use 'host' instead.";
$host = $server;
}
my $self = bless {
host => $server,
server => $host,
port => $port,
timeout => $timeout,
classes => $classes,
class_map => $class_map,
ssl => $ssl,
ssl_ca_file => $ssl_ca_file,
auth_user => $auth_user,
auth_pass => $auth_pass,
error_cb => $error_cb,
loaded_classes => {},
connected => 0,
}, $class;
return $self;
}
sub connect {
my $self = shift;
croak "Client is already connected" if $self->get_connected;
my $ssl = $self->get_ssl;
my $server = $self->get_server;
my $port = $self->get_port;
my $timeout = $self->get_timeout;
if ( $ssl ) {
eval { require IO::Socket::SSL };
croak "SSL requested, but IO::Socket::SSL not installed" if $@;
}
my $sock;
if ( $ssl ) {
my @verify_opts;
if ( $self->get_ssl_ca_file or $self->get_ssl_ca_path ) {
push @verify_opts, (
SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_PEER(),
SSL_ca_file => $self->get_ssl_ca_file,
SSL_ca_path => $self->get_ssl_ca_path,
);
}
else {
push @verify_opts, (
SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_NONE(),
);
}
$sock = IO::Socket::SSL->new(
Proto => 'tcp',
PeerPort => $port,
PeerAddr => $server,
Type => SOCK_STREAM,
Timeout => $timeout,
@verify_opts
)
or croak "Can't open SSL connection to $server:$port: $IO::Socket::SSL::ERROR";
}
else {
$sock = IO::Socket::INET->new(
Proto => 'tcp',
PeerPort => $port,
PeerAddr => $server,
Type => SOCK_STREAM,
Timeout => $timeout,
)
or croak "Can't open connection to $server:$port - $!";
}
$sock->autoflush(1);
$self->set_sock($sock);
$self->check_version;
my $auth_user = $self->get_auth_user;
my $auth_pass = $self->get_auth_pass;
if ( $auth_user ) {
my $rc = $self->send_request(
{ cmd => 'auth',
user => $auth_user,
pass => $auth_pass,
}
);
if ( not $rc->{ok} ) {
$self->disconnect;
croak $rc->{msg};
}
}
if ( not $self->get_classes ) {
$self->load_all_classes;
}
else {
$self->load_classes;
}
$self->set_connected(1);
1;
}
sub log_connect {
my $class = shift;
my %par = @_;
my ( $server, $port ) = @par{ 'server', 'port' };
my $sock = IO::Socket::INET->new(
Proto => 'tcp',
PeerPort => $port,
PeerAddr => $server,
Type => SOCK_STREAM
)
or croak "Can't open connection to $server:$port - $!";
return $sock;
}
sub disconnect {
my $self = shift;
close( $self->get_sock ) if $self->get_sock;
$self->set_connected(0);
1;
}
sub DESTROY {
shift->disconnect;
}
sub error {
my $self = shift;
my ($message) = @_;
my $error_cb = $self->get_error_cb;
if ($error_cb) {
&$error_cb( $self, $message );
}
else {
die "Unhandled error in client/server communication: $message";
}
1;
}
sub check_version {
my $self = shift;
my $rc = $self->send_request( { cmd => 'version', } );
$self->set_server_version( $rc->{version} );
$self->set_server_protocol( $rc->{protocol} );
if ( $rc->{version} ne $self->get_client_version ) {
warn "Event::RPC warning: server version $rc->{version} != "
. "client version "
. $self->get_client_version;
}
if ( $rc->{protocol} < $self->get_client_protocol ) {
die "FATAL: Server protocol version $rc->{protocol} < "
. "client protocol version "
. $self->get_client_protocol;
}
1;
}
sub load_all_classes {
my $self = shift;
my $rc = $self->send_request( { cmd => 'class_info_all', } );
my $class_info_all = $rc->{class_info_all};
foreach my $class ( keys %{$class_info_all} ) {
$self->load_class( $class, $class_info_all->{$class} );
}
1;
}
sub load_classes {
my $self = shift;
my $classes = $self->get_classes;
my %classes;
@classes{ @{$classes} } = (1) x @{$classes};
my $rc = $self->send_request( { cmd => 'classes_list', } );
foreach my $class ( @{ $rc->{classes} } ) {
next if not $classes{$class};
$classes{$class} = 0;
my $rc = $self->send_request(
{ cmd => 'class_info',
class => $class,
}
);
$self->load_class( $class, $rc->{methods} );
}
foreach my $class ( @{$classes} ) {
warn "WARNING: Class '$class' not exported by server"
if $classes{$class};
}
1;
}
sub load_class {
my $self = shift;
my ( $class, $methods ) = @_;
my $loaded_classes = $self->get_loaded_classes;
return 1 if $loaded_classes->{$class};
$loaded_classes->{$class} = 1;
my $local_method;
my $class_map = $self->get_class_map;
my $local_class = $class_map->{$class} || $class;
# create local destructor for this class
{
no strict 'refs';
my $local_method = $local_class . '::' . "DESTROY";
*$local_method = sub {
return if not $self->get_connected;
my $oid_ref = shift;
$self->send_request({
cmd => "client_destroy",
oid => ${$oid_ref},
});
};
}
# create local methods for this class
foreach my $method ( keys %{$methods} ) {
$local_method = $local_class . '::' . $method;
my $method_type = $methods->{$method};
if ( $method_type eq '_constructor' ) {
# this is a constructor for this class
my $request_method = $class . '::' . $method;
no strict 'refs';
*$local_method = sub {
shift;
my $rc = $self->send_request({
cmd => 'new',
method => $request_method,
params => \@_,
});
my $oid = $rc->{oid};
return bless \$oid, $local_class;
};
}
elsif ( $method_type eq '1' ) {
# this is a simple method
my $request_method = $method;
no strict 'refs';
*$local_method = sub {
my $oid_ref = shift;
my $rc = $self->send_request({
cmd => 'exec',
oid => ${$oid_ref},
method => $request_method,
params => \@_,
});
return unless $rc;
$rc = $rc->{rc};
return @{$rc} if wantarray;
return $rc->[0];
};
}
else {
# this is a object returner
my $request_method = $method;
no strict 'refs';
*$local_method = sub {
my $oid_ref = shift;
my $rc = $self->send_request({
cmd => 'exec',
oid => ${$oid_ref},
method => $request_method,
params => \@_,
});
return unless $rc;
$rc = $rc->{rc};
foreach my $val ( @{$rc} ) {
if ( ref $val eq 'ARRAY' ) {
foreach my $list_elem ( @{$val} ) {
my ($class) = split( "=", "$list_elem", 2 );
$self->load_class($class)
unless $loaded_classes->{$class};
my $list_elem_copy = $list_elem;
$list_elem = \$list_elem_copy;
bless $list_elem,
( $class_map->{$class} || $class );
}
}
elsif ( ref $val eq 'HASH' ) {
foreach my $hash_elem ( values %{$val} ) {
my ($class) = split( "=", "$hash_elem", 2 );
$self->load_class($class)
unless $loaded_classes->{$class};
my $hash_elem_copy = $hash_elem;
$hash_elem = \$hash_elem_copy;
bless $hash_elem,
( $class_map->{$class} || $class );
}
}
elsif ( defined $val ) {
my ($class) = split( "=", "$val", 2 );
$self->load_class($class)
unless $loaded_classes->{$class};
my $val_copy = $val;
$val = \$val_copy;
bless $val, ( $class_map->{$class} || $class );
}
}
return @{$rc} if wantarray;
return $rc->[0];
};
}
}
return $local_class;
}
sub send_request {
my $self = shift;
my ($request) = @_;
my $message = Event::RPC::Message->new( $self->get_sock );
$message->write_blocked($request);
my $rc = eval { $message->read_blocked };
if ($@) {
$self->error($@);
return;
}
if ( not $rc->{ok} ) {
$rc->{msg} .= "\n" if not $rc->{msg} =~ /\n$/;
croak ("$rc->{msg} -- called via Event::RPC::Client");
}
return $rc;
}
1;
__END__
=encoding latin1
=head1 NAME
Event::RPC::Client - Client API to connect to Event::RPC Servers
=head1 SYNOPSIS
use Event::RPC::Client;
my $rpc_client = Event::RPC::Client->new (
#-- Required arguments
host => "localhost",
port => 5555,
#-- Optional arguments
classes => [ "Event::RPC::Test" ],
class_map => { "Event::RPC::Test" => "My::Event::RPC::Test" },
ssl => 1,
ssl_ca_file => "some/ca.crt",
ssl_ca_path => "some/ca/dir",
timeout => 10,
auth_user => "fred",
auth_pass => Event::RPC->crypt("fred",$password),
error_cb => sub {
my ($client, $error) = @_;
print "An RPC error occured: $error\n";
$client->disconnect;
exit;
},
);
$rpc_client->connect;
#-- And now use classes and methods to which the
#-- server allows access via RPC, here My::TestModule
#-- from the Event::RPC::Server manpage SYNPOSIS.
my $obj = My::TestModule->new( data => "foobar" );
print "obj says hello: ".$obj->hello."\n";
$obj->set_data("new foobar");
print "updated data: ".$obj->get_data."\n";
$rpc_client->disconnect;
=head1 DESCRIPTION
Use this module to write clients accessing objects and methods
exported by a Event::RPC driven server.
Just connect to the server over the network, optionally with
SSL and user authentication, and then simply use the exported classes
and methods like having them locally in the client.
General information about the architecture of Event::RPC driven
applications is collected in the Event::RPC manpage.
The following documentation describes the client connection
options in detail.
=head1 CONFIGURATION OPTIONS
You need to specify at least the server hostname and TCP port
to connect a Event::RPC server instance. If the server requires
a SSL connection or user authentication you need to supply
the corresponding options as well, otherwise connecting will
fail.
All options described here may be passed to the new() constructor of
Event::RPC::Client. As well you may set or modify them using set_OPTION style
mutators, but not after connect() was called!
All options may be read using get_OPTION style accessors.
=head2 REQUIRED OPTIONS
These are necessary to connect the server:
=over 4
=item B<server>
This is the hostname of the server running Event::RPC::Server.
Use a IP address or DNS name here.
=item B<port>
This is the TCP port the server is listening to.
=back
=head2 NETWORK OPTIONS
=over 4
=item B<timeout>
Specify a timeout (in seconds), which is applied when connecting
the server.
=back
=head2 CLASS IMPORT OPTION
=over 4
=item B<classes>
This is reference to a list of classes which should be imported
into the client. You get a warning if you request a class which
is not exported by the server.
By default all server classes are imported. Use this feature if
your server exports a huge list of classes, but your client
doesn't need all of them. This saves memory in the client and
connect performance increases.
=item B<class_map>
Optionally you can map the class names from the server to a
different name on the local client using the B<class_map> hash.
This is necessary if you like to use the same classes locally
and remotely. Imported classes from the server are by default
registered under the same name on the client, so this conflicts
with local classes named identically.
On the client you access the remote classes under the name
assigned in the class map. For example with this map
class_map => { "Event::ExecFlow::Job" => "_srv::Event::ExecFlow::Job" }
you need to write this on the client, if you like to create
an object remotely on the server:
my $server_job = _srv::Event::ExecFlow::Job->new ( ... );
and this to create an object on the client:
my $client_job = Event::ExecFlow::Job->new ( ... );
The server knows nothing of the renaming on client side, so you
still write this on the server to create objects there:
my $job = Event::ExecFlow::Job->new ( ... );
=back
=head2 SSL OPTIONS
If the server accepts only SSL connections you need to enable
ssl here in the client as well. By default the SSL connection
will be established without any peer verification, which makes
Man-in-the-Middle attacks possible. If you want to prevent that,
you need to set either B<ssl_ca_file> or B<ssl_ca_path> option.
=over 4
=item B<ssl>
Set this option to 1 to encrypt the network connection using SSL.
=item B<ssl_ca_file>
Path to the the Certificate Authority's certificate file
(ca.crt), your server key was signed with.
=item B<ssl_ca_path>
Path of a directory containing several trusted certificates with
a proper index. Please refer to the OpenSSL documentation for
details about setting up such a directory.
=back
=head2 AUTHENTICATION OPTIONS
If the server requires user authentication you need to set
the following options:
=over 4
=item B<auth_user>
A valid username.
=item B<auth_pass>
The corresponding password, encrypted using Perl's crypt() function,
using the username as the salt.
Event::RPC has a convenience function for generating such a crypted
password, although it's currently just a wrapper around Perl's
builtin crypt() function, but probably this changes someday, so better
use this method:
$crypted_pass = Event::RPC->crypt($user, $pass);
=back
If the passed credentials are invalid the Event::RPC::Client->connect()
method throws a correspondent exception.
=head2 ERROR HANDLING
Any exceptions thrown on the server during execution of a remote
method will result in a corresponding exception on the client. So
you can use normal exception handling with eval {} when executing
remote methods.
But besides this the network connection between your client and
the server may break at any time. This raises an exception as well,
but you can override this behaviour with the following attribute:
=over 4
=item B<error_cb>
This subroutine is called if any error occurs in the network
communication between the client and the server. The actual
Event::RPC::Client object and an error string are passed
as arguments.
This is B<no> generic exception handler for exceptions thrown from the
executed methods on the server! If you like to catch such
exceptions you need to put an eval {} around your method calls,
as you would do for local method calls.
If you don't specify an B<error_cb> an exception is thrown instead.
=back
=head1 METHODS
=over 4
=item $rpc_client->B<connect>
This establishes the configured connection to the server. An exception
is thrown if something goes wrong, e.g. server not available, credentials
are invalid or something like this.
=item $rpc_client->B<disconnect>
Closes the connection to the server. You may omit explicit disconnecting
since it's done automatically once the Event::RPC::Client object gets
destroyed.
=back
=head1 READY ONLY ATTRIBUTES
=over 4
=item $rpc_client->B<get_server_version>
Returns the Event::RPC version number of the server after connecting.
=item $rpc_client->B<get_server_protocol>
Returns the Event::RPC protocol number of the server after connecting.
=item $rpc_client->B<get_client_version>
Returns the Event::RPC version number of the client.
=item $rpc_client->B<get_client_protocol>
Returns the Event::RPC protocol number of the client.
=back
=head1 AUTHORS
Jörn Reder <joern at zyn dot de>
=head1 COPYRIGHT AND LICENSE
Copyright (C) 2002-2006 by Joern Reder, All Rights Reserved.
This library is free software; you can redistribute it
and/or modify it under the same terms as Perl itself.
=cut
|