-
Notifications
You must be signed in to change notification settings - Fork 36
/
Server.java
244 lines (210 loc) · 8.07 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
234
235
236
237
238
239
240
241
242
243
244
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.InputStream;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.awt.Color;
public class Server {
private int port;
private List<User> clients;
private ServerSocket server;
public static void main(String[] args) throws IOException {
new Server(12345).run();
}
public Server(int port) {
this.port = port;
this.clients = new ArrayList<User>();
}
public void run() throws IOException {
server = new ServerSocket(port) {
protected void finalize() throws IOException {
this.close();
}
};
System.out.println("Port 12345 is now open.");
while (true) {
// accepts a new client
Socket client = server.accept();
// get nickname of newUser
String nickname = (new Scanner ( client.getInputStream() )).nextLine();
nickname = nickname.replace(",", ""); // ',' use for serialisation
nickname = nickname.replace(" ", "_");
System.out.println("New Client: \"" + nickname + "\"\n\t Host:" + client.getInetAddress().getHostAddress());
// create new User
User newUser = new User(client, nickname);
// add newUser message to list
this.clients.add(newUser);
// Welcome msg
newUser.getOutStream().println(
"<img src='https://www.kizoa.fr/img/e8nZC.gif' height='42' width='42'>"
+ "<b>Welcome</b> " + newUser.toString() +
"<img src='https://www.kizoa.fr/img/e8nZC.gif' height='42' width='42'>"
);
// create a new thread for newUser incoming messages handling
new Thread(new UserHandler(this, newUser)).start();
}
}
// delete a user from the list
public void removeUser(User user){
this.clients.remove(user);
}
// send incoming msg to all Users
public void broadcastMessages(String msg, User userSender) {
for (User client : this.clients) {
client.getOutStream().println(
userSender.toString() + "<span>: " + msg+"</span>");
}
}
// send list of clients to all Users
public void broadcastAllUsers(){
for (User client : this.clients) {
client.getOutStream().println(this.clients);
}
}
// send message to a User (String)
public void sendMessageToUser(String msg, User userSender, String user){
boolean find = false;
for (User client : this.clients) {
if (client.getNickname().equals(user) && client != userSender) {
find = true;
userSender.getOutStream().println(userSender.toString() + " -> " + client.toString() +": " + msg);
client.getOutStream().println(
"(<b>Private</b>)" + userSender.toString() + "<span>: " + msg+"</span>");
}
}
if (!find) {
userSender.getOutStream().println(userSender.toString() + " -> (<b>no one!</b>): " + msg);
}
}
}
class UserHandler implements Runnable {
private Server server;
private User user;
public UserHandler(Server server, User user) {
this.server = server;
this.user = user;
this.server.broadcastAllUsers();
}
public void run() {
String message;
// when there is a new message, broadcast to all
Scanner sc = new Scanner(this.user.getInputStream());
while (sc.hasNextLine()) {
message = sc.nextLine();
// smiley
message = message.replace(":)", "<img src='http://4.bp.blogspot.com/-ZgtYQpXq0Yo/UZEDl_PJLhI/AAAAAAAADnk/2pgkDG-nlGs/s1600/facebook-smiley-face-for-comments.png'>");
message = message.replace(":D", "<img src='http://2.bp.blogspot.com/-OsnLCK0vg6Y/UZD8pZha0NI/AAAAAAAADnY/sViYKsYof-w/s1600/big-smile-emoticon-for-facebook.png'>");
message = message.replace(":d", "<img src='http://2.bp.blogspot.com/-OsnLCK0vg6Y/UZD8pZha0NI/AAAAAAAADnY/sViYKsYof-w/s1600/big-smile-emoticon-for-facebook.png'>");
message = message.replace(":(", "<img src='http://2.bp.blogspot.com/-rnfZUujszZI/UZEFYJ269-I/AAAAAAAADnw/BbB-v_QWo1w/s1600/facebook-frown-emoticon.png'>");
message = message.replace("-_-", "<img src='http://3.bp.blogspot.com/-wn2wPLAukW8/U1vy7Ol5aEI/AAAAAAAAGq0/f7C6-otIDY0/s1600/squinting-emoticon.png'>");
message = message.replace(";)", "<img src='http://1.bp.blogspot.com/-lX5leyrnSb4/Tv5TjIVEKfI/AAAAAAAAAi0/GR6QxObL5kM/s400/wink%2Bemoticon.png'>");
message = message.replace(":P", "<img src='http://4.bp.blogspot.com/-bTF2qiAqvi0/UZCuIO7xbOI/AAAAAAAADnI/GVx0hhhmM40/s1600/facebook-tongue-out-emoticon.png'>");
message = message.replace(":p", "<img src='http://4.bp.blogspot.com/-bTF2qiAqvi0/UZCuIO7xbOI/AAAAAAAADnI/GVx0hhhmM40/s1600/facebook-tongue-out-emoticon.png'>");
message = message.replace(":o", "<img src='http://1.bp.blogspot.com/-MB8OSM9zcmM/TvitChHcRRI/AAAAAAAAAiE/kdA6RbnbzFU/s400/surprised%2Bemoticon.png'>");
message = message.replace(":O", "<img src='http://1.bp.blogspot.com/-MB8OSM9zcmM/TvitChHcRRI/AAAAAAAAAiE/kdA6RbnbzFU/s400/surprised%2Bemoticon.png'>");
// Gestion des messages private
if (message.charAt(0) == '@'){
if(message.contains(" ")){
System.out.println("private msg : " + message);
int firstSpace = message.indexOf(" ");
String userPrivate= message.substring(1, firstSpace);
server.sendMessageToUser(
message.substring(
firstSpace+1, message.length()
), user, userPrivate
);
}
// Gestion du changement
}else if (message.charAt(0) == '#'){
user.changeColor(message);
// update color for all other users
this.server.broadcastAllUsers();
}else{
// update user list
server.broadcastMessages(message, user);
}
}
// end of Thread
server.removeUser(user);
this.server.broadcastAllUsers();
sc.close();
}
}
class User {
private static int nbUser = 0;
private int userId;
private PrintStream streamOut;
private InputStream streamIn;
private String nickname;
private Socket client;
private String color;
// constructor
public User(Socket client, String name) throws IOException {
this.streamOut = new PrintStream(client.getOutputStream());
this.streamIn = client.getInputStream();
this.client = client;
this.nickname = name;
this.userId = nbUser;
this.color = ColorInt.getColor(this.userId);
nbUser += 1;
}
// change color user
public void changeColor(String hexColor){
// check if it's a valid hexColor
Pattern colorPattern = Pattern.compile("#([0-9a-f]{3}|[0-9a-f]{6}|[0-9a-f]{8})");
Matcher m = colorPattern.matcher(hexColor);
if (m.matches()){
Color c = Color.decode(hexColor);
// if the Color is too Bright don't change
double luma = 0.2126 * c.getRed() + 0.7152 * c.getGreen() + 0.0722 * c.getBlue(); // per ITU-R BT.709
if (luma > 160) {
this.getOutStream().println("<b>Color Too Bright</b>");
return;
}
this.color = hexColor;
this.getOutStream().println("<b>Color changed successfully</b> " + this.toString());
return;
}
this.getOutStream().println("<b>Failed to change color</b>");
}
// getteur
public PrintStream getOutStream(){
return this.streamOut;
}
public InputStream getInputStream(){
return this.streamIn;
}
public String getNickname(){
return this.nickname;
}
// print user with his color
public String toString(){
return "<u><span style='color:"+ this.color
+"'>" + this.getNickname() + "</span></u>";
}
}
class ColorInt {
public static String[] mColors = {
"#3079ab", // dark blue
"#e15258", // red
"#f9845b", // orange
"#7d669e", // purple
"#53bbb4", // aqua
"#51b46d", // green
"#e0ab18", // mustard
"#f092b0", // pink
"#e8d174", // yellow
"#e39e54", // orange
"#d64d4d", // red
"#4d7358", // green
};
public static String getColor(int i) {
return mColors[i % mColors.length];
}
}