source: chevmsgr/trunk/msg.cpp@ 6

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

modify client interface and message protocol

File size: 2.5 KB
Line 
1#include "msg.hpp"
2
3#define DECLARE_TEMPLATE_OBJECT(_obj, _type) \
4 json::Object _obj; \
5 makeTemplate(_obj, _type); \
6
7#define DELIMITER "$"
8
9namespace Protocol
10{
11 // parser
12 void Message::parse(const std::string & message)
13 {
14 mObject = json::Deserialize(message);
15 }
16
17 std::string Message::type() const
18 {
19 return get<std::string>("type");
20 }
21
22 // request
23 void Request::setUserID(const std::string & id)
24 {
25 mID = id;
26 }
27
28 const std::string & Request::getUserID()
29 {
30 return mID;
31 }
32
33 void Request::makeTemplate(json::Object & obj, const std::string & type) const
34 {
35 obj[ProtocolType::ID ] = mID;
36 obj[ProtocolType::TYPE] = type;
37 }
38
39 std::string Request::sms(const std::string & phone) const
40 {
41 DECLARE_TEMPLATE_OBJECT(obj, ProtocolType::SMS);
42
43 obj[ProtocolType::PHONE] = phone;
44
45 return json::Serialize(obj);
46 }
47
48 std::string Request::join(const std::string & id, const std::string & pw, const std::string & sms)
49 {
50 DECLARE_TEMPLATE_OBJECT(obj, ProtocolType::JOIN);
51
52 obj[ProtocolType::ID ] = id;
53 obj[ProtocolType::PW ] = pw;
54 obj[ProtocolType::SMS] = sms;
55
56 return json::Serialize(obj);
57 }
58
59 std::string Request::login(const std::string & pw) const
60 {
61 DECLARE_TEMPLATE_OBJECT(obj, ProtocolType::LOGIN);
62
63 obj[ProtocolType::PW] = mID + DELIMITER + pw;
64
65 return json::Serialize(obj);
66 }
67
68 std::string Request::addFriend(const std::string & id) const
69 {
70 DECLARE_TEMPLATE_OBJECT(obj, ProtocolType::ADD_FRIEND);
71
72 obj[ProtocolType::ID] = id;
73
74 return json::Serialize(obj);
75 }
76
77 std::string Request::openSession(const std::string & to) const
78 {
79 DECLARE_TEMPLATE_OBJECT(obj, ProtocolType::OPEN_SESSION);
80
81 obj[ProtocolType::TO] = to;
82
83 return json::Serialize(obj);
84 }
85
86 std::string Request::chat(const std::string & sessid, const std::string & message, const int sensitive) const
87 {
88 DECLARE_TEMPLATE_OBJECT(obj, ProtocolType::CHAT);
89
90 obj[ProtocolType::FROM ] = mID;
91 obj[ProtocolType::SESSION_ID] = sessid;
92 obj[ProtocolType::MESSAGE ] = message;
93 obj[ProtocolType::SENSITIVE ] = sensitive;
94
95 return json::Serialize(obj);
96 }
97
98 std::string Request::getFriendList() const
99 {
100 DECLARE_TEMPLATE_OBJECT(obj, ProtocolType::GET_FRIEND_LIST);
101
102 return json::Serialize(obj);
103 }
104
105 // response
106 void Response::makeTemplate(json::Object & obj, const std::string & type) const
107 {
108 obj[ProtocolType::TYPE] = type;
109 }
110
111 std::string Response::result(const std::string & requestType, const bool status) const
112 {
113 DECLARE_TEMPLATE_OBJECT(obj, ProtocolType::RESULT);
114
115 obj[ProtocolType::RESULT] = status;
116
117 return json::Serialize(obj);
118 }
119};
Note: See TracBrowser for help on using the repository browser.