mod_helloclient.cpp 2.1 KB

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