-
Notifications
You must be signed in to change notification settings - Fork 1
/
Server.java
233 lines (204 loc) · 6.88 KB
/
Server.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
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.BindException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import com.ukpatel.chatly.Utils;
import com.ukpatel.chatly.FileSending;
import com.ukpatel.chatly.Message;
/**
* @author Urveshkumar Patel
*/
public class Server implements Runnable {
private static final ArrayList<ObjectOutputStream> clientsOutputStreams = new ArrayList<>();
private static final ArrayList<ObjectInputStream> clientsInputStreams = new ArrayList<>();
public static final String SERVER_DATA_PARENT_DIRECTORY = "Chatly_Server_Data";
public static final String SERVER_FILE_EXTENSION = "chatly.filedata";
private final ExecutorService executorService = Executors.newSingleThreadExecutor();
private static int PORT;
private static String HOST_NAME;
private static String HOST_ADDRESS;
private Socket socket;
private Thread readerThread;
private String clientName;
private ObjectOutputStream writer;
private ObjectInputStream reader;
public Server(Socket socket) {
this.socket = socket;
readerThread = new Thread(this);
try {
this.writer = new ObjectOutputStream(socket.getOutputStream());
this.reader = new ObjectInputStream(socket.getInputStream());
} catch (IOException e) {
}
readerThread.start();
}
public void run() {
try {
clientsOutputStreams.add(writer);
clientsInputStreams.add(reader);
Message userInfo = (Message) reader.readObject();
clientName = userInfo.getAuthor();
String ipAddress = userInfo.getMessage();
sendOtherClients(new Message(clientName, Message.USER_JOIN, clientName + " is join the chat."));
System.out.println(clientName + " at " + ipAddress + " is join the chat.");
Message message;
while (true) {
message = (Message) reader.readObject();
switch (message.getMessageType()) {
case Message.USER_EXIT:
throw new Exception();
case Message.FILE_INFO_SEND:
fileSendMsg = message;
fileInfoSendAction(message);
break;
case Message.FILE_SENDING:
fileSendingAction(message);
break;
case Message.FILE_SENT:
fileSentAction(fileSendMsg);
break;
case Message.FILE_INFO_RECEIVE:
fileInfoReceiveAction(message);
break;
case Message.MESSAGE_SEND:
System.out.println(clientName + " : " + message.getMessage());
sendOtherClients(new Message(clientName, Message.MESSAGE_RECEIVE, message.getMessage()));
break;
default:
System.out.println(message + " " + message.getMessageType());
}
}
} catch (Exception e) {
System.out.println(clientName + " left the chat.");
sendOtherClients(new Message("Server", Message.USER_EXIT, clientName + " left the chat."));
} finally {
try {
socket.close();
} catch (Exception e) {
}
clientsOutputStreams.remove(writer);
clientsInputStreams.remove(reader);
}
}
private BufferedOutputStream fileOut = null;
private Message fileSendMsg = null;
private String getServerFileName(Message message) {
String time = Message.FILE_INFO_SEND == message.getMessageType() ? message.getTime() : message.getMessage();
String fileName = String.format("%s_%s_%s.%s", message.getFile().getName(), message.getAuthor(),
time.replace(":", ""), SERVER_FILE_EXTENSION);
return fileName;
}
private void fileInfoSendAction(Message message) {
try {
String fileName = getServerFileName(message);
File file = new File(SERVER_DATA_PARENT_DIRECTORY, fileName);
fileOut = new BufferedOutputStream(new FileOutputStream(file));
} catch (Exception e) {
e.printStackTrace();
}
}
private void fileSendingAction(Message message) {
try {
fileOut.write(Utils.getPrimitiveArray(message.getData(), message.getByteRead()), 0, message.getByteRead());
fileOut.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
private void fileSentAction(Message message) {
// Closing the fileOut
if (fileOut != null) {
try {
fileOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println(String.format("%s sent the %s file.", message.getAuthor(), message.getFile().getName()));
// Sending other client to file info.
Message msg = new Message(message.getAuthor(), Message.FILE_INFO, message.getMessage());
msg.setFile(message.getFile());
msg.setTime(message.getTime());
sendOtherClients(msg);
}
private void fileInfoReceiveAction(Message message) {
File file = new File(SERVER_DATA_PARENT_DIRECTORY, getServerFileName(message));
Message msgSend = new Message(message.getAuthor(), Message.FILE_INFO_RECEIVE, "" + message.getFile().length());
msgSend.setFile(message.getFile());
executorService.execute(new FileSending(msgSend, file, writer));
}
private void sendOtherClients(Message message) {
try {
for (ObjectOutputStream clientWriter : clientsOutputStreams) {
if (clientWriter == writer) {
continue;
}
try {
clientWriter.writeObject(message);
clientWriter.flush();
} catch (SocketException e) {
}
}
} catch (Exception e) {
}
}
public static void main(String arg[]) {
ServerSocket ss = null;
try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) {
HOST_NAME = InetAddress.getLocalHost().getHostName();
HOST_ADDRESS = InetAddress.getLocalHost().getHostAddress();
// Initializing Server Data Directory.
File directory = new File(SERVER_DATA_PARENT_DIRECTORY);
if (directory.exists()) {
deletePreviousFiles(directory);
} else {
directory.mkdir();
}
System.out.print("Port For Server (Default 54321) : ");
try {
PORT = Integer.parseInt(reader.readLine());
} catch (NumberFormatException e) {
System.out.println("\nPort No. you enter is invalid.\nServer is started on default port no 54321.");
PORT = 54321;
}
ss = new ServerSocket(PORT);
System.out.println(String.format("\nServer HOST NAME : %s", HOST_NAME));
System.out.println(String.format("Server HOST ADDRESS : %s", HOST_ADDRESS));
System.out.println(String.format("Server is started on Port No.: %d\n", PORT));
while (true) {
Socket socket = ss.accept();
new Server(socket);
}
} catch (BindException e) {
System.out.println("Port is already inuse. Please choose another.");
} catch (Exception e) {
System.out.println("Something is wrong.");
} finally {
if (ss != null) {
try {
ss.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
}
private static void deletePreviousFiles(File directory) {
File[] files = directory.listFiles();
for (File file : files) {
file.delete();
}
}
}