Date: Thu, 28 Mar 2024 22:21:16 +0000 (UTC) Message-ID: <302243558.24681.1711664476894@ae5f4610bf64> Subject: Exported From Confluence MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_Part_24680_335675356.1711664476894" ------=_Part_24680_335675356.1711664476894 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Location: file:///C:/exported.html
This page shows how to create an Ice client application with Rub= y.
On this page:
The first step in creating our Ruby application is to compile our Slice definition to generate = Ruby proxies. You can compile the definition as follows:
slice2rb&nbs= p;Printer.ice
The slice2rb
compiler produces a single source file, =
Printer.rb
, from this definition. The exact contents of the source f=
ile do not concern us for now =E2=80=94 it contains the generated code that=
corresponds to the Printer
interface we defined in Prin=
ter.ice
.
The client code, in client.rb
, is shown below in full:
require = 'Printer.rb' Ice::initialize(ARGV) do |communicator| base =3D communicator.stringToProxy("SimplePrinter:default -h localhost= -p 10000") printer =3D Demo::PrinterPrx::checkedCast(base) if not printer raise "Invalid proxy" end printer.printString("Hello World!") end
The program begins with a require
statement, which loads th=
e Ruby code we generated from our Slice definition in the previous section.=
It is not necessary for the client to explicitly load the Ice
=
module because Printer.rb
does that for you.
The body of the main program goes through the following steps:
Ice::initialize
.=
(We pass ARGV
to this call because the client may have comman=
d-line arguments that are of interest to the run time; for this example, th=
e client does not require any command-line arguments.)Ice::Com=
municator
reference, which is the main object in the Ice run time.&n=
bsp;Ice::initialize
will automatically destroy the communicato=
r when the block completes.stringToProxy
on the communicator, with the s=
tring "SimplePrinter:default -p 10000"
. Note that the string c=
ontains the object identity and the port number that were used by the serve=
r. (Obviously, hard-coding object identities and port numbers into our appl=
ications is a bad idea, but it will do for now; we will see more architectu=
rally sound ways of doing this when we discuss IceGrid.)stringToProxy
is of type Ice::=
ObjectPrx
, which is at the root of the inheritance tree for interfac=
es. But to actually talk to our printer, we need a proxy for a Demo::=
Printer
interface, not an Object
interface. To do this,=
we need to do a down-cast by calling Demo::PrinterPrx::checkedCast=
code>. A checked cast sends a message to the server, effectively asking "is=
this a proxy for a Demo::Printer
interface?" If so, the call =
returns a proxy of type Demo::PrinterPrx
; otherwise, if the pr=
oxy denotes an interface of some other type, the call returns nil.
pr=
intString
method, passing it the time-honored "Hello World!"=
code> string. The server prints that string on its terminal.
The server must be started before the client. Since Ice for Ruby does no= t support server-side behavior, we need to use a server from another langua= ge mapping. In this case, we will use the C++ server:
server
At this point, we won't see anything because the server simply waits for= a client to connect to it. We run the client in a different window:
ruby client.= rb
The client runs and exits without producing any output; however, in the =
server window, we see the "Hello World!"
that is produced by t=
he printer. To get rid of the server, we interrupt it on the command line.<=
/p>
If anything goes wrong, the client will print an error message. For exam= ple, if we run the client without having first started the server, we get s= omething like the following:
exception&nb= sp;::Ice::ConnectionRefusedException { error =3D 111 }
Note that, to successfully run the client, the Ruby interpreter must be = able to locate the Ice extension for Ruby. See the Ice for Ruby installatio= n instructions for more information.