KSynDataSet.cpp
上传用户:dzyhzl
上传日期:2019-04-29
资源大小:56270k
文件大小:13k
源码类别:

模拟服务器

开发平台:

C/C++

  1. // KSynDataSet.cpp: implementation of the KSynDataSet class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "KSynDataSet.h"
  5. #include "KSynDataSets.h"
  6. #include "iostream.h"
  7. //////////////////////////////////////////////////////////////////////
  8. // Construction/Destruction
  9. //////////////////////////////////////////////////////////////////////
  10. //#define printf ;
  11. extern FILE * stream;
  12. extern FILE * stream1;
  13. extern CORBA::ORB_var g_Orb;
  14. CORBA::Object_ptr  GetObjectReference(char * szObjName, char * szObjType, CORBA::ORB_ptr orb);
  15. CORBA::Boolean BindObjectToName(CORBA::ORB_ptr orb, CORBA::Object_ptr objref, char * ObjName, char * ObjType);
  16. KSynDataSet::KSynDataSet()
  17. {
  18. m_nTopIndex = 0;
  19. for(long i = 0; i < MAX_UNITNUM; i++)
  20. {
  21. m_Sets[i] = NULL;
  22. }
  23. }
  24. KSynDataSet::~KSynDataSet()
  25. {
  26. }
  27. //
  28. CORBA::Long KSynDataSet::Lock(CORBA::Long id, const char* ObjName)
  29. {
  30. long lockid = 0;
  31. HANDLE hevent;
  32. char e_name[20];
  33. itoa(id, e_name,10);
  34. //同时只能有一个线程申请Lock服务。
  35. hevent = OpenEvent(EVENT_ALL_ACCESS, FALSE, e_name);
  36. if (!hevent)
  37. hevent = CreateEvent(NULL,TRUE,TRUE,e_name);
  38. if (!hevent)
  39. hevent = OpenEvent(EVENT_ALL_ACCESS, FALSE, e_name);
  40. WaitForSingleObject(hevent,INFINITE);
  41. if ((lockid = m_LockSets[id].Lock()) < 0)
  42. {
  43. LockInfoNode * pNode = new LockInfoNode;
  44. pNode->id = id;
  45. pNode->index =  -1 * lockid;
  46. pNode->pServer = NULL;
  47. long ServerId = g_FileName2Id((char *)ObjName);
  48. TServerNode *pServerData = NULL; 
  49. if(!m_ServerList.IsEmpty())
  50. {
  51. //在服务对象列表中查找是否有一致id的服务器对象
  52. for( pServerData = (TServerNode*) m_ServerList.GetHead(); pServerData != NULL; )
  53. {
  54. if (pServerData->ServerId == ServerId)
  55. {
  56. pNode->pServer = pServerData->pServer;
  57. break;
  58. }
  59. pServerData = (TServerNode *) pServerData->GetNext();
  60. }
  61. if (!pNode->pServer)
  62. {
  63. }
  64. }
  65. else 
  66. {
  67. }
  68. //未找到的话
  69. if (!pNode->pServer)//Server  is Not Exist ,Then Build a New Server Connection
  70. {
  71. char cString[100];
  72. strcpy(cString,ObjName); 
  73. char * sName = cString;
  74. char * sType = strstr(cString, ".");
  75. sType ++;
  76. cString[sType - sName - 1] = '';
  77. CORBA::Object_ptr obj_ptr = GetObjectReference(sName,sType,g_Orb);
  78. // printf("Servername is %s,%s and %sn",ObjName,sName,sType);
  79. if (obj_ptr)
  80. {
  81. TServerNode * pNewServer = new TServerNode;
  82. pNewServer->pServer = ReqServer::_narrow(obj_ptr);
  83. pNewServer->ServerId = ServerId;
  84. pNode->pServer = pNewServer->pServer;
  85. m_ServerList.AddTail((KNode*) pNewServer);
  86. }
  87. else
  88. {
  89. SetEvent(hevent);
  90. return -100000;
  91. }
  92. }
  93. m_ReqLockList.AddTail((KNode*)pNode);
  94. }
  95. SetEvent(hevent);
  96. return lockid;
  97. }
  98. CORBA::Long KSynDataSet::UnLock(CORBA::Long id, CORBA::Long index)  
  99. {
  100. HANDLE hevent;
  101. char e_name[20];
  102. e_name[0]='U';
  103. itoa(id, e_name+1,10);
  104. //同时只能有一个线程申请UnLock服务
  105. hevent = OpenEvent(EVENT_ALL_ACCESS, FALSE, e_name);
  106. if (!hevent)
  107. hevent = CreateEvent(NULL,TRUE,TRUE,e_name);
  108. WaitForSingleObject(hevent,INFINITE);
  109. LockInfoNode *pNode;
  110. fflush(stream);
  111. fflush(stream1);
  112. if (m_LockSets[id].UnLock(index) == TRUE)//如果本次解锁成功
  113. {
  114. if(!m_ReqLockList.IsEmpty())
  115. {
  116. ///在数据锁定请求列表中找到下一个请求相关的对象,并调用通知其。
  117. if (m_LockSets[id].GetCurId() > 0)
  118. {
  119. for( pNode = (LockInfoNode*) m_ReqLockList.GetHead(); pNode != NULL; )
  120. {
  121. if (pNode->id == id && pNode->index == m_LockSets[id].GetCurId()) 
  122. {
  123. pNode->pServer->SendResult(id, m_LockSets[id].GetCurId(),0);
  124. pNode->Remove();
  125. SetEvent(hevent);
  126. return 1;
  127. }
  128. pNode = (LockInfoNode *) pNode->GetNext();
  129. }
  130. }
  131. }
  132. else
  133. {
  134. }
  135. SetEvent(hevent);
  136. return 1;
  137. }
  138. else 
  139. {
  140. SetEvent(hevent);
  141. return -1;
  142. }
  143. SetEvent(hevent);
  144. }
  145. CORBA::Any* KSynDataSet::GetData(CORBA::Long id) 
  146. {
  147. CORBA::Any* result = new CORBA::Any;
  148. *result = m_Sets[id]->GetData();
  149. return result;
  150. }
  151. CORBA::Any* KSynDataSet::GetDataWithLock(CORBA::Long id, CORBA::Long& Result, const char* ObjName) 
  152. {
  153. if ((Result = Lock(id,ObjName)) > 0)
  154. {
  155. return GetData(id);
  156. }
  157. else
  158. {
  159. return NULL;
  160. }
  161. }
  162. CORBA::Long KSynDataSet::SetData(CORBA::Long id, const CORBA::Any& data) 
  163. {
  164. m_Sets[id]->SetData(data);
  165. return 1;
  166. }
  167. CORBA::Long KSynDataSet::SetDataWithLock(CORBA::Long id, const CORBA::Any& data, CORBA::Long& Result, const char* ObjName) 
  168. {
  169. if ( (Result = Lock(id,ObjName)) > 0)
  170. {
  171. SetData(id,data);
  172. return Result;
  173. }
  174. else
  175. return Result;
  176. }
  177. CORBA::Long KSynDataSet::SetDataWithOnce(CORBA::Long id, const CORBA::Any& data, CORBA::Long& Result, const char* ObjName) 
  178. {
  179. long nLockId;
  180. if ((nLockId = Lock(id,ObjName)) > 0)
  181. {
  182. SetData(id,data);
  183. UnLock(id, nLockId);
  184. Result = nLockId;
  185. return Result;
  186. }
  187. else 
  188. {
  189. if (nLockId < 0){};
  190. Result = nLockId;
  191. return nLockId;
  192. }
  193. }
  194. CORBA::Any* KSynDataSet::GetDataWithOnce(CORBA::Long id, CORBA::Long& Result, const char* ObjName) 
  195. {
  196. CORBA::Any *result;
  197. if ((Result = Lock(id,ObjName)) > 0)
  198. {
  199. result = GetData(id);
  200. UnLock(id,Result);
  201. return result;
  202. }
  203. else
  204. return NULL;
  205. }
  206. CORBA::Object_ptr  GetObjectReference(char * szObjName, char * szObjType, CORBA::ORB_ptr orb)
  207. {
  208. CosNaming::NamingContext_var rootContext;
  209. try {
  210. // Obtain a reference to the root context of the Name service:
  211. CORBA::Object_var obj;
  212. obj = orb->resolve_initial_references("NameService");
  213. // Narrow the reference returned.
  214. rootContext = CosNaming::NamingContext::_narrow(obj);
  215. if( CORBA::is_nil(rootContext) ) {
  216. cerr << "Failed to narrow the root naming context." << endl;
  217. return CORBA::Object::_nil();
  218. }
  219. }
  220. catch(CORBA::ORB::InvalidName& ex) {
  221. // This should not happen!
  222. cerr << "Service required is invalid [does not exist]." << endl;
  223. return CORBA::Object::_nil();
  224. }
  225. // Create a name object, containing the name test/context:
  226. CosNaming::Name name;
  227. name.length(2);
  228. name[0].id   = (const char*) "test";       // string copied
  229. name[0].kind = (const char*) "my_context"; // string copied
  230. name[1].id   = (const char*) szObjName;//"CorbaPartical";
  231. name[1].kind = (const char*) szObjType;//"Object";
  232. // Note on kind: The kind field is used to indicate the type
  233. // of the object. This is to avoid conventions such as that used
  234. // by files (name.type -- e.g. test.ps = postscript etc.)
  235. try {
  236. // Resolve the name to an object reference.
  237. return rootContext->resolve(name);
  238. }
  239. catch(CosNaming::NamingContext::NotFound& ex) {
  240. // This exception is thrown if any of the components of the
  241. // path [contexts or the object] aren't found:
  242. cerr << "Context not found." << endl;
  243. }
  244. catch(CORBA::COMM_FAILURE& ex) {
  245. cerr << "Caught system exception COMM_FAILURE -- unable to contact the "
  246. << "naming service." << endl;
  247. }
  248. catch(CORBA::SystemException&) {
  249. cerr << "Caught a CORBA::SystemException while using the naming service."
  250. << endl;
  251. }
  252. }
  253. CORBA::Boolean BindObjectToName(CORBA::ORB_ptr orb, CORBA::Object_ptr objref, char * ObjName, char * ObjType)
  254. {
  255. CosNaming::NamingContext_var rootContext;
  256. try {
  257. // Obtain a reference to the root context of the Name service:
  258. CORBA::Object_var obj;
  259. obj = orb->resolve_initial_references("NameService");
  260. // Narrow the reference returned.
  261. rootContext = CosNaming::NamingContext::_narrow(obj);
  262. if( CORBA::is_nil(rootContext) ) {
  263. cerr << "Failed to narrow the root naming context." << endl;
  264. return 0;
  265. }
  266. }
  267. catch(CORBA::ORB::InvalidName& ex) {
  268. // This should not happen!
  269. cerr << "Service required is invalid [does not exist]." << endl;
  270. return 0;
  271. }
  272. try {
  273. // Bind a context called "test" to the root context:
  274. CosNaming::Name contextName;
  275. contextName.length(1);
  276. contextName[0].id   = (const char*) "test";       // string copied
  277. contextName[0].kind = (const char*) "my_context"; // string copied
  278. // Note on kind: The kind field is used to indicate the type
  279. // of the object. This is to avoid conventions such as that used
  280. // by files (name.type -- e.g. test.ps = postscript etc.)
  281. CosNaming::NamingContext_var testContext;
  282. try {
  283. // Bind the context to root.
  284. testContext = rootContext->bind_new_context(contextName);
  285. }
  286. catch(CosNaming::NamingContext::AlreadyBound& ex) {
  287. // If the context already exists, this exception will be raised.
  288. // In this case, just resolve the name and assign testContext
  289. // to the object returned:
  290. CORBA::Object_var obj;
  291. obj = rootContext->resolve(contextName);
  292. testContext = CosNaming::NamingContext::_narrow(obj);
  293. if( CORBA::is_nil(testContext) ) {
  294. cerr << "Failed to narrow d context." << endl;
  295. return 0;
  296. }
  297. }
  298. // Bind objref with name Echo to the testContext:
  299. CosNaming::Name objectName;
  300. objectName.length(1);
  301. objectName[0].id   = (const char*) ObjName; //"CorbaPartical";   // string copied
  302. objectName[0].kind = (const char*) ObjType;//"Object"; // string copied
  303. try {
  304. testContext->bind(objectName, objref);
  305. }
  306. catch(CosNaming::NamingContext::AlreadyBound& ex) {
  307. testContext->rebind(objectName, objref);
  308. }
  309. // Note: Using rebind() will overwrite any Object previously bound
  310. //       to /test/Echo with obj.
  311. //       Alternatively, bind() can be used, which will raise a
  312. //       CosNaming::NamingContext::AlreadyBound exception if the name
  313. //       supplied is already bound to an object.
  314. // Amendment: When using OrbixNames, it is necessary to first try bind
  315. // and then rebind, as rebind on it's own will throw a NotFoundexception if
  316. // the Name has not already been bound. [This is incorrect behaviour -
  317. // it should just bind].
  318. }
  319. catch(CORBA::COMM_FAILURE& ex) {
  320. cerr << "Caught system exception COMM_FAILURE -- unable to contact the "
  321. << "naming service." << endl;
  322. return 0;
  323. }
  324. catch(CORBA::SystemException&) {
  325. cerr << "Caught a CORBA::SystemException while using the naming service."
  326. << endl;
  327. return 0;
  328. }
  329. return 1;
  330. }
  331. CORBA::Any* KSynDataSet::GetDataMember(CORBA::Long id,CORBA::Long MemberId)
  332. {
  333. CORBA::Any* result = new CORBA::Any;
  334. *result = m_Sets[id]->GetDataMember(MemberId);
  335. return result;
  336. }
  337. CORBA::Long KSynDataSet::SetDataMember(CORBA::Long id, CORBA::Long MemberId, const CORBA::Any& data) 
  338. {
  339. m_Sets[id]->SetDataMember(MemberId, data);
  340. return 1;
  341. }
  342. CORBA::Any* KSynDataSet::GetDataMemberWithLock(CORBA::Long id, CORBA::Long MemberId, CORBA::Long& Result, const char* ObjName) 
  343. {
  344. if ((Result = Lock(id,ObjName)) > 0)
  345. {
  346. return GetDataMember(id,MemberId);
  347. }
  348. else
  349. {
  350. return NULL;
  351. }
  352. }
  353. CORBA::Long KSynDataSet::SetDataMemberWithLock(CORBA::Long id, CORBA::Long MemberId, const CORBA::Any& data, CORBA::Long& Result, const char* ObjName) 
  354. {
  355. if ( (Result = Lock(id,ObjName)) > 0)
  356. {
  357. SetDataMember(id,MemberId,data);
  358. return Result;
  359. }
  360. else
  361. return Result;
  362. }
  363. CORBA::Any* KSynDataSet::GetDataMemberWithOnce(CORBA::Long id, CORBA::Long MemberId, CORBA::Long& Result, const char* ObjName) 
  364. {
  365. CORBA::Any *result;
  366. if ((Result = Lock(id,ObjName)) > 0)
  367. {
  368. result = GetDataMember(id, MemberId);
  369. UnLock(id,Result);
  370. return result;
  371. }
  372. else
  373. return NULL;
  374. }
  375. CORBA::Long KSynDataSet::SetDataMemberWithOnce(CORBA::Long id, CORBA::Long MemberId, const CORBA::Any& data, CORBA::Long& Result, const char* ObjName) 
  376. {
  377. long nLockId;
  378. if ((nLockId = Lock(id,ObjName)) > 0)
  379. {
  380. SetDataMember(id, MemberId, data);
  381. UnLock(id, nLockId);
  382. Result = nLockId;
  383. return Result;
  384. }
  385. else 
  386. {
  387. if (nLockId < 0){};
  388. Result = nLockId;
  389. return nLockId;
  390. }
  391. }
  392. BOOL KSynDataSet::AddData(KSynData * pData, long id)
  393. {
  394. if(m_Sets[id])
  395. {
  396. if (m_Sets[id]->m_bExist == FALSE)
  397. {
  398. m_Sets[id] = pData;
  399. m_Sets[id]->m_bExist = TRUE;
  400. return TRUE;
  401. }
  402. else 
  403. return FALSE;
  404. }
  405. else 
  406. {
  407. m_Sets[id] = pData;
  408. m_Sets[id]->m_bExist = TRUE;
  409. if (id > m_nTopIndex) m_nTopIndex = id;
  410. return TRUE;
  411. }
  412. }
  413. DataSeq * KSynDataSet::SaveData()
  414. {
  415. long Len = 0;
  416. for(long j = 0; j< m_nTopIndex ;j++)
  417. {
  418. if(m_Sets[j]->m_bExist)
  419. Len ++;
  420. }
  421. DataSeq*  pdataSeq = new DataSeq;
  422. pdataSeq->length(Len);
  423. for(long i= 0;i<Len;i++)
  424. {
  425. if (m_Sets[i]->m_bExist)
  426. {
  427. CORBA::Any * pAny;
  428. pAny = GetData(i);
  429. (*pdataSeq)[i] = *pAny;
  430. }
  431. }
  432. return pdataSeq;
  433. }