-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.java
276 lines (214 loc) · 7.97 KB
/
Client.java
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package com.assignments.chat;
import java.io.*;
import java.net.Socket;
import java.nio.file.Files;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author chggtak003 nglbri001 mrkane001
*/
class Client {
private Socket socket;
private PrintStream output;
private final ClientInterface clientInterface;
private final String ip;
private String username;
private String tag;
private String fileRequest;
private ArrayList<String> serverFiles;
Client(ClientInterface clientUI, String ip, String name) {
this.ip =ip;
int PortNumber = 1024; //ports 0-1023 is reserved
clientInterface=clientUI;
username=name;
try
{
socket = new Socket(ip, PortNumber);//socket
ServerListener serverListener = new ServerListener(
new BufferedReader(new InputStreamReader(socket.getInputStream())),this);
serverListener.start();
output= new PrintStream(socket.getOutputStream());//output stream
tag=output.toString();
} catch (IOException ex)
{
System.out.println("Something went wrong with creating the CLIENT");
System.out.println(ex);
}
this.sendCommunication("NEW_USER",username);
}
/**
*
* @param tag tag number provided server to uniquely identify client
*/
void setTag(String tag){
this.tag =tag;
}
/**
*
* @param message update the UI with the received message
*/
void updateChat(String message) {
System.out.println(message);
String sentTag=message.substring(0,message.indexOf(" "));
String senderName=message.substring(message.indexOf(" ")+1,message.indexOf(":")+1);
message=message.substring(message.indexOf(":")+2);
if(sentTag.equals(tag))clientInterface.updateChat("ME:"+" "+message);
else if(!sentTag.equals(tag))clientInterface.updateChat(senderName+" "+message);
}
/**
*
* @param fileName the name of the file (as saved on the server) to be downloaded
*/
void showFileRequestDialog(String fileName){
if(serverFiles==null)serverFiles=new ArrayList();
serverFiles.add(fileName);
clientInterface.fileDialog();
}
/**
*
* @param message message to be sent to the server
*/
void sendMessage(String message){
output.println(tag+" "+username+": "+message);
}
private void sendCommunication(String instruction, String communication){
output.println("CLIENT_COMMUNICATION "+instruction+" "+communication);
}
/**
*
* @param stringList a space separated list of users on the server;
*/
void onlineUsers(String stringList){
stringList=stringList.substring(stringList.indexOf(" ")+1);
stringList=stringList.replaceFirst(username,"(Me)");
clientInterface.updateOnlineUsers(stringList);
}
/**
* detach from the server
*/
public void disconnect(){
sendCommunication("DISCONNECT",username);
try {
socket.close();
} catch (IOException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
}
/**
* Send file to server
* @param fileName name of file to be sent
*/
void transmitFile(String fileName){
File file = new File(fileName);
DataOutputStream os;
try {
byte[] content = Files.readAllBytes(file.toPath());
os = new DataOutputStream(output);
os.writeInt(content.length);
os.write(content);
System.out.println("sent "+fileName);
} catch (IOException ex) {
ex.printStackTrace();
}
}
/**
* Convenience method for GUI. Starts file sending process by communicating with server
* @param filePath absolute path of file to be sent
*/
void sendFile(String filePath){
sendCommunication("FILE",filePath);
}
/**
* Convenience method for GUI. Starts file receiving process by communicating with server
*/
void getFileFromServer(){
sendCommunication("ACCEPT",serverFiles.get(serverFiles.size()-1));
}
/**
* Get last file that was sent to the server;
*/
void receiveFile(){
SimpleDateFormat ft = new SimpleDateFormat ("'client_'YYMMdd_HHmm");
fileRequest=serverFiles.get(serverFiles.size()-1);
String fileName= ft.format(new Date());
try {
DataInputStream dis = new DataInputStream(socket.getInputStream());
int length = dis.readInt();
byte[] ba = new byte[length];
for(int i = 0; i<length; i++){
ba[i] = dis.readByte();
}
FileOutputStream fileout = new FileOutputStream(fileName);
fileout.write(ba);
fileout.close();
} catch (IOException ex) {
ex.printStackTrace();
}
clientInterface.successDialog(fileRequest);
}
/**
* Objects of this class 'listen' for incoming communication from Server and respond accordingly
*/
public static class ServerListener extends Thread
{
private final BufferedReader input ;
private final Client client;
enum ComType{UPDATE_USERS,TAG,RECEIVE,NONE,TEST,SENDING}
ServerListener(BufferedReader dataInputStream, Client aclient)
{
input=dataInputStream;
client = aclient;
}
public void run(){
while(true){
try
{
if(input.ready()){
String r = ""+input.readLine();
switch(parseCommunication(r)){
case TAG:
client.setTag(r.substring(r.lastIndexOf(" ")+1));
break;
case UPDATE_USERS:
client.onlineUsers(r.substring(r.indexOf(" ")+1));
break;
case RECEIVE:
client.showFileRequestDialog(r.substring(r.lastIndexOf(" ")+1));
break;
case SENDING:
client.receiveFile();
break;
case TEST:
client.transmitFile(r.substring(r.lastIndexOf(" ")+1));
case NONE:
client.updateChat(r);
break;
}
}
} catch (IOException ex)
{
System.out.println(ex);
Logger.getLogger(ClientInterface.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
/**
* Determines the type of communication received from the server
* @param message the communication
* @return the <code>ComType</code> of the message
*/
private ComType parseCommunication(String message){
String[] breakdown= message.split(" ");
if(!breakdown[0].equals("SERVER_COMMUNICATION"))return ComType.NONE;
try{
return ComType.valueOf(breakdown[1]);
}catch (IllegalArgumentException ex){
return ComType.NONE;
}
}
}
}