123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- #include "stdafx.h"
- #include "modVer.h"
- #include "SpBase.h"
- #include "SpHelper.h"
- #include "SpTest.h"
- #include <map>
- class CDeviceSwitch : public CEntityBase
- {
- public:
- CDeviceSwitch() {}
-
- virtual ~CDeviceSwitch() {}
- virtual const char *GetEntityName() const { return "DeviceSwitch"; }
- const char* GetEntityVersion() const { return MODULE_VERSION_FULL; }
- virtual bool IsService()const{return true;}
- ON_ENTITYT_TEST()
- virtual void OnPreStart(CAutoArray<CSimpleStringA> strArgs,CSmartPointer<ITransactionContext> pTransactionContext)
- {
- GetMachineType();
- ErrorCodeEnum Error = LoadConfig();
- pTransactionContext->SendAnswer(Error);
- }
- virtual void OnPreClose(EntityCloseCauseEnum eCloseCause,CSmartPointer<ITransactionContext> pTransactionContext)
- {
- pTransactionContext->SendAnswer(Error_Succeed);
- }
-
- virtual CServerSessionBase *OnNewSession(const char* /*pszRemoteEntityName*/, const char * pszParam)
- {
- CSimpleStringA strClass;
- CSimpleStringA strFunction;
- LOG_TRACE("OnNewSession, %s", pszParam);
- ErrorCodeEnum Error = SpExtractClassFunctionName(pszParam, strClass, strFunction);
- if (Error == Error_Succeed) {
- DeviceMap::const_iterator it = m_configMap.find(strFunction+m_machineType);
- if (it != m_configMap.end()) {
- GetFunction()->RedirectSession(it->second);
- } else {
- LOG_TRACE("cannot find Function %s", (LPCTSTR)(strFunction+m_machineType));
- }
- } else {
- LOG_TRACE("Extract SpExtractClassFunctionName failed!");
- }
- return NULL;
- }
- virtual void OnBroadcastSubscribe(CUUID SubID, const char * /*pszFromEntityName*/, const char * pszParam)
- {
- LOG_TRACE("OnBroadcastSubscribe %s", pszParam);
- CSimpleStringA strParam(pszParam);
- strParam += m_machineType;
- Dbg("strParam %s",(LPCTSTR)strParam);
- if (pszParam) {
- // pszParam is Function
- DeviceMap::const_iterator it = m_configMap.find(strParam);
- if (it != m_configMap.end()) {
- Dbg("redirect broadcast to %s",(LPCTSTR)it->second);
- GetFunction()->RedirectSubscribBroadcast(SubID, it->second);
- }
- else
- {
- Dbg("can't find.");
- }
- }
- }
- private:
- ErrorCodeEnum LoadConfig()
- {
- CSmartPointer<IConfigInfo> spConfig;
- ErrorCodeEnum Error = GetFunction()->OpenConfig(Config_Software, spConfig);
- if (Error == Error_Succeed) {
- const char *STR_BINDING = "Binding";
- CAutoArray<CSimpleStringA> Keys;
- Error = spConfig->ReadAllKeys(STR_BINDING, Keys);
- if (Error == Error_Succeed) {
- for (int i = 0; i < Keys.GetCount(); ++i) {
- CSimpleStringA Value;
- Error = spConfig->ReadConfigValue(STR_BINDING, Keys[i], Value);
- if (Error == Error_Succeed) {
- if (ExistEntity(Value)) {
- if (m_configMap.find(Keys[i]) == m_configMap.end()) {
- Dbg("add key = %s, value = %s", (LPCSTR)Keys[i], (LPCSTR)Value);
- m_configMap[Keys[i]] = Value;
- } else {
- LOG_TRACE("duplication! %s", (LPCSTR)Keys[i]);
- break;
- }
- } else {
- LOG_TRACE("%s=%s, entity does not exist!", (LPCSTR)Keys[i], (LPCSTR)Value);
- break;
- }
- } else {
- LOG_TRACE("read value failed! key = %s", Keys[i].GetData());
- break;
- }
- }
- } else {
- LOG_TRACE("read all keys failed!");
- }
- } else {
- LOG_TRACE("open config failed!");
- }
- return Error;
- }
- BOOL ExistEntity(const char *pszName)
- {
- CEntityStaticInfo Info;
- ErrorCodeEnum Error = GetFunction()->GetEntityStaticInfo(pszName, Info);
- return Error == Error_Succeed;
- }
- void GetMachineType()
- {
- ErrorCodeEnum eErr;
- CSystemStaticInfo sysInfo;
- eErr = GetFunction()->GetSystemStaticInfo(sysInfo);
- m_machineType = "";
- if (eErr != Error_Succeed)
- {
- Dbg("GetSystemStaticInfo failed(%d). To set machine type default value.",eErr);
- m_machineType = "Stand2S";
- }
- Dbg("machinetype[%s]",(LPCTSTR)sysInfo.strMachineType);
- m_machineType = m_machineType + "." + sysInfo.strMachineType;
- }
- private:
- typedef std::map<CSimpleStringA, CSimpleStringA> DeviceMap;
- DeviceMap m_configMap; // Function -> Detail EntityName
- CSimpleStringA m_machineType;
- };
- SP_BEGIN_ENTITY_MAP()
- SP_ENTITY(CDeviceSwitch)
- SP_END_ENTITY_MAP()
|