source: chevmsgr/trunk/msg.cpp@ 5

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

클라이언트 메시지 추가 및 안정화

File size: 2.0 KB
Line 
1#include "msg.hpp"
2
3#define DECLARE_TEMPLATE_OBJECT(_obj, _type) \
4 json::Object _obj; \
5 makeTemplate(_obj, _type); \
6
7namespace Message
8{
9 // parser
10 void Parser::parse(const std::string & message)
11 {
12 mObject = json::Deserialize(message);
13 }
14
15 std::string Parser::type() const
16 {
17 return get<std::string>("type");
18 }
19
20 // request
21 void Request::setUserID(const std::string & id)
22 {
23 mID = id;
24 }
25
26 const std::string & Request::getUserID()
27 {
28 return mID;
29 }
30
31 void Request::makeTemplate(json::Object & obj, const std::string & type) const
32 {
33 obj["id"] = mID;
34 obj["type"] = type;
35 }
36
37 std::string Request::command(const std::string & cmd) const
38 {
39 DECLARE_TEMPLATE_OBJECT(obj, "command");
40
41 obj["command"] = cmd;
42
43 return json::Serialize(obj);
44 }
45
46 std::string Request::sms(const std::string & phone) const
47 {
48 DECLARE_TEMPLATE_OBJECT(obj, "sms");
49
50 obj["phone"] = phone;
51
52 return json::Serialize(obj);
53 }
54
55 std::string Request::join(const std::string & id, const std::string & pw, const std::string & sms)
56 {
57 DECLARE_TEMPLATE_OBJECT(obj, "join");
58
59 obj["id" ] = id;
60 obj["pw" ] = pw;
61 obj["sms"] = sms;
62
63 return json::Serialize(obj);
64 }
65
66 std::string Request::login(const std::string & pw) const
67 {
68 DECLARE_TEMPLATE_OBJECT(obj, "login");
69
70 obj["pw"] = mID + "$" + pw;
71
72 return json::Serialize(obj);
73 }
74
75 std::string Request::chat(const std::string & to, const std::string & sessid, const std::string & message) const
76 {
77 DECLARE_TEMPLATE_OBJECT(obj, "chat");
78
79 obj["from" ] = mID;
80 obj["to" ] = to;
81 obj["sess-id"] = sessid;
82 obj["message"] = message;
83
84 return json::Serialize(obj);
85 }
86
87 std::string Request::friendList() const
88 {
89 DECLARE_TEMPLATE_OBJECT(obj, "friend-list");
90
91 return json::Serialize(obj);
92 }
93
94 // response
95 void Response::makeTemplate(json::Object & obj, const std::string & type) const
96 {
97 obj["type"] = type;
98 }
99
100 std::string Response::result(const bool status) const
101 {
102 DECLARE_TEMPLATE_OBJECT(obj, "result");
103
104 obj["result"] = status;
105
106 return json::Serialize(obj);
107 }
108};
Note: See TracBrowser for help on using the repository browser.