1. 编写idl文件, 命名为Messenger.idl
interface Messenger
{
boolean send_mssage(
in string user_name,
in string subject,
inout string message
);
};
2. 编译idl, 生成cpp文件!
tao_idl -GI Messenger.idl
此步会生成服务端, 客户端文件.
3. 修改 MessengerI.cpp,完成send_message的功能.在end_mssage函数体内添加:
cout<<"Message from:" << user_name <<endl;
cout<<"Subject: "<<subject<<endl;
cout<<"Message: "<<message<<endl;
CORBA::string_free(message);
message=CORBA::string_dup("THanks for the message.");
return true;
|
由于使用了cout, 所以在该文件的开头需要使用
#incude
using namespace std;
4.编写服务器实现代码!
代码如下:
#include "MessengerI.h"
#include <iostream>
#include <fstream>
int main(int argc, char* argv[])
{
try {
// Initialize the ORB.
CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
//Get a reference to the RootPOA.
CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
PortableServer::POA_var poa = PortableServer::POA::_narrow(obj);
// Activate the POAManager.
PortableServer::POAManager_var mgr = poa->the_POAManager();
mgr->activate();
// Register the servant with the RootPOA, obtain its object
// reference, stringify it, and write it to a file.
PortableServer::ObjectId_var oid = poa->activate_object(new Messenger_i());
obj = poa->id_to_reference(oid.in());
CORBA::String_var str = orb->object_to_string(obj.in());
//这样客户端通过使用该ior文件,可以创建出接口对象,从而可以对该对象的远程调用!
ofstream iorFile("Messenger.ior");
iorFile << str.in() << std::endl;
iorFile.close();
std::cout << "IOR written to file Messenger.ior" << std::endl;
// Accept requests from clients. 等待客户端消息.
orb->run();
orb->destroy();
return 0;
}
catch (CORBA::Exception& ex) {
std::cerr << "MessengerServer CORBA exception: " << ex << std::endl;
}
return 1;
}
|
5. 对服务端户端编译:
通过如下命令完成对服务端的编译:
CC -mt -g -DSUN_CC_HAS_PVFC_BUG -I/tool/eesd/ACE_wrappers -I/tool/eesd/ACE_wrappers/TAO -DACE_HAS_EXCEPTIONS -D__ACE_INLINE__ -o server -L/tool/eesd/ACE_wrappers/lib -lTAO_PortableServer -lTAO -lACE -lsocket -ldl -lnsl -lgen -lposix4 -ladm MessengerServer.cpp MessengerS.cpp MessengerC.cpp MessengerI.cpp
其中 -mt是供多线程使用的,可以使用-D_REENTRANT -lthread
6. 编写客户端代码:
#include "MessengerC.h"
#include <iostream>
int main(int argc, char* argv[])
{
try {
// Initialize the ORB.
CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
// Read and destringify the Messenger object's IOR.
CORBA::Object_var obj = orb->string_to_object("file://Messenger.ior");
if( CORBA::is_nil(obj.in())) {
std::cerr << "Could not get Messenger IOR." << std::endl;
return 1;
}
// Narrow the IOR to a Messenger object reference.
Messenger_var messenger = Messenger::_narrow(obj.in());
if( CORBA::is_nil(messenger.in())) {
std::cerr << "IOR was not a Messenger object reference." << std::endl;
return 1;
}
// Send a message the the Messenger object.
CORBA::String_var msg = CORBA::string_dup("Hello!");
messenger->send_mssage("TAO User", "Test", msg.inout());
// Print the Messenger's reply.
std::cout << "Reply: " << msg.in() << std::endl;
return 0;
}
catch (CORBA::Exception& ex) {
std::cerr << "MessengerClient CORBA exception: " << ex << std::endl;
}
return 1;
}
|
7. 编译客户端代码:
CC -mt -g -DSUN_CC_HAS_PVFC_BUG -I/tool/eesd/ACE_wrappers -I/tool/eesd/ACE_wrappers/TAO -DACE_HAS_EXCEPTIONS -D__ACE_INLINE__ -o client -L/tool/eesd/ACE_wrappers/lib -lTAO_PortableServer -lTAO -lACE -lsocket -ldl -lnsl -lgen -lposix4 -ladm MessengerClient.cpp MessengerC.cpp
8. 完成.
开启两个终端.
一个运行./server
IOR written to file Messenger.ior
Message from:TAO User
Subject: Test
Message: Hello!
另一个运行./client
Reply: THanks for the message.
9. makefile 文件如下:
LIB=-lTAO_PortableServer -lTAO -lACE -lsocket -ldl -lnsl -lgen -lposix4 -ladm
LIB_PATH=-L/tool/eesd/ACE_wrappers/lib
MACRO=-DSUN_CC_HAS_PVFC_BUG -DACE_HAS_EXCEPTIONS -D__ACE_INLINE_
INC=-I/tool/eesd/ACE_wrappers -I/tool/eesd/ACE_wrappers/TAO
flag=-mt -g
sobject=MessengerServer.o MessengerS.o MessengerC.o MessengerI.o
cobject=MessengerC.o MessengerClient.o
CXX=CC #隐含规则需要的编译器.
CPPFLAGS=$(INC) $(MACRO) $(flag) #隐含规则需要的flag.
server: $(sobject)
CC $(LIB_PATH) $(LIB) $^ -o $@
client: $(cobject)
CC $(LIB_PATH) $(LIB) $^ -o $@
clean:
rm $(sobject) $(cobject)
rm client
rm server
|
阅读(1518) | 评论(0) | 转发(1) |