source: chevmsgr/trunk/msg.hpp@ 12

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

add dialogs

File size: 3.3 KB
Line 
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
11typedef struct SFriend
12{
13 std::string id;
14 std::string name;
15} SFriend;
16
17#define DELIMITER "$"
18
19// --------------------------------------------------------------
20
21namespace 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
48namespace Protocol
49{
50 class Message
51 {
52 private:
53 json::Object mObject;
54
55 public:
56 Message();
57
58 void parse(const std::string & message);
59
60 template<typename T>
61 T get(const std::string & key) const
62 {
63 return (T)mObject[key];
64 }
65
66 template<typename T>
67 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 std::vector<SFriend> getFriendList() const;
79
80 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 void setUserID(const std::string & id);
96
97 const std::string & getUserID();
98
99 void makeTemplate(json::Object & obj, const std::string & type) const;
100
101 std::string sms(const std::string & phone) const;
102
103 std::string join(const std::string & id, const std::string & pw, const std::string & sms);
104
105 std::string login(const std::string & pw) const;
106
107 std::string addFriend(const std::string & id) const;
108
109 std::string openSession(const std::string & to) const;
110
111 std::string chat(const std::string & sessid, const std::string & message, const int sensitive) const;
112
113 std::string getFriendList() const;
114 };
115
116 class Response : public IProtocol
117 {
118 public:
119 void makeTemplate(json::Object & obj, const std::string & type) const;
120
121 std::string result(const std::string & requestType, const bool status) const;
122
123 std::string friendList(const std::vector<SFriend> & friendList) const;
124
125 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
133static 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
153static 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 += strings[iter];
159
160 return concat;
161}
Note: See TracBrowser for help on using the repository browser.