mod_deviceswitch.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #include "stdafx.h"
  2. #include "modVer.h"
  3. #include "SpBase.h"
  4. #include "SpHelper.h"
  5. #include "SpTest.h"
  6. #include <map>
  7. class CDeviceSwitch : public CEntityBase
  8. {
  9. public:
  10. CDeviceSwitch() {}
  11. virtual ~CDeviceSwitch() {}
  12. virtual const char *GetEntityName() const { return "DeviceSwitch"; }
  13. const char* GetEntityVersion() const { return MODULE_VERSION_FULL; }
  14. virtual bool IsService()const{return true;}
  15. ON_ENTITYT_TEST()
  16. virtual void OnPreStart(CAutoArray<CSimpleStringA> strArgs,CSmartPointer<ITransactionContext> pTransactionContext)
  17. {
  18. GetMachineType();
  19. ErrorCodeEnum Error = LoadConfig();
  20. pTransactionContext->SendAnswer(Error);
  21. }
  22. virtual void OnPreClose(EntityCloseCauseEnum eCloseCause,CSmartPointer<ITransactionContext> pTransactionContext)
  23. {
  24. pTransactionContext->SendAnswer(Error_Succeed);
  25. }
  26. virtual CServerSessionBase *OnNewSession(const char* /*pszRemoteEntityName*/, const char * pszParam)
  27. {
  28. CSimpleStringA strClass;
  29. CSimpleStringA strFunction;
  30. LOG_TRACE("OnNewSession, %s", pszParam);
  31. ErrorCodeEnum Error = SpExtractClassFunctionName(pszParam, strClass, strFunction);
  32. if (Error == Error_Succeed) {
  33. DeviceMap::const_iterator it = m_configMap.find(strFunction+m_machineType);
  34. if (it != m_configMap.end()) {
  35. GetFunction()->RedirectSession(it->second);
  36. } else {
  37. LOG_TRACE("cannot find Function %s", (LPCTSTR)(strFunction+m_machineType));
  38. }
  39. } else {
  40. LOG_TRACE("Extract SpExtractClassFunctionName failed!");
  41. }
  42. return NULL;
  43. }
  44. virtual void OnBroadcastSubscribe(CUUID SubID, const char * /*pszFromEntityName*/, const char * pszParam)
  45. {
  46. LOG_TRACE("OnBroadcastSubscribe %s", pszParam);
  47. CSimpleStringA strParam(pszParam);
  48. strParam += m_machineType;
  49. Dbg("strParam %s",(LPCTSTR)strParam);
  50. if (pszParam) {
  51. // pszParam is Function
  52. DeviceMap::const_iterator it = m_configMap.find(strParam);
  53. if (it != m_configMap.end()) {
  54. Dbg("redirect broadcast to %s",(LPCTSTR)it->second);
  55. GetFunction()->RedirectSubscribBroadcast(SubID, it->second);
  56. }
  57. else
  58. {
  59. Dbg("can't find.");
  60. }
  61. }
  62. }
  63. private:
  64. ErrorCodeEnum LoadConfig()
  65. {
  66. CSmartPointer<IConfigInfo> spConfig;
  67. ErrorCodeEnum Error = GetFunction()->OpenConfig(Config_Software, spConfig);
  68. if (Error == Error_Succeed) {
  69. const char *STR_BINDING = "Binding";
  70. CAutoArray<CSimpleStringA> Keys;
  71. Error = spConfig->ReadAllKeys(STR_BINDING, Keys);
  72. if (Error == Error_Succeed) {
  73. for (int i = 0; i < Keys.GetCount(); ++i) {
  74. CSimpleStringA Value;
  75. Error = spConfig->ReadConfigValue(STR_BINDING, Keys[i], Value);
  76. if (Error == Error_Succeed) {
  77. if (ExistEntity(Value)) {
  78. if (m_configMap.find(Keys[i]) == m_configMap.end()) {
  79. Dbg("add key = %s, value = %s", (LPCSTR)Keys[i], (LPCSTR)Value);
  80. m_configMap[Keys[i]] = Value;
  81. } else {
  82. LOG_TRACE("duplication! %s", (LPCSTR)Keys[i]);
  83. break;
  84. }
  85. } else {
  86. LOG_TRACE("%s=%s, entity does not exist!", (LPCSTR)Keys[i], (LPCSTR)Value);
  87. break;
  88. }
  89. } else {
  90. LOG_TRACE("read value failed! key = %s", Keys[i].GetData());
  91. break;
  92. }
  93. }
  94. } else {
  95. LOG_TRACE("read all keys failed!");
  96. }
  97. } else {
  98. LOG_TRACE("open config failed!");
  99. }
  100. return Error;
  101. }
  102. BOOL ExistEntity(const char *pszName)
  103. {
  104. CEntityStaticInfo Info;
  105. ErrorCodeEnum Error = GetFunction()->GetEntityStaticInfo(pszName, Info);
  106. return Error == Error_Succeed;
  107. }
  108. void GetMachineType()
  109. {
  110. ErrorCodeEnum eErr;
  111. CSystemStaticInfo sysInfo;
  112. eErr = GetFunction()->GetSystemStaticInfo(sysInfo);
  113. m_machineType = "";
  114. if (eErr != Error_Succeed)
  115. {
  116. Dbg("GetSystemStaticInfo failed(%d). To set machine type default value.",eErr);
  117. m_machineType = "Stand2S";
  118. }
  119. Dbg("machinetype[%s]",(LPCTSTR)sysInfo.strMachineType);
  120. m_machineType = m_machineType + "." + sysInfo.strMachineType;
  121. }
  122. private:
  123. typedef std::map<CSimpleStringA, CSimpleStringA> DeviceMap;
  124. DeviceMap m_configMap; // Function -> Detail EntityName
  125. CSimpleStringA m_machineType;
  126. };
  127. SP_BEGIN_ENTITY_MAP()
  128. SP_ENTITY(CDeviceSwitch)
  129. SP_END_ENTITY_MAP()