Event-RPC

 view release on metacpan or  search on metacpan

examples/client.pl  view on Meta::CPAN

  -a user:pass   Pass this authorization data to the server
  -h host        Server hostname. Default: localhost

__EOU

sub HELP_MESSAGE {
    my ($fh) = @_;
    $fh ||= \*STDOUT;
    print $fh $USAGE;
    exit;
}

main: {
    my %opts;
    my $opts_ok = getopts('h:l:a:s',\%opts);
   
    HELP_MESSAGE() unless $opts_ok;

    my $ssl = $opts{s} || 0;

    my %auth_args;
    if ( $opts{a} ) {
        my ($user, $pass) = split(":", $opts{a}); 
        $pass = Event::RPC->crypt($user,$pass);
        %auth_args = (
            auth_user => $user,
            auth_pass => $pass,
        );
    }

    #-- Host parameter
    my $host = $opts{h} || 'localhost';

    #-- This connects to the server, requests the exported
    #-- interfaces and establishes correspondent proxy methods
    #-- in the correspondent packages.
    my $client;
    $client = Event::RPC::Client->new (
        host     => $host,
        port     => 5555,
        ssl      => $ssl,
        %auth_args,
        error_cb => sub {
            my ($client, $error) = @_;
            print "An RPC error occured!\n> $error";
            print "Disconnect and exit.\n";
            $client->disconnect if $client;
            exit;
        },
        classes => [ "Test_class" ],
    );

    $client->connect;

    print "\nConnected to localhost:5555\n\n";
    print "Server version:  ".$client->get_server_version,"\n";
    print "Server protocol: ".$client->get_server_protocol,"\n";
    print "Message format:  ".eval { $client->get_message_format },"\n";
    print "\n";

    #-- So the call to Event::RPC::Test->new is handled transparently
    #-- by Event::RPC::Client
    print "** Create object on server\n";
    my $object = Test_class->new (
            data => "Initial data",
    );
    print "=> Object created with data: '".$object->get_data."'\n\n";

    #-- and methods calls as well...
    print "** Say hello to server.\n";
    print "=> Server returned: >>".$object->hello,"<<\n";

    print "\n** Update object data.\n";
    $object->set_data ("Yes, updating works");
    print "=> Retrieve data from server: '".$object->get_data."'\n";

    print "\n** Disconnecting\n\n";
    $client->disconnect;

}



( run in 0.560 second using v1.01-cache-2.11-cpan-524268b4103 )