Have no idea what is going on.
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;
public class Server {
private static Map<String, Connection> connectionMap = new ConcurrentHashMap<>();
public static void main(String args[]) throws Exception {
ServerSocket temp = null;
temp = new ServerSocket(ConsoleHelper.readInt());
System.out.println("server is running");
try {
while (true) {
Socket newSocket = temp.accept();
Handler newConnection = new Handler(newSocket);
newConnection.start();
continue;
}
} catch (Exception e) {
}
System.out.println("Server not implemented");
temp.close();
}
public static void sendBroadcastMessage(Message message) {
for (Connection x : connectionMap.values()) {
try {
x.send(message);
} catch (Exception e) {
e.printStackTrace();
}
}
}
private static class Handler extends Thread {
private Socket socket;
public void run() {
String userName = null;
Connection myConnect = null;
for (int i = 0; i < 1; i++) {
try {
System.out.println("Connection established: " + socket.getRemoteSocketAddress());
myConnect = new Connection(socket);
} catch (Exception ig) {
break;
}
try {
userName = serverHandshake(myConnect);
} catch (Exception ig) {
try {
myConnect.close();
} catch (IOException e) {
e.printStackTrace();
}
break;
}
try {
sendBroadcastMessage(new Message(MessageType.USER_ADDED, userName));
notifyUsers(myConnect, userName);
serverMainLoop(myConnect, userName);
} catch (Exception e) {
} finally {
connectionMap.remove(userName);
sendBroadcastMessage(new Message(MessageType.USER_REMOVED, userName));
try {
myConnect.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
System.out.println("Connection closed: "+socket.getRemoteSocketAddress());
}
public Handler(Socket socket) {
this.socket = socket;
}
private String serverHandshake(Connection connection) throws IOException, ClassNotFoundException{
Message response = null;
connection.send(new Message(MessageType.NAME_REQUEST));
while(true) {
response = connection.receive();
if(response.getType() != MessageType.USER_NAME){
connection.send(new Message(MessageType.NAME_REQUEST));
continue;
}
String data = response.getData();
if(data.length() == 0 || response == null){
connection.send(new Message(MessageType.NAME_REQUEST));
continue;
}
if(connectionMap.containsKey(data)){
connection.send(new Message(MessageType.NAME_REQUEST));
continue;
}
connectionMap.put(response.getData(),connection);
connection.send(new Message(MessageType.NAME_ACCEPTED));
return response.getData();
}
}
private void notifyUsers(Connection connection, String userName) throws IOException{
for(Map.Entry<String,Connection> user : connectionMap.entrySet()){
if(userName.equals(user.getKey())) continue;
connection.send(new Message(MessageType.USER_ADDED, user.getKey()));
}
}
private void serverMainLoop(Connection connection, String userName) throws IOException, ClassNotFoundException{
while(true) {
Message received= connection.receive();
if (received.getType() == MessageType.TEXT)
sendBroadcastMessage(new Message(MessageType.TEXT, String.format("%s: %s", userName, received.getData())));
else ConsoleHelper.writeMessage("Error, text not available");
}
}
}
}