diff --git a/src/chat/ReceiveThread.java b/src/chat/ReceiveThread.java index 5433eff..0c51c8a 100644 --- a/src/chat/ReceiveThread.java +++ b/src/chat/ReceiveThread.java @@ -44,6 +44,21 @@ class ReceiveThread extends Thread { ObjectInputStream in = new ObjectInputStream(socket.getInputStream()); Notification notif = (Notification) in.readObject(); + if(!(notif.getAuthor().equals(user)) && !notif.isRedirected()) + { + System.out.println("Redirecting message"); + for(Socket s:outdoor_dest_sockets) + { + try + { + ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream()); + notif.setRedirected(true); + out.writeObject(notif); + } + catch(IOException e){} + } + } + if(isOutdoor) { for(Socket s:dest_sockets) @@ -59,36 +74,21 @@ class ReceiveThread extends Thread { } else { - if(notif instanceof Message) - { - Message m = (Message) notif; - displayArea.append(m.getAuthor() + " : " + m.getText() + "\n"); - } + 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"); - for(int i = 0;i < known_users.size();i ++) - { - if (known_users.get(i).getName().equals(dn.getAuthor())) - { - known_users.remove(i); - break; - //System.out.println("Removing " + message.getAuthor()); - } - } - for(int i = 0;i < known_outdoor_users.size();i ++) - { - if (known_outdoor_users.get(i).getName().equals(dn.getAuthor())) - { - known_outdoor_users.remove(i); - break; - //System.out.println("Removing " + message.getAuthor()); - } - } - if(!dn.isRedirected() && !dn.getAuthor().equals(user.getName())) + known_users.remove(dn.getAuthor()); + known_outdoor_users.remove(dn.getAuthor()); + + if(!dn.isRedirected() && !dn.getAuthor().equals(user)) { socket.close(); if(!isOutdoor) @@ -105,17 +105,22 @@ class ReceiveThread extends Thread { displayArea.append(cn.getAuthor().getName() + " has joined the chat.\n"); if(cn.isOutdoor()) { - known_outdoor_users.add(cn.getAuthor()); - Collections.sort(known_outdoor_users); + if(!known_outdoor_users.contains(cn.getAuthor())) + { + known_outdoor_users.add(cn.getAuthor()); + Collections.sort(known_outdoor_users); + } } else { - known_users.add(cn.getAuthor()); - Collections.sort(known_users); + if(!known_users.contains(cn.getAuthor())) + { + known_users.add(cn.getAuthor()); + Collections.sort(known_users); + } } } - - knownUsersPanel.setText(""); + knownUsersPanel.setText(""); knownUsersPanel.append("Indoor users:\n"); for(User a:known_users) { @@ -128,19 +133,6 @@ class ReceiveThread extends Thread { } displayArea.setCaretPosition(displayArea.getDocument().getLength()); } - if(!notif.getAuthor().equals(user.getName()) && !notif.isRedirected()) - { - for(Socket s:outdoor_dest_sockets) - { - try - { - ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream()); - notif.setRedirected(true); - out.writeObject(notif); - } - catch(IOException e){} - } - } } } catch(EOFException e) {} diff --git a/src/chat/User.java b/src/chat/User.java index d53e802..698a554 100644 --- a/src/chat/User.java +++ b/src/chat/User.java @@ -32,9 +32,13 @@ public class User implements Comparable, Serializable{ return address; }*/ - boolean equals(User b) + public boolean equals(Object b) { - return (b.getName() == name); + if(!(b instanceof User)) + return false; + + User u = (User) b; + return u.getName().equals(name); } public int compareTo (User otherUser)