mod_helloclient.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #include "stdafx.h"
  2. #include "SpBase.h"
  3. #include "HelloService_client_g.h"
  4. using namespace HelloService;
  5. class HelloClientEntity : public CEntityBase, public ITimerListener, public ISysVarListener
  6. {
  7. public:
  8. HelloClientEntity() : m_pClient(NULL) {}
  9. virtual ~HelloClientEntity() {}
  10. virtual const char *GetEntityName() const { return "HelloClient"; }
  11. virtual void OnTimeout(DWORD dwTimerID)
  12. {
  13. LOG_FUNCTION();
  14. m_pClient->Ping();
  15. }
  16. virtual void OnSysVarEvent(const char *pszKey, const char *pszValue,const char *pszOldValue,const char *pszEntityName)
  17. {
  18. LOG_TRACE("entity:%s key:%s value:%s", pszEntityName, pszKey, pszValue);
  19. }
  20. virtual void OnStarted()
  21. {
  22. LOG_FUNCTION();
  23. //MessageBox(0, 0, 0, 0);
  24. CSmartPointer<IEntityFunction> pFunc = GetFunction();
  25. //pFunc->SetTimer(1, this, 1000);
  26. //pFunc->RegistSysVarEvent("HelloState", this);
  27. #if 1
  28. HelloService_ClientBase *client = new HelloService_ClientBase(this);
  29. Dbg("before connect");
  30. ErrorCodeEnum Error = client->Connect();
  31. Dbg("after connect");
  32. if (Error == Error_Succeed) {
  33. LOG_TRACE("connect ok!");
  34. // invoke Ping method
  35. Dbg("before ping");
  36. Error = client->Ping();
  37. LOG_ASSERT(Error == Error_Succeed);
  38. Dbg("after ping");
  39. // invoke Hello method
  40. HelloService_Hello_Req Hello_Req; // request msg
  41. HelloService_Hello_Ans Hello_Ans; // answer msg,
  42. Hello_Req.txt = "Hello, this message comes from HelloClient entity!";
  43. Dbg("before hello");
  44. Error = client->Hello(Hello_Req, Hello_Ans, -1);
  45. Dbg("after hello");
  46. //LOG_ASSERT(Error == Error_Succeed);
  47. //LOG_TRACE((LPCSTR)Hello_Ans.txt);
  48. client->GetFunction()->CloseSession();
  49. }
  50. client->SafeDelete();
  51. #endif
  52. m_pClient = new HelloService_ClientBase(this);
  53. m_pClient->Connect();
  54. }
  55. virtual void OnPreClose(EntityCloseCauseEnum eCloseCause,CSmartPointer<ITransactionContext> pTransactionContext)
  56. {
  57. LOG_FUNCTION();
  58. pTransactionContext->SendAnswer(Error_Succeed);
  59. }
  60. private:
  61. HelloService_ClientBase *m_pClient;
  62. };
  63. SP_BEGIN_ENTITY_MAP()
  64. SP_ENTITY(HelloClientEntity)
  65. SP_END_ENTITY_MAP()