⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 thiefserver.cpp

📁 一个使用CORBA DSI编程的例子
💻 CPP
字号:
#include <iostream.h>#include <map>#include <time.h>#include <string>#include <list>#include "ThiefS.h"class Valuable_impl : public virtual POA_Thief::Valuable{public:	virtual void sayValue ()	{		cout << "Valuable:sayValue" << endl;	}};class Count_impl : public virtual POA_Thief::Count{public:	Count_impl()	{		time_t t = time(NULL);		char buf[64];		sprintf(buf, "%d", t);		ms = buf;	}		virtual void print ()	{		cout << "COUNT: " << ms << endl;	}private:	string ms;};class Money_impl : public virtual POA_Thief::Money,				public virtual Valuable_impl{public:	Money_impl(int mode) : myMode(mode)	{	}		virtual void sayValue ()	{		cout << "Money:sayValue : " << myMode<< endl;	}		virtual void getCount (Thief::Count_out count)	{		Count_impl* p = new Count_impl();		count = p->_this();	}		void registerCount()	{			}private:	int myMode;};class Jewelry_impl : public virtual POA_Thief::Jewelry,				public virtual Valuable_impl{public:	Jewelry_impl(int mode) : myMode(mode)	{	}		virtual void sayValue ()	{		cout << "Jewelry:sayValue : " << myMode << endl;	}private:	int myMode;};struct IFSuite{	Money_impl* mMoney;	Jewelry_impl* mJewelry;};class Session_impl;class ThiefLocator_impl : public virtual PortableServer::ServantLocator{public:	ThiefLocator_impl(IFSuite* ifsuites, map <string, Session_impl*>* sessions);	~ThiefLocator_impl();	virtual PortableServer::Servant preinvoke (const PortableServer::ObjectId& oid,												PortableServer::POA_ptr poa,												const char* operation,												void*& cookie);		virtual void postinvoke (const PortableServer::ObjectId& oid,							PortableServer::POA_ptr poa,							const char* operation,							void* cookie,							PortableServer::Servant servant);private:	map <string, Session_impl*>* mSessions;	IFSuite* mIFSuites;	};class Session_impl : public virtual POA_Thief::Session{public:	Session_impl(PortableServer::POA_ptr poa, 				IFSuite* ifSuites, string& sessionID, PortableServer::ServantLocator_ptr locator)			: myMode(0) //default Mode=0;	{		ifPoa = poa;		mSessionID = sessionID;		mIFSuite = ifSuites;//		mSessions = sessions;	}		virtual void applyKey (const char * key,Thief::Valuable_out theft)	{		cout << "myMode=" << myMode << endl;		string s_key = key;				const char* repos_id = "IDL:Thief:1.0";		string s_oid;				if (s_key == "money")		{			s_oid = mSessionID + "_" + "money";		}		else		{			s_oid = mSessionID + "_" + "jewelry";		}				PortableServer::ObjectId_var oid = 			PortableServer::string_to_ObjectId(s_oid.c_str());		CORBA::Object_var obj = ifPoa->create_reference_with_id(oid, repos_id);		theft = Thief::Valuable::_narrow(obj);		cout << "reference created" << endl;		//		if (s_key == "money")//		{			//theft = Thief::Valuable::_narrow(objs[0]);			/*cout << "try to activate money object" << endl;			string s_oid = mSessionID + "_" + "money";			PortableServer::ObjectId_var oid = 				PortableServer::string_to_ObjectId(s_oid.c_str());			cout << "money oid = " << s_oid << endl;						Money_impl* p_money = mIFSuite[myMode].mMoney;			if (p_money != NULL)			{				cout << "money impl ok" << endl;			}			else			{				cout << "money impl KO" << endl;			}			try			{				mPoa->activate_object_with_id(oid, p_money);			}			catch (const PortableServer::POA::WrongPolicy& wp)			{				cout << "wrong policy" << endl;			}			catch (const CORBA::Exception& e)			{				CORBA::SystemException *se;				if ((se = CORBA::INV_POLICY::_downcast(&e)) != 0)				{					cout << "policy" << endl;				}				else if ((se = CORBA::MARSHAL::_downcast(&e)) != 0)				{					cout << "marshal" << endl;				}				else if ((se = CORBA::BAD_INV_ORDER::_downcast(&e)) != 0)				{					cout << "order" << endl;				}				else if ((se = CORBA::BAD_CONTEXT::_downcast(&e)) != 0)				{					cout << "context" << endl;				}				else if ((se = CORBA::IMP_LIMIT::_downcast(&e)) != 0)				{					cout << "limit" << endl;				}				else if ((se = CORBA::INTF_REPOS::_downcast(&e)) != 0)				{					cout << "REPOS" << endl;				}				else if ((se = CORBA::INV_FLAG::_downcast(&e)) != 0)				{					cout << "flag" << endl;				}				else if ((se = CORBA::INV_IDENT::_downcast(&e)) != 0)				{					cout << "ident" << endl;				}				else if ((se = CORBA::INV_OBJREF::_downcast(&e)) != 0)				{					cout << "objref" << endl;				}				else if ((se = CORBA::NO_IMPLEMENT::_downcast(&e)) != 0)				{					cout << "implement" << endl;				}				else if ((se = CORBA::OBJECT_NOT_EXIST::_downcast(&e)) != 0)				{					cout << "exist" << endl;				}				else if ((se = CORBA::OBJ_ADAPTER::_downcast(&e)) != 0)				{					cout << "adapter" << endl;				}				else if ((se = CORBA::UNKNOWN::_downcast(&e)) != 0)				{					cout << "corba unknow" << endl;				}				else				{					cout << "unknow" << endl;				}			}			cout << "money object actived" << endl;			theft = p_money->_this();*///			theft = Thief::Valuable::_narrow(objs[0]);			//theft = Thief::Valuable::_narrow(objs[0]);						//theft = mIFSuite[myMode].mMoney->_this();//		}//		else//		{			//theft = Thief::Valuable::_narrow(objs[1]);			/*cout << "try to activate jewelry object" << endl;			string s_oid = mSessionID + "_" + "jewelry";			PortableServer::ObjectId_var oid = 				PortableServer::string_to_ObjectId(s_oid.c_str());			cout << "jewelry oid = " << s_oid << endl;						Jewelry_impl* p_jewelry = mIFSuite[myMode].mJewelry;			mPoa->activate_object_with_id(oid, p_jewelry);			cout << "jewelry object actived" << endl;			theft = p_jewelry->_this();*///			theft = Thief::Valuable::_narrow(objs[1]);			//theft = Thief::Valuable::_narrow(objs[1]);						//theft = mIFSuite[myMode].mJewelry->_this();//		}			}		virtual void setMode (CORBA::Short mode)	{		cout << "change Mode to " << mode << endl;		myMode = mode;				/*const char* repos_id = "IDL:Thief:1.0";		string s_oid = mSessionID + "_" + "money";		PortableServer::ObjectId_var oid = 			PortableServer::string_to_ObjectId(s_oid.c_str());		Money_impl* p_money = mIFSuite[myMode].mMoney;		cout << "p_money address : " << p_money << endl;		cout << "money oid = " << s_oid << endl;		mPoa->deactivate_object(oid);		mPoa->create_reference_with_id(oid, repos_id);		mPoa->activate_object_with_id(oid, p_money);		cout << "money object actived" << endl;				s_oid = mSessionID + "_" + "jewelry";		oid = PortableServer::string_to_ObjectId(s_oid.c_str());		Jewelry_impl* p_jewelry = mIFSuite[myMode].mJewelry;		cout << "jewelry oid = " << s_oid << endl;		mPoa->deactivate_object(oid);		mPoa->create_reference_with_id(oid, repos_id);		mPoa->activate_object_with_id(oid, p_jewelry);		cout << "jewelry object actived" << endl;		cout << "change Mode ok" << endl;		*/	}public:	int myMode;private:	IFSuite* mIFSuite;	PortableServer::POA_ptr ifPoa;	string mSessionID;//	map <string, Session_impl*>* mSessions};ThiefLocator_impl::ThiefLocator_impl( IFSuite* ifsuites, map <string, Session_impl*>* sessions){	mSessions = sessions;	mIFSuites = ifsuites;}	ThiefLocator_impl::~ThiefLocator_impl(){}	PortableServer::Servant ThiefLocator_impl::preinvoke (const PortableServer::ObjectId& oid,												PortableServer::POA_ptr poa,												const char* operation,												void*& cookie){	string s_op = operation;	cout << "preinvoke------------" << endl;	string s_oid = PortableServer::ObjectId_to_string(oid);	cout << "invoke oid" << endl;	int pos = s_oid.find("_");	string sessionID = s_oid.substr(0, pos);	cout << "sessionID: " << s_oid << endl;	int mode = (*mSessions)[sessionID]->myMode;	cout << "mode:" << mode << endl;		string key = s_oid.substr(pos+1);	if (key == "money")	{		cout << "return money" << endl;		PortableServer::Servant servant = mIFSuites[mode].mMoney;		return servant;	}	else	{		cout << "return jewelry" << endl;		PortableServer::Servant servant = mIFSuites[mode].mJewelry;		return servant;	} }		void ThiefLocator_impl::postinvoke (const PortableServer::ObjectId& oid,							PortableServer::POA_ptr poa,							const char* operation,							void* cookie,							PortableServer::Servant servant){	string s_op = operation;	cout << "opereation = " << s_op << endl;	if (s_op = "getCount")	{		Money_impl* m = (Money_impl*)servant;		m->	}	cout << "end invoke with oid " << PortableServer::ObjectId_to_string(oid) << endl;}class SessionFactory_impl : public virtual POA_Thief::SessionFactory{public:	SessionFactory_impl(PortableServer::POA_var& parent_Poa)	{		parentPoa = parent_Poa;				mIFSuites[0].mMoney = new Money_impl(3);		cout << "Money 0 address : " << mIFSuites[0].mMoney << endl;		mIFSuites[0].mJewelry = new Jewelry_impl(3);		//mIFSuites[0] = ifSuite0;				//IFSuite ifSuite1;		mIFSuites[1].mMoney = new Money_impl(5);		cout << "Money 1 address : " << mIFSuites[1].mMoney << endl;		mIFSuites[1].mJewelry = new Jewelry_impl(5);		//mIFSuites[1] = ifSuite1;				CORBA::PolicyList policy_list;		policy_list.length(3);		PortableServer::IdAssignmentPolicy_var idAssignment = parent_Poa->create_id_assignment_policy(PortableServer::USER_ID);		policy_list[0] = PortableServer::IdAssignmentPolicy::_duplicate(idAssignment);		PortableServer::LifespanPolicy_var lifespan = parent_Poa->create_lifespan_policy(PortableServer::PERSISTENT);		policy_list[1] = PortableServer::LifespanPolicy::_duplicate(lifespan);		PortableServer::IdUniquenessPolicy_var idUniqueness = parent_Poa->create_id_uniqueness_policy(PortableServer::MULTIPLE_ID);		policy_list[1] = PortableServer::IdUniquenessPolicy::_duplicate(idUniqueness);				sessionPoa = parent_Poa->create_POA("SessionPOA",									parentPoa->the_POAManager(),									policy_list);		sessionPoa->the_POAManager()->activate();				locator = new ThiefLocator_impl(mIFSuites, &mSessions);				CORBA::PolicyList policy_list1;		policy_list1.length(3);//		PortableServer::IdAssignmentPolicy_var idAssignment = poa->create_id_assignment_policy(PortableServer::USER_ID);		policy_list1[0] = PortableServer::IdAssignmentPolicy::_duplicate(idAssignment);//		PortableServer::IdUniquenessPolicy_var idUniqueness = parent_Poa->create_id_uniqueness_policy(PortableServer::MULTIPLE_ID);//		policy_list[1] = PortableServer::IdUniquenessPolicy::_duplicate(idUniqueness);//		PortableServer::ImplicitActivationPolicy_var implicitActivation = parent_Poa->create_implicit_activation_policy(PortableServer::NO_IMPLICIT_ACTIVATION);//		policy_list[2] = PortableServer::ImplicitActivationPolicy::_duplicate(implicitActivation);		PortableServer::ServantRetentionPolicy_var servantRetention = sessionPoa->create_servant_retention_policy(PortableServer::NON_RETAIN);		policy_list1[1] = PortableServer::ServantRetentionPolicy::_duplicate(servantRetention);		PortableServer::RequestProcessingPolicy_var requestProcessing = sessionPoa->create_request_processing_policy(PortableServer::USE_SERVANT_MANAGER);		policy_list1[2] = PortableServer::RequestProcessingPolicy::_duplicate(requestProcessing);				string s_ifPoa = "InterfacePOA";		ifPoa = sessionPoa->create_POA(s_ifPoa.c_str(),									sessionPoa->the_POAManager(),									policy_list1);		//		PortableServer::ServantLocator_var locator = new ThiefLocator_impl(mIFSuite, mSessions);				ifPoa->set_servant_manager(locator);	}		~SessionFactory_impl()	{		/*map <int, IFSuite>::iterator it = mIFSuites.begin();		while(it != mIFSuites.end()	)		{			delete (it->second.mMoney);			delete (it->second.mJewelry);		}*/		for (int i = 0; i < 2; i++)		{			delete mIFSuites[i].mMoney;			delete mIFSuites[i].mJewelry;		}				map <string, Session_impl*>::iterator s_it = mSessions.begin();		while (s_it != mSessions.end())		{			delete (s_it->second);		}	}		virtual void login (Thief::Session_out session)	{		time_t t = time(NULL);		char buf[64];		sprintf(buf, "%d", t);		string s_buf = buf;		cout << "sessionID=" << s_buf << endl;		Session_impl* session_impl = new Session_impl(ifPoa, mIFSuites, s_buf, locator);				cout << "new instance created" << endl;		mSessions[s_buf] = session_impl;				/*const char* repos_id = "IDL:Thief:1.0";		string s_oid = mSessionID + "_" + "session";		PortableServer::ObjectId_var oid = 			PortableServer::string_to_ObjectId(s_oid.c_str());		objs[1] = mPoa->create_reference_with_id(oid, repos_id);*/		string s_oid = s_buf + "_" + "session";		PortableServer::ObjectId_var oid = 			PortableServer::string_to_ObjectId(s_oid.c_str());		cout << "oid generated " << s_oid << endl;				sessionPoa->activate_object_with_id(oid, session_impl);		cout << "New Session activated " << s_oid << endl;		CORBA::Object_var obj = sessionPoa->id_to_reference(oid);		session = Thief::Session::_narrow(obj);//		session = session_impl->_this();		cout << "logined" << endl;	}	private:	PortableServer::ServantLocator_var locator;	IFSuite mIFSuites[2];	PortableServer::POA_var sessionPoa;	PortableServer::POA_var ifPoa;	PortableServer::POA_ptr parentPoa;	map <string, Session_impl*> mSessions;};int main(int argc, char* argv[]){	try	{		CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);				CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");		PortableServer::POA_var poa = PortableServer::POA::_narrow(obj);//		PortableServer::POAManager_var mgr = poa->the_POAManager();//		mgr->activate();		CORBA::PolicyList policy_list;		policy_list.length(2);		PortableServer::IdAssignmentPolicy_var idAssignment = poa->create_id_assignment_policy(PortableServer::USER_ID);		policy_list[0] = PortableServer::IdAssignmentPolicy::_duplicate(idAssignment);		PortableServer::LifespanPolicy_var lifespan = poa->create_lifespan_policy(PortableServer::PERSISTENT);		policy_list[1] = PortableServer::LifespanPolicy::_duplicate(lifespan);				PortableServer::POA_var sessionFactoryPoa = poa->create_POA("SessionFactoryPOA",									poa->the_POAManager(),									policy_list);				const char* repos_id = "IDL:Thief:1.0";		PortableServer::ObjectId_var oid = 			PortableServer::string_to_ObjectId("SessionFactory");		SessionFactory_impl factory(sessionFactoryPoa);		cout << "--------------------" << endl;		sessionFactoryPoa->activate_object_with_id(oid, &factory);		cout << "---------------------------" << endl;		//		CORBA::Object_var obj1 = sessionFactoryPoa->create_reference_with_id(oid, repos_id);		CORBA::Object_var obj1 = sessionFactoryPoa->id_to_reference(oid);				cout << "--------------------------------------" << endl;		Thief::SessionFactory_var servant = Thief::SessionFactory::_narrow(obj1);				cout << "--------------------------------------------------------" << endl;//		Thief::SessionFactory_var servant = factory._this();		CORBA::String_var str = orb->object_to_string(servant);		cout << str << endl;				orb->run();	}	catch (const PortableServer::POA::WrongPolicy &)	{		cerr << "wrong policy" << endl;		return 1;	}	catch (const CORBA::Exception&)	{		cerr << "Uncaught CORBA exception" << endl;		return 1;	}	return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -