1 | #pragma once
|
---|
2 |
|
---|
3 | #include "cf/logger.h"
|
---|
4 |
|
---|
5 | #include "json.h"
|
---|
6 |
|
---|
7 | #include <iostream>
|
---|
8 | #include <string>
|
---|
9 | #include <vector>
|
---|
10 |
|
---|
11 | typedef struct SFriend
|
---|
12 | {
|
---|
13 | std::string id;
|
---|
14 | std::string name;
|
---|
15 | } SFriend;
|
---|
16 |
|
---|
17 | #define DELIMITER "$"
|
---|
18 |
|
---|
19 | // --------------------------------------------------------------
|
---|
20 |
|
---|
21 | namespace ProtocolType
|
---|
22 | {
|
---|
23 | #define DECLARE_KEY(_t) static const std::string _t = #_t
|
---|
24 |
|
---|
25 | DECLARE_KEY(NONE);
|
---|
26 | DECLARE_KEY(ID);
|
---|
27 | DECLARE_KEY(TYPE);
|
---|
28 | DECLARE_KEY(NAME);
|
---|
29 | DECLARE_KEY(SMS);
|
---|
30 | DECLARE_KEY(PHONE);
|
---|
31 | DECLARE_KEY(JOIN);
|
---|
32 | DECLARE_KEY(PW);
|
---|
33 | DECLARE_KEY(LOGIN);
|
---|
34 | DECLARE_KEY(LOGOUT);
|
---|
35 | DECLARE_KEY(ADD_FRIEND);
|
---|
36 | DECLARE_KEY(OPEN_SESSION);
|
---|
37 | DECLARE_KEY(TO);
|
---|
38 | DECLARE_KEY(LISTEN);
|
---|
39 | DECLARE_KEY(TELL);
|
---|
40 | DECLARE_KEY(FROM);
|
---|
41 | DECLARE_KEY(SESSION_ID);
|
---|
42 | DECLARE_KEY(MESSAGE);
|
---|
43 | DECLARE_KEY(SENSITIVE);
|
---|
44 | DECLARE_KEY(GET_FRIEND_LIST);
|
---|
45 | DECLARE_KEY(FRIEND_LIST);
|
---|
46 | DECLARE_KEY(ID_LIST);
|
---|
47 | DECLARE_KEY(RESULT);
|
---|
48 | }
|
---|
49 |
|
---|
50 | namespace Protocol
|
---|
51 | {
|
---|
52 | class Message
|
---|
53 | {
|
---|
54 | public:
|
---|
55 | json::Object mObject;
|
---|
56 |
|
---|
57 | Message();
|
---|
58 |
|
---|
59 | void parse(const std::string & message);
|
---|
60 |
|
---|
61 | template<typename T>
|
---|
62 | T get(const std::string & key) const
|
---|
63 | {
|
---|
64 | return (T)mObject[key];
|
---|
65 | }
|
---|
66 |
|
---|
67 | template<typename T>
|
---|
68 | std::vector<T> getList(const std::string & key) const
|
---|
69 | {
|
---|
70 | std::vector<T> ret;
|
---|
71 | json::Array ar = mObject[key];
|
---|
72 |
|
---|
73 | for (size_t iter = 0; iter < ar.size(); iter++)
|
---|
74 | ret.push_back(ar[iter]);
|
---|
75 |
|
---|
76 | return ret;
|
---|
77 | }
|
---|
78 |
|
---|
79 | std::vector<SFriend> getFriendList() const;
|
---|
80 |
|
---|
81 | std::string type() const;
|
---|
82 |
|
---|
83 | std::string serialize() const;
|
---|
84 | };
|
---|
85 |
|
---|
86 | class IProtocol
|
---|
87 | {
|
---|
88 | public:
|
---|
89 | virtual void makeTemplate(json::Object & obj, const std::string & type) const = 0;
|
---|
90 | };
|
---|
91 |
|
---|
92 | class Request : public IProtocol
|
---|
93 | {
|
---|
94 | private:
|
---|
95 | std::string mID;
|
---|
96 |
|
---|
97 | public:
|
---|
98 | void setUserID(const std::string & id);
|
---|
99 |
|
---|
100 | const std::string & getUserID();
|
---|
101 |
|
---|
102 | void makeTemplate(json::Object & obj, const std::string & type) const;
|
---|
103 |
|
---|
104 | std::string sms(const std::string & phone) const;
|
---|
105 |
|
---|
106 | std::string join(const std::string & id, const std::string & pw, const std::string & sms);
|
---|
107 |
|
---|
108 | std::string login(const std::string & pw) const;
|
---|
109 |
|
---|
110 | std::string logout() const;
|
---|
111 |
|
---|
112 | std::string addFriend(const std::string & id) const;
|
---|
113 |
|
---|
114 | std::string openSession(const std::vector<std::string> & idList) const;
|
---|
115 |
|
---|
116 | std::string tell(const std::string & sessid, const std::string & message, const int sensitive) const;
|
---|
117 |
|
---|
118 | std::string getFriendList() const;
|
---|
119 | };
|
---|
120 |
|
---|
121 | class Response : public IProtocol
|
---|
122 | {
|
---|
123 | public:
|
---|
124 | void makeTemplate(json::Object & obj, const std::string & type) const;
|
---|
125 |
|
---|
126 | std::string result(const std::string & requestType, const bool status) const;
|
---|
127 |
|
---|
128 | std::string friendList(const std::vector<SFriend> & friendList) const;
|
---|
129 | };
|
---|
130 | };
|
---|
131 |
|
---|
132 | // --------------------------------------------------------------
|
---|
133 |
|
---|
134 | #define LOG(message) log(__FILE__,__func__,__LINE__,message)
|
---|
135 |
|
---|
136 | static void log(const std::string & file,
|
---|
137 | const std::string & func,
|
---|
138 | const int line,
|
---|
139 | const std::string & message)
|
---|
140 | {
|
---|
141 | static cf::logger * logger = NULL;
|
---|
142 |
|
---|
143 | if (!logger)
|
---|
144 | {
|
---|
145 | logger = cf::logger::getInstance();
|
---|
146 | logger->init(".");
|
---|
147 | logger->setLevel(1);
|
---|
148 | logger->add("messenger", 1, "INFO", 0);
|
---|
149 | }
|
---|
150 |
|
---|
151 | logger->write(1, message);
|
---|
152 | std::cerr << "[" << file << ":" << line << "][" << func << "] "
|
---|
153 | << message << std::endl;
|
---|
154 | }
|
---|
155 |
|
---|
156 | static std::string joinStrings(const std::vector<std::string> & strings)
|
---|
157 | {
|
---|
158 | std::string concat = strings[0];
|
---|
159 |
|
---|
160 | for (size_t iter = 1; iter < strings.size(); iter++)
|
---|
161 | concat += "," + strings[iter];
|
---|
162 |
|
---|
163 | return concat;
|
---|
164 | }
|
---|