Hello I read and check different examples of wait and modify ,I try different things and I really don't know what is wrong ,I need help
package com.codegym.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;
import java.util.logging.Handler;
public class Server {
private static Map<String,Connection> connectionMap;
static {
connectionMap = new ConcurrentHashMap<> ( );
}
public static void main(String[] args) throws IOException {
int port = ConsoleHelper.readInt ();
java.net.ServerSocket serverSocket = new ServerSocket (port );
ConsoleHelper.writeMessage ( "Server is running");
Socket socket = null;
while(true) {
socket = serverSocket.accept ();
Thread handler = new Handler ( socket );
handler.start ();
try {
}catch (Exception o) {
serverSocket.close ();
ConsoleHelper.writeMessage ( "Error" );
}
//serverSocket.close ();
}
}
private static class Handler extends Thread {
private Socket socket;
public Handler(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
ConsoleHelper.writeMessage ( "Connection has been established whit remote address " + socket.getRemoteSocketAddress () );
String userName = null;
Connection connection = null;
try {
connection = new Connection ( socket );
userName = serverHandshake ( connection );
sendBroadcastMessage ( new Message ( MessageType.USER_ADDED,userName ) );
notifyUsers ( connection,userName );
serverMainLoop ( connection,userName );
connection.close ();
connectionMap.remove ( userName );
sendBroadcastMessage ( new Message ( MessageType.USER_REMOVED,userName ) );
ConsoleHelper.writeMessage ( "connection is finished whit remote adress" + socket.getRemoteSocketAddress () );
}catch (Exception o){
}
}
private String serverHandshake(Connection connection) throws IOException,ClassNotFoundException{
while(true){
connection.send ( new Message ( MessageType.NAME_REQUEST ) );
Message answer = connection.receive ();
if(answer.getType ().equals (MessageType.USER_NAME)){
String data = answer.getData ();
if(data != null & (!data.isEmpty ()) & (!connectionMap.containsKey ( data ))){
connectionMap.put (data,connection );
connection.send ( new Message ( MessageType.NAME_ACCEPTED ) );
return data;
}else {
continue;
}
}else {
continue;
}
}
}
private void notifyUsers(Connection connection,String userName) throws IOException{
for(ConcurrentHashMap.Entry<String,Connection> map : connectionMap.entrySet ()){
//String name = map.getKey ();
// Message message = new Message ( MessageType.USER_ADDED,name );
if(!map.getKey ().equals ( userName )) {
connection.send ( new Message ( MessageType.USER_ADDED,map.getKey () ) );
}
}
}
private void serverMainLoop(Connection connection,String userName)
throws IOException,ClassNotFoundException{
while(true) {
// try {
Message message = connection.receive ();
if (message.getType ().equals ( MessageType.TEXT )) {
Message newMessage = new Message ( MessageType.TEXT, userName + ": " + message.getData () );
sendBroadcastMessage ( newMessage );
} else
ConsoleHelper.writeMessage ( "Error" );
// }catch (Exception o){
// }
}
}
}
public static void sendBroadcastMessage(Message message){
for(Connection c : connectionMap.values()){
try {
c.send(message);
} catch (IOException e) {
ConsoleHelper.writeMessage("Message couldn't be sent.");
}
}
}
}