-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.java
More file actions
115 lines (91 loc) · 4.23 KB
/
Server.java
File metadata and controls
115 lines (91 loc) · 4.23 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
package scr;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class Server {
private static Map<String, Connection> connectionMap = new ConcurrentHashMap<String, Connection>();
public static void main(String[] args) throws IOException {
ConsoleHelper.writeMessage("Введите номер порта:");
try (ServerSocket serverSocket = new ServerSocket(ConsoleHelper.readInt())) {
System.out.println("Сервер запущен");
Socket socket = null;
while (true) {
socket = serverSocket.accept();
Handler handler = new Handler(socket);
handler.start();
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
public static void sendBroadcastMessage(Message message) {
for (Map.Entry<String, Connection> m : connectionMap.entrySet()) {
try {
Connection c = m.getValue();
c.send(message);
} catch (IOException e) {
System.out.println("Сообщение не может быть отправлено");
}
}
}
private static class Handler extends Thread {
Socket socket;
public Handler(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
try {
ConsoleHelper.writeMessage("Было установлено соединение с сервером: " + socket.getRemoteSocketAddress());
Connection connection = new Connection(socket);
String user = serverHandshake(connection);
sendBroadcastMessage(new Message(MessageType.USER_ADDED, user));
notifyUsers(connection, user);
serverMainLoop(connection, user);
for(Map.Entry<String,Connection> m : connectionMap.entrySet()){
String key = m.getKey();
Connection value = m.getValue();
if(key.equals(user)) connectionMap.remove(key, value);
}
sendBroadcastMessage(new Message(MessageType.USER_REMOVED, user));
ConsoleHelper.writeMessage("Соединение с сервером было разорвано.");
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
private String serverHandshake(Connection connection) throws IOException, ClassNotFoundException {
while (true) {
connection.send(new Message(MessageType.NAME_REQUEST));
Message answer = (Message) connection.receive();
if (answer.getType() == MessageType.USER_NAME && !answer.getData().isEmpty()) {
if (!connectionMap.containsKey(answer.getData())) {
connectionMap.put(answer.getData(), connection);
connection.send(new Message(MessageType.NAME_ACCEPTED));
return answer.getData();
}
}
}
}
private void notifyUsers(Connection connection, String userName) throws IOException{
for(Map.Entry<String, Connection> m : connectionMap.entrySet()){
String name = m.getKey();
if(!name.equals(userName)){
connection.send(new Message(MessageType.USER_ADDED, name));
}
}
}
private void serverMainLoop(Connection connection, String userName) throws IOException, ClassNotFoundException{
while (true){
Message message = connection.receive();
if(message.getType() != MessageType.TEXT){
ConsoleHelper.writeMessage("Ошибка!");
} else {
Message formatMessage = new Message(MessageType.TEXT, userName + ": "+ message.getData());
sendBroadcastMessage(formatMessage);
}
}
}
}
}