source: chevmsgr/trunk/msg.cpp@ 11

Last change on this file since 11 was 11, checked in by cheese, 9 years ago

add util function to msg

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