RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:9:30-18:00
你可能遇到了下面的问题
关闭右侧工具栏
ICE阅读笔记
  • 作者:zhaozj
  • 发表时间:2020-12-23 10:57
  • 来源:未知

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 input parameter; the text ofthat 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 (thestring passed to the printString operation) on the client side and unmarshalsthat data on the 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 <Ice/Ice.h>#include <Printer.h>using namespace std;class PrinterI : public Printer {public:virtual void printString(const string & s,const Ice::Current &);};voidPrinterI::printString(const string & s, const Ice::Current &){cout << s << endl;}intmain(int argc, char* argv[]){int status = 0;Ice::CommunicatorPtr ic;try {ic = Ice::initialize(argc, argv);Ice::ObjectAdapterPtr adapter= ic->createObjectAdapterWithEndpoints("SimplePrinterAdapter", "default -p 10000");Ice::ObjectPtr object = new PrinterI;adapter->add(object,Ice::stringToIdentity("SimplePrinter"));adapter->activate();ic->waitForShutdown();} catch (const Ice::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 <Ice/Ice.h>#include <Printer.h>using namespace std;Our server implements a single printer servant, of type PrinterI. Looking atthe generated code in Printer.h, we find the following (tidied up a little to getrid of irrelevant detail):class Printer : virtual public Ice::Object {public:virtual void printString(const std::string &,const Ice::Current & = Ice::Current()) = 0;};The Printer skeleton class definition is generated by the Slice compiler. (Notethat the printString method is pure virtual so the skeleton class cannot beinstantiated.) Our servant class inherits from the skeleton class to provide animplementation of the pure virtual printString method. (By convention, weuse an I-suffix to indicate that the class implements an interface.)