ICE阅读笔记

原创 2004年08月17日 21:16:00

Chapter 3
A Hello World Application

------------------------------------

3.1Chapter Overview
In this chapter, we will see how to create a very simple client–server application in
both C++ and Java. The application enables remote printing: a client sends the text
to 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 idiosyncrasies
of print spoolers for various platforms), our printer will simply print to
a terminal instead of a real printer. This is no great loss: the purpose of the exercise
is to show how a client can communicate with a server; once the thread of
control has reached the server application code, that code can of course do
anything it likes (including sending the text to a real printer). How to do this is
independent of Ice and therefore not relevant here.
Note that much of the detail of the source code will remain unexplained for
now. The intent is to show you how to get started and give you a feel for what the
development environment looks like; we will provide all the detail throughout the
remainder of this book.
3.2Writing a Slice Definition
The first step in writing any Ice application is to write a Slice definition containing
the 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, the
interface is very simple and provides only a single operation, called printString.
The printString operation accepts a string as its sole input parameter; the text of
that 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 Java
version 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 to
generate C++ proxies and skeletons. Under UNIX, you can compile the definition
as follows:
$ slice2cpp Printer.ice
The slice2cpp compiler produces two C++ source files from this definition,
Printer.h and Printer.cpp.
• Printer.h
The Printer.h header file contains C++ type definitions that correspond to
the Slice definitions for our Printer interface. This header file must be
included in both the client and the server source code.
• Printer.cpp
The Printer.cpp file contains the source code for our Printer interface.
The generated source contains type-specific run-time support for both clients
and servers. For example, it contains code that marshals parameter data (the
string passed to the printString operation) on the client side and unmarshals
that data on the server side.
The Printer.cpp file must be compiled and linked into both client and
server.
Writing and Compiling a Server
The 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 &);
};
void
PrinterI::
printString(const string & s, const Ice::Current &)
{
cout << s << endl;
}
int
main(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 just
prints a string. Do not be concerned by this: most of the preceding code is boiler
plate that never changes. For this very simple server, the code is dominated by this
boiler plate.
Every Slice source file starts with an include directive for Ice.h, which
contains the definitions for the Ice run time. We also include Printer.h, which
was generated by the Slice compiler and contains the C++ definitions for our
printer interface, and we import the contents of the std namespace for brevity in
the code that follows:
#include <Ice/Ice.h>
#include <Printer.h>
using namespace std;
Our server implements a single printer servant, of type PrinterI. Looking at
the generated code in Printer.h, we find the following (tidied up a little to get
rid 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. (Note
that the printString method is pure virtual so the skeleton class cannot be
instantiated.) Our servant class inherits from the skeleton class to provide an
implementation of the pure virtual printString method. (By convention, we
use an I-suffix to indicate that the class implements an interface.)

 

如何有效地阅读文献并做笔记

简要介绍了文献的检索、阅读、积累、写作的方法。
  • u014749005
  • u014749005
  • 2016年09月16日 22:16
  • 1281

《Linux内核完全剖析》阅读笔记

http://blog.csdn.net/songguozhi/article/details/3496455 我是通过阅读赵炯老师编的厚厚的linux内核完全剖析看完LINUX0.11的代码,不得...
  • fyh2003
  • fyh2003
  • 2016年01月04日 13:40
  • 4133

【java】《java编程思想》 读书笔记

之前主要用的C++的比较多,之前花了快2个月的实际认真系统全面的学习了以下java的基础语法,《java编程思想》这本书翻译水平确实不是很好,很多话读着会比较拗口。推荐读之前,先去网上搜索 “java...
  • a2888409
  • a2888409
  • 2015年10月15日 10:07
  • 1912

Ice笔记---异步程序设计demo

最近学习了异步通信这一章:异步包括服务端的异步分派(AMD)以及客户端的异步调用(AMI)。 下面介绍一下如何实现简单的异步通信(主要实现了客户端的异步调用,AMD暂未体现):    1.首先编...
  • zhiweiarm
  • zhiweiarm
  • 2013年02月05日 08:56
  • 434

Ice 读书笔记--Slice语言

Slice语言   1. Slice(Specification Language for Ice)是一种用于使对象接口与其实现相分离的基础性抽象机制。 因为Slice描述的是接口和类型,它是一...
  • moxiaomomo
  • moxiaomomo
  • 2011年08月22日 22:04
  • 3484

Ice读书笔记--C++线程与并发(一)

C++线程与并发 1. Ice线程模型      Ice服务器是多线程的。服务器端run time维护有一个线程池,用于处理到来的请求。通过领导者-跟随者线程模型,客户发来的每个操作调用都...
  • moxiaomomo
  • moxiaomomo
  • 2011年08月29日 20:59
  • 3445

《ZeroC Ice 权威指南》笔记

Ice介绍         Ice(Internet Communications Engine)是ZeroC公司的杰作,继承了CORBA的血统,是新一代的面向对象的分布式系统中间件。Ice是RPC...
  • u013898698
  • u013898698
  • 2017年05月02日 17:46
  • 569

Ice笔记---异步程序设计demo

最近学习了异步通信这一章:异步包括服务端的异步分派(AMD)以及客户端的异步调用(AMI)。 下面介绍一下如何实现简单的异步通信(主要实现了客户端的异步调用,AMD暂未体现):    1.首先编...
  • moxiaomomo
  • moxiaomomo
  • 2011年09月08日 22:39
  • 3473

RFC学习笔记 -- 5245 ICE & 3261 SIP

http://www.bubuko.com/infodetail-673742.html RFC list: 3550 (rtp/rtcp)  - 3711(srtp) - 5245 (ice)...
  • zxygww
  • zxygww
  • 2016年03月16日 19:55
  • 359

ICE中间件笔记

ICE中间件说明文档 1      ICE中间件简介...2 2      平台核心功能...2 2.1       接口描述语言(Slice)...2 2.2       ICE运行时......
  • wxl1986622
  • wxl1986622
  • 2011年12月14日 11:06
  • 2788
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:ICE阅读笔记
举报原因:
原因补充:

(最多只允许输入30个字)