mod_helloclient.cpp 2.2 KB

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