123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426 |
- #include "stdafx.h"
- #include "SpBase.h"
- #include "SpHelper.h"
- #include <map>
- // 交互上下文管理InteractionContext 0x309
- // 生命周期改成操作会话
- #include "InteractionContext_server_g.h"
- using namespace InteractionContext;
- #include "mod_assistantchannel/AssistantChannel_client_g.h"
- using namespace AssistantChannel;
- #include "mod_assistantchannel/chan_protocol.h"
- #define LOG_EVT_SELFCHECK_ASSISTANTCHANNEL_IDLE 0x50500001 //协助通道重启
- #define SYSVAR_CALLTYPE "CallType"
- #define CALLTYPE_NORMAL 'N' // 呼叫类型,普通模式
- #define CALLTYPE_MOBILE 'M' // 呼叫类型,手机模式
- class CInteractionContextEntity;
- class CtxSyncServerSession : public SyncService_ServerSessionBase
- {
- public:
- CtxSyncServerSession(CInteractionContextEntity* pEntity){m_pEntity = pEntity;}
- virtual void Handle_SetSessionData(SpOnewayCallContext<SyncService_SetSessionData_Info>::Pointer ctx);
- virtual void Handle_GetSessionData(SpReqAnsContext<SyncService_GetSessionData_Req, SyncService_GetSessionData_Ans>::Pointer ctx);
- virtual void Handle_SetMachineData(SpOnewayCallContext<SyncService_SetMachineData_Info>::Pointer ctx);
- virtual void Handle_GetMachineData(SpReqAnsContext<SyncService_GetMachineData_Req, SyncService_GetMachineData_Ans>::Pointer ctx);
- private:
- CInteractionContextEntity* m_pEntity;
- };
- class ChannelClient : public ChannelService_ClientBase
- {
- public:
- ChannelClient(CInteractionContextEntity* pEntity);
- virtual void OnMessage(ErrorCodeEnum Error, ChannelService_State_Info &Msg, CSmartPointer<IReleasable> pData);
- private:
- CInteractionContextEntity* m_pEntity;
- };
- using namespace InteractionContext;
- class CInteractionContextEntity : public CEntityBase,public ILogListener, public ISysVarListener
- {
- public:
- CInteractionContextEntity() : m_pChannelClient(NULL) {}
- virtual ~CInteractionContextEntity() {}
- virtual const char *GetEntityName() const { return "InteractionContext"; }
- virtual bool IsService()const{return true;}
- virtual void OnPreStart(CAutoArray<CSimpleStringA> strArgs,CSmartPointer<ITransactionContext> pTransactionContext)
- {
- LOG_FUNCTION();
- InitMachineData();
- ErrorCodeEnum Error = __OnStart(Error_Succeed);
- pTransactionContext->SendAnswer(Error);
- }
- ErrorCodeEnum __OnStart(ErrorCodeEnum preOperationError)
- {
- LOG_FUNCTION();
- CSmartPointer<IEntityFunction> pFunc = GetFunction();
- int i = 0;
- m_arrListener.Init(1);
- pFunc->SubscribeLog(m_arrListener[i++], this, Log_Event, Severity_None, Error_IgnoreAll, LOG_EVT_SELFCHECK_ASSISTANTCHANNEL_IDLE,NULL,false);
- m_pChannelClient = new ChannelClient(this);
- ErrorCodeEnum Error = m_pChannelClient->Connect();
- if (Error == Error_Succeed) {
- ChannelService_BeginState_Sub Sub;
- Error = m_pChannelClient->BeginState(Sub);
- if (Error != Error_Succeed) {
- m_pChannelClient->GetFunction()->CloseSession();
- m_pChannelClient = NULL;
- }
- } else {
- m_pChannelClient->SafeDelete();
- m_pChannelClient = NULL;
- }
- {
- CSmartPointer<IEntityFunction> Func = GetFunction();
- CSimpleStringA strValue;
- Func->RegistSysVarEvent(SYSVAR_CALLTYPE, this);
- Func->GetSysVar(SYSVAR_CALLTYPE, strValue);
- if (strValue[0] == CALLTYPE_NORMAL)
- {
- m_nSysCallType = 0;
- }
- else if (strValue[0] == CALLTYPE_MOBILE)
- {
- m_nSysCallType = 1;
- }
- else
- {
- assert(0);
- }
- }
- return Error;
- }
- virtual void OnLog(const CAutoArray<CUUID> &SubIDs, const CUUID nLogID,const LogTypeEnum eLogType, const SeverityLevelEnum eLevel,
- const DWORD dwSysError,const DWORD dwUserCode,const DWORD dwEntityInstanceID, const WORD wEntityDevelID,
- const CAutoArray<DWORD> &Param, const char *pszEntityName, const char *pszModuleName,const char *pszMessage)
- {
- if (dwUserCode == LOG_EVT_SELFCHECK_ASSISTANTCHANNEL_IDLE)
- {
- Dbg("recv LOG_EVT_SELFCHECK_ASSISTANTCHANNEL_IDLE");
- if (m_pChannelClient!=NULL)
- {
- m_pChannelClient->GetFunction()->CloseSession();
- m_pChannelClient = NULL;
- Dbg("Close AssistChannel Session ");
- }
- if (m_pChannelClient == NULL)
- {
- Dbg("ReConnection AssistChannel Session");
- m_pChannelClient = new ChannelClient(this);
- ErrorCodeEnum Error = m_pChannelClient->Connect();
- if (Error != Error_Succeed)
- {
- m_pChannelClient->SafeDelete();
- m_pChannelClient = NULL;
- Dbg("AssistChannelClient connect fail!");
- }
- if (Error == Error_Succeed)
- {
- ChannelService_BeginState_Sub Sub;
- Error = m_pChannelClient->BeginState(Sub);
- if (Error != Error_Succeed)
- {
- LOG_TRACE("BeginState biz channel failed!");
- m_pChannelClient->GetFunction()->CloseSession();
- m_pChannelClient = NULL;
- }
- }
- }
- }
- }
- virtual void OnPreClose(EntityCloseCauseEnum eCloseCause,CSmartPointer<ITransactionContext> pTransactionContext)
- {
- ErrorCodeEnum Error = __OnClose(Error_Succeed);
- pTransactionContext->SendAnswer(Error);
- }
- ErrorCodeEnum __OnClose(ErrorCodeEnum preOperationError)
- {
- CSmartPointer<IEntityFunction> spFunction = GetFunction();
- for (int i = 0; i < m_arrListener.GetCount(); ++i)
- {
- spFunction->UnsubscribeLog(m_arrListener[i]);
- }
- spFunction->UnregistSysVarEvent(SYSVAR_CALLTYPE);
- m_pChannelClient->GetFunction()->CloseSession();
- m_pChannelClient = NULL;
- return Error_Succeed;
- }
- virtual CServerSessionBase *OnNewSession(const char* /*pszRemoteEntityName*/, const char * /*pszParam*/)
- {
- return new CtxSyncServerSession(this);
- }
- virtual void OnSysVarEvent(const char *pszKey, const char *pszValue,const char *pszOldValue,const char *pszEntityName)
- {
- if (_stricmp(pszKey, SYSVAR_CALLTYPE) == 0)
- {
- Dbg("sys calltype from: %c to %c", pszOldValue[0], pszValue[0]);
- if(pszValue[0] == CALLTYPE_NORMAL)
- {
- m_nSysCallType = 0;
- }
- else if(pszValue[0] == CALLTYPE_MOBILE)
- {
- m_nSysCallType = 1;
- }
- else
- {
- m_nSysCallType =-1;
- }
- }
- }
- CAutoBuffer SerializeTable()
- {
- SpBuffer buf;
- int size = (int)m_kvpTable.size();
- buf.OpenWrite();
- buf & size;
- for (KeyTable::iterator it = m_kvpTable.begin(); it != m_kvpTable.end(); ++it) {
- buf & it->second;
- }
- return buf.ToBuffer();
- }
- bool SetValue(int type, CSimpleStringA key, CSimpleStringA value);
- bool GetValue(int type, CSimpleStringA key, CSimpleStringA& value);
- void GetBlob(CSimpleStringA key, CSimpleStringA& value, CBlob &blob)
- {
- int nkey = key.GetLength();
- int nvalue = value.GetLength();
- SpBuffer buf;
- int cnt = 1;
- buf.OpenWrite(nkey + nvalue + 32);
- buf & cnt & key & value;
- blob = buf.ToBlob();
- }
- void SendSingleEntry(int type, CSimpleStringA key, CSimpleStringA& value)
- {
- if (m_pChannelClient)
- {
- if (m_nSysCallType == 0)
- {
- ChannelService_Send_Info Info;
- Info.compress = true;
- Info.encrypt = true;
- Info.sub_type = type;
- Info.type = ACM_TYPE_CTX;
- GetBlob(key, value, Info.data);
- m_pChannelClient->Send(Info);
- }
- else
- {
- Dbg("cur calltype cannot send pkt");
- }
- }
- }
- void GetTableBlob(int type, CBlob &blob)
- {
- SpBuffer buf;
- KeyTable& ktTable = type == ACM_CTX_MACHINE ? m_machineTable : m_kvpTable;
- int cnt = (int)ktTable.size();
- buf.OpenWrite();
- buf & cnt;
- for (KeyTable::iterator it = ktTable.begin(); it != ktTable.end(); ++it) {
- buf & it->first & it->second;
- }
- blob = buf.ToBlob();
- }
- void SendTableEntry(int type)
- {
- KeyTable& ktTable = type == ACM_CTX_MACHINE ? m_machineTable : m_kvpTable;
- if (m_pChannelClient && ktTable.size() > 0)
- {
- if (m_nSysCallType == 0)
- {
- ChannelService_Send_Info Info;
- Info.compress = true;
- Info.encrypt = true;
- Info.sub_type = type;
- Info.type = ACM_TYPE_CTX;
- GetTableBlob(type, Info.data);
- m_pChannelClient->Send(Info);
- }
- else
- {
- Dbg("cur calltype cannot send pkt");
- }
- }
- }
- void ClearSessionEntry()
- {
- m_kvpTable.clear();
- }
- private:
- void InitMachineData()
- {
- CSystemStaticInfo Info;
- GetFunction()->GetSystemStaticInfo(Info);
- m_machineTable["TerminalNo"] = Info.strTerminalID;
- m_machineTable["MachineType"] = Info.strMachineType;
- m_machineTable["MachineVersion"] = Info.MachineVersion.ToString();
- m_machineTable["Site"] = Info.strSite;
- m_machineTable["Screen"] = CSimpleStringA::Format("%d", Info.eScreen);
- m_machineTable["EnrolGPS"] = CSimpleStringA::Format("%f,%f", Info.EnrolGPS.GetLongitude(), Info.EnrolGPS.GetLatitude());
- m_machineTable["TerminalAddress"] = "";//Info.strEnrolAddr;
- m_machineTable["DelayTime"] = "0";
- }
- private:
- typedef std::map<CSimpleStringA, CSimpleStringA> KeyTable;
- KeyTable m_kvpTable;
- KeyTable m_machineTable;
- int m_local_version;
- int m_remote_version;
- ChannelClient *m_pChannelClient;
- CAutoArray<CUUID> m_arrListener;
- int m_nSysCallType;//0:普通模式,1:p2p模式
- };
- bool CInteractionContextEntity::SetValue(int type, CSimpleStringA key, CSimpleStringA value)
- {
- std::map<CSimpleStringA, CSimpleStringA>::iterator it;
- if (type == ACM_CTX_MACHINE)
- {
- it = m_machineTable.find(key);
- if (it == m_machineTable.end())
- {
- m_machineTable[key] = value;
- SendSingleEntry(type, key, value);
- return true;
- }
- }
- else if (type == ACM_CTX_SESSION)
- {
- it = m_kvpTable.find(key);
- if (it == m_kvpTable.end())
- {
- m_kvpTable[key] = value;
- SendSingleEntry(type, key, value);
- return true;
- }
- }
- it->second = value;
- SendSingleEntry(type, key, it->second);
- return true;
- }
- bool CInteractionContextEntity::GetValue(int type, CSimpleStringA key, CSimpleStringA& value)
- {
- std::map<CSimpleStringA, CSimpleStringA>::iterator it;
- if (type == ACM_CTX_MACHINE)
- {
- it = m_machineTable.find(key);
- if (it == m_machineTable.end())
- return false;
- }
- else if (type == ACM_CTX_SESSION)
- {
- it = m_kvpTable.find(key);
- if (it == m_kvpTable.end())
- return false;
- }
- value = it->second;
-
- return true;
- }
- void CtxSyncServerSession::Handle_SetSessionData(SpOnewayCallContext<SyncService_SetSessionData_Info>::Pointer ctx)
- {
- bool rt;
- rt = m_pEntity->SetValue(ACM_CTX_SESSION, ctx->Info.key, ctx->Info.data);
- }
- void CtxSyncServerSession::Handle_GetSessionData(SpReqAnsContext<SyncService_GetSessionData_Req, SyncService_GetSessionData_Ans>::Pointer ctx)
- {
- CSimpleStringA value;
- bool rt;
- Dbg("get session Data: key=%s", (LPCSTR)ctx->Req.key);
- rt = m_pEntity->GetValue(ACM_CTX_SESSION, ctx->Req.key,value);
- if (rt == true)
- {
- Dbg("founded, data=%s", (LPCSTR)value);
- ctx->Ans.value = value;
- ctx->Answer(Error_Succeed);
- }
- else
- {
- Dbg("not founded!");
- ctx->Answer(Error_NotExist);
- }
-
- }
- void CtxSyncServerSession::Handle_SetMachineData(SpOnewayCallContext<SyncService_SetMachineData_Info>::Pointer ctx)
- {
- bool rt;
- Dbg("Set machine Data: key=%s, data=%s", (LPCSTR)ctx->Info.key, (LPCSTR)ctx->Info.data);
- rt = m_pEntity->SetValue(ACM_CTX_MACHINE, ctx->Info.key, ctx->Info.data);
- }
- void CtxSyncServerSession::Handle_GetMachineData(SpReqAnsContext<SyncService_GetMachineData_Req, SyncService_GetMachineData_Ans>::Pointer ctx)
- {
- CSimpleStringA value;
- bool rt;
- Dbg("get machine Data: key=%s", (LPCSTR)ctx->Req.key);
- rt = m_pEntity->GetValue(ACM_CTX_MACHINE, ctx->Req.key,value);
- if (rt == true)
- {
- Dbg("founded, data=%s", (LPCSTR)value);
- ctx->Ans.value = value;
- ctx->Answer(Error_Succeed);
- }
- else
- {
- Dbg("not founded!");
- ctx->Answer(Error_NotExist);
- }
- }
- ChannelClient::ChannelClient( CInteractionContextEntity* pEntity ) : ChannelService_ClientBase(pEntity), m_pEntity(pEntity)
- {
- }
- void ChannelClient::OnMessage( ErrorCodeEnum Error, ChannelService_State_Info &Msg, CSmartPointer<IReleasable> pData)
- {
- if (Error == Error_Succeed) {
- switch(Msg.state)
- {
- case eChannelState_Connected:
- {
- m_pEntity->SendTableEntry(ACM_CTX_MACHINE);
- }
- break;
- case eChannelState_Idle:
- {
- m_pEntity->ClearSessionEntry();
- }
- break;
- default:
- break;
- }
- }
- }
- SP_BEGIN_ENTITY_MAP()
- SP_ENTITY(CInteractionContextEntity)
- SP_END_ENTITY_MAP()
|