1 | #include "msg.hpp"
|
---|
2 |
|
---|
3 | #define DECLARE_TEMPLATE_OBJECT(_obj, _type) \
|
---|
4 | json::Object _obj; \
|
---|
5 | makeTemplate(_obj, _type); \
|
---|
6 |
|
---|
7 | namespace 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::login(const std::string & pw) const
|
---|
47 | {
|
---|
48 | DECLARE_TEMPLATE_OBJECT(obj, "login");
|
---|
49 |
|
---|
50 | obj["pw"] = mID + "$" + pw;
|
---|
51 |
|
---|
52 | return json::Serialize(obj);
|
---|
53 | }
|
---|
54 |
|
---|
55 | std::string Request::chat(const std::string & to, const std::string & sessid, const std::string & message) const
|
---|
56 | {
|
---|
57 | DECLARE_TEMPLATE_OBJECT(obj, "chat");
|
---|
58 |
|
---|
59 | obj["from" ] = mID;
|
---|
60 | obj["to" ] = to;
|
---|
61 | obj["sess-id"] = sessid;
|
---|
62 | obj["message"] = message;
|
---|
63 |
|
---|
64 | return json::Serialize(obj);
|
---|
65 | }
|
---|
66 |
|
---|
67 | std::string Request::friendList() const
|
---|
68 | {
|
---|
69 | DECLARE_TEMPLATE_OBJECT(obj, "friend-list");
|
---|
70 |
|
---|
71 | return json::Serialize(obj);
|
---|
72 | }
|
---|
73 |
|
---|
74 | // response
|
---|
75 | void Response::makeTemplate(json::Object & obj, const std::string & type) const
|
---|
76 | {
|
---|
77 | obj["type"] = type;
|
---|
78 | }
|
---|
79 |
|
---|
80 | std::string Response::result(const bool status) const
|
---|
81 | {
|
---|
82 | DECLARE_TEMPLATE_OBJECT(obj, "result");
|
---|
83 |
|
---|
84 | obj["result"] = status;
|
---|
85 |
|
---|
86 | return json::Serialize(obj);
|
---|
87 | }
|
---|
88 | };
|
---|