Skip to content
This repository was archived by the owner on Nov 20, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 29 additions & 5 deletions src/main/java/jcoreadv/lesson7/server/ClientHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,41 @@ public class ClientHandler {
private static String userLeaveChatNotification = "%s вышел из чата";
private static final String nickIsBusyMessage = "Учетная запись уже используется";
private static final String incorrectAuthPairMessage = "Неверные логин/пароль";
private boolean isAuthorize = false;
private static final int SECONDS_TO_WAIT_AUTH = 20;

public String getName() {
return name;
}

public ClientHandler(MyServer myServer) {
public ClientHandler(MyServer myServer, Socket socket) {
try {
this.myServer = myServer;
this.socket = myServer.getSocket();
this.socket = socket;
this.in = new DataInputStream(socket.getInputStream());
this.out = new DataOutputStream(socket.getOutputStream());
this.name = "";

new Thread(() -> {
final long beginWaiting = System.currentTimeMillis();
while (!isAuthorize & System.currentTimeMillis() - beginWaiting < SECONDS_TO_WAIT_AUTH*1000){
try {
Thread.sleep(500);
System.out.println(System.currentTimeMillis());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (!isAuthorize) {
sendMsg("Вы отключены от сервера. Для новой попытки входа, перезапустите приложение.");
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();

new Thread(() -> {
try {
while (true) { // цикл авторизации
Expand All @@ -45,6 +68,7 @@ public ClientHandler(MyServer myServer) {
name = nick;
myServer.broadcastMsg(name + newUserNotification);
myServer.subscribe(this);
this.isAuthorize = true;
break;
} else {
sendMsg(nickIsBusyMessage);
Expand All @@ -59,10 +83,9 @@ public ClientHandler(MyServer myServer) {
System.out.printf(incomingMessageTemplate, name, message);
if (message.equals(Contract.END_COMMAND)) break;
if (message.matches("(\\/w\\s+)(\\w+\\s+)(\\w+.*\\s*)+")) {
final String[] split = message.split("\\s+",3);
final String[] split = message.split("\\s+", 3);
myServer.privateMessage(split[1], split[2], name);
}
else myServer.broadcastMsg(name + ": " + message);
} else myServer.broadcastMsg(name + ": " + message);
}
} catch (IOException e) {
e.printStackTrace();
Expand All @@ -79,6 +102,7 @@ public ClientHandler(MyServer myServer) {
} catch (IOException e) {
throw new RuntimeException("Проблемы при создании обработчика клиента");
}

}

public void sendMsg(String msg) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/jcoreadv/lesson7/server/MyServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public MyServer() {
System.out.println("Сервер ожидает подключения");
socket = server.accept();
System.out.println("Клиент подключился");
new ClientHandler(this);
new ClientHandler(this, socket);
}
} catch (IOException e) {
System.out.println("Ошибка при работе сервера");
Expand Down