-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.java
More file actions
executable file
·201 lines (171 loc) · 5.52 KB
/
Copy pathClient.java
File metadata and controls
executable file
·201 lines (171 loc) · 5.52 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
192
193
194
195
196
197
198
199
200
201
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Arrays;
class Client {
// Set username once authenticated so we can reuse it.
public static String username ;
// The result array first element always return an indicator whether the request
// is successfully reply with a result or not
// Login request from user
public static boolean login(String userName, String password) {
String[] result = send("LOGIN", new String[] { userName, password });
if (result[0].equals("Success")) {
return true;
}
return false;
}
// Sign up request from user
public static boolean signup(String username, String email, String password, String firstname, String lastname) {
String[] result = send("SIGNUP", new String[] { username, email,password, firstname, lastname });
if (result[0].equals("Success")) {
return true;
}
return false;
}
// Request that get the time period for every single difficult level
public static boolean time_period(String difficulty)
{
String[] result = send("TIME_PERIOD", new String[] { difficulty});
if (result[0].equals("Success")) {
return true;
}
return false;
}
// Request that send images to the server
public static boolean imagesend(String userID,String word_id,String difficulty,String image_data)
{
String[] result = send("IMAGESEND", new String[] { userID, word_id, difficulty,image_data});
if (result[0].equals("Success")) {
return true;
}
return false;
}
// Request that insert correct guess to the database
public static boolean insertGuess(String ... args)
{
String[] result = send("INSERT_GUESS", args);
if (result[0].equals("Success")) {
return true;
}
return false;
}
// Request that update points for correct guess
public static boolean updatePoint(String userID, String diffLevel,String drawerID, String drawingID) {
String[] result = send("UPDATE_POINT", new String[] {userID,diffLevel,drawerID,drawingID});
if (result[0].equals("Success")) {
return true;
}
return false;
}
// Request that insert guesses no matter it right or not
public static boolean insertCorrectGuess(String userID, String drawingID) {
String[] result = send("INSERT_CORRECT_GUESS", new String[] {userID,drawingID});
if (result[0].equals("Success")) {
return true;
}
return false;
}
// Request that get general information from the users
public static String [] getNeededInfor(String info,String... args) {
String[] result =send(info, args);
return result;
}
// Method that in charge of sending user request package to the server for processing
public static String[] send(String type, String[] Data) {
clientConnection myConn = new clientConnection("localhost");
// Send the request
myConn.sendData(type, Data);
myConn.receiveData();
if (myConn.getType().equals(type)) {
System.out.println("FROM SERVER:" + myConn.getData()[0]); // prints success/fail
} else {
System.out.println("ERROR FROM SERVER: " + myConn.getData()[0]);
}
myConn.closeConnection();
return myConn.getData();
}
}
// Class that handle connection
class clientConnection {
InetAddress IPAddress = null;
DatagramSocket clientSocket = null;
String rawData = null;
String mType = null;
String[] mData = null;
public clientConnection(String Name) {
try {
clientSocket = new DatagramSocket();
} catch (SocketException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
try {
IPAddress = InetAddress.getByName(Name);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// Method concatenate request type and the data array
public void sendData(String Type, String[] data) {
sendrawData(Type + "|" + Arrays.toString(data));
}
// Method that send the package to the server
public void sendrawData(String request) {
byte[] sendData = new byte[100000];
sendData = request.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
try {
clientSocket.send(sendPacket);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// Method that handle receive the package from the server
public String receiveData() {
byte[] receiveData = new byte[100000];
String databasePackage;
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
try {
clientSocket.receive(receivePacket);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
databasePackage = new String(receivePacket.getData());
rawData = databasePackage;
parseData();
return databasePackage;
}
// Type getter
public String getType() {
return mType;
}
// data setter
public String[] getData() {
return mData;
}
// split the package that include request type and data itself
public void parseData() {
String[] f = rawData.trim().split("\\|");
mType = f[0];
mData = pData(f[1]);
}
// Process data
public String[] pData(String x) {
// remove first character [
String m = x.substring(1);
// remove last character ]
m = m.substring(0, m.length() - 1);
return m.split(", ");
}
// Close connection
public void closeConnection() {
clientSocket.close();
}
}