鏈嶅姟绔?/h2>
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 <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: imxylz@gmail.com
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
緙栬瘧
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
鏈嶅姟绔厤緗?br />
server.cfg
PrinterAdapter.AdapterId=PrinterAdapter
PrinterAdapter.Endpoints=default
Ice.Default.Locator=DemoGrid/Locator:tcp -p 4061
榪愯鏈嶅姟绔?br />
./server
瀹㈡埛绔?/h2>
Client.cpp
1 //**********************************************************************
2 //
3 // Copyright (c) 2012 Ady Liu. All rights reserved.
4 //
5 // Email: imxylz@gmail.com
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
瀹㈡埛绔厤緗?br />
client.cfg
Ice.Default.Locator=DemoGrid/Locator:default -p 4061
榪愯瀹㈡埛绔?br />
./client --Ice.Config=./client.cfg
鎵鏈夋枃浠?/h2>
grid
├── client.cfg
├── Client.cpp
├── Printer.cpp
├── Printer.h
├── PrinterI.cpp
├── PrinterI.h
├── registry.cfg
├── server.cfg
└── Server.cpp
鎵鏈夋枃浠朵笅杞? Ice Mini Guide
]]>