source: chevmsgr/trunk/msg.cpp@ 14

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

테스트 코드 추가
UI 모양 작업
채팅되는 상태까지 확인

File size: 3.8 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 std::string Message::serialize() const
45 {
46 return json::Serialize(mObject);
47 }
48
49 // request
50 void Request::setUserID(const std::string & id)
51 {
52 mID = id;
53 }
54
55 const std::string & Request::getUserID()
56 {
57 return mID;
58 }
59
60 void Request::makeTemplate(json::Object & obj, const std::string & type) const
61 {
62 obj[ProtocolType::ID] = mID;
63 obj[ProtocolType::TYPE] = type;
64 }
65
66 std::string Request::sms(const std::string & phone) const
67 {
68 DECLARE_TEMPLATE_OBJECT(obj, ProtocolType::SMS);
69
70 obj[ProtocolType::PHONE] = phone;
71
72 return json::Serialize(obj);
73 }
74
75 std::string Request::join(const std::string & id, const std::string & pw, const std::string & sms)
76 {
77 DECLARE_TEMPLATE_OBJECT(obj, ProtocolType::JOIN);
78
79 obj[ProtocolType::ID] = id;
80 obj[ProtocolType::PW] = pw;
81 obj[ProtocolType::SMS] = sms;
82
83 return json::Serialize(obj);
84 }
85
86 std::string Request::login(const std::string & pw) const
87 {
88 DECLARE_TEMPLATE_OBJECT(obj, ProtocolType::LOGIN);
89
90 obj[ProtocolType::PW] = pw;
91
92 return json::Serialize(obj);
93 }
94
95 std::string Request::logout() const
96 {
97 DECLARE_TEMPLATE_OBJECT(obj, ProtocolType::LOGOUT);
98
99 return json::Serialize(obj);
100 }
101
102 std::string Request::addFriend(const std::string & id) const
103 {
104 DECLARE_TEMPLATE_OBJECT(obj, ProtocolType::ADD_FRIEND);
105
106 obj[ProtocolType::ID] = id;
107
108 return json::Serialize(obj);
109 }
110
111 std::string Request::openSession(const std::vector<std::string> & idList) const
112 {
113 DECLARE_TEMPLATE_OBJECT(obj, ProtocolType::OPEN_SESSION);
114
115 json::Array ar;
116 std::vector<std::string>::const_iterator iter;
117 for (iter = idList.begin(); iter != idList.end(); iter++)
118 ar.push_back(*iter);
119
120 obj[ProtocolType::ID_LIST] = ar;
121
122 return json::Serialize(obj);
123 }
124
125 std::string Request::tell(const std::string & sessid, const std::string & message, const int sensitive) const
126 {
127 DECLARE_TEMPLATE_OBJECT(obj, ProtocolType::TELL);
128
129 obj[ProtocolType::FROM] = mID;
130 obj[ProtocolType::SESSION_ID] = sessid;
131 obj[ProtocolType::MESSAGE] = message;
132 obj[ProtocolType::SENSITIVE] = sensitive;
133
134 return json::Serialize(obj);
135 }
136
137 std::string Request::getFriendList() const
138 {
139 DECLARE_TEMPLATE_OBJECT(obj, ProtocolType::GET_FRIEND_LIST);
140
141 return json::Serialize(obj);
142 }
143
144 // response
145 void Response::makeTemplate(json::Object & obj, const std::string & type) const
146 {
147 obj[ProtocolType::TYPE] = type;
148 }
149
150 std::string Response::result(const std::string & requestType, const bool status) const
151 {
152 DECLARE_TEMPLATE_OBJECT(obj, requestType);
153
154 obj[ProtocolType::RESULT] = status;
155
156 return json::Serialize(obj);
157 }
158
159 std::string Response::friendList(const std::vector<SFriend> & friendList) const
160 {
161 DECLARE_TEMPLATE_OBJECT(obj, ProtocolType::FRIEND_LIST);
162
163 json::Array ar;
164 std::vector<SFriend>::const_iterator iter;
165 for (iter = friendList.begin() ; iter != friendList.end() ; iter++)
166 {
167 json::Object fr;
168
169 fr[ProtocolType::ID] = iter->id;
170 fr[ProtocolType::NAME] = iter->name;
171
172 ar.push_back(fr);
173 }
174
175 obj[ProtocolType::FRIEND_LIST] = ar;
176
177 return json::Serialize(obj);
178 }
179};
Note: See TracBrowser for help on using the repository browser.