diff --git a/src/chat/ReceiveThread.java b/src/chat/ReceiveThread.java index 062d6c8..fb31602 100644 --- a/src/chat/ReceiveThread.java +++ b/src/chat/ReceiveThread.java @@ -38,113 +38,113 @@ class ReceiveThread extends Thread { public void run() { boolean exit = false; - try - { - while(!exit) + while(!exit) + { + try { - ObjectInputStream in = new ObjectInputStream(socket.getInputStream()); - Notification notif = (Notification) in.readObject(); - - if(!(notif.getAuthor().equals(user)) && !(notif.getAuthor().isOutdoor() && !isOutdoor)) - { - for(Socket s:outdoor_dest_sockets) - { - try - { - ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream()); - - out.writeObject(notif); - - } - catch(IOException e){} - } - } - - if(isOutdoor) - { - for(Socket s:dest_sockets) + ObjectInputStream in = new ObjectInputStream(socket.getInputStream()); + Notification notif = (Notification) in.readObject(); + + if(!(notif.getAuthor().equals(user)) && !(notif.getAuthor().isOutdoor() && !isOutdoor)) + { + for(Socket s:outdoor_dest_sockets) + { + try { - try - { - ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream()); - - out.writeObject(notif); - } - catch(IOException e){} + ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream()); + + out.writeObject(notif); + } + catch(IOException e){} + } + } + + if(isOutdoor) + { + for(Socket s:dest_sockets) + { + try + { + ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream()); + + out.writeObject(notif); + } + catch(IOException e){} + } + } + else + { + if(notif instanceof Message) + { + Message m = (Message) notif; + displayArea.append(m.getAuthor().getName() + " : " + m.getText() + "\n"); + } + else if (notif instanceof DisconnectNotification) + { + DisconnectNotification dn = (DisconnectNotification) notif; + + displayArea.append(dn.getAuthor().getName() + " has left the chat.\n"); + + known_users.remove(dn.getAuthor()); + known_outdoor_users.remove(dn.getAuthor()); + + if(!dn.getAuthor().equals(user) && !(dn.getAuthor().isOutdoor() && !isOutdoor)) + { + exit = true; + + socket.close(); + if(!isOutdoor) + dest_sockets.remove(socket); + else + outdoor_dest_sockets.remove(socket); + } + } + + else if (notif instanceof ConnectNotification) + { + ConnectNotification cn = (ConnectNotification) notif; + + displayArea.append(cn.getAuthor().getName() + " has joined the chat.\n"); + if(cn.getAuthor().isOutdoor()) + { + if(!known_outdoor_users.contains(cn.getAuthor())) + { + known_outdoor_users.add(cn.getAuthor()); + Collections.sort(known_outdoor_users); + } + } + else + { + if(!known_users.contains(cn.getAuthor())) + { + known_users.add(cn.getAuthor()); + Collections.sort(known_users); + } + } + } + knownUsersPanel.setText(""); + knownUsersPanel.append("Indoor users:\n"); + for(User a:known_users) + { + knownUsersPanel.append(" " + a.getName() + " \n"); } - else - { - if(notif instanceof Message) - { - Message m = (Message) notif; - displayArea.append(m.getAuthor().getName() + " : " + m.getText() + "\n"); - } - else if (notif instanceof DisconnectNotification) - { - DisconnectNotification dn = (DisconnectNotification) notif; - - displayArea.append(dn.getAuthor().getName() + " has left the chat.\n"); - - known_users.remove(dn.getAuthor()); - known_outdoor_users.remove(dn.getAuthor()); - - if(!dn.getAuthor().equals(user) && !(dn.getAuthor().isOutdoor() && !isOutdoor)) - { - exit = true; - - socket.close(); - if(!isOutdoor) - dest_sockets.remove(socket); - else - outdoor_dest_sockets.remove(socket); - } - } - - else if (notif instanceof ConnectNotification) - { - ConnectNotification cn = (ConnectNotification) notif; - - displayArea.append(cn.getAuthor().getName() + " has joined the chat.\n"); - if(cn.getAuthor().isOutdoor()) - { - if(!known_outdoor_users.contains(cn.getAuthor())) - { - known_outdoor_users.add(cn.getAuthor()); - Collections.sort(known_outdoor_users); - } - } - else - { - if(!known_users.contains(cn.getAuthor())) - { - known_users.add(cn.getAuthor()); - Collections.sort(known_users); - } - } - } - knownUsersPanel.setText(""); - knownUsersPanel.append("Indoor users:\n"); - for(User a:known_users) - { - knownUsersPanel.append(" " + a.getName() + " \n"); - } - knownUsersPanel.append("\nOutdoor users:\n"); - for(User a:known_outdoor_users) - { - knownUsersPanel.append(" " + a.getName() + " \n"); - } - displayArea.setCaretPosition(displayArea.getDocument().getLength()); - } - } - } - catch(Exception e) - { - /*e.printStackTrace(); - System.out.println("Socket closed"); - dest_sockets.remove(socket); - outdoor_dest_sockets.remove(socket);*/ - } + knownUsersPanel.append("\nOutdoor users:\n"); + for(User a:known_outdoor_users) + { + knownUsersPanel.append(" " + a.getName() + " \n"); + } + displayArea.setCaretPosition(displayArea.getDocument().getLength()); + } + } + catch(Exception e) + { + e.printStackTrace(); + /*System.out.println("Socket closed"); + dest_sockets.remove(socket); + outdoor_dest_sockets.remove(socket);*/ + } + } } }