source: chevmsgr/trunk/msg.hpp@ 6

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

modify client interface and message protocol

File size: 2.6 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// --------------------------------------------------------------
18
19namespace ProtocolType
20{
21#define DECLARE_KEY(_t) static const std::string _t = #_t
22
23 DECLARE_KEY(ID);
24 DECLARE_KEY(TYPE);
25 DECLARE_KEY(SMS);
26 DECLARE_KEY(PHONE);
27 DECLARE_KEY(JOIN);
28 DECLARE_KEY(PW);
29 DECLARE_KEY(LOGIN);
30 DECLARE_KEY(ADD_FRIEND);
31 DECLARE_KEY(OPEN_SESSION);
32 DECLARE_KEY(TO);
33 DECLARE_KEY(CHAT);
34 DECLARE_KEY(FROM);
35 DECLARE_KEY(SESSION_ID);
36 DECLARE_KEY(MESSAGE);
37 DECLARE_KEY(SENSITIVE);
38 DECLARE_KEY(GET_FRIEND_LIST);
39 DECLARE_KEY(RESULT);
40}
41
42namespace Protocol
43{
44 class Message
45 {
46 private:
47 json::Object mObject;
48
49 public:
50 void parse(const std::string & message);
51
52 template<typename T>
53 inline T get(const std::string & key) const
54 {
55 return (T)mObject[key];
56 }
57
58 std::string type() const;
59 };
60
61 class IProtocol
62 {
63 public:
64 virtual void makeTemplate(json::Object & obj, const std::string & type) const = 0;
65 };
66
67 class Request : public IProtocol
68 {
69 private:
70 std::string mID;
71
72 public:
73 void setUserID(const std::string & id);
74
75 const std::string & getUserID();
76
77 void makeTemplate(json::Object & obj, const std::string & type) const;
78
79 std::string sms(const std::string & phone) const;
80
81 std::string join(const std::string & id, const std::string & pw, const std::string & sms);
82
83 std::string login(const std::string & pw) const;
84
85 std::string addFriend(const std::string & id) const;
86
87 std::string openSession(const std::string & to) const;
88
89 std::string chat(const std::string & sessid, const std::string & message, const int sensitive) const;
90
91 std::string getFriendList() const;
92 };
93
94 class Response : public IProtocol
95 {
96 public:
97 void makeTemplate(json::Object & obj, const std::string & type) const;
98
99 std::string result(const std::string & requestType, const bool status) const;
100
101 std::string friendList(const std::vector<SFriend> & friendList) const;
102
103 std::string openSession(const std::vector<std::string> & idList) const;
104 };
105};
106
107// --------------------------------------------------------------
108
109#define LOG(message) log(__FILE__,__func__,__LINE__,message)
110
111static void log(const std::string & file,
112 const std::string & func,
113 const int line,
114 const std::string & message)
115{
116 static cf::logger * logger = NULL;
117
118 if (!logger)
119 {
120 logger = cf::logger::getInstance();
121 logger->init(".");
122 logger->setLevel(1);
123 logger->add("messenger", 1, "INFO", 0);
124 }
125
126 logger->write(1, message);
127 std::cerr << "[" << file << ":" << line << "][" << func << "] "
128 << message << std::endl;
129}
Note: See TracBrowser for help on using the repository browser.