Skip to content

Commit

Permalink
fixed bug Stackoverflow exception while exiting not connected client.…
Browse files Browse the repository at this point in the history
… Refactoring of server logic
  • Loading branch information
prozb committed Jan 22, 2018
1 parent 09b9cc0 commit fcb322d
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 27 deletions.
7 changes: 4 additions & 3 deletions Network/src/pis/client/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public Client(ISend gui) throws Exception{
this.in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
this.out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
} catch (IOException e) {
e.printStackTrace();
disconnect();
}

this.clientThread = new Thread(() -> {
Expand All @@ -48,7 +48,7 @@ public Client(ISend gui) throws Exception{
System.out.println(msg);
}
} catch (IOException e) {
e.printStackTrace();
gui.showTextOnGui("cannot send message.");
disconnect();
}
}
Expand All @@ -57,7 +57,8 @@ public Client(ISend gui) throws Exception{
this.clientThread.start();
}
//just disconnect client from server
private void disconnect(){
public void disconnect(){
clientThread.interrupt();
try {
socket.close();
} catch (IOException e) {
Expand Down
29 changes: 22 additions & 7 deletions Network/src/pis/client/LaunchClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ private void initFirstScene(){
if(msg != null && !msg.equals("")) {
if (!isLoggedIn && clientStarted) {
client.sendString(Constants.CONNECT + ": " + msg);
}else{
}else if(!clientStarted){
createClient();
client.sendString(Constants.CONNECT + ": " + msg);
}
Expand Down Expand Up @@ -97,6 +97,10 @@ private void initFirstScene(){
this.exitButton = new Button("Exit");
this.exitButton.setPrefWidth(80);
this.exitButton.setOnAction(e -> {
if(client != null && !client.equals(null)) {
client.sendString("disconnect:");
client.disconnect();
}
Platform.exit();
});

Expand Down Expand Up @@ -188,7 +192,14 @@ public void showTextOnGui(String text){
msg = msg.replace("name list", "");
this.clientList.setText(msg);
}
firstTextArea.appendText(text + "\n");

if(isManyUsers(text)){
this.clientStarted = false;
this.client = null;
}
if(!isNamelist(text)) {
firstTextArea.appendText(text + "\n");
}
}

//method "be never DRY"
Expand All @@ -205,8 +216,12 @@ private void changeButtonsState(boolean state){
//creating new client
private void createClient(){
try {
this.client = new Client(LaunchClient.this);
this.clientStarted = true;
try {
this.client = new Client(LaunchClient.this);
this.clientStarted = true;
}catch (Exception e){
client.disconnect();
}
} catch (Exception e) {
showTextOnGui("cannot connect to server!\n");
this.clientStarted = false;
Expand All @@ -217,7 +232,7 @@ private void createClient(){
private boolean isConnect(String msg){
pattern = Pattern.compile("^connect: ok$");
matcher = pattern.matcher(msg);
return matcher.matches();
return matcher.matches();
}
private boolean isDisconnect(String msg){
pattern = Pattern.compile("^disconnect:$");
Expand All @@ -230,8 +245,8 @@ private boolean isNamelist(String msg){
return matcher.matches();
}

private boolean isSomeDisconnected(String msg){
pattern = Pattern.compile("disconnected.$");
private boolean isManyUsers(String msg){
pattern = Pattern.compile("^refused: too_many_users$");
matcher = pattern.matcher(msg);
return matcher.matches();
}
Expand Down
38 changes: 22 additions & 16 deletions Network/src/pis/server/Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,38 +83,44 @@ private void processInMessages(String msg) throws Exception{
if(disconnectedCommand(msg)){
this.disconnectedFlag = true;
disconnect();
}else
}
//message command processing
if(messageCommand(msg) && isLoggedIn()){
actionListener.receiveMessage(this, msg.replaceAll("message: ", ""));
}else
//don't touch this stuff
if(connectedCommand(msg) && !isLoggedIn() && correctName(msg) && !nameExists(msg)){
}

actionListener.receiveNames(this);
int clientsCount = names.size();
if(connectedCommand(msg) && !isLoggedIn() && correctName(msg) && !nameExists(msg) &&
clientsCount < Constants.MAX_CLIENTS_SIZE){
loggedIn = true;
System.out.println("LOLOLOL");
sendString("connect: ok");
this.clientName = msg.replaceAll("connect:\\s", "");
//sendString("Welcome in this chat dear " + clientName);
actionListener.receiveMessage(this, "logged in successfully!");
//be using method receiveNames, sends the server numbers on all connections
actionListener.receiveNames(this);
}else if(!isLoggedIn() && connectedCommand(msg) && correctName(msg) && nameExists(msg)){
//actionListener.receiveNames(this);
}

if(!isLoggedIn() && connectedCommand(msg) && correctName(msg) && nameExists(msg)){
sendString("refused: name_in_use");
actionListener.log("user " + socket.getInetAddress() + " is trying to use chosen name.");
}else if(!isLoggedIn() && connectedCommand(msg) && !correctName(msg) && !nameExists(msg)){
}

if(!isLoggedIn() && connectedCommand(msg) && !correctName(msg) && !nameExists(msg)){
sendString("refused: invalid_name");
actionListener.log("user " + socket.getInetAddress() + " is trying to use chosen name.");
}else if(isLoggedIn() && connectedCommand(msg) && correctName(msg) && !nameExists(msg)){
}

if(isLoggedIn() && connectedCommand(msg) && correctName(msg) && !nameExists(msg)){
sendString("refused: cannot_change_name");
actionListener.log("user " + socket.getInetAddress() + " is trying change his name.");
}else{
sendString("refused: see_how_to_use_chat");
actionListener.log((!isLoggedIn() ? socket.getInetAddress() : clientName) + " wrong_command");
}

/*if (receiveNameList(msg)) {
actionListener.log("TEST");
actionListener.receiveNames(this);
}*/
// else{
// sendString("refused: see_how_to_use_chat");
// actionListener.log((!isLoggedIn() ? socket.getInetAddress() : clientName) + " wrong_command");
// }
}
//connection proves whether this name exists or not
private boolean nameExists(String val){
Expand Down
2 changes: 1 addition & 1 deletion Network/src/pis/server/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public synchronized void receiveNames(Connection connection) {
var.sendString("name list: " + val);
}
}
log("name list: " + val);
//log("name list: " + val);
}
}

Expand Down

0 comments on commit fcb322d

Please sign in to comment.