-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequestClient.java
More file actions
129 lines (125 loc) · 4.53 KB
/
RequestClient.java
File metadata and controls
129 lines (125 loc) · 4.53 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
package login;
import java.io.*;
import java.net.Socket;
public class RequestClient implements AutoCloseable{
private Socket socket;
private Request.ReqMsg request=null;
private Response.ResMsg response=null;
public static void main(String[]args){
// UserInterface();
test(true, "James", "123");
test(false, "Jame", "123");
test(false, "Zhangsan", "123");
test(true, "Zhangsan", "123");
test(false, "Tom", "123");
test(false, "ALias", "123");
test(true, "ALias", "123");
}
public static boolean test(boolean login,String name,String psw){
RequestClient Client=new RequestClient();
Client.loadReq(login, name,psw);
try {
if(login)
System.out.println("登录结果:"+Client.request());
else System.out.println("注册结果:"+Client.request());
} catch (RePException e) {
return false;
}return true;
}
private static boolean UIcheck(String str){
return str.length() == 1 && str.charAt(0) < 3;
}
public static RequestClient UserInterface(){
String name=null,pass=null;
boolean login=false;
System.out.println("0 -- 登录\n1 -- 注册\n2 -- 退出\n输入后按回车继续");
BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
try {
String tmp=reader.readLine();
while(!UIcheck(tmp)){
System.out.println("选项不存在");
tmp=reader.readLine();
}
if(tmp.charAt(0)==2)System.exit(0);
login=tmp.charAt(0)==0;
name=reader.readLine();
while("".equals(name)){
System.out.println("姓名不能为空 请重新输入");
name=reader.readLine();
}
pass=reader.readLine();
while("".equals(pass)){
System.out.println("密码不能为空 请重新输入");
pass=reader.readLine();
}
return new RequestClient(login,name,pass);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public RequestClient(boolean login,String name,String passwd){
this("127.0.0.1",12345,login,name,passwd);
}
public RequestClient(String ipaddr, int port,boolean login,String name,String passwd){
try {
this.socket=new Socket(ipaddr,port);
loadReq(login,name,passwd);
} catch (IOException e) {
e.printStackTrace();
}
}
public RequestClient(){
this("127.0.0.1",ResponseServer.DefaultPort);
}
public RequestClient(String ipaddr, int port){
try {
this.socket=new Socket(ipaddr,port);
} catch (IOException e) {
// todo
e.printStackTrace();
}
}
public boolean loadReq(boolean login,String name,String passwd){
ReProtocol.Command cmd=login?
ReProtocol.Command.loginRequest: ReProtocol.Command.registrationRequest;
if(passwd.isEmpty())return false;
request= new Request.ReqMsg(cmd,name,passwd);
return true;
}
public String request()throws RePException{
if(socket==null)throw new RePException("socket is not connected");
try {
return send().recv();
// String rel=send().recv();
// System.out.println(response.statusCode);
// return rel;
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
protected RequestClient send() throws IOException, RePException {
DataOutputStream BOS=new DataOutputStream(socket.getOutputStream());
if(request==null)throw new IOException("uninitialized request");
Request req=null;
if(request.id==3){ // login
req=new LoginRequest(request.name, request.psw);
}else{
req=new RegistrationRequest(request.name, request.psw);
}
BOS.write(req.getBytes());
BOS.flush();
return this;
}
protected String recv() throws IOException, RePException {
DataInputStream DIS=new DataInputStream(socket.getInputStream());
response=Response.getMsg(ReProtocol.msgRead(DIS));
DIS.close();
return response.desc;
}
@Override
public void close() throws Exception {
socket.close();
}
}