mod_SampleEntity.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. #ifndef __MOD_SAMPLEENTITY_H__
  2. #define __MOD_SAMPLEENTITY_H__
  3. #pragma once
  4. // This code is generated by spgenex tool!
  5. #include "SampleEntity_server_g.h"
  6. #include "SampleFSM.h"
  7. #include "SpTest.h"
  8. #include "modVer.h"
  9. using namespace SampleEntity;
  10. class SampleService_Session : public SampleService_ServerSessionBase
  11. {
  12. virtual void Handle_OnewayFuncOverlap(SpOnewayCallContext<SampleService_OnewayFuncOverlap_Info>::Pointer ctx)
  13. {
  14. LOG_FUNCTION();
  15. /// override by user
  16. }
  17. virtual void Handle_OnewayFuncNoOverlap(SpOnewayCallContext<SampleService_OnewayFuncNoOverlap_Info>::Pointer ctx)
  18. {
  19. LOG_FUNCTION();
  20. /// override by user
  21. }
  22. virtual void Handle_OnewayFuncNormal(SpOnewayCallContext<SampleService_OnewayFuncNormal_Info>::Pointer ctx)
  23. {
  24. LOG_FUNCTION();
  25. /// override by user
  26. }
  27. virtual void Handle_TwoWayFuncOverlap(SpReqAnsContext<SampleService_TwoWayFuncOverlap_Req, SampleService_TwoWayFuncOverlap_Ans>::Pointer ctx)
  28. {
  29. LOG_FUNCTION();
  30. /// override by user
  31. ctx->Answer();
  32. }
  33. virtual void Handle_TwoWayFuncNotOverlap(SpReqAnsContext<SampleService_TwoWayFuncNotOverlap_Req, SampleService_TwoWayFuncNotOverlap_Ans>::Pointer ctx)
  34. {
  35. LOG_FUNCTION();
  36. /// override by user
  37. ctx->Answer();
  38. }
  39. virtual void Handle_TwoWayFuncWithID(SpReqAnsContext<SampleService_TwoWayFuncWithID_Req, SampleService_TwoWayFuncWithID_Ans>::Pointer ctx)
  40. {
  41. LOG_FUNCTION();
  42. /// override by user
  43. ctx->Answer();
  44. }
  45. virtual void Handle_TwoWayFuncNormal(SpReqAnsContext<SampleService_TwoWayFuncNormal_Req, SampleService_TwoWayFuncNormal_Ans>::Pointer ctx)
  46. {
  47. LOG_FUNCTION();
  48. /// override by user
  49. ctx->Answer();
  50. }
  51. virtual void Handle_SubscribeOverlap(SpSubscribeContext<SampleService_SubscribeOverlap_Sub, SampleService_SubscribeOverlap_Message_Info>::Pointer ctx)
  52. {
  53. LOG_FUNCTION();
  54. /// override by user
  55. }
  56. virtual void Handle_SubscribeOverlap_Cancel(SpOnewayCallContext<SampleService_SubscribeOverlap_Cancel_Info>::Pointer ctx)
  57. {
  58. LOG_FUNCTION();
  59. /// override by user
  60. }
  61. };
  62. class CSampleEntity : public CEntityBase, public ITestCaseSuite<CSampleEntity>, public ITimerListener
  63. {
  64. public:
  65. CSampleEntity(){}
  66. virtual ~CSampleEntity(){}
  67. virtual const char* GetEntityVersion() const {
  68. return MODULE_VERSION_FULL;
  69. }
  70. virtual const char *GetEntityName() const { return "SampleEntity"; }
  71. virtual bool IsService() const { return true; }
  72. virtual bool IsMultiThread() const { return true; }
  73. virtual CServerSessionBase* OnNewSession(const char* pszRemoteEntityName, const char* pszClass)
  74. {
  75. LOG_FUNCTION();
  76. return new SampleService_Session();
  77. }
  78. //void OnSelfTest(EntityTestEnum eTestType, CSmartPointer<ITransactionContext> pTransactionContext)
  79. //{
  80. // m_sampleFsm.SelfTest(eTestType, pTransactionContext);
  81. //}
  82. virtual void OnPreStart(CAutoArray<CSimpleStringA> strArgs,CSmartPointer<ITransactionContext> pTransactionContext)
  83. {
  84. LOG_FUNCTION();
  85. ErrorCodeEnum ec = Error_Succeed;
  86. if(m_sampleFsm.Init(this) != 0) {
  87. ec = Error_InvalidState;
  88. }
  89. AddTransMethod(&CSampleEntity::TestTwoWayFuncNormal);
  90. pTransactionContext->SendAnswer(ec);
  91. }
  92. virtual void OnStarted() override
  93. {
  94. CSimpleStringA strValue;
  95. CHECK(Error_NotExist == GetFunction()->GetSysVar("NoExistSysValue", strValue));
  96. IFFAILBREAK(GetFunction()->GetSysVar("SampleState", strValue));
  97. CHECK_FALSE(strValue.IsNullOrEmpty());
  98. /*set sys val*/
  99. Dbg("Update sysVal...");
  100. strValue = strValue.Compare("1") == 0 ? "0" : "1";
  101. IFFAILBREAK(GetFunction()->SetSysVar("SampleState", strValue, true));
  102. CSimpleStringA strNewValue;
  103. IFFAILBREAK(GetFunction()->GetSysVar("SampleState", strNewValue));
  104. CHECK(strValue.Compare(strNewValue) == 0);
  105. Dbg("Update sysVal again...");
  106. strValue = strValue.Compare("1") == 0 ? "0" : "1";
  107. IFFAILBREAK(GetFunction()->SetSysVar("SampleState", strValue));
  108. IFFAILBREAK(GetFunction()->GetSysVar("SampleState", strNewValue));
  109. CHECK(strValue.Compare(strNewValue) == 0);
  110. //depecrate: assert would happen!
  111. //IFFAILBREAK(GetFunction()->SetSysVar("SampleState", NULL)); //would clear the sys val value.
  112. Dbg("test some illegal action.");
  113. CHECK(Error_NotExist == GetFunction()->SetSysVar("NoExistSysValue", "1", true));
  114. CHECK(Error_NotExist == GetFunction()->SetSysVar("NoExistSysValue", "1"));
  115. CHECK(Error_Null == GetFunction()->SetSysVar(NULL, "1", true));
  116. CHECK(Error_Null == GetFunction()->SetSysVar(NULL, "1"));
  117. CHECK(Error_NoPrivilege == GetFunction()->SetSysVar("PrivilegeState", "1", true));
  118. CHECK(Error_NoPrivilege == GetFunction()->SetSysVar("PrivilegeState", "1"));
  119. IFFAILBREAK(GetFunction()->SetTimer(1, this, 3 * 1000));
  120. TestCaseDemo();
  121. }
  122. void TestCaseDemo()
  123. {
  124. /** simple test way, user declare and implement the test method*/
  125. AddTransMethod(&CSampleEntity::TestTwoWayFuncWithID);
  126. /** this is meanless usage way beacause we cannot set req member*/
  127. AddMethodTestCase(
  128. new TwoWayMethodTestCaseT<SampleService_TwoWayFuncNotOverlap_Req, SampleService_TwoWayFuncNotOverlap_Ans>(
  129. this,
  130. &CSampleEntity::TwoWayFuncNotOverlap));
  131. /** but we can change it like this.*/
  132. auto methdTest1 = new TwoWayMethodTestCaseT<SampleService_TwoWayFuncNotOverlap_Req, SampleService_TwoWayFuncNotOverlap_Ans>(
  133. this,
  134. &CSampleEntity::TwoWayFuncNotOverlap);
  135. methdTest1->Req.req_context = "I'am Test";
  136. AddMethodTestCase(methdTest1);
  137. /** but still, we cannot check the Ans member's validity, only one thing what we can do is
  138. just checking the aim furncion returned 'ErrorCodeEnum' type value*/
  139. /** last but not lease, we can inherit TwoWayMethodTestCaseT and override PreTest(to set Req) and PostTest(to check Ans)*/
  140. struct TwoWayFuncOverlapTestCase :
  141. public TwoWayMethodTestCaseT<SampleService_TwoWayFuncOverlap_Req, SampleService_TwoWayFuncOverlap_Ans>
  142. {
  143. //TwoWayFuncOverlapTestCase(CSampleEntity* ent, TestTwoWayFunc testFunc) :TwoWayMethodTestCaseT(ent, testFunc) {}
  144. using TwoWayMethodTestCaseT::TwoWayMethodTestCaseT;
  145. void PreTest() override
  146. {
  147. LOG_FUNCTION();
  148. Req.req_context = "hello";
  149. }
  150. ErrorCodeEnum PostTest() override
  151. {
  152. LOG_FUNCTION();
  153. if (Ans.ans_context.Compare("world", true) != 0) {
  154. LOG_TRACE("Check failed, return except Error_Succeed");
  155. return Error_Unexpect;
  156. }
  157. return Error_Succeed;
  158. }
  159. };
  160. AddMethodTestCase(new TwoWayFuncOverlapTestCase(this, &CSampleEntity::TwoWayFuncOverlap));
  161. }
  162. #if 0
  163. TESTCASE_OVERRIDE_ON_EXAM_AND_IMPLEMENT()
  164. #else
  165. void OnExam(CSmartPointer<ITransactionContext> pTransactionContext) override
  166. {
  167. LOG_FUNCTION();
  168. TestConfig defaultConfig;
  169. std::vector<TestCase> const& allTestCases = GetRegistryHub().GetTestCaseRegistry().getAllTests(defaultConfig);
  170. ErrorCodeEnum result = (allTestCases.size() > 0) ? Error_Succeed : Error_IgnoreAll;
  171. for (std::vector<TestCase>::const_iterator itStart = allTestCases.begin(), itEnd = allTestCases.end(); itStart != itEnd; ++itStart) {
  172. TestCaseInfo const& testCaseInfo = itStart->GetTestInfo();
  173. ErrorCodeEnum testResult = itStart->RunTest();
  174. if (testResult != Error_Succeed) {
  175. ERR("TestCase<%s %s> %s failed the test, return %s.",
  176. testCaseInfo.strName.c_str(),
  177. testCaseInfo.strDescription.empty() ? "(No description)": testCaseInfo.strDescription.c_str(),
  178. testCaseInfo.lineInfo.ToString().c_str() , SpStrError(testResult));
  179. if (result != Error_Failed)
  180. result = Error_Failed;
  181. }
  182. }
  183. pTransactionContext->SendAnswer(result);
  184. }
  185. #endif
  186. ErrorCodeEnum AdditionalTest() override
  187. {
  188. LOG_FUNCTION();
  189. /** User add any other test logical*/
  190. return Error_Succeed;
  191. }
  192. ErrorCodeEnum OnewayFuncOverlap(
  193. SpOnewayCallContext<SampleService_OnewayFuncOverlap_Info>::Pointer ctx)
  194. {
  195. ErrorCodeEnum ec = Error_Succeed;
  196. // TODO: user should implement the function!
  197. REQUIRE(ctx->Info.req_context.Compare("SampleService_OnewayFuncOverlap_Info::xvmx.cvmsifjwoejklfjlkdfsdjfoiwjeiofjlsfsdgjlfdg") == 0);
  198. return ec;
  199. }
  200. ErrorCodeEnum OnewayFuncNoOverlap(
  201. SpOnewayCallContext<SampleService_OnewayFuncNoOverlap_Info>::Pointer ctx)
  202. {
  203. ErrorCodeEnum ec = Error_Unexpect;
  204. // TODO: user should implement the function!
  205. return ec;
  206. }
  207. ErrorCodeEnum OnewayFuncNormal(
  208. SpOnewayCallContext<SampleService_OnewayFuncNormal_Info>::Pointer ctx)
  209. {
  210. ErrorCodeEnum ec = Error_Unexpect;
  211. // TODO: user should implement the function!
  212. REQUIRE(ctx->Info.tstring.Compare("SampleService_OnewayFuncNormal_Info::tstring::xvmx.cvmsifjwoejklfjlkdfsdjfoiwjeiofjlsfsdgjlfdg") == 0);
  213. REQUIRE(ctx->Info.twstring.Compare(L"SampleService_OnewayFuncNormal_Info::twstring::xvmx.cvmsifjwoejklfjlkdfsdjfoiwjeiofjlsfsdgjlfdg") == 0);
  214. return ec;
  215. }
  216. ErrorCodeEnum TwoWayFuncOverlap(
  217. SpReqAnsContext<SampleService_TwoWayFuncOverlap_Req, SampleService_TwoWayFuncOverlap_Ans>::Pointer ctx)
  218. {
  219. LOG_FUNCTION();
  220. ErrorCodeEnum ec = Error_Succeed;
  221. LOG_ASSERT(ctx != NULL);
  222. if (ctx->Req.req_context.IsNullOrEmpty() || ctx->Req.req_context.Compare("hello") != 0) {
  223. return Error_NotIntegrated;
  224. }
  225. // TODO:
  226. ctx->Ans.ans_context = "World";
  227. ctx->Answer();
  228. return ec;
  229. }
  230. ErrorCodeEnum TwoWayFuncNotOverlap(
  231. SpReqAnsContext<SampleService_TwoWayFuncNotOverlap_Req, SampleService_TwoWayFuncNotOverlap_Ans>::Pointer ctx)
  232. {
  233. LOG_FUNCTION();
  234. ErrorCodeEnum ec = Error_Succeed;
  235. ctx->Answer(ec);
  236. return ec;
  237. }
  238. ErrorCodeEnum TwoWayFuncWithID(
  239. SpReqAnsContext<SampleService_TwoWayFuncWithID_Req, SampleService_TwoWayFuncWithID_Ans>::Pointer ctx)
  240. {
  241. LOG_FUNCTION();
  242. ErrorCodeEnum ec = Error_Succeed;
  243. // TODO:
  244. //ctx->Answer(Error_Unexpect, Error_Unexpect);
  245. return ec;
  246. }
  247. ErrorCodeEnum __CheckTwoWayFuncNormal(
  248. SpReqAnsContext<SampleService_TwoWayFuncNormal_Req, SampleService_TwoWayFuncNormal_Ans>::Pointer ctx)
  249. {
  250. REQUIRE(ctx->Req.tbool);
  251. REQUIRE(ctx->Req.tchar == 'I');
  252. REQUIRE(ctx->Req.tint == 123);
  253. REQUIRE(ctx->Req.tuint == 123);
  254. REQUIRE(ctx->Req.tshort == 123);
  255. REQUIRE(ctx->Req.tushort == 123);
  256. REQUIRE(ctx->Req.tuchar == 'I');
  257. REQUIRE(ctx->Req.tstring.Compare("SampleService_TwoWayFuncNormal_Req::tstring::xclvmsdiofjweoij3r283908u238925j4ioijn;klgnfakjsfsfhffjeoiwjhoi") == 0);
  258. REQUIRE(ctx->Req.twstring.Compare("SampleService_TwoWayFuncNormal_Req::twstring::xclvmsdiofjweoij3r283908u238925j4ioijn;klgnfakjsfsfhffjeoiwjhoi") == 0);
  259. REQUIRE(ctx->Req.tfloat == 123.456f);
  260. REQUIRE(ctx->Req.tdouble == 123.456);
  261. REQUIRE(ctx->Req.tblob.m_iLength == 0);
  262. REQUIRE(ctx->Req.tint64 == 123);
  263. REQUIRE(ctx->Req.tuint64 == 123);
  264. ctx->Ans.sarray_string.Init(3);
  265. ctx->Ans.sarray_string[0] = "String1";
  266. ctx->Ans.sarray_string[1] = "String2";
  267. ctx->Ans.sarray_string[2] = "String3";
  268. return Error_Succeed;
  269. }
  270. ErrorCodeEnum TwoWayFuncNormal(
  271. SpReqAnsContext<SampleService_TwoWayFuncNormal_Req, SampleService_TwoWayFuncNormal_Ans>::Pointer ctx)
  272. {
  273. LOG_FUNCTION();
  274. ErrorCodeEnum ec = Error_Succeed;
  275. ctx->Answer(__CheckTwoWayFuncNormal(ctx));
  276. return ec;
  277. }
  278. ErrorCodeEnum SubscribeOverlap(
  279. SpSubscribeContext<SampleService_SubscribeOverlap_Sub, SampleService_SubscribeOverlap_Message_Info>::Pointer ctx)
  280. {
  281. ErrorCodeEnum ec = Error_NotImpl;
  282. // TODO:
  283. return ec;
  284. }
  285. ErrorCodeEnum SubscribeOverlap_Cancel(
  286. SpOnewayCallContext<SampleService_SubscribeOverlap_Cancel_Info>::Pointer ctx)
  287. {
  288. ErrorCodeEnum ec = Error_NotImpl;
  289. // TODO:
  290. return ec;
  291. }
  292. void OnTimeout(DWORD dwTimerID)
  293. {
  294. static int state = -2;
  295. const int curState = m_sampleFsm.SimulateJump();
  296. CHECK(state != curState);
  297. state = curState;
  298. }
  299. ///////////////////////////
  300. void TestTwoWayFuncNormal(CSmartPointer<ITransactionContext> pTransactionContext)
  301. {
  302. SpReqAnsContext<SampleService_TwoWayFuncNotOverlap_Req, SampleService_TwoWayFuncNotOverlap_Ans>::Pointer ctx =
  303. new SpReqAnsContext<SampleService_TwoWayFuncNotOverlap_Req, SampleService_TwoWayFuncNotOverlap_Ans>(pTransactionContext);
  304. /** Set ctx->Req*/
  305. /*The aim function you would like test.*/
  306. if (Error_Succeed == TwoWayFuncNotOverlap(ctx))
  307. {
  308. /** Check ctx->Ans*/
  309. /** Overwite the Result using pTransactionContext->SendAnswer()*/
  310. }
  311. }
  312. void TestTwoWayFuncWithID(CSmartPointer<ITransactionContext> pTransactionContext)
  313. {
  314. SpReqAnsContext< SampleService_TwoWayFuncWithID_Req, SampleService_TwoWayFuncWithID_Ans>::Pointer ctx =
  315. new SpReqAnsContext< SampleService_TwoWayFuncWithID_Req, SampleService_TwoWayFuncWithID_Ans>(pTransactionContext);
  316. TwoWayFuncWithID(ctx);
  317. }
  318. //TESTCASE_DECLARE_BEGIN(SampleService, TwoWayFuncOverlap);
  319. //ctx->Req.req_context = "hello";
  320. //TESTCASE_DECLARE_INVOKE(TwoWayFuncOverlap);
  321. //TESTCASE_DECLARE_END(TwoWayFuncOverlap);
  322. private:
  323. CSampleFSM m_sampleFsm;
  324. };
  325. class CSampleServiceSession : public SampleService_ServerSessionBase
  326. {
  327. public:
  328. CSampleServiceSession(CSampleEntity *pEntity) : m_pEntity(pEntity) {}
  329. virtual ~CSampleServiceSession() {}
  330. virtual void Handle_OnewayFuncOverlap(
  331. SpOnewayCallContext<SampleService_OnewayFuncOverlap_Info>::Pointer ctx)
  332. {
  333. auto rc = m_pEntity->OnewayFuncOverlap(ctx);
  334. }
  335. virtual void Handle_OnewayFuncNoOverlap(
  336. SpOnewayCallContext<SampleService_OnewayFuncNoOverlap_Info>::Pointer ctx)
  337. {
  338. auto rc = m_pEntity->OnewayFuncNoOverlap(ctx);
  339. }
  340. virtual void Handle_OnewayFuncNormal(
  341. SpOnewayCallContext<SampleService_OnewayFuncNormal_Info>::Pointer ctx)
  342. {
  343. auto rc = m_pEntity->OnewayFuncNormal(ctx);
  344. }
  345. virtual void Handle_TwoWayFuncOverlap(
  346. SpReqAnsContext<SampleService_TwoWayFuncOverlap_Req, SampleService_TwoWayFuncOverlap_Ans>::Pointer ctx)
  347. {
  348. auto rc = m_pEntity->TwoWayFuncOverlap(ctx);
  349. ctx->Answer(rc);
  350. }
  351. virtual void Handle_TwoWayFuncNotOverlap(
  352. SpReqAnsContext<SampleService_TwoWayFuncNotOverlap_Req, SampleService_TwoWayFuncNotOverlap_Ans>::Pointer ctx)
  353. {
  354. auto rc = m_pEntity->TwoWayFuncNotOverlap(ctx);
  355. ctx->Answer(rc);
  356. }
  357. virtual void Handle_TwoWayFuncWithID(
  358. SpReqAnsContext<SampleService_TwoWayFuncWithID_Req, SampleService_TwoWayFuncWithID_Ans>::Pointer ctx)
  359. {
  360. auto rc = m_pEntity->TwoWayFuncWithID(ctx);
  361. ctx->Answer(rc);
  362. }
  363. virtual void Handle_TwoWayFuncNormal(
  364. SpReqAnsContext<SampleService_TwoWayFuncNormal_Req, SampleService_TwoWayFuncNormal_Ans>::Pointer ctx)
  365. {
  366. auto rc = m_pEntity->TwoWayFuncNormal(ctx);
  367. ctx->Answer(rc);
  368. }
  369. virtual void Handle_SubscribeOverlap(
  370. SpSubscribeContext<SampleService_SubscribeOverlap_Sub, SampleService_SubscribeOverlap_Message_Info>::Pointer ctx)
  371. {
  372. auto rc = m_pEntity->SubscribeOverlap(ctx);
  373. }
  374. virtual void Handle_SubscribeOverlap_Cancel(
  375. SpOnewayCallContext<SampleService_SubscribeOverlap_Cancel_Info>::Pointer ctx)
  376. {
  377. auto rc = m_pEntity->SubscribeOverlap_Cancel(ctx);
  378. }
  379. private:
  380. CSampleEntity *m_pEntity;
  381. };
  382. ///////////////////////////
  383. #endif // __MOD_SAMPLEENTITY_H__