Chapter 3a Hello World Application
------------------------------------
3.1Chapter OverviewIn this chapter, we will see how to create a very simple client-server application inboth C and Java The application enables remote printing:. A client sends the textto be printed to a server, which in turn sends that text to a printer .For simplicity (and because we do not want to concern ourselves with the idiosyncrasiesof print spoolers for various platforms), our printer will simply print toa terminal instead of a real printer This is no great loss:. the purpose of the exerciseis to show how a client can communicate with a server; once the thread ofcontrol has reached the server application code, that code can of course doanything it likes (including sending the text to a real printer) How to do this isindependent of Ice and therefore not relevant here. .Note that much of the detail of the source code will remain unexplained fornow The intent is to show you how to get started and give you a feel for what thedevelopment environment looks like;. we will provide all the detail throughout theremainder of this book.3.2Writing a Slice DefinitionThe first step in writing any Ice application is to write a Slice definition containingthe interfaces that are used by the application For our minimal printing application, we write the following Slice definition:. interface Printer {void printString (string s);};. We save this text in a file called Printer.ice.Our Slice definition contains a single interface called Printer For now, theinterface is very simple and provides only a single operation, called printString.The PrintString Operation Accepts a string as its sole infut parameter; the text offhat string is what Appears on The (Possibly Remote) Printer.3.3Writing An Ice Application with C
This section shows how to create an Ice application with C . The equivalent Javaversion is shown in Section 3.4 on page 42.Compiling a Slice Definition for C The first step in creating our C application is to compile our Slice definition togenerate C proxies and . skeletons Under UNIX, you can compile the definitionas follows:. $ slice2cpp Printer.iceThe slice2cpp compiler produces two C source files from this definition, Printer.h and Printer.cpp • Printer.hThe Printer.h header file contains C type definitions that correspond tothe Slice definitions for our Printer interface. This header file must beincluded in both the client and the server source code. • Printer.cppThe Printer.cpp file contains the source code for our Printer interface.The generated source contains type-specific run- Time Support for Both Clientsand Servers. for Example, IT Contains Code That Marshals Parameter Data (The Client Side and Unmarshalsthat Data on The Printstring Operation) on the client server side.The Printer.cpp file must be compiled and linked into both client andserver.Writing and Compiling a ServerThe source code for the server takes only a few lines and is shown in full here: #include
SimplePrinterAdapter "," default -p 10000 "); Ice :: ObjectPtr object = new PrinterI; adapter-> add (object, Ice :: stringToIdentity (" SimplePrinter ")); adapter-> activate (); ic-> waitForShutdown ( );} catch (const same :: exception & e) {CERR << E << endl; status = 1;} catch (const char * msg) {CERR << msg << endl; status = 1;} if ic) ic-> destroy (); return status;} There appears to be a lot of code here for something as simple as a server that justprints a string Do not be concerned by this:. most of the preceding code is boilerplate that never changes. for this very simple server, the code is dominated by thisboiler plate.Every Slice source file starts with an include directive for Ice.h, whichcontains the definitions for the Ice run time. We also include Printer.h, whichwas generated by the Slice compiler and contains the C definitions for ourprinter interface, and we import the contents of the std namespace for brevity inthe code that follows: #include