1 | #include "msg.hpp"
|
---|
2 |
|
---|
3 | #define DECLARE_TEMPLATE_OBJECT(_obj, _type) \
|
---|
4 | json::Object _obj; \
|
---|
5 | makeTemplate(_obj, _type); \
|
---|
6 |
|
---|
7 | namespace Protocol
|
---|
8 | {
|
---|
9 | // parser
|
---|
10 | void Message::parse(const std::string & message)
|
---|
11 | {
|
---|
12 | mObject = json::Deserialize(message);
|
---|
13 | }
|
---|
14 |
|
---|
15 | std::string Message::type() const
|
---|
16 | {
|
---|
17 | return get<std::string>("type");
|
---|
18 | }
|
---|
19 |
|
---|
20 | std::vector<SFriend> Message::getFriendList() const
|
---|
21 | {
|
---|
22 | json::Array ar = mObject[ProtocolType::FRIEND_LIST];
|
---|
23 |
|
---|
24 | std::vector<SFriend> friendList;
|
---|
25 |
|
---|
26 | std::vector<json::Value>::iterator iter;
|
---|
27 | for (iter = ar.begin(); iter != ar.end(); iter++)
|
---|
28 | {
|
---|
29 | SFriend fr;
|
---|
30 |
|
---|
31 | fr.id = (*iter)[ProtocolType::ID ];
|
---|
32 | fr.name = (*iter)[ProtocolType::NAME];
|
---|
33 |
|
---|
34 | friendList.push_back(fr);
|
---|
35 | }
|
---|
36 |
|
---|
37 | return friendList;
|
---|
38 | }
|
---|
39 |
|
---|
40 | // request
|
---|
41 | void Request::setUserID(const std::string & id)
|
---|
42 | {
|
---|
43 | mID = id;
|
---|
44 | }
|
---|
45 |
|
---|
46 | const std::string & Request::getUserID()
|
---|
47 | {
|
---|
48 | return mID;
|
---|
49 | }
|
---|
50 |
|
---|
51 | void Request::makeTemplate(json::Object & obj, const std::string & type) const
|
---|
52 | {
|
---|
53 | obj[ProtocolType::ID] = mID;
|
---|
54 | obj[ProtocolType::TYPE] = type;
|
---|
55 | }
|
---|
56 |
|
---|
57 | std::string Request::sms(const std::string & phone) const
|
---|
58 | {
|
---|
59 | DECLARE_TEMPLATE_OBJECT(obj, ProtocolType::SMS);
|
---|
60 |
|
---|
61 | obj[ProtocolType::PHONE] = phone;
|
---|
62 |
|
---|
63 | return json::Serialize(obj);
|
---|
64 | }
|
---|
65 |
|
---|
66 | std::string Request::join(const std::string & id, const std::string & pw, const std::string & sms)
|
---|
67 | {
|
---|
68 | DECLARE_TEMPLATE_OBJECT(obj, ProtocolType::JOIN);
|
---|
69 |
|
---|
70 | obj[ProtocolType::ID] = id;
|
---|
71 | obj[ProtocolType::PW] = pw;
|
---|
72 | obj[ProtocolType::SMS] = sms;
|
---|
73 |
|
---|
74 | return json::Serialize(obj);
|
---|
75 | }
|
---|
76 |
|
---|
77 | std::string Request::login(const std::string & pw) const
|
---|
78 | {
|
---|
79 | DECLARE_TEMPLATE_OBJECT(obj, ProtocolType::LOGIN);
|
---|
80 |
|
---|
81 | obj[ProtocolType::PW] = mID + DELIMITER + pw;
|
---|
82 |
|
---|
83 | return json::Serialize(obj);
|
---|
84 | }
|
---|
85 |
|
---|
86 | std::string Request::addFriend(const std::string & id) const
|
---|
87 | {
|
---|
88 | DECLARE_TEMPLATE_OBJECT(obj, ProtocolType::ADD_FRIEND);
|
---|
89 |
|
---|
90 | obj[ProtocolType::ID] = id;
|
---|
91 |
|
---|
92 | return json::Serialize(obj);
|
---|
93 | }
|
---|
94 |
|
---|
95 | std::string Request::openSession(const std::string & to) const
|
---|
96 | {
|
---|
97 | DECLARE_TEMPLATE_OBJECT(obj, ProtocolType::OPEN_SESSION);
|
---|
98 |
|
---|
99 | obj[ProtocolType::TO] = to;
|
---|
100 |
|
---|
101 | return json::Serialize(obj);
|
---|
102 | }
|
---|
103 |
|
---|
104 | std::string Request::chat(const std::string & sessid, const std::string & message, const int sensitive) const
|
---|
105 | {
|
---|
106 | DECLARE_TEMPLATE_OBJECT(obj, ProtocolType::CHAT);
|
---|
107 |
|
---|
108 | obj[ProtocolType::FROM] = mID;
|
---|
109 | obj[ProtocolType::SESSION_ID] = sessid;
|
---|
110 | obj[ProtocolType::MESSAGE] = message;
|
---|
111 | obj[ProtocolType::SENSITIVE] = sensitive;
|
---|
112 |
|
---|
113 | return json::Serialize(obj);
|
---|
114 | }
|
---|
115 |
|
---|
116 | std::string Request::getFriendList() const
|
---|
117 | {
|
---|
118 | DECLARE_TEMPLATE_OBJECT(obj, ProtocolType::GET_FRIEND_LIST);
|
---|
119 |
|
---|
120 | return json::Serialize(obj);
|
---|
121 | }
|
---|
122 |
|
---|
123 | // response
|
---|
124 | void Response::makeTemplate(json::Object & obj, const std::string & type) const
|
---|
125 | {
|
---|
126 | obj[ProtocolType::TYPE] = type;
|
---|
127 | }
|
---|
128 |
|
---|
129 | std::string Response::result(const std::string & requestType, const bool status) const
|
---|
130 | {
|
---|
131 | DECLARE_TEMPLATE_OBJECT(obj, ProtocolType::RESULT);
|
---|
132 |
|
---|
133 | obj[ProtocolType::RESULT] = status;
|
---|
134 |
|
---|
135 | return json::Serialize(obj);
|
---|
136 | }
|
---|
137 |
|
---|
138 | std::string Response::friendList(const std::vector<SFriend> & friendList) const
|
---|
139 | {
|
---|
140 | DECLARE_TEMPLATE_OBJECT(obj, ProtocolType::FRIEND_LIST);
|
---|
141 |
|
---|
142 | json::Array ar;
|
---|
143 | std::vector<SFriend>::const_iterator iter;
|
---|
144 | for (iter = friendList.begin() ; iter != friendList.end() ; iter++)
|
---|
145 | {
|
---|
146 | json::Object fr;
|
---|
147 |
|
---|
148 | fr[ProtocolType::ID ] = iter->id;
|
---|
149 | fr[ProtocolType::NAME] = iter->name;
|
---|
150 |
|
---|
151 | ar.push_back(fr);
|
---|
152 | }
|
---|
153 |
|
---|
154 | obj[ProtocolType::FRIEND_LIST] = ar;
|
---|
155 |
|
---|
156 | return json::Serialize(obj);
|
---|
157 | }
|
---|
158 | };
|
---|