On this page:
slice2js compiler produces a single source file,
Printer.js, from this definition. The exact contents of the source file do not concern us for now — it contains the generated code that corresponds to the
Printer interface we defined in
The client code, in
Client.js, is shown below in full:
The program begins with
require statements that assign modules from the Ice run time and the generated code to convenient local variables. (These statements are necessary for use with NodeJS. Browser applications would omit these statements and load the modules a different way.)
The program begins with a call to
Ice.Promise.try to launch a chain of promises (or futures) that handles the asynchronous nature of Ice invocations with a structure that resembles synchronous code.
- The function passed to
tryis executed immediately. The body of this function begins by calling
Ice.initializeto initialize the Ice run time. The call to
Ice.Communicatorreference, which is the main object in the Ice run time.
- The next step is to obtain a proxy for the remote printer. We create a proxy by calling
stringToProxyon the communicator, with the string
"SimplePrinter:default -p 10000". Note that the string contains the object identity and the port number that were used by the server. (Obviously, hard-coding object identities and port numbers into our applications is a bad idea, but it will do for now; we will see more architecturally sound ways of doing this when we discuss IceGrid.)
- The proxy returned by
stringToProxyis of type
Ice.ObjectPrx, which is at the root of the inheritance tree for interfaces and classes. But to actually talk to our printer, we need a proxy for a
Demo::Printerinterface, not an
Objectinterface. To do this, we need to do a down-cast by calling
Demo.PrinterPrx.checkedCast. A checked cast sends a message to the server, effectively asking "is this a proxy for a
Demo::Printerinterface?" If so, the call returns a proxy of type
Demo::PrinterPrx; otherwise, if the proxy denotes an interface of some other type, the call returns
checkedCastfunction involves a remote invocation to the server, which means this function has asynchronous semantics and therefore it returns a new promise object.
- We call
thenon the promise returned by
checkedCastand supply a "success" function, meaning the code that's executed when
checkedCastsucceeds. This inner function accepts one argument,
printer, representing a proxy to the newly-downcasted object, or
nullif the remote object doesn't support the
- Inside the success function, we now have a live proxy in our address space and can call the
printStringmethod, passing it the time-honored
"Hello World!"string. The server prints that string on its terminal. Again,
printStringis a remote invocation, and it returns a promise that the success function passes along as its own return value.
thenfunction also returns a new promise which our outer function passes back to
try. This outer promise is chained to the promise associated with the
printStringinvocation; the outer promise completes successfully if and when the
printStringinvocation completes successfully.
- The function passed to
finallyis executed after the
tryblock has completed, whether or not it completes successfully. If we created a communicator in the
tryblock, we destroy it here. Doing this is essential in order to correctly finalize the Ice run time: the program must call
destroyon any communicator it has created; otherwise, undefined behavior results. The
destroyfunction has asynchronous semantics, so we return its promise to ensure no subsequent code is executed until
- Lastly, the function passed to
exceptionis the default exception handler for this entire promise chain.
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:
The client runs and exits without producing any output; however, in the server window, we see the
"Hello World!" that is produced by the printer. To get rid of the server, we interrupt it on the command line.
If anything goes wrong, the client will print an error message. For example, if we run the client without having first started the server, we get something like the following: