mod_helloservice.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #include "stdafx.h"
  2. #include "SpBase.h"
  3. #include "HelloService_server_g.h"
  4. #include "HelloService_msg_g.h"
  5. using namespace HelloService;
  6. class HelloService_Session : public HelloService_ServerSessionBase
  7. {
  8. public:
  9. virtual void Handle_Ping(SpOnewayCallContext<HelloService_Ping_Info>::Pointer ctx)
  10. {
  11. LOG_FUNCTION();
  12. LOG_TRACE("handle ping msg !");
  13. }
  14. virtual void Handle_Hello(SpReqAnsContext<HelloService_Hello_Req, HelloService_Hello_Ans>::Pointer ctx)
  15. {
  16. LOG_FUNCTION();
  17. LOG_TRACE("handle hello msg %s!", (LPCTSTR)ctx->Req.txt);
  18. ctx->Ans.txt = "Hello, this message comes from Hello Service entity!";
  19. ctx->Answer();
  20. }
  21. };
  22. class HelloServiceEntity : public CEntityBase, public ITimerListener
  23. {
  24. public:
  25. HelloServiceEntity() {}
  26. virtual ~HelloServiceEntity() {}
  27. virtual const char *GetEntityName() const { return "HelloService"; }
  28. virtual void OnTimeout(DWORD dwTimerID)
  29. {
  30. Dbg("on timer id %d ", dwTimerID);
  31. #if 0
  32. CSmartPointer<IEntityFunction> pFunc = GetFunction();
  33. CSimpleStringA strValue;
  34. pFunc->GetSysVar("HelloState", strValue);
  35. LOG_TRACE("sys var = %s", (LPCSTR)strValue);
  36. CSimpleStringA strValue1 = CSimpleStringA::Format("%d", atoi((LPCSTR)strValue)+1);
  37. pFunc->SetSysVar("HelloState", strValue1);
  38. #endif
  39. #if 0
  40. TestEvent evt;
  41. evt.m1 = "from hello service, m1";
  42. evt.m2 = 1234;
  43. SpSendBroadcast(pFunc, SP_MSG_OF(TestEvent), SP_MSG_SIG_OF(TestEvent), evt);
  44. #endif
  45. LogEvent(Severity_Middle, 0x20300001, "hello service test");
  46. }
  47. void OnPreStart(CAutoArray<CSimpleStringA> strArgs, CSmartPointer<ITransactionContext> pTransactionContext)
  48. {
  49. LOG_FUNCTION();
  50. pTransactionContext->SendAnswer(Error_Succeed);
  51. }
  52. virtual void OnStarted()
  53. {
  54. LOG_FUNCTION();
  55. //CSmartPointer<IEntityFunction> pFunc = GetFunction();
  56. //pFunc->SetTimer(1, this, 2000);
  57. }
  58. virtual void OnPreClose(EntityCloseCauseEnum eCloseCause,CSmartPointer<ITransactionContext> pTransactionContext)
  59. {
  60. LOG_FUNCTION();
  61. pTransactionContext->SendAnswer(Error_Succeed);
  62. }
  63. virtual CServerSessionBase *OnNewSession(const char* pszRemoteEntityName, const char * pszClass)
  64. {
  65. LOG_FUNCTION();
  66. return new HelloService_Session();
  67. }
  68. virtual bool IsService()const{return true;}
  69. };
  70. SP_BEGIN_ENTITY_MAP()
  71. SP_ENTITY(HelloServiceEntity)
  72. SP_END_ENTITY_MAP()