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(ADD_FRIEND);
|
---|
35 | DECLARE_KEY(OPEN_SESSION);
|
---|
36 | DECLARE_KEY(TO);
|
---|
37 | DECLARE_KEY(CHAT);
|
---|
38 | DECLARE_KEY(FROM);
|
---|
39 | DECLARE_KEY(SESSION_ID);
|
---|
40 | DECLARE_KEY(MESSAGE);
|
---|
41 | DECLARE_KEY(SENSITIVE);
|
---|
42 | DECLARE_KEY(GET_FRIEND_LIST);
|
---|
43 | DECLARE_KEY(FRIEND_LIST);
|
---|
44 | DECLARE_KEY(ID_LIST);
|
---|
45 | DECLARE_KEY(RESULT);
|
---|
46 | }
|
---|
47 |
|
---|
48 | namespace Protocol
|
---|
49 | {
|
---|
50 | class Message
|
---|
51 | {
|
---|
52 | private:
|
---|
53 | json::Object mObject;
|
---|
54 |
|
---|
55 | public:
|
---|
56 | Message();
|
---|
57 |
|
---|
58 | inline void parse(const std::string & message);
|
---|
59 |
|
---|
60 | template<typename T>
|
---|
61 | inline T get(const std::string & key) const
|
---|
62 | {
|
---|
63 | return (T)mObject[key];
|
---|
64 | }
|
---|
65 |
|
---|
66 | template<typename T>
|
---|
67 | inline std::vector<T> getList(const std::string & key) const
|
---|
68 | {
|
---|
69 | std::vector<T> ret;
|
---|
70 | json::Array ar = mObject[key];
|
---|
71 |
|
---|
72 | for (size_t iter = 0; iter < ar.size(); iter++)
|
---|
73 | ret.push_back(ar[iter]);
|
---|
74 |
|
---|
75 | return ret;
|
---|
76 | }
|
---|
77 |
|
---|
78 | inline std::vector<SFriend> getFriendList() const;
|
---|
79 |
|
---|
80 | inline std::string type() const;
|
---|
81 | };
|
---|
82 |
|
---|
83 | class IProtocol
|
---|
84 | {
|
---|
85 | public:
|
---|
86 | virtual void makeTemplate(json::Object & obj, const std::string & type) const = 0;
|
---|
87 | };
|
---|
88 |
|
---|
89 | class Request : public IProtocol
|
---|
90 | {
|
---|
91 | private:
|
---|
92 | std::string mID;
|
---|
93 |
|
---|
94 | public:
|
---|
95 | inline void setUserID(const std::string & id);
|
---|
96 |
|
---|
97 | inline const std::string & getUserID();
|
---|
98 |
|
---|
99 | inline void makeTemplate(json::Object & obj, const std::string & type) const;
|
---|
100 |
|
---|
101 | inline std::string sms(const std::string & phone) const;
|
---|
102 |
|
---|
103 | inline std::string join(const std::string & id, const std::string & pw, const std::string & sms);
|
---|
104 |
|
---|
105 | inline std::string login(const std::string & pw) const;
|
---|
106 |
|
---|
107 | inline std::string addFriend(const std::string & id) const;
|
---|
108 |
|
---|
109 | inline std::string openSession(const std::string & to) const;
|
---|
110 |
|
---|
111 | inline std::string chat(const std::string & sessid, const std::string & message, const int sensitive) const;
|
---|
112 |
|
---|
113 | inline std::string getFriendList() const;
|
---|
114 | };
|
---|
115 |
|
---|
116 | class Response : public IProtocol
|
---|
117 | {
|
---|
118 | public:
|
---|
119 | inline void makeTemplate(json::Object & obj, const std::string & type) const;
|
---|
120 |
|
---|
121 | inline std::string result(const std::string & requestType, const bool status) const;
|
---|
122 |
|
---|
123 | inline std::string friendList(const std::vector<SFriend> & friendList) const;
|
---|
124 |
|
---|
125 | inline std::string openSession(const std::vector<std::string> & idList) const;
|
---|
126 | };
|
---|
127 | };
|
---|
128 |
|
---|
129 | // --------------------------------------------------------------
|
---|
130 |
|
---|
131 | #define LOG(message) log(__FILE__,__func__,__LINE__,message)
|
---|
132 |
|
---|
133 | static void log(const std::string & file,
|
---|
134 | const std::string & func,
|
---|
135 | const int line,
|
---|
136 | const std::string & message)
|
---|
137 | {
|
---|
138 | static cf::logger * logger = NULL;
|
---|
139 |
|
---|
140 | if (!logger)
|
---|
141 | {
|
---|
142 | logger = cf::logger::getInstance();
|
---|
143 | logger->init(".");
|
---|
144 | logger->setLevel(1);
|
---|
145 | logger->add("messenger", 1, "INFO", 0);
|
---|
146 | }
|
---|
147 |
|
---|
148 | logger->write(1, message);
|
---|
149 | std::cerr << "[" << file << ":" << line << "][" << func << "] "
|
---|
150 | << message << std::endl;
|
---|
151 | }
|
---|
152 |
|
---|
153 | static std::string joinStrings(const std::vector<std::string> & strings)
|
---|
154 | {
|
---|
155 | std::string concat = strings[0];
|
---|
156 |
|
---|
157 | for (size_t iter = 1; iter < strings.size(); iter++)
|
---|
158 | concat += DELIMITER + strings[iter];
|
---|
159 |
|
---|
160 | return concat;
|
---|
161 | }
|
---|