基於CORBA的分散式程式設計(九) (轉)
基於技術的:
本實現了CORBA與TUXEDO的互連,為聯創整合軟體公司的資料網綜合業務計費與管理系統(LICMS)與佳都國際軟體公司的飛馬系統的整合打好了技術基礎。
該軟體的介面定義(IDL)如下::namespace prefix = o ns = "urn:schemas--com::office" />
// Chat.idl
module Chat {
typedef string User;
typedef string Time;
struct Message {
User sender;
User receiver;
Time timestamp;
string content;
};
typedef sequence
typedef sequence
interface ChatServer {
short getOnlineUsers(out UserList onlineUsers);
short getMessages(out MessageList msgList, in User regName);
short registerUser(in User regName);
short UnregisterUser(in User UnregName);
short sendMessage(in Message sendMsg);
};
};
該軟體的端Skeleton(骨架)定義如下:
// Server.cpp : Defines the entry point for the console application.
//
#include
#include "ChatServerImpl.h"
#if defined(_VIS_STD)
# include
#else
# include
#endif
int main(int argc, char* const* argv)
{
try {
// Initialize the ORB.
CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
// Get the property manager; notice the value returned
// is not placed into a 'var' type.
VISPropertyManager_ptr pm = orb->getPropertyManager();
pm->addProperty("vbroker.se.mySe.scms", "scmlist");
pm->addProperty("vbroker.se.mySe.scm.scmlist.manager.type", "Socket");
pm->addProperty("vbroker.se.mySe.scm.scmlist.manager.connectionMax", (CORBA::ULong)100);
pm->addProperty("vbroker.se.mySe.scm.scmlist.manager.connectionMaxIdle",
(CORBA::ULong)300);
pm->addProperty("vbroker.se.mySe.scm.scmlist.listener.type", "IIOP");
// We just add the Port host will be set to NULL
// and will be filled with the default host
pm->addProperty("vbroker.se.mySe.scm.scmlist.listener.port", (CORBA::ULong)55000);
pm->addProperty("vbroker.se.mySe.scm.scmlist.listener.Port", (CORBA::ULong)0);
pm->addProperty("vbroker.se.mySe.scm.scmlist.dispatcher.type",
"ThreadPool");
pm->addProperty("vbroker.se.mySe.scm.scmlist.dispatcher.threadMax", (CORBA::ULong)100);
pm->addProperty("vbroker.se.mySe.scm.scmlist.dispatcher.threadMin", (CORBA::ULong)5);
pm->addProperty("vbroker.se.mySe.scm.scmlist.dispatcher.threadMaxIdle",
(CORBA::ULong)300);
// Get a reference to the POA
CORBA::_var obj = orb->resolve_initial_references("RootPOA");
PortableServer::POA_var rootPOA = PortableServer::POA::_narrow(obj);
// Create the policies
CORBA::Any_var seAny(new CORBA::Any);
// The SERVER_ENGINE_POLICY_TYPE requires a sequence, even if
// only one engine is being specified.
CORBA::StringSequence_var engines = new CORBA::StringSequence(1UL);
engines->length(1UL);
engines[(CORBA::ULong)0] = CORBA::string_dup("mySe");
seAny <<= engines;
CORBA::PolicyList_var policies = new CORBA::PolicyList(2UL);
policies->length(2UL);
policies[(CORBA::ULong)0] = orb->create_policy(PortableServerExt::SERVER_ENGINE_POLICY_TYPE, seAny);
policies[(CORBA::ULong)1] = rootPOA->create_lifespan_policy(PortableServer::PERSISTENT);
// get the POA Manager
PortableServer::POAManager_var poa_manager = rootPOA->the_POAManager();
// Create our POA with our policies
PortableServer::POA_var myPOA = rootPOA->create_POA("chat_server_poa",
poa_manager,
policies);
// Create the servant
ChatServerImpl managerServant;
// Dec on the ID for the servant
PortableServer::ObjectId_var managerId =
PortableServer::string_to_ObjectId("ChatServer");
// Activate the servant with the ID on myPOA
myPOA->activate_object_with_id(managerId, &managerServant);
// Obtain the reference
CORBA::Object_var ref = myPOA->servant_to_reference(&managerServant);
CORBA::String_var string_ref = orb->object_to_string(ref.in());
ofstream refFile("ref.dat");
refFile << string_ref << endl;
refFile.close();
/*
// Initialize the ORB.
CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
// get a reference to the root POA
CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
PortableServer::POA_var rootPOA = PortableServer::POA::_narrow(obj);
CORBA::PolicyList policies;
policies.length(1);
policies[(CORBA::ULong)0] = rootPOA->create_lifespan_policy(
PortableServer::PERSISTENT);
// get the POA Manager
PortableServer::POAManager_var poa_manager = rootPOA->the_POAManager();
// Create myPOA with the right policies
PortableServer::POA_var myPOA = rootPOA->create_POA("chat_server_poa",
poa_manager,
policies);
// Create the servant
ChatServerImpl managerServant;
// Decide on the ID for the servant
PortableServer::ObjectId_var managerId =
PortableServer::string_to_ObjectId("ChatServer");
// Activate the servant with the ID on myPOA
myPOA->activate_object_with_id(managerId, &managerServant);
*/
// Activate the POA manager
poa_manager->activate();
/// Wait for Incoming Requests
cout << "managerServant Server ready" << endl;
orb->run();
}
catch(const CORBA::Exception& e) {
cerr << e << endl;
return 1;
}
return 0;
}
該軟體的客戶端Stub(存根)定義如下:
// ORBClient.h
#if !defined corba_client_h_
#define corba_client_h_
#include "chat_c.h"
//USE_STD_NS
class corba_client{
private:
CORBA::ORB_var orb;
// PortableServer::ObjectId_var managerId;
Chat::ChatServer_var chatserver;
Chat::MessageList_var msglist;
Chat::UserList_var usrlist;
char username[40];
int osagentport;
static corba_client *_Manager;
corba_client(int port,char* name);
public:
static corba_client *GetInstance (int port,char* name);
static void destroyInstance();
~corba_client();
int corba_client::orb_init();
int getOnlineUsers();
int get_user_by_num(char * name,int message);
int getMessages();
int get_message_by_num(char * sender,char * receiver,char * timestamp,char * content,int num);
int registerUser();
int UnregisterUser();
int sendMessage(const char* sender,const char* receiver,const char* content);
};
#endif // !defined corba_client_h_
//ORBClient.cpp
#include "stdafx.h"
#include "OrbClient.h"
#include
corba_client *corba_client::_Manager = 0;
corba_client::corba_client(int port,char* name){
osagentport = port;
strcpy(username,name);
}
corba_client::~corba_client(){
}
int corba_client::orb_init()
{
cout<
int argc=3;
char* argv[3];
argv[0] = "Chat";
argv[1] = "-ORBid";
argv[2] = "BEA_IIOP";
const int MAXBUF = 1024;
char ior[MAXBUF];
try {
// Initialize the ORB.
CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
// Default to checking
CORBA::String_var fileName(CORBA::string_dup("ref.dat"));
// Convert from string to object
ifstream in(fileName);
in.getline(ior, MAXBUF);
CORBA::Object_var object = orb->string_to_object(ior);
// Locate an account manager. Give the full POA name and the servant ID.
chatserver = Chat::ChatServer::_narrow(object);
/*
orb = CORBA::ORB_init(argc,argv);
TimeBase::TimeT timeout = 20000000;
CORBA::Any timeout_value;
timeout_value <<= timeout;
CORBA::Policy_var policy;
policy = orb->create_policy(Messaging::RELATIVE_RT_TIMEOUT_POLICY_TYPE,timeout_value);
cout<
managerId = PortableServer::string_to_ObjectId("ChatServer");
cout<
chatserver = Chat::ChatServer::_bind("/chat_server_poa", managerId);
CORBA::PolicyList policies;
policies.length(1);
policies[0] = CORBA::Policy::_duplicate(policy);
CORBA::Object_var pmobj = orb->resolve_initial_references("ORBPolicyManager");
CORBA::PolicyManager_var orb_mgr = CORBA::PolicyManager::_narrow(pmobj);
orb_mgr->set_policy_overrides(policies, CORBA::SET_OVERRIDE);
cout<
*/
}
catch(CORBA::OBJECT_NOT_EXIST& e)
{
cout << "Chat Server is not start up!" < return -1; } catch (CORBA::Exception& e) { cout << "CORBA exception : " << e.get_id() << endl; return -1; } catch(...){ cout << "There are some Unknown error while inititial ORB"< return -1; } return 0; } corba_client * corba_client::GetInstance (int port,char* name) { if (!_Manager) { _Manager = new corba_client(port,name); #ifdef _DE cout << " new corba_client " << endl; #endif } return _Manager; } void corba_client::destroyInstance(){ #ifdef _DEBUG cout << " delete corba_client " << endl; #endif if (_Manager) delete _Manager; _Manager = 0; } int corba_client::getOnlineUsers() { if((Chat::ChatServer_ptr)chatserver == NULL) return -1; int i; try{ cout<
if((Chat::ChatServer_ptr)chatserver != NULL) i=chatserver->getOnlineUsers(usrlist); cout<
} catch(CORBA::Exception &e) { cerr << e.get_id() << endl; i=-2; } return i; } int corba_client::get_user_by_num(char * name,int num) { if (num < 0 || num > usrlist->length()) return -1; if((Chat::ChatServer_ptr)chatserver != NULL){ Chat::UserList::_ForSeq_var* item = &((*usrlist)[num]); strcpy(name,*item); // ?? } return 0; } int corba_client::getMessages() { if((Chat::ChatServer_ptr)chatserver == NULL) return -1; int i; try{ cout<
if((Chat::ChatServer_ptr)chatserver != NULL) i=chatserver->getMessages(msglist,username); cout<
} catch(CORBA::Exception &e) { cerr << e.get_id() << endl; i=-2; } return i; } int corba_client::get_message_by_num(char * sender,char * receiver,char * timestamp,char * content,int num) { if (num < 0 || num > msglist->length()) return -1; if((Chat::ChatServer_ptr)chatserver != NULL){ Chat::Message * item = &((*msglist)[num]); strcpy(sender,item->sender); strcpy(receiver,item->receiver); strcpy(timestamp,item->timestamp); strcpy(content,item->content); } return 0; } int corba_client::registerUser() { if((Chat::ChatServer_ptr)chatserver == NULL) return -1; int i; try{ i=chatserver->registerUser(username); } catch(CORBA::Exception &e) { cerr << e.get_id() << endl; i=-2; } return i; } int corba_client::UnregisterUser() { if((Chat::ChatServer_ptr)chatserver == NULL) return -1; int i; try{ i=chatserver->UnregisterUser(username); } catch(CORBA::Exception &e) { cerr << e.get_id() << endl; i=-2; } return i; } int corba_client::sendMessage(const char* sender,const char* receiver,const char* content) { if((Chat::ChatServer_ptr)chatserver == NULL) return -1; int i; try{ Chat::Message msg; msg.sender = CORBA::string_dup(sender); msg.receiver = CORBA::string_dup(receiver); msg.content = CORBA::string_dup(content); i = chatserver->sendMessage(msg); } catch(CORBA::Exception &e) { cerr << e.get_id() << endl; i=-2; } return i; } 本軟體的開發環境使用了Visual C++、VIsiBroker版本的CORBA產品和Tuxedo 8.1。 軟體除了具備傳送訊息、接收訊息等基礎的功能外,還實現了檢視線上、群發訊息、收發中文訊息等許多功能,並解決了頭疼的中文訊息互發問題。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752043/viewspace-997896/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 轉轉基於MQ的分散式重試框架設計方案MQ分散式框架
- 基於SpringBoot AOP面向切面程式設計實現Redis分散式鎖Spring Boot程式設計Redis分散式
- 基於Scrapy分散式爬蟲的開發與設計分散式爬蟲
- [轉載]基於Redis的分散式鎖到底安全嗎?Redis分散式
- 分散式系統程式設計分散式程式設計
- 基於redis的分散式鎖Redis分散式
- 基於 Redis 的分散式鎖Redis分散式
- 基於kubernetes的分散式限流分散式
- 如何管理基於微服務的分散式應用程式微服務分散式
- 基於UDP程式設計UDP程式設計
- 基於java的分散式爬蟲Java分散式爬蟲
- 基於 dubbo 的分散式架構分散式架構
- 基於 Redis 分散式鎖Redis分散式
- 後端程式設計師必備:分散式事務基礎篇後端程式設計師分散式
- 一種基於柔性事務的分散式事務解決方案設計探究分散式
- 基於promise的阻塞式佇列設計Promise佇列
- 關於分散式鎖在程式設計中的一些應用場景分散式程式設計
- [翻譯]基於redis的分散式鎖Redis分散式
- 基於zookeeper的分散式配置中心(一)分散式
- 基於Redis分散式BitMap的應用Redis分散式
- 基於 Zookeeper 的分散式鎖實現分散式
- 基於redis做分散式鎖Redis分散式
- 基於SpringCloud分散式架構SpringGCCloud分散式架構
- 非同步程式設計:基於事件的非同步程式設計模式(EAP)非同步程式設計事件設計模式
- React基礎:宣告式程式設計React程式設計
- 基於Dynomite的分散式延遲佇列MIT分散式佇列
- 分散式ID設計方案分散式
- 基於redis實現分散式鎖Redis分散式
- 基於ZK實現分散式鎖分散式
- 分散式應用開發的核心技術系列之——基於TCP/IP的原始訊息設計分散式TCP
- 基於SPI的增強式外掛框架設計框架
- 圖解|搞定分散式?程式設計師進階之路圖解分散式程式設計師
- 多利熊基於分散式架構實踐穩定性建設分散式架構
- 基於RocketMq的分散式事務解決方案MQ分散式
- 分散式搜尋系統的設計分散式
- 「如何設計」高可用的分散式鎖分散式
- 跟著原始碼學IM(九):基於Netty實現一套分散式IM系統原始碼Netty分散式
- 【分散式架構】(10)---基於Redis元件的特性,實現一個分散式限流分散式架構Redis元件
- 基於Spring Cloud搭建分散式配置中心SpringCloud分散式