-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResponse.java
More file actions
68 lines (66 loc) · 2.39 KB
/
Response.java
File metadata and controls
68 lines (66 loc) · 2.39 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
package login;
import java.nio.charset.StandardCharsets;
public class Response extends ReProtocol{
boolean status;
String desc;
static final String[] DESCRIPTION= {
"The User Name exists!", //fall_user_name_repeated
"The User Name doesn't exist!", //fall_user_name_no_exist
"Incorrect password or UserName", //fall_no_match
"Registration Successfully!", //succ_registration
"Login successfully!" //succ_login
};
// for desc index
static int
fall_user_name_repeated=0,
fall_user_name_no_exist=1,
fall_no_match=2,
succ_registration=3,
succ_login=4;
public static class ResMsg extends ReMsgBase{
int statusCode;
String desc;
public ResMsg(Command c,boolean status,String description) {
super(ResponseLen, c);
this.statusCode=status?1:0;
this.desc=description;
}
public ResMsg(Command c,boolean status,int index) {
this(c,status,DESCRIPTION[index]);
}
}
public Response(Command cmd,boolean successful,String description) {
super(cmd);
this.status=successful;
this.desc=description;
}
public Response(Command cmd,boolean successful,int index) {
this(cmd,successful,DESCRIPTION[index]);
}
public Response(ResMsg rmsg)throws RePException{
this(IntToCommand(rmsg.id),rmsg.statusCode==1, rmsg.desc);
}
public ResMsg getMsg(){
return new ResMsg(type,status, desc);
}
private byte[] descFill(){
return fillBase(desc, 64);
}
@Override
public byte[] getBytes() throws RePException{
byte[] msg=new byte[this.totalLen];
int offset=prefill(msg, this.type);
msg[offset++]=(byte)(status?1:0);
System.arraycopy(descFill(), 0, msg, offset, DescriptionLen);
return msg;
}
public static ResMsg getMsg(byte[] data) throws RePException {
if(data.length!=ReProtocol.ResponseLen){
throw new RePException("Wrong-format Response");
}else{
byte[]desc=new byte[DescriptionLen];
System.arraycopy(data, HeaderLen+StatusCodeLen, desc, 0, DescriptionLen);
return new ResMsg(IntToCommand(getValueFromMsg(data, MsgLen)),data[8]==1,decodeFill(desc));
}
}
}