2 use strict;use warnings;
4 use OpenILS::Utils::Config;
5 use OpenILS::DomainObject::oilsMethod;
6 use OpenILS::DomainObject::oilsPrimitive;
7 use OpenILS::EX qw/:try/;
10 # ----------------------------------------------------------------------------------------
11 # Simple math shell where you can test the transport system.
12 # Enter simple, binary equations ony using +, -, *, and /
14 # Usage: % perl math_shell.pl
15 # ----------------------------------------------------------------------------------------
18 my $config = OpenILS::Utils::Config->current;
20 # connect to the transport (jabber) server
21 OpenILS::System->bootstrap_client();
23 # build the AppSession object.
24 my $session = OpenILS::AppSession->create(
25 "math", username => 'math_bench', secret => '12345' );
28 print "type 'exit' or 'quit' to leave the shell\n";
30 while( my $request = <> ) {
34 # exit loop if user enters 'exit' or 'quit'
35 if( $request =~ /exit/i or $request =~ /quit/i ) { last; }
37 # figure out what the user entered
38 my( $a, $mname, $b ) = parse_request( $request );
41 print "Parse Error. Try again. \nExample # 1+1\n";
48 # Connect to the MATH server
49 if( ! ($session->connect()) ) { die "Connect timed out\n"; }
51 } catch OpenILS::EX with {
53 die "* * Connection Failed with:\n$e";
56 my $method = OpenILS::DomainObject::oilsMethod->new( method => $mname );
57 $method->params( $a, $b );
63 $req = $session->request( $method );
65 # we know that this request only has a single reply
66 # if your expecting a 'stream' of results, you can
67 # do: while( $resp = $req->recv( timeout => 10 ) ) {}
68 $resp = $req->recv( timeout => 10 );
70 } catch OpenILS::EX with {
72 # Any transport layer or server problems will launch an exception
74 die "ERROR Receiving\n $e";
78 # something just died somethere
80 die "Caught unknown error: $e";
84 # ----------------------------------------------------------------------------------------
85 # $resp is an OpenILS::DomainObject::oilsResponse object. $resp->content() returns whatever
86 # data the object has. If the server returns an exception that we're meant to see, then
87 # the data will be an exception object. In this case, barring any exception, we know that
88 # the data is an OpenILS::DomainObject::oilsScalar object which has a value() method
89 # that returns a perl scalar. For us, that scalar is just a number.
90 # ----------------------------------------------------------------------------------------
92 if( UNIVERSAL::isa( $resp, "OpenILS::EX" ) ) {
96 my $ret = $resp->content();
106 # disconnect from the MATH server
110 # ------------------------------------------------------------------------------------
111 # parse the user input string
112 # returns a list of the form (first param, operation, second param)
113 # These operations are what the MATH server recognizes as method names
114 # ------------------------------------------------------------------------------------
122 @ops = split( /\+/, $string );
123 if( @ops > 1 ) { $op = "add"; last; }
125 @ops = split( /\-/, $string );
126 if( @ops > 1 ) { $op = "sub"; last; }
128 @ops = split( /\*/, $string );
129 if( @ops > 1 ) { $op = "mult", last; }
131 @ops = split( /\//, $string );
132 if( @ops > 1 ) { $op = "div"; last; }
137 return ($ops[0], $op, $ops[1]);