ICE Mini Grid
ICE Grid 入门篇
这篇是ICE Grid入门的最简单版本(不涉及到IceGride Node)。这里面设计到过多的概念和知识,暂且不表。
创建slice文件
Printer.ice
1
//
**********************************************************************
2 //
3 // Copyright (c) 2012 Ady Liu. All rights reserved.
4 //
5 // Email: [email protected]
6 //
7 // **********************************************************************
8
9 module Demo{
10 interface Printer {
11 void printString( string s);
12 };
13 };
2 //
3 // Copyright (c) 2012 Ady Liu. All rights reserved.
4 //
5 // Email: [email protected]
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
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: [email protected]
6 //
7 // **********************************************************************
8
9 #ifndef PRINTER_I_H
10 #define PRINTER_I_H
11
12 #include <Printer.h>
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
2 //
3 // Copyright (c) 2012 Ady Liu. All rights reserved.
4 //
5 // Email: [email protected]
6 //
7 // **********************************************************************
8
9 #ifndef PRINTER_I_H
10 #define PRINTER_I_H
11
12 #include <Printer.h>
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: [email protected]
6 //
7 // **********************************************************************
8
9 #include <Ice/Ice.h>
10 #include <PrinterI.h>
11
12 using namespace std;
13
14 void PrinterI :: printString( const string& s, const Ice::Current&){
15 cout << s << endl;
16 }
17
2 //
3 // Copyright (c) 2012 Ady Liu. All rights reserved.
4 //
5 // Email: [email protected]
6 //
7 // **********************************************************************
8
9 #include <Ice/Ice.h>
10 #include <PrinterI.h>
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 <Ice/Ice.h>
2 #include <PrinterI.h>
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<<appName()<<": too many arguments"<<endl;
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
2 #include <PrinterI.h>
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<<appName()<<": too many arguments"<<endl;
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
运行服务端
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: [email protected]
6 //
7 // **********************************************************************
8
9 #include <Ice/Ice.h>
10 #include <IceGrid/IceGrid.h>
11 #include <Printer.h>
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=>"<<ic->stringToProxy("printer@PrinterAdapter")<<endl;
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."<<endl;
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
2 //
3 // Copyright (c) 2012 Ady Liu. All rights reserved.
4 //
5 // Email: [email protected]
6 //
7 // **********************************************************************
8
9 #include <Ice/Ice.h>
10 #include <IceGrid/IceGrid.h>
11 #include <Printer.h>
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=>"<<ic->stringToProxy("printer@PrinterAdapter")<<endl;
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."<<endl;
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
├── client.cfg
├── Client.cpp
├── Printer.cpp
├── Printer.h
├── PrinterI.cpp
├── PrinterI.h
├── registry.cfg
├── server.cfg
└── Server.cpp
所有文件下载: Ice Mini Guide
©2009-2014 IMXYLZ
imxylz.com
| 求贤若渴