-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequest.java
More file actions
83 lines (80 loc) · 2.57 KB
/
Request.java
File metadata and controls
83 lines (80 loc) · 2.57 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
package login;
abstract public class Request extends ReProtocol{
public static class ReqMsg extends ReMsgBase{
public String name,psw;
public ReqMsg(Command cid,String UserName,String PassWord) {
super(RequestLen,cid);
this.name=UserName;
this.psw=PassWord;
}
}
protected String name,psw;
public Request(ReqMsg rmsg)throws RePException {
this(IntToCommand(rmsg.id),rmsg.name,rmsg.psw);
}
public Request(Command cmd,String username,String userpasswd) {
super(cmd);
this.name=username;
this.psw=userpasswd;
}
public ReqMsg getMsg(){
return new ReqMsg(this.type,name,psw);
}
/**
* get the msg for bytes array
* */
public static ReqMsg getMsg(byte[]data) throws RePException {
if(data.length!=ReProtocol.RequestLen){
throw new RePException("Wrong-format Request");
}else{
byte[] name=new byte[UserNameLen];
byte[] pass=new byte[UserPswLen];
System.arraycopy(data, HeaderLen, name, 0, UserNameLen); // copy a name
System.arraycopy(data, HeaderLen+UserNameLen, pass, 0, UserPswLen); // copy a psw
return new ReqMsg(IntToCommand(getValueFromMsg(data, MsgLen)),decodeFill(name),decodeFill(pass));
}
}
/**
* @return 0 for OK
* 1 : name too long
* 2 : psw too long
*/
public int dataValidate(){
if(name.length()>UserNameLen/2){
return 1;
}
if (psw.length()>UserPswLen/2){
return 2;
}
return 0;
}
/**
* @brief no length check here
* @return bytes of UTF8
* */
public byte[] nameFill(){
return fillBase(name, UserNameLen);
}
/**
* @brief no length check here
* @return bytes of UTF8
* */
public byte[] passWdFill(){
return fillBase(psw, UserPswLen);
}
@Override
public byte[] getBytes() throws RePException{
byte[] msg=new byte[this.totalLen];
switch (dataValidate()){
case 1->{throw new RePException("User Name is too long");}
case 2->{throw new RePException("User password is too long");}
default -> {
int offset=prefill(msg, type);
System.arraycopy(nameFill(), 0, msg, offset, UserNameLen);
offset+=20;
System.arraycopy(passWdFill(), 0, msg, offset, UserPswLen);
}
}
return msg;
}
}