-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.java
More file actions
137 lines (113 loc) · 4.21 KB
/
Client.java
File metadata and controls
137 lines (113 loc) · 4.21 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
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.ParseException;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.lang.Math.*;
import java.util.Random;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Client implements Runnable {
public static int uniqueId(int min, int max) {
Random rand = new Random();
int randomNum =rand.nextInt((max - min) + 1) + min;
return randomNum;
}
private List<JSONObject> history = new ArrayList<JSONObject>();
private MessageExchange messageExchange = new MessageExchange();
private String host;
private Integer port;
private String user;
public Client(String host, Integer port,String user) {
this.host = host;
this.port = port;
this.user = user;
}
public static void main(String[] args) {
if (args.length != 3)
System.out.println("Usage: java ChatClient host port");
else {
System.out.println("Connection to server...");
String serverHost = args[0];
Integer serverPort = Integer.parseInt(args[1]);
String username=args[2];
Client client = new Client(serverHost, serverPort,username);
new Thread(client).start();
System.out.println("Connected to server: " + serverHost + ":" + serverPort);
System.out.println("Your name is: " + username);
client.listen();
}
}
private HttpURLConnection getHttpURLConnection() throws IOException {
URL url = new URL("http://" + host + ":" + port + "/chat?token=" + messageExchange.getToken(history.size()));
return (HttpURLConnection) url.openConnection();
}
public List<JSONObject> getMessages() {
List<JSONObject> list = new ArrayList<JSONObject>();
HttpURLConnection connection = null;
try {
connection = getHttpURLConnection();
connection.connect();
String response = messageExchange.inputStreamToString(connection.getInputStream());
JSONObject jsonObject = messageExchange.getJSONObject(response);
JSONArray jsonArray = (JSONArray) jsonObject.get("messages");
for (Object o : jsonArray) {
System.out.println( ((JSONObject)o).get("user") + " : " + ((JSONObject)o).get("message"));
list.add((JSONObject)o);
}
} catch (IOException e) {
System.err.println("ERROR: " + e.getMessage());
} catch (ParseException e) {
System.err.println("ERROR: " + e.getMessage());
} finally {
if (connection != null) {
connection.disconnect();
}
}
return list;
}
public void sendMessage(String message) {
HttpURLConnection connection = null;
try {
connection = getHttpURLConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
byte[] bytes = messageExchange.getClientSendMessageRequest(this.user,message,uniqueId(1,1000)).getBytes();
wr.write(bytes, 0, bytes.length);
wr.flush();
wr.close();
connection.getInputStream();
} catch (IOException e) {
System.err.println("ERROR: " + e.getMessage());
} finally {
if (connection != null) {
connection.disconnect();
}
}
}
public void listen() {
while (true) {
List<JSONObject> list = getMessages();
if (list.size() > 0) {
history.addAll(list);
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.err.println("ERROR: " + e.getMessage());
}
}
}
@Override
public void run() {
Scanner scanner = new Scanner(System.in);
while (true) {
String message = scanner.nextLine();
sendMessage(message);
}
}
}