-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser.cc
309 lines (283 loc) · 10.7 KB
/
parser.cc
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#include "./include/parser.hpp"
void outCommand(Command c){
printf("***********************\n /add:%s\n/port:%s\n/posi:%s\n/index:%s\n/op:%s\n/value:%s\n/res:%s\n***********************"
,c.add.c_str(),c.port.c_str(),c.pos.c_str(),c.index.c_str(),c.op.c_str(),c.value.c_str(),c.res.c_str());
}
//在末尾加上/是为了防止粘包!!!!!!!!!很重要!!!
Command Parser::commandParser(string cmd){//!!!!!!!!!!!!need gengxin !
// printf("--------------------par.5\n");
// printf("%s\n",cmd.c_str());
// printf("--------------------par.7\n");
int beg=0,typePoi=-1,posPoi=-1,indexPoi=-1,opPoi=-1,valuePoi=-1,portPoi=-1,addPoi=-1,resPoi=-1,endPoi=-1;
string type,add,op,value,index,pos,port,res;
if(cmd.find("/add")!=string::npos){addPoi=cmd.find("/add:");}
if(cmd.find("/port")!=string::npos){portPoi=cmd.find("/port:");}
if(cmd.find("/posi")!=string::npos){posPoi=cmd.find("/posi:");}
if(cmd.find("/index:")!=string::npos){indexPoi=cmd.find("/index:");}
if(cmd.find("/op:")!=string::npos){opPoi=cmd.find("/op:");}
if(cmd.find("/value:")!=string::npos){valuePoi=cmd.find("/value:");}
if(cmd.find("/res")!=string::npos){resPoi=cmd.find("/res:");}
if(cmd.find("/",resPoi+1)!=string::npos){endPoi=cmd.find("/",resPoi+1);}
if((typePoi=cmd.find("[PRE]"))!=string::npos){
type=cmd.substr(typePoi,5);
add=cmd.substr(addPoi+5,portPoi-addPoi-5);
port=cmd.substr(portPoi+6,posPoi-portPoi-6);
pos=cmd.substr(posPoi+6,indexPoi-posPoi-6);
index=cmd.substr(indexPoi+7,resPoi-indexPoi-7);
op=cmd.substr(opPoi+4,resPoi-opPoi-4);
res=cmd.substr(resPoi+5,endPoi-resPoi-5);
// res=cmd.substr(resPoi+5);
}else if((typePoi=cmd.find("[ROLL]"))!=string::npos){
type=cmd.substr(typePoi,6);
add=cmd.substr(addPoi+5,portPoi-addPoi-5);
port=cmd.substr(portPoi+6,posPoi-portPoi-6);
pos=cmd.substr(posPoi+6,indexPoi-posPoi-6);
index=cmd.substr(indexPoi+7,resPoi-indexPoi-7);
res=cmd.substr(resPoi+5,endPoi-resPoi-5);
}else if((typePoi=cmd.find("[COM]"))!=string::npos){
type=cmd.substr(typePoi,5);
add=cmd.substr(addPoi+5,portPoi-addPoi-5);
port=cmd.substr(portPoi+6,posPoi-portPoi-6);
pos=cmd.substr(posPoi+6,indexPoi-posPoi-6);
index=cmd.substr(indexPoi+7,resPoi-indexPoi-7);
res=cmd.substr(resPoi+5,endPoi-resPoi-5);
}else if((typePoi=cmd.find("[PRERE]"))!=string::npos){
type=cmd.substr(typePoi,7);
add=cmd.substr(addPoi+5,portPoi-addPoi-5);
port=cmd.substr(portPoi+6,posPoi-portPoi-6);
pos=cmd.substr(posPoi+6,indexPoi-posPoi-6);
index=cmd.substr(indexPoi+7,resPoi-indexPoi-7);
res=cmd.substr(resPoi+5,endPoi-resPoi-5);
}else if((typePoi=cmd.find("[ROLLRE]"))!=string::npos){
type=cmd.substr(typePoi,8);
add=cmd.substr(addPoi+5,portPoi-addPoi-5);
port=cmd.substr(portPoi+6,posPoi-portPoi-6);
pos=cmd.substr(posPoi+6,indexPoi-posPoi-6);
index=cmd.substr(indexPoi+7,resPoi-indexPoi-7);
res=cmd.substr(resPoi+5,endPoi-resPoi-5);
}else if((typePoi=cmd.find("[COMRE]"))!=string::npos){
type=cmd.substr(typePoi,7);
add=cmd.substr(addPoi+5,portPoi-addPoi-5);
port=cmd.substr(portPoi+6,posPoi-portPoi-6);
pos=cmd.substr(posPoi+6,indexPoi-posPoi-6);
index=cmd.substr(indexPoi+7,resPoi-indexPoi-7);
res=cmd.substr(resPoi+5,endPoi-resPoi-5);
}else if((typePoi=cmd.find("[HEART]"))!=string::npos){
printf("--------------------par.50\n");
type=cmd.substr(typePoi,7);
// printf("%s\n",type.c_str());
add=cmd.substr(addPoi+5,portPoi-addPoi-5);
// printf("%s\n",add.c_str());
port=cmd.substr(portPoi+6,posPoi-portPoi-6);
// printf("%s\n",port.c_str());
pos=cmd.substr(posPoi+6,indexPoi-posPoi-6);
// printf("%s\n",pos.c_str());
index=cmd.substr(indexPoi+7,resPoi-indexPoi-7);
// printf("%s\n",index.c_str());
res=cmd.substr(resPoi+5,endPoi-resPoi-5);
// printf("%s\n",res.c_str());
}else if((typePoi=cmd.find("[HEARTRE]"))!=string::npos){
printf("--------------------par.57\n");
type=cmd.substr(typePoi,9);
add=cmd.substr(addPoi+5,portPoi-addPoi-5);
port=cmd.substr(portPoi+6,posPoi-portPoi-6);
pos=cmd.substr(posPoi+6,indexPoi-posPoi-6);
index=cmd.substr(indexPoi+7,resPoi-indexPoi-7);
res=cmd.substr(resPoi+5,endPoi-resPoi-5);
// printf("%s\n",type.c_str());
// printf("%s\n",add.c_str());
// printf("%s\n",port.c_str());
// printf("%s\n",pos.c_str());
// printf("%s\n",index.c_str());
// printf("%s\n",res.c_str());
}else if((typePoi=cmd.find("[VOTE]"))!=string::npos){
type=cmd.substr(typePoi,6);
add=cmd.substr(addPoi+5,portPoi-addPoi-5);
port=cmd.substr(portPoi+6,posPoi-portPoi-6);
pos=cmd.substr(posPoi+6,indexPoi-posPoi-6);
index=cmd.substr(indexPoi+7,resPoi-indexPoi-7);
res=cmd.substr(resPoi+5,endPoi-resPoi-5);
}else if((typePoi=cmd.find("[VOTERE]"))!=string::npos){
type=cmd.substr(typePoi,8);
add=cmd.substr(addPoi+5,portPoi-addPoi-5);
port=cmd.substr(portPoi+6,posPoi-portPoi-6);
pos=cmd.substr(posPoi+6,indexPoi-posPoi-6);
index=cmd.substr(indexPoi+7,resPoi-indexPoi-7);
res=cmd.substr(resPoi+5,endPoi-resPoi-5);
}else if((typePoi=cmd.find("[CLIENT]"))!=string::npos){
type=cmd.substr(typePoi,8);
add=cmd.substr(addPoi+5,portPoi-addPoi-5);
op=cmd.substr(opPoi+4,resPoi-opPoi-4);
res=cmd.substr(resPoi+5,endPoi-resPoi-5);
}
outCommand(Command(type,add,port,pos,index,op,value,res));
return Command(type,add,port,pos,index,op,value,res);
}
void Parser::reComPar(string &cmd,string &op,string &val,string &oldval,string &state){
int opPos,valPos,oldvalPos,statePos,endPoi;
printf("--------------------par.117\n");
printf("%s\n",cmd.c_str());
opPos=cmd.find("op:");valPos=cmd.find("val:");oldvalPos=cmd.find("oldval:");
statePos=cmd.find("state:");endPoi=cmd.find("}");
printf("cmd: %s\n",cmd.c_str());
printf("--------------------par.120\n");
op=cmd.substr(opPos+3,valPos-opPos-3);
printf("--------------------par.122\n");
printf("op:%d val:%d oldval:%d state:%d ",opPos,valPos,oldvalPos,statePos);
val=cmd.substr(valPos+4,oldvalPos-valPos-4);
printf("--------------------par.124\n");
printf("op:%d val:%d oldval:%d state:%d ",opPos,valPos,oldvalPos,statePos);
oldval=cmd.substr(oldvalPos+7,statePos-oldvalPos-7);
printf("--------------------par.126\n");
printf("op:%d val:%d oldval:%d state:%d ",opPos,valPos,oldvalPos,statePos);
state=cmd.substr(statePos+6,endPoi-statePos-6);
printf("--------------------par.128\n");
printf("op:%d val:%d oldval:%d state:%d ",opPos,valPos,oldvalPos,statePos);
}
/*
@param raw the raw string you want to warp into the string message
@return a string message
e.g. CS06142 -> $7\r\nCS06142\r\n
*/
string Parser::getStringMessage(string & raw)//��������ĸ�ʽ
{
string rc = "$";
int length = (int) raw.length();
rc += std::to_string(length);
rc += "\r\n";
rc += raw;
rc += "\r\n";
return rc;
}
/*
@return a success message
*/
string Parser::getSuccessMessage()//����ɹ�
{
string rc = "+OK\r\n";
return rc;
}
/*
@ return an error message
*/
string Parser::getErrorMessage()//����ʧ��
{
string rc = "-ERROR\r\n";
return rc;
}
/*
@param raw the raw interger you wanna warp into the interger message
@return a interger message
e.g. 10 -> :10\r\n
*/
string Parser::getIntergerMessage(int raw)//�����ʽ
{
string rc = ":";
rc += std::to_string(raw);
rc += "\r\n";
return rc;
}
/*
@param raw the raw string vactor you wanna warp into the RESP message
@return a string vactor message
e.g. Cloud Computing-> *2\r\n$5\r\nCloud\r\n$9\r\nComputing\r\n
*/
string Parser::getRESPArry(std::vector<string> & raw)//ת����RESP���и�ʽ������ֵ
{
string rc = "*";
int number = raw.size();
rc += std::to_string(number);
rc += "\r\n";
for(size_t i = 0; i < raw.size(); i++)
{
rc += getStringMessage(raw[i]);
}
return rc;
}
string Parser::getRESPArry(string raw)//��ֵ
{
string rc = "*";
int number = 1;
rc += std::to_string(number);
rc += "\r\n";
rc += getStringMessage(raw);
return rc;
}
/*
@para message the raw message recieved
@return the string parsered from message
e.g. CS06142 <- $7\r\nCS06142\r\n
*/
string Parser::parseStringMessage(char * message, int * length)//�Ӹ�ʽ����ȡ������Ϣ����key
{
string rc = "";
int strl = 0;
int pos = 0;
assert(message[0] == '$');
strl = atoi(message + 1);
pos = 3 + std::to_string(strl).size();
rc = string(message + pos, strl);
if(length) {
*length = pos + strl + 2;
}
return rc;
}
/*
@para message the raw message recieved
@return the interger parsered from message
e.g. 10 <- :10\r\n
*/
int Parser::parserIntergerMessage(char * message, int * length)//�õ�������Ϣ
{
int rc = 0;
assert(message[0] == ':');
rc = atoi(message + 1);
if(length) {
*length = 3 + std::to_string(rc).length();
}
return rc;
}
/*
@para message the raw message recieved
@return the status of the excution
e.g. +OK\r\n -> true
-ERROR\r\n -> false
*/
bool Parser::parserSuccessMessage(char * message, int * length)//�ӳɹ�/ʧ������ȡ��Ϣ
{
assert(message[0] == '+' || message[0] == '-');
if(message[0] == '+') {
if(length) {
*length = 5;
}
return true;
}
else {
if(length) {
*length = 8;
}
return false;
}
}
/*
@para message the raw message recieved
@return the string vector parsered from message
e.g. Cloud Computing <- *2\r\n$5\r\nCloud\r\n$9\r\nComputing\r\n
*/
void Parser::parserRESPArry(char * message, std::vector<string> & rc, int * length)//�õ����ݿ���value��Ϣ
{
int number = 0;
int pos = 0;
assert(message[0] == '*');
number = atoi(message + 1);
pos += std::to_string(number).length() + 3;
for(int i = 0; i < number; i++) {
int len = 0;
string tmp = parseStringMessage(message + pos, &len);
rc.push_back(tmp);
pos += len;
}
if(length) {
*length = pos;
}
}