source: chevmsgr/trunk/msg.hpp@ 8

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

클라 UI 쪼끔 수정
프로토콜 메시지 좀 수정
클라 메시지 핸들링 추가

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