I don't understand what is wrong with the code...
package com.codegym.task.task30.task3008;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import static com.codegym.task.task30.task3008.MessageType.*;
public class Server {
private static Map<String, Connection> connectionMap = new ConcurrentHashMap<>();
public static void sendBroadcastMessage(Message message) {
for (Map.Entry<String, Connection> entry : connectionMap.entrySet()){
try{
entry.getValue().send(message);
}
catch (IOException e){
try{
entry.getValue().send(new Message(TEXT, "The message couldn't be sent"));
}
catch (IOException e2){}
}
}
}
private static class Handler extends Thread{
private Socket socket;
private Handler(Socket socket) {
this.socket = socket;
}
private String serverHandshake(Connection connection) throws IOException, ClassNotFoundException{
connection.send(new Message(NAME_REQUEST));
Message name = null;
while (true){
name = connection.receive();
if (!name.getType().equals(MessageType.USER_NAME) || name.getData() == null || name.getData().isEmpty() || connectionMap.containsKey(name.getData())){
connection.send(new Message(NAME_REQUEST));
}
else {
break;
}
}
connectionMap.put(name.getData(), connection);
connection.send(new Message(NAME_ACCEPTED));
return name.getData();
}
private void notifyUsers(Connection connection, String userName) throws IOException{
for (Map.Entry<String, Connection> entry : connectionMap.entrySet()){
if (!entry.getKey().equals(userName)){
connection.send(new Message(USER_ADDED, entry.getKey()));
}
}
}
private void serverMainLoop(Connection connection, String userName) throws IOException, ClassNotFoundException {
while (true){
Message message = connection.receive();
if (message.getType().equals(TEXT)){
String text = String.format("%s: %s", userName, message.getData());
sendBroadcastMessage(new Message(TEXT, text));
}
else {
ConsoleHelper.writeMessage("Error!");
}
}
}
public void run(){
ConsoleHelper.writeMessage(String.format("New connection established with remote address %s", socket.getRemoteSocketAddress()));
Connection newConnection = null;
String clientName = "";
do{
try {
newConnection = new Connection(socket);
}
catch (Exception ig){
ConsoleHelper.writeMessage("An error occurred while communicating with the remote address");
break;
}
try {
clientName = serverHandshake(newConnection);
}
catch (Exception ig2){
try{
newConnection.close();
ConsoleHelper.writeMessage("An error occurred while communicating with the remote address");
}
catch (IOException e2){}
break;
}
try{
sendBroadcastMessage(new Message(USER_ADDED, clientName));
notifyUsers(newConnection, clientName);
serverMainLoop(newConnection, clientName);
}
catch (Exception e){
ConsoleHelper.writeMessage("An error occurred while communicating with the remote address");
break;
}
finally{
if (!clientName.equals("")){
connectionMap.remove(clientName);
sendBroadcastMessage(new Message(USER_REMOVED, clientName));
}
try{
newConnection.close();
}
catch (IOException e){}
ConsoleHelper.writeMessage("The connection with the remote address is closed.");
}
} while(false);
}
}
public static void main(String[] args) throws IOException{
int serverPort = ConsoleHelper.readInt();
ServerSocket server = new ServerSocket(serverPort);
System.out.println("Server is running!");
try{
while (true){
Socket socket = server.accept();
Thread handler = new Handler(socket);
handler.start();
}
}
catch (IOException e){
System.out.println(e.getMessage());
server.close();
}
}
}