Someone can give me some advice or golden solution?
I need this to past to next task.
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 sendBroadcastMessage(Message message) {
try {
for (Connection c : connectionMap.values()) {
c.send(message);
}
} catch (Exception e) {
ConsoleHelper.writeMessage("Message couldn't be sent");
}
}
private static class Handler extends Thread {
// zajmje sie tym, by byly wymieniane wiadomosci
private Socket socket;
public Handler(Socket socket) {
this.socket = socket;
}
//tzw handszake, server meets the client, zwraca imie klienta
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 response.getData();
}
}
}
}
}
private void notifyUsers(Connection connection, String userName) throws IOException {
// wysylanie info klientowi o innych uczestnikach czatu
for (Map.Entry<String, Connection> entry : connectionMap.entrySet()) {
if (!entry.getKey().equals(userName)) {
connection.send(new Message(MessageType.USER_ADDED, entry.getKey()));
}
}
}
private void serverMainLoop(Connection connection, String userName) throws IOException, ClassNotFoundException {
while (true) {
try {
Message recieved = connection.receive();
if (recieved.getType().equals(MessageType.TEXT)) {
String m = userName + ": " + recieved.getData();
sendBroadcastMessage(new Message(MessageType.TEXT, m));
} else {
ConsoleHelper.writeMessage("This is not a text...");
}
} catch (Exception e) {
e.getMessage();
}
}
}
@Override
public void run() {
String userName = null;
ConsoleHelper.writeMessage("A connection has been established with " + socket.getRemoteSocketAddress());
try (Connection connection = new Connection(socket)) {
// userName = serverHandshake(connection);
sendBroadcastMessage(new Message(MessageType.USER_ADDED, userName));
notifyUsers(connection, userName);
serverMainLoop(connection, userName);
} catch (Exception e) {
ConsoleHelper.writeMessage("An error occurred while communicating with the remote address.");
} finally {
if (userName != null) connectionMap.remove(userName);
sendBroadcastMessage(new Message(MessageType.USER_REMOVED, userName));
}
ConsoleHelper.writeMessage("Connection with " + socket.getRemoteSocketAddress() + " closed.");
}
}
public static void main(String[] args) throws IOException {
ConsoleHelper.writeMessage("Input server port: ");
try (ServerSocket serverSocket = new ServerSocket(ConsoleHelper.readInt())) {
while (true) {
new Handler(serverSocket.accept()).start();
}
} catch (IOException e) {
ConsoleHelper.writeMessage("Something wrong, Server socket closed.");
}
}
}