-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReProtocol.java
More file actions
191 lines (188 loc) · 6 KB
/
ReProtocol.java
File metadata and controls
191 lines (188 loc) · 6 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package login;
import java.io.DataInputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
abstract public class ReProtocol {
/** length
* Header and each parts' length bytesHeader and each parts' length
* bytes
*/
static public final Integer
HeaderLen=8,
MsgLen=4,
CmdLen=4,
StatusCodeLen=1,
DescriptionLen=64,
UserNameLen=20,
UserPswLen=30,
RequestLen=58,
ResponseLen=73
;
static public final byte[]
BytesRReq={0,0,0,1},
BytesRRes={0,0,0,2},
BytesLReq={0,0,0,3},
BytesLRes={0,0,0,4},
BytesReqHd={0,0,0,58}, // Request Header Length
BytesResHd={0,0,0,73} // Response Header Length
;
static public final String
LRequest=RequestString(true,true),
RRequest=RequestString(false,true),
LResponse=RequestString(true,false),
RResponse=RequestString(false,false)
;
/**
* @return Octet BytesString without inner filling ZERO
* */
static public String RequestString(boolean login,boolean request){
char zero=0,len=0,id=0;
int rd= request?58:73;
int idd=request?1:2;
if(login)idd+=2;
// assign part
len= (char) rd;
id= (char) idd;
return String.valueOf(zero)+len+zero+id;
}
public enum Command {
registrationRequest,
registrationResponse,
loginRequest,
loginResponse
}
public static class ReMsgBase{
public int len,id;
public ReMsgBase(int l,int cid){
this.len=l;
this.id=cid;
}
public ReMsgBase(int l,Command c){
this.len=l;
this.id=CommandToInt(c);
}
}
public static int CommandToInt(Command c){
return switch (c){
case registrationRequest -> 1;
case registrationResponse -> 2;
case loginRequest -> 3;
case loginResponse -> 4;
};
}
public static Command IntToCommand(int value)throws RePException{
return switch (value){
case 1->Command.registrationRequest;
case 2->Command.registrationResponse;
case 3->Command.loginRequest;
case 4->Command.loginResponse;
default -> throw new RePException("Command not found!");
};
}
protected Command type;
protected Integer totalLen;
public ReProtocol(Command cmd){
this.type=cmd;
switch (cmd){
case loginRequest,registrationRequest ->
{totalLen=RequestLen;}
case loginResponse,registrationResponse ->
{totalLen=ResponseLen;}
}
}
/**
* Extracting Value from msg header
* @param beg only two value available
* 0 for length
* 4 for commandId
* both are 4 bytes long
* @param data expected 8 bytes long
* a standard header length
* */
static public int getValueFromMsg(byte[]data,int beg){
int value=0;
for (int i = 0; i < CmdLen; i++) {
value+=(((int)data[i+beg])<<((7-i)*8));
}
return value;
}
/**
* message read through DataInputStream by tcp socket
* @param DataIn the wrapper of socket input stream
* @return return the bytes array of msg
* */
static public byte[] msgRead(DataInputStream DataIn) throws IOException {
byte[] re=new byte[HeaderLen];
int len,sum=0;
do{ // receive header
len = DataIn.read(re,sum,re.length-sum);
sum+=len;
}while(sum!=re.length);
byte[] wholeRe=new byte[getValueFromMsg(re, 0)];
System.arraycopy(re, 0, wholeRe, 0, sum);
do {
len = DataIn.read(wholeRe, sum, wholeRe.length-sum);
sum+=len;
}while (sum!=wholeRe.length);
return wholeRe;
}
abstract public byte[] getBytes() throws RePException;
public int prefill(byte[] msg,Command cmd){
int offset=0;
System.arraycopy(ReProtocol.BytesLReq, 0, msg, offset, 3);
offset+=4;
System.arraycopy(ReProtocol.BytesReqHd, 0, msg, offset, 4);
switch (cmd){
case loginResponse -> {
msg[MsgLen-1]=ResponseLen.byteValue();
msg[HeaderLen-1]=4;
}
case loginRequest -> {
msg[MsgLen-1]=RequestLen.byteValue();
msg[HeaderLen-1]=3;
}
case registrationResponse -> {
msg[MsgLen-1]=ResponseLen.byteValue();
msg[HeaderLen-1]=2;
}
case registrationRequest -> {
msg[MsgLen-1]=RequestLen.byteValue();
msg[HeaderLen-1]=1;
}
}
return HeaderLen;
}
/**
* bytes fill before send
* */
protected byte[] fillBase(String field,int len){
byte[] msg=new byte[len];
byte[] Bin=field.getBytes(StandardCharsets.UTF_8);
for (int i = 0; i < len; ++i) {
if(i<Bin.length)
msg[i]=Bin[i];
else msg[i]=0;
}return msg;
}
protected static byte[] decodeBase(byte[]filled){
int newlen=0;
byte[] fit=null;
if(filled[filled.length-1]==0) {
for (; filled[newlen] != 0; ++newlen) ;
fit=new byte[newlen];
System.arraycopy(filled, 0, fit, 0, newlen);
}
else fit=filled;
return fit;
}
protected static String decode(byte[]raw, Charset set){
return new String(raw,set);
}
protected static String decode(byte[]raw){
return new String(raw,StandardCharsets.UTF_8);
}
protected static String decodeFill(byte[]fill){
return decode(decodeBase(fill));
}
}