forked from jimingmin/frame
/
cmd_handler.cpp
149 lines (120 loc) · 3.8 KB
/
cmd_handler.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
* cmd_handler.cpp
*
* Created on: Apr 29, 2015
* Author: jimm
*/
#include "cmd_handler.h"
#include "common/common_codeengine.h"
#include "common/common_api.h"
#include "logger/logger.h"
#include "frame_msghandle.h"
#include "frame_define.h"
#include "frame.h"
#include "cmd_define.h"
#include <string.h>
using namespace LOGGER;
using namespace FRAME;
CCmdHandler::CCmdHandler(CriticalSection *pSection, const char *szServiceName, int32_t nServiceType, uint16_t nServiceID)
{
m_pSection = pSection;
memset(m_szServiceName, 0, sizeof(m_szServiceName));
strcpy(m_szServiceName, szServiceName);
m_nServiceType = nServiceType;
m_nServiceID = nServiceID;
m_pConnector = NULL;
m_bLoadConfigFinish = false;
}
int32_t CCmdHandler::OnOpened(IIOSession *pIoSession)
{
WRITE_DEBUG_LOG(MODULE_NAME, "new session!{peeraddress=%s, peerport=%d}\n",
pIoSession->GetPeerAddressStr(), pIoSession->GetPeerPort());
CmdRegistReq stCmdRegist(m_szServiceName, m_nServiceType, m_nServiceID, m_bLoadConfigFinish);
string cmd = stCmdRegist.Encode();
uint8_t arrBuf[1024];
uint32_t nOffset = 0;
CCodeEngine::Encode(arrBuf, sizeof(arrBuf), nOffset, cmd.size());
strncpy((char *)arrBuf + nOffset, cmd.c_str(), cmd.size());
pIoSession->Write(arrBuf, cmd.size() + nOffset);
return 0;
}
int32_t CCmdHandler::OnRecved(IIOSession *pIoSession, uint8_t *pBuf, uint32_t nBufSize)
{
WRITE_DEBUG_LOG(MODULE_NAME, "recv message : [size=%d]:%s\n", nBufSize, (char *)pBuf);
Document document;
if(document.Parse<0>((char *)pBuf).HasParseError())
{
return 0;
}
if(!document.IsObject())
{
return 0;
}
Value &stCmd = document["cmd"];
Value &stParams = document["params"];
if(strcmp(stCmd.GetString(), "load config") == 0)
{
if(LoadConfig(stParams["config"].GetString(), stParams["content"].GetString()))
{
LoadConfigError stLoadConfigError(m_szServiceName, m_nServiceType, m_nServiceID, (char *)stParams["config"].GetString());
string cmd = stLoadConfigError.Encode();
uint8_t arrBuf[2048];
uint32_t nOffset = 0;
CCodeEngine::Encode(arrBuf, sizeof(arrBuf), nOffset, cmd.size());
strncpy((char *)arrBuf + nOffset, cmd.c_str(), cmd.size());
pIoSession->Write(arrBuf, cmd.size() + nOffset);
}
}
else if(strcmp(stCmd.GetString(), "load finish") == 0)
{
m_pSection->leave();
m_bLoadConfigFinish = true;
}
return 0;
}
int32_t CCmdHandler::OnSent(IIOSession *pIoSession, uint8_t *pBuf, uint32_t nBufSize)
{
WRITE_DEBUG_LOG(MODULE_NAME, "sent message : [size=%d]:%s\n", nBufSize, (char *)pBuf);
return 0;
}
int32_t CCmdHandler::OnClosed(IIOSession *pIoSession)
{
WRITE_DEBUG_LOG(MODULE_NAME, "session closed!{peeraddress=%s, peerport=%d}\n",
pIoSession->GetPeerAddressStr(), pIoSession->GetPeerPort());
Delay(3 * US_PER_SECOND);
m_pConnector->Connect(pIoSession->GetPeerAddressStr(), pIoSession->GetPeerPort());
return 0;
}
int32_t CCmdHandler::OnError(IIOSession *pIoSession)
{
WRITE_DEBUG_LOG(MODULE_NAME, "session error!{peeraddress=%s, peerport=%d}\n",
pIoSession->GetPeerAddressStr(), pIoSession->GetPeerPort());
Delay(3 * US_PER_SECOND);
m_pConnector->Connect(pIoSession->GetPeerAddressStr(), pIoSession->GetPeerPort());
return 0;
}
int32_t CCmdHandler::OnTimeout(IIOSession *pIoSession)
{
WRITE_DEBUG_LOG(MODULE_NAME, "session timeout!{peeraddress=%s, peerport=%d}\n",
pIoSession->GetPeerAddressStr(), pIoSession->GetPeerPort());
m_pConnector->Connect(pIoSession->GetPeerAddressStr(), pIoSession->GetPeerPort());
return 0;
}
void CCmdHandler::SetConnector(CConnector *pConnector)
{
m_pConnector = pConnector;
}
int32_t CCmdHandler::LoadConfig(const char *pConfigName, const char *pFileContent)
{
int32_t nResult = 0;
IConfig *pConfig = g_Frame.GetSafeConfig(pConfigName);
if(pConfig != NULL)
{
nResult = pConfig->Parser((char *)pFileContent);
}
else
{
nResult = -1;
}
return nResult;
}