Skip to content

Commit

Permalink
implemented new feature name list on the client side.
Browse files Browse the repository at this point in the history
  • Loading branch information
prozb committed Jan 21, 2018
1 parent a018ca5 commit 09b9cc0
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 9 deletions.
41 changes: 37 additions & 4 deletions Network/src/pis/client/LaunchClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(){
Expand Down Expand Up @@ -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!");
}
Expand All @@ -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);
Expand All @@ -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);
}


Expand All @@ -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();
}
Expand All @@ -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");
}

Expand Down Expand Up @@ -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==================
}
11 changes: 11 additions & 0 deletions Network/src/pis/server/Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand All @@ -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: ", ""));
Expand Down
23 changes: 18 additions & 5 deletions Network/src/pis/server/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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());
}
}

Expand All @@ -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) {
Expand All @@ -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);
Expand All @@ -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
Expand Down

0 comments on commit 09b9cc0

Please sign in to comment.