/
QuarkChatProtocol.java
142 lines (124 loc) · 3.44 KB
/
QuarkChatProtocol.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package com.quark.chat.protocol;
import com.alibaba.fastjson.JSONObject;
import com.quark.chat.utils.DateTimeUtil;
import com.quark.common.entity.User;
import java.util.HashMap;
import java.util.Set;
import static com.quark.chat.protocol.QuarkChatType.*;
/**
* @Author : ChinaLHR
* @Date : Create in 11:20 2017/10/22
* @Email : 13435500980@163.com
*
* QuarkChatProtocol
* __ __ __ __ __ __ __ __ __ __ __ ____ __ __ ____ __
* | | | |
* 4 1 Uncertainty
* |__ __ __ __|__ __ __ __|__ __ __ __ __ __ __ __ __|
* | | | |
* Magic Type Body
*|__ __ __ __|__ __ __ __|__ __ __ __ __ __ __ __ __|
*
*
* auth:result
* SysyMsg:msg
* ServerMsg:uid(userid) name icon time(create time)
* UserInfo:count,Set<userinfo>
*/
public class QuarkChatProtocol {
/**
* Maginc
*/
private static final int MAGIC = 0xabcd;
/**
* Type
*/
private byte type;
/**
* body
*/
private HashMap<String,Object> body;
public QuarkChatProtocol(byte type) {
this.type = type;
}
/**
* Auth(server)
* @param result
* @return
*/
public static String buildAuthProto(boolean result){
HashMap<String, Object> map = new HashMap<>();
map.put("result", result);
return buildProto(AUTH_RESPONSE_CODE,map);
}
/**
* SysMessage
* @param sysmsg
* @return
*/
public static String buildSysMessage(String sysmsg){
HashMap<String, Object> map = new HashMap<>();
map.put("msg",sysmsg);
return buildProto(SYS_MESSAGE_CODE,map);
}
/**
* SysUserInfo
* @param users
* @return
*/
public static String buildSysUserInfo(Set<User> users){
HashMap<String, Object> map = new HashMap<>();
map.put("count",users.size());
map.put("userinfo",users);
return buildProto(SYS_USERSINFO_CODE,map);
}
/**
* Ping
* @return
*/
public static String buildPing(){
return buildProto(PING_CODE);
}
/**
* Message(Server)
* @return
*/
public static String buildMessageCode(User user,String msg){
HashMap<String, Object> map = new HashMap<>();
map.put("uid",user.getId());
map.put("name",user.getUsername());
map.put("icon",user.getIcon());
map.put("time", DateTimeUtil.getCurrentDateTime());
map.put("msg",msg);
return buildProto(MESSAGE_RESPONSE_CODE,map);
}
/**
* Proto生成
* @param type
* @return
*/
private static String buildProto(byte type){
QuarkChatProtocol protocol = new QuarkChatProtocol(type);
return JSONObject.toJSONString(protocol);
}
private static String buildProto(byte type,HashMap<String,Object> map){
QuarkChatProtocol protocol = new QuarkChatProtocol(type);
protocol.body = map;
return JSONObject.toJSONString(protocol);
}
public static int getMAGIC() {
return MAGIC;
}
public byte getType() {
return type;
}
public void setType(byte type) {
this.type = type;
}
public HashMap<String, Object> getBody() {
return body;
}
public void setBody(HashMap<String, Object> body) {
this.body = body;
}
}