Вроде бы по логике все правильно работает но последние 2 пункта не проходят проверку. Подскажите что не так, пожалуйста
package com.javarush.task.task30.task3008;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class Server {
private static Map<String, Connection> connectionMap = new ConcurrentHashMap<> ();
private static class Handler extends Thread
{
private Socket socket;
public Handler(Socket socket)
{
this.socket=socket;
}
private String serverHandshake(Connection connection) throws IOException, ClassNotFoundException
{
Message message = new Message (MessageType.NAME_REQUEST,"Отправьте имя клиента");
Message answer;
String name;
boolean flag=true;
connection.send (message);
answer=connection.receive ();
if (answer.getType ().equals (MessageType.USER_NAME))
{
name=answer.getData ();
if (name!=null&&!name.equals ("")) {
for (HashMap.Entry<String, Connection> conn : connectionMap.entrySet ()
) {
if (conn.getKey ().equals (name)) {
flag=false;
}
}
if (flag)
{
connectionMap.put (name,connection);
connection.send (new Message (MessageType.USER_ADDED,"Ваше имя принято"));
return name;
}
else
{
serverHandshake (connection);
}
}
else
{
serverHandshake (connection);
}
}
else
{
serverHandshake (connection);
}
return null;
}
}
public static void sendBroadcastMessage(Message message) throws IOException {
for (HashMap.Entry<String,Connection>map:connectionMap.entrySet ()) {
try {
map.getValue ().send (message);
}catch (IOException e)
{
System.out.println (e.getMessage ());
}
}
}
public static void main (String[] args) {
int port = ConsoleHelper.readInt ();
ServerSocket serverSocket=null;
try {
serverSocket = new ServerSocket (port);
System.out.println ("Соединение установлено");
while (true)
{
new Handler (serverSocket.accept ()).start ();
}
} catch (IOException e) {
try {
serverSocket.close ();
} catch (IOException ex) {
System.out.println (e.getMessage ());
}
e.printStackTrace ();
}
}
}