- CORBA基本架构
- IDL文件编写
- CPP示例实现(上)
- CPP示例实现(下)
- C示例实现(IOR+NS上)
- C示例实现(IOR+NS下)
- C示例实现(IOR上)
- C示例实现(IOR下)
然后完成客户端部分,同样分了三个文件:
HiClientImpl.hh
#include <iostream> #include <fstream> #include "Hi.hh" using namespace std; class HiClientImpl { public: HiClientImpl(); ~HiClientImpl(); void TestAdd(); CosNaming::Name m_corbaCosName; // CORBA ORB CORBA::ORB_var m_orb; // ORB Object CORBA::Object_var m_obj; // Resolved id to object reference CORBA::Object_var m_obj1; // Resolved and narrowed CORBA object for proxy calls HiCorba::Hi_var m_Data; }; class DS_ServerConnectionException{ public: DS_ServerConnectionException() { cerr << "CORBA COMM_FAILURE" << endl; }; }; class DS_SystemException{ public: DS_SystemException() { cerr << "CORBA Exception" << endl; }; }; class DS_FatalException{ public: DS_FatalException() { cerr << "CORBA Fatal Exception" << endl; }; }; class DS_Exception{ public: DS_Exception() { cerr << "Exception" << endl; }; };
HiClientImpl.cc
#include "assert.h" #include "HiClientImpl.hh" #include "Hi.hh" HiClientImpl::HiClientImpl() { try { //------------------------------------------------------------------------ // Initialize ORB object. //------------------------------------------------------------------------ int argc=0; // Dummy variables to support following call. char** argv=0; CORBA::ORB_var orb = CORBA::ORB_init(argc, argv); //------------------------------------------------------------------------ // Bind ORB object to name service object. // (Reference to Name service root context.) //------------------------------------------------------------------------ //CORBA::Object_var obj = orb->resolve_initial_references("OmniNameService"); CORBA::Object_var obj = orb->resolve_initial_references("NameService"); assert (!CORBA::is_nil(obj.in())); cerr << "resolve_initial_reference OK" << endl; //------------------------------------------------------------------------ // Narrow this to the naming context (Narrowed reference to root context.) //------------------------------------------------------------------------ CosNaming::NamingContext_var nc = CosNaming::NamingContext::_narrow(obj.in()); assert (!CORBA::is_nil(nc.in())); cerr << "narrow ok" << endl; //------------------------------------------------------------------------ // The "name text" put forth by CORBA server in name service. // This same name ("DataServiceName1") is used by the CORBA server when // binding to the name server (CosNaming::Name). //------------------------------------------------------------------------ CosNaming::Name _corbaCosName; _corbaCosName.length(1); _corbaCosName[0].id=CORBA::string_dup("Hi"); //------------------------------------------------------------------------ // Resolve "name text" identifier to an object reference. //------------------------------------------------------------------------ CORBA::Object_var obj1 = nc->resolve(_corbaCosName); assert(!CORBA::is_nil(obj1.in())); cerr << "resolve OK" << endl; m_Data = HiCorba::Hi::_narrow(obj1.in()); if (CORBA::is_nil(m_Data.in())) { cerr << "IOR is not an SA object reference." << endl; } else{ cerr << "narrow OK" << endl; } } catch(CORBA::COMM_FAILURE& ex) { cerr << "Caught system exception COMM_FAILURE -- unable to contact the " << "object." << endl; throw DS_ServerConnectionException(); return; } catch(CORBA::SystemException& ) { cerr << "Caught a CORBA::SystemException." << endl; throw DS_SystemException(); return; } catch(CORBA::Exception& ) { cerr << "Caught CORBA::Exception." << endl; throw DS_Exception(); return; } catch(omniORB::fatalException& fe) { cerr << "Caught omniORB::fatalException:" << endl; cerr << " file: " << fe.file() << endl; cerr << " line: " << fe.line() << endl; cerr << " mesg: " << fe.errmsg() << endl; throw DS_FatalException(); return; } catch(...) { cerr << "Caught unknown exception." << endl; throw DS_Exception(); return; } return; } HiClientImpl::~HiClientImpl() { } void HiClientImpl::TestAdd() { CORBA::Long num1=70; CORBA::Long num2=80; CORBA::Long retNum; cout << "Values input to Server: " << num1 << " " << num2 << " " << endl; if( retNum = m_Data->add( num1, num2)) // This is the CORBA call which is to be executed remotely { // Ok cout << "Values returned by Server: " << num1 << "+" << num2 << "=" << retNum << endl; } }
HiClient.cc
#include "HiClientImpl.hh" int main(int argc, char** argv) { HiClientImpl impl; impl.TestAdd(); return 0; }
MakeClient
CC = /usr/bin/g++ CPPFLAGS = -g -c -I/usr/include -I/usr/include/omniORB4 LDFLAGS = -g -L/usr/lib -lomniORB4 -lomnithread -lomniDynamic4 OMNIIDL = /usr/bin/omniidl all: HiClient.bin HiClient.bin: HiClient.o HiClientImpl.o HiSK.o $(CC) $(LDFLAGS) -o HiClient.bin HiClient.o HiClientImpl.o HiSK.o HiSK.o: HiSK.cc Hi.hh $(CC) $(CPPFLAGS) $(INCLUDES) HiSK.cc HiClient.o: HiClient.cc HiClientImpl.hh $(CC) $(CPPFLAGS) HiClient.cc HiClientImpl.o: HiClientImpl.cc HiClientImpl.hh Hi.hh $(CC) $(CPPFLAGS) HiClientImpl.cc nidl: Hi.idl $(OMNIIDL) -bcxx Hi.idl clean: rm -f *.o
编译
make -f MakeClient
然后是运行
#首先检查OMNI Naming Service是否在运行,默认端口2809 ps -aux | grep omni root 1585 0.0 0.2 315608 4912 ? Sl 12:04 0:00 /usr/bin/omniNames -errlog /var/log/omniorb-nameserver.log neohope 1943 0.0 0.1 12720 2180 tty2 S+ 14:57 0:00 grep omni #运行服务端 ./HiServer #运行客户端 ./HiClient >>Values input to Server: 70 80 >>Values returned by Server: 70+80=150