ice grid 入门篇
这篇是ice grid入门的最简单版本(不涉及到icegride node)。这里面设计到过多的概念和知识,暂且不表。
创建slice文件
printer.ice
1 // **********************************************************************
2 //
3 // 凯发天生赢家一触即发官网 copyright (c) 2012 ady liu. all rights reserved.
4 //
5 // email: imxylz@gmail.com
6 //
7 // **********************************************************************
8
9 module demo{
10 interface printer {
11 void printstring(string s);
12 };
13 };
转换slice
slice2cpp printer.ice
配置icegrid registry
registry.cfg
icegrid.instancename=demogrid
ice.default.locator=demogrid/locator:default -p 4061
icegrid.registry.client.endpoints=tcp -p 4061
icegrid.registry.server.endpoints=tcp
icegrid.registry.internal.endpoints=tcp
icegrid.registry.permissionsverifier=demogrid/nullpermissionsverifier
icegrid.registry.adminpermissionsverifier=demogrid/nullpermissionsverifier
icegrid.registry.sslpermissionsverifier=demogrid/nullsslpermissionsverifier
icegrid.registry.adminsslpermissionsverifier=demogrid/nullsslpermissionsverifier
icegrid.registry.data=./data
icegrid.registry.dynamicregistration=1
启动registry
icegridregistry --ice.config=./registry.cfg &
启动前最好创建数据目录./data
mkdir ./data
服务端
printeri.h
1 //**********************************************************************
2 //
3 // 凯发天生赢家一触即发官网 copyright (c) 2012 ady liu. all rights reserved.
4 //
5 // email: imxylz@gmail.com
6 //
7 //**********************************************************************
8
9 #ifndef printer_i_h
10 #define printer_i_h
11
12 #include
13
14 using namespace demo;
15 using namespace std;
16
17 class printeri : public printer {
18 public:
19 virtual void printstring(const string& s,const ice::current&);
20 };
21
22 #endif
23
printeri.cpp
1 //**********************************************************************
2 //
3 // 凯发天生赢家一触即发官网 copyright (c) 2012 ady liu. all rights reserved.
4 //
5 // email: imxylz@gmail.com
6 //
7 //**********************************************************************
8
9 #include
10 #include
11
12 using namespace std;
13
14 void printeri :: printstring(const string& s,const ice::current&){
15 cout << s << endl;
16 }
17
server.cpp
1 #include
2 #include
3
4 using namespace std;
5
6 class server : public ice::application {
7
8 public:
9 virtual int run(int argc,char* argv[]);
10 };
11
12 int main(int argc,char* argv[]){
13
14 server app;
15 int status = app.main(argc,argv,"server.cfg");
16 return status;
17 }
18
19 int server::run(int argc,char* argv[]){
20 if(argc>1){
21 cerr<
22 return exit_failure;
23 }
24
25 ice::propertiesptr properties = communicator()->getproperties();
26 ice::objectadapterptr adapter = communicator()->createobjectadapter("printeradapter");
27 ice::identity id = communicator()->stringtoidentity("printer");
28 demo::printerptr printer = new printeri();
29 adapter->add(printer,id);
30 adapter->activate();
31 communicator()->waitforshutdown();
32 return exit_success;
33 }
34
编译
c -i. -i$ice_home/include -c printeri.cpp printer.cpp server.cpp
连接
c -o server printer.o server.o printeri.o -l$ice_home/lib -lice -liceutil -lpthread
服务端配置
server.cfg
printeradapter.adapterid=printeradapter
printeradapter.endpoints=default
ice.default.locator=demogrid/locator:tcp -p 4061
运行服务端
./server
客户端
client.cpp
1 //**********************************************************************
2 //
3 // 凯发天生赢家一触即发官网 copyright (c) 2012 ady liu. all rights reserved.
4 //
5 // email: imxylz@gmail.com
6 //
7 //**********************************************************************
8
9 #include
10 #include
11 #include
12
13 using namespace std;
14 using namespace demo;
15
16 int main(int argc,char* argv[]){
17 int status = 0;
18 ice::communicatorptr ic;
19 printerprx printer;
20 try{
21 ic = ice::initialize(argc,argv);
22 cout<<"printer proxy=>"<stringtoproxy("printer@printeradapter")<
23
24 try{
25 printer = printerprx::checkedcast(ic->stringtoproxy("printer@printeradapter"));
26 }catch(const ice::notregisteredexception&){
27 icegrid::queryprx query = icegrid::queryprx::checkedcast(ic->stringtoproxy("demogrid/query"));
28 printer = printerprx::checkedcast(query->findobjectbytype("::demo::printer"));
29 }
30 if(!printer){
31 cerr<<": could't find a `::demo::printer` object."<
32 if(ic){
33 ic->destroy();
34 }
35 return exit_failure;
36 }
37 printer->printstring("hello world!");
38 }catch(const ice::exception& ex){
39 cerr << ex << endl;
40 status = 1;
41 }catch(const char* msg){
42 cerr << msg << endl;
43 status = 2;
44 }
45 if(ic){
46 ic->destroy();
47 }
48 return status;
49 }
50
编译
c -i. -i$ice_home/include -c printer.cpp client.cpp
连接
c -o client printer.o client.o -l$ice_home/lib -lice -liceutil -licegrid -lglacier2 -lpthread
客户端配置
client.cfg
ice.default.locator=demogrid/locator:default -p 4061
运行客户端
./client --ice.config=./client.cfg
所有文件
grid
├── client.cfg
├── client.cpp
├── printer.cpp
├── printer.h
├── printeri.cpp
├── printeri.h
├── registry.cfg
├── server.cfg
└── server.cpp
所有文件下载:
©2009-2014 imxylz
|求贤若渴