It seems easy in the condition so I don't really understand where is the problem :(
package com.codegym.task.task30.task3008;
import java.net.*;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.io.IOException;
public class Server {
private static Map<String, Connection> connectionMap = new ConcurrentHashMap<String, Connection>();
public static void sendBroadcastMessage(Message message) {
Iterator<Connection> valueIterator=connectionMap.values().iterator();
while(valueIterator.hasNext()){
try{
valueIterator.next().send(message);
}
catch (IOException e){
System.out.println("Message couldn't be sent");
}
}
}
public static void main (String[] args){
int port = ConsoleHelper.readInt();
ServerSocket serverSocket = null;
try{
serverSocket= new ServerSocket(port);
System.out.println("Server is running");
while (true){
Socket s = serverSocket.accept();
Thread thread = new Handler(s);
thread.start();
}
}
catch (Exception e){
System.out.println(e.getMessage());
try{
serverSocket.close();
}catch (Exception ex){
ex.printStackTrace();
}
}
}
private static class Handler extends Thread{
private Socket socket;
public Handler (Socket socket){
this.socket = socket;
}
private String serverHandshake(Connection connection) throws IOException, ClassNotFoundException {
boolean nameRequestOk = true;
boolean isAlreadyTaken = false;
Message response = null;
boolean isKeyPresent = false;
while (nameRequestOk) {
connection.send(new Message(MessageType.NAME_REQUEST));
response = connection.receive();
if (response.getType().equals(MessageType.USER_NAME)){
if (!response.getData().equals("")){
isKeyPresent = connectionMap.containsKey(response.getData());
if (!isKeyPresent){
connectionMap.put(response.getData(), connection);
connection.send(new Message(MessageType.NAME_ACCEPTED));
nameRequestOk = false;
}
}
}
}
return response.getData();
}
private void notifyUsers(Connection connection, String userName) throws IOException {
String name = userName;
for (Map.Entry<String, Connection> entry : connectionMap.entrySet()){
if( entry.getKey() != userName) {
connection.send(new Message(MessageType.USER_ADDED, entry.getKey()));
}
}
}
private void serverMainLoop(Connection connection, String userName) throws IOException, ClassNotFoundException {
while (true) {
try {
Message received = connection.receive();
if (received.getType().equals(MessageType.TEXT)) {
String m = userName + ": " + received.getData();
sendBroadcastMessage(new Message(MessageType.TEXT, m));
} else {
String error = "not text error";
ConsoleHelper.writeMessage(error);
}
} catch (Exception e) {
ConsoleHelper.writeMessage(e.getMessage());
}
}
}
@Override
public void run(){
ConsoleHelper.writeMessage("New connection using " +socket.getRemoteSocketAddress()+" is established.");
String newClient="";
Connection connection=null;
try {
connection = new Connection(socket);
newClient = serverHandshake(connection);
sendBroadcastMessage(new Message(MessageType.USER_ADDED, newClient));
notifyUsers(connection, newClient);
serverMainLoop(connection, newClient);
connectionMap.remove(newClient);
sendBroadcastMessage(new Message(MessageType.USER_REMOVED, newClient));
connection.close();
}catch (IOException i){
if(newClient != ""){
connectionMap.remove(newClient);
sendBroadcastMessage(new Message(MessageType.USER_REMOVED, newClient));
try{
connection.close();
}catch (IOException y){
}
}
}catch (ClassNotFoundException c){
if(newClient != ""){
connectionMap.remove(newClient);
sendBroadcastMessage(new Message(MessageType.USER_REMOVED, newClient));
try{
connection.close();
}catch (IOException y){
}
}
}catch (Exception e){
if(newClient != ""){
connectionMap.remove(newClient);
sendBroadcastMessage(new Message(MessageType.USER_REMOVED, newClient));
try{
connection.close();
}catch (IOException y){
}
}
}
}
}
}