Пытался по-разному, но последние 3 пункта не проходят.
package com.javarush.task.task30.task3008;
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<>();
public static void sendBroadcastMessage(Message message){
for(Map.Entry<String, Connection> map : connectionMap.entrySet()){
try {
map.getValue().send(message);
} catch (IOException e) {
System.out.println("Сообщение не было отправлено.");
}
}
}
private static class Handler extends Thread{
private Socket socket;
public Handler(Socket socket){
this.socket = socket;
}
private String serverHandshake(Connection connection) throws IOException, ClassNotFoundException{
while (true){
connection.send(new Message(MessageType.NAME_REQUEST));
Message receive = connection.receive();
if(receive.getType()!=(MessageType.USER_NAME)) continue;
else if(receive.getType()==MessageType.USER_NAME&&receive.getData()!=null&&receive.getData()!=""){
if(!connectionMap.containsKey(receive.getData())) {connectionMap.put(receive.getData(), connection);
connection.send(new Message(MessageType.NAME_ACCEPTED));
ConsoleHelper.writeMessage(receive.getData() + " принято.");}
return receive.getData();
}
}
}
}
public static void main(String[] args) throws IOException {
try(ServerSocket serverSocket = new ServerSocket(ConsoleHelper.readInt())){
ConsoleHelper.writeMessage("Сервер запущен.");
while (true){
Handler handler = new Handler(serverSocket.accept());
handler.start();
}
}
catch (IOException e){
e.printStackTrace();
}
}
}