]> git.evergreen-ils.org Git - Evergreen.git/blob - OpenSRF/examples/math_simple.pl
C based xinclude processor
[Evergreen.git] / OpenSRF / examples / math_simple.pl
1 #!/usr/bin/perl -w
2 use strict;use warnings;
3 use OpenILS::System;
4 use OpenILS::Utils::Config;
5 use OpenILS::DomainObject::oilsMethod;
6 use OpenILS::DomainObject::oilsPrimitive;
7 use OpenILS::EX qw/:try/;
8 $| = 1;
9
10
11 # ----------------------------------------------------------------------------------------
12 # This script makes a single query, 1 + 2,  to the the MATH test app and prints the result
13 # Usage: % perl math_simple.pl 
14 # ----------------------------------------------------------------------------------------
15
16
17 # connect to the transport (jabber) server
18 OpenILS::System->bootstrap_client();
19
20 # build the AppSession object.
21 my $session = OpenILS::AppSession->create( 
22         "math", username => 'math_bench', secret => '12345' );
23
24 try {
25
26         # Connect to the MATH server
27         if( ! ($session->connect()) ) { die "Connect timed out\n"; }
28
29 } catch OpenILS::EX with {
30         my $e = shift;
31         die "* * Connection Failed with:\n$e";
32 };
33
34 my $method = OpenILS::DomainObject::oilsMethod->new( method => "add" );
35 $method->params( 1, 2 );
36
37 my $req;
38 my $resp;
39
40 try {
41         $req = $session->request( $method );
42
43         # we know that this request only has a single reply
44         # if your expecting a 'stream' of results, you can
45         # do: while( $resp = $req->recv( timeout => 10 ) ) {}
46         $resp = $req->recv( timeout => 10 );
47
48 } catch OpenILS::EX with {
49
50         # Any transport layer or server problems will launch an exception
51         my $e = shift;
52         die "ERROR Receiving\n $e";
53
54 } catch Error with {
55
56         # something just died somethere
57         my $e = shift;
58         die "Caught unknown error: $e";
59 };
60
61 if ( $resp ) {
62         # ----------------------------------------------------------------------------------------
63         # $resp is an OpenILS::DomainObject::oilsResponse object. $resp->content() returns whatever 
64         # data the object has.  If the server returns an exception that we're meant to see, then
65         # the data will be an exception object.  In this case, barring any exception, we know that 
66         # the data is an OpenILS::DomainObject::oilsScalar object which has a value() method 
67         # that returns a perl scalar.  For us, that scalar is just a number.
68         # ----------------------------------------------------------------------------------------
69
70         if( UNIVERSAL::isa( $resp, "OpenILS::EX" ) ) {
71                         throw $resp;
72         }
73
74         my $ret = $resp->content();
75         print "Should print 3 => " . $ret->value() . "\n";
76
77 } else {
78         die "No Response from Server!\n";
79 }
80
81 $req->finish();
82
83 # disconnect from the MATH server
84 $session->kill_me();
85 exit;
86