diff --git a/Network/src/pis/client/LaunchClient.java b/Network/src/pis/client/LaunchClient.java index 406f5f1..ac6ce73 100644 --- a/Network/src/pis/client/LaunchClient.java +++ b/Network/src/pis/client/LaunchClient.java @@ -6,6 +6,7 @@ import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Button; +import javafx.scene.control.Label; import javafx.scene.control.TextArea; import javafx.scene.control.TextField; import javafx.scene.layout.HBox; @@ -34,11 +35,13 @@ public class LaunchClient extends Application implements ISend { private Button setNameButton; private TextField setNameField; private TextField messageField; + private TextField clientList; private boolean clientStarted; private Client client; private boolean isLoggedIn; private Pattern pattern; private Matcher matcher; + private Label nameListLabel; //just initialization of gui private void initFirstScene(){ @@ -84,6 +87,8 @@ private void initFirstScene(){ this.stopButton.setOnAction(e -> { if(clientStarted) { showTextOnGui(Constants.DISCONNECT + ":"); + setNameField.clear(); + clientList.clear(); }else{ showTextOnGui("you are already disconnected!"); } @@ -99,15 +104,22 @@ private void initFirstScene(){ this.setNameField.setEditable(true); this.setNameField.setPrefColumnCount(26); + this.clientList = new TextField("input your name"); + this.clientList.setEditable(false); + this.clientList.setText("0 clients"); + this.clientList.setPrefColumnCount(26); + this.messageField = new TextField("input your message"); this.messageField.setEditable(false); this.messageField.setDisable(true); this.messageField.setPrefColumnCount(26); + this.nameListLabel = new Label("are online."); + this.firstLayout = new VBox(); this.firstLayout.setAlignment(Pos.CENTER); this.firstLayout.setPadding(new Insets(10,25,25,25)); - this.firstScene = new Scene(firstLayout, 500, 300, Color.BLACK); + this.firstScene = new Scene(firstLayout, 500, 400, Color.BLACK); this.firstTextArea = new TextArea(); this.firstTextArea.setEditable(false); @@ -129,7 +141,11 @@ private void initFirstScene(){ HBox hBox4 = new HBox(); hBox4.getChildren().addAll(stopButton, clearButton, exitButton); hBox4.setSpacing(50); - this.firstLayout.getChildren().addAll(hBox1, hBox2, hBox3, hBox4); + HBox hBox5 = new HBox(); + hBox5.getChildren().addAll(clientList, nameListLabel); + hBox5.setSpacing(30); + hBox5.setPadding(new Insets(10,0,25, 0)); + this.firstLayout.getChildren().addAll(hBox1, hBox5, hBox2, hBox3, hBox4); } @@ -141,12 +157,11 @@ public void init() throws Exception { } @Override - public void start(Stage primaryStage) throws Exception { + public void start(Stage primaryStage) { this.primaryStage = primaryStage; primaryStage.setResizable(false); primaryStage.setTitle("Client"); - this.primaryStage.setScene(firstScene); this.primaryStage.show(); } @@ -166,6 +181,13 @@ public void showTextOnGui(String text){ client.sendString(Constants.DISCONNECT + ":"); changeButtonsState(false); } + + if(isNamelist(text)){ + this.clientList.setText(""); + String msg = text.replace(":", " "); + msg = msg.replace("name list", ""); + this.clientList.setText(msg); + } firstTextArea.appendText(text + "\n"); } @@ -202,5 +224,16 @@ private boolean isDisconnect(String msg){ matcher = pattern.matcher(msg); return matcher.matches(); } + private boolean isNamelist(String msg){ + pattern = Pattern.compile("^name list: .+"); + matcher = pattern.matcher(msg); + return matcher.matches(); + } + + private boolean isSomeDisconnected(String msg){ + pattern = Pattern.compile("disconnected.$"); + matcher = pattern.matcher(msg); + return matcher.matches(); + } //=================REGEX for commands================== } diff --git a/Network/src/pis/server/Connection.java b/Network/src/pis/server/Connection.java index 6025ecc..98778e4 100644 --- a/Network/src/pis/server/Connection.java +++ b/Network/src/pis/server/Connection.java @@ -110,6 +110,11 @@ private void processInMessages(String msg) throws Exception{ sendString("refused: see_how_to_use_chat"); actionListener.log((!isLoggedIn() ? socket.getInetAddress() : clientName) + " wrong_command"); } + + /*if (receiveNameList(msg)) { + actionListener.log("TEST"); + actionListener.receiveNames(this); + }*/ } //connection proves whether this name exists or not private boolean nameExists(String val){ @@ -125,6 +130,12 @@ private boolean nameExists(String val){ return false; } //================checking with regex=========================== + + private boolean receiveNameList(String val){ + pattern = Pattern.compile("^receive_names:$"); + matcher = pattern.matcher(val.replaceAll("connect: ", "")); + return matcher.matches(); + } private boolean correctName(String val){ pattern = Pattern.compile("^[a-zA-Z0-9_]{3,30}"); matcher = pattern.matcher(val.replaceAll("connect: ", "")); diff --git a/Network/src/pis/server/Server.java b/Network/src/pis/server/Server.java index b2b7152..423fd59 100644 --- a/Network/src/pis/server/Server.java +++ b/Network/src/pis/server/Server.java @@ -56,7 +56,9 @@ public Server(IInterconnect gui){ Socket socket = serverSocket.accept(); //number of connections must be restricted if (connections.size() < Constants.MAX_CLIENTS_SIZE) { - connected(new Connection(socket, Server.this)); + Connection conn = new Connection(socket, Server.this); + conn.sendString("name list: " + createNamesList()); + connected(conn); } else { Connection connection = new Connection(socket, Server.this); connection.sendString("refused: too_many_users"); @@ -114,6 +116,7 @@ public synchronized void disconnectClient(Connection connection) { connections.remove(connection); log(getClientName(connection) + " disconnected."); sendOnAll(connection, getClientName(connection) + " disconnected."); + sendOnAll("name list: " + createNamesList()); } } @@ -123,10 +126,7 @@ public synchronized void receiveNames(Connection connection) { //send names on all users if(connection.isLoggedIn() && names.toString() != null){ //making name list: [NAME]: {NAME} with simple manipulations - String val = names.toString(); - val = val.replaceAll(",",":"); - val = val.replaceAll("\\[",""); - val = val.replaceAll("]",""); + String val = createNamesList(); //send names on all clients for(Connection var : connections){ if(var.getClientName() != null) { @@ -137,6 +137,14 @@ public synchronized void receiveNames(Connection connection) { } } + private String createNamesList(){ + String val = names.toString(); + val = val.replaceAll(",",":"); + val = val.replaceAll("\\[",""); + val = val.replaceAll("]",""); + return val; + } + @Override public synchronized void log(String msg) { System.out.println(msg); @@ -158,6 +166,11 @@ public void sendOnAll(Connection connection, String msg){ } } } + private void sendOnAll(String msg){ + for(Connection val : connections){ + val.sendString(msg); + } + } //=============================GETTERS & SETTERS====================================== /** * @param connection