help
package com.codegym.task.task30.task3008;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class Server {
private static Map<String,Connection> connectionMap = new ConcurrentHashMap<>();
public static void main(String[] args) throws IOException {
int port = ConsoleHelper.readInt();
ServerSocket serverSocket = new ServerSocket(port);
System.out.println("Server is running");
try{
while(true) {
Socket socket = serverSocket.accept();
Handler handler = new Handler(socket);
handler.start();
}
}catch (IOException e){
e.printStackTrace();
serverSocket.close();
}
}
public static void sendBroadcastMessage(Message message){
Iterator<Map.Entry<String,Connection>> itr = connectionMap.entrySet().iterator();
while(itr.hasNext()){
Map.Entry<String,Connection> pair = itr.next();
Connection connection = pair.getValue();
String user = pair.getKey();
try{
connection.send(message);
}catch (IOException e){
System.out.println(user+" Message couldn't sent");
}
}
}
private static class Handler extends Thread{
private Socket socket;
public Handler(Socket socket){
this.socket = socket;
}
@Override
public void run() {
ConsoleHelper.writeMessage("Connection established with "+socket.getRemoteSocketAddress());
try( Connection connection = new Connection(this.socket)) {
String userName = serverHandshake(connection);
sendBroadcastMessage(new Message(MessageType.USER_ADDED,userName));
notifyUsers(connection,userName);
serverMainLoop(connection,userName);
connectionMap.remove(userName);
sendBroadcastMessage(new Message(MessageType.USER_REMOVED,userName));
ConsoleHelper.writeMessage("Connection is closed with : "+socket.getRemoteSocketAddress());
}catch (IOException e){
}catch (ClassNotFoundException e){
}
}
private void notifyUsers(Connection connection, String userName) throws IOException{
for(Map.Entry<String,Connection> pair : connectionMap.entrySet()){
if(!(userName.equals(pair.getKey())))
connection.send(new Message(MessageType.USER_ADDED, pair.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) {
System.out.println(e.getMessage());
}
}
}
private String serverHandshake(Connection connection) throws IOException,ClassNotFoundException{
while(true) {
connection.send(new Message(MessageType.NAME_REQUEST));
Message response = connection.receive();
if (response.getType().equals(MessageType.USER_NAME)) {
String data = response.getData();
if (data != null && !(data.equals(""))) {
if(!(connectionMap.containsKey(data))) {
connectionMap.put(data, connection);
connection.send(new Message(MessageType.NAME_ACCEPTED));
return data;
}
}
}
}
}
}
}