diff --git a/Projet.jar b/Projet.jar index 5cbe56c..4959554 100644 Binary files a/Projet.jar and b/Projet.jar differ diff --git a/src/chat/ClientWindow.java b/src/chat/ClientWindow.java index 8497f2e..78216e9 100644 --- a/src/chat/ClientWindow.java +++ b/src/chat/ClientWindow.java @@ -137,7 +137,7 @@ public class ClientWindow implements ActionListener { if("".compareTo(message) != 0) { messageField.setText(""); - network.send(new Message(network.getUser(), message, false)); + network.send(new Message(network.getUser(), message)); } } } diff --git a/src/chat/ConnectNotification.java b/src/chat/ConnectNotification.java index 699c3ad..957daea 100644 --- a/src/chat/ConnectNotification.java +++ b/src/chat/ConnectNotification.java @@ -1,16 +1,9 @@ package chat; public class ConnectNotification extends Notification { - private Boolean outdoor; - ConnectNotification (User in_author, Boolean in_redirected, Boolean in_outdoor) + ConnectNotification (User in_author) { - author = in_author; - redirected = in_redirected; - outdoor = in_outdoor; + super(in_author); } - public Boolean isOutdoor() - { - return outdoor; - } } diff --git a/src/chat/DisconnectNotification.java b/src/chat/DisconnectNotification.java index 17cdef8..d1bb8f6 100644 --- a/src/chat/DisconnectNotification.java +++ b/src/chat/DisconnectNotification.java @@ -1,9 +1,8 @@ package chat; public class DisconnectNotification extends Notification { - DisconnectNotification (User in_author, Boolean in_redirected) + DisconnectNotification (User in_author) { - author = in_author; - redirected = in_redirected; + super(in_author); } } \ No newline at end of file diff --git a/src/chat/Message.java b/src/chat/Message.java index 6d4331b..b2cb704 100644 --- a/src/chat/Message.java +++ b/src/chat/Message.java @@ -6,11 +6,10 @@ import java.util.*; public class Message extends Notification { private String text; - Message (User in_author, String in_text, Boolean in_redirected) + Message (User in_author, String in_text) { - author = in_author; + super(in_author); text = in_text; - redirected = in_redirected; } public void setText(String new_text) { diff --git a/src/chat/NetworkClient.java b/src/chat/NetworkClient.java index e21a6ba..121ab90 100644 --- a/src/chat/NetworkClient.java +++ b/src/chat/NetworkClient.java @@ -38,7 +38,7 @@ public class NetworkClient { NetworkClient(JTextArea in_chatText, JTextArea in_knownUsersPanel) { - user = new User(""); + user = null; chatText = in_chatText; knownUsersPanel = in_knownUsersPanel; known_users = new ArrayList(); @@ -123,11 +123,11 @@ public class NetworkClient { System.out.println(usernameList.length + " users currently connected"); for(String u:usernameList) { - known_users.add(new User (u)); + known_users.add(new User (u, false)); } for(String u:outdoorUsernameList) { - known_outdoor_users.add(new User (u)); + known_outdoor_users.add(new User (u, true)); } for(String a:addressList) { @@ -193,9 +193,9 @@ public class NetworkClient { } } - user.setName(username); + user = new User(username, outdoor); - send(new ConnectNotification(user, false, outdoor)); + send(new ConnectNotification(user)); } return connected; @@ -229,7 +229,7 @@ public class NetworkClient { void disconnect() { - send(new DisconnectNotification(user, false)); + send(new DisconnectNotification(user)); try { Thread.sleep(1000); @@ -244,6 +244,16 @@ public class NetworkClient { } catch(IOException e){} } + for(Socket s:outdoor_dest_sockets) + { + try + { + s.close(); + } + catch(IOException e){} + } + dest_sockets.clear(); + outdoor_dest_sockets.clear(); } User getUser() diff --git a/src/chat/Notification.java b/src/chat/Notification.java index ab148f6..a631b26 100644 --- a/src/chat/Notification.java +++ b/src/chat/Notification.java @@ -4,8 +4,12 @@ import java.io.Serializable; abstract class Notification implements Serializable{ - protected User author; - protected Boolean redirected; + private User author; + + Notification(User in_author) + { + author = in_author; + } public void setAuthor(User new_author) { @@ -15,12 +19,4 @@ abstract class Notification implements Serializable{ { return author; } - public Boolean isRedirected() - { - return redirected; - } - public void setRedirected(Boolean in_redirected) - { - redirected = in_redirected; - } } diff --git a/src/chat/ReceiveThread.java b/src/chat/ReceiveThread.java index b7b6a4a..d25e104 100644 --- a/src/chat/ReceiveThread.java +++ b/src/chat/ReceiveThread.java @@ -45,16 +45,16 @@ class ReceiveThread extends Thread { ObjectInputStream in = new ObjectInputStream(socket.getInputStream()); Notification notif = (Notification) in.readObject(); - if(!(notif.getAuthor().equals(user)) && !notif.isRedirected()) + if(!(notif.getAuthor().equals(user)) && !(notif.getAuthor().isOutdoor() && !isOutdoor)) { for(Socket s:outdoor_dest_sockets) { try { ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream()); - notif.setRedirected(true); + out.writeObject(notif); - notif.setRedirected(false); + } catch(IOException e){} } @@ -67,11 +67,12 @@ class ReceiveThread extends Thread { try { ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream()); - notif.setRedirected(true); + out.writeObject(notif); } catch(IOException e){} } + } else { @@ -89,7 +90,7 @@ class ReceiveThread extends Thread { known_users.remove(dn.getAuthor()); known_outdoor_users.remove(dn.getAuthor()); - if(!dn.isRedirected() && !dn.getAuthor().equals(user)) + /*if(!dn.getAuthor().equals(user) && !(dn.getAuthor().isOutdoor() && !isOutdoor)) { exit = true; @@ -98,7 +99,7 @@ class ReceiveThread extends Thread { dest_sockets.remove(socket); else outdoor_dest_sockets.remove(socket); - } + }*/ } else if (notif instanceof ConnectNotification) @@ -106,7 +107,7 @@ class ReceiveThread extends Thread { ConnectNotification cn = (ConnectNotification) notif; displayArea.append(cn.getAuthor().getName() + " has joined the chat.\n"); - if(cn.isOutdoor()) + if(cn.getAuthor().isOutdoor()) { if(!known_outdoor_users.contains(cn.getAuthor())) { @@ -138,17 +139,11 @@ class ReceiveThread extends Thread { } } } - catch(EOFException e) {} - catch(SocketException e) - { - if(!isOutdoor) - dest_sockets.remove(socket); - else - outdoor_dest_sockets.remove(socket); - } catch(Exception e) { - e.printStackTrace(); + System.out.println("Socket closed"); + dest_sockets.remove(socket); + outdoor_dest_sockets.remove(socket); } } } diff --git a/src/chat/User.java b/src/chat/User.java index 698a554..00d115e 100644 --- a/src/chat/User.java +++ b/src/chat/User.java @@ -6,12 +6,14 @@ import java.util.*; public class User implements Comparable, Serializable{ private String name; + private Boolean outdoor; // private String address; - public User(String in_name) + public User(String in_name, Boolean in_outdoor) { name = in_name; + outdoor = in_outdoor; // address = in_address; } @@ -23,6 +25,10 @@ public class User implements Comparable, Serializable{ { return name; } + public Boolean isOutdoor() + { + return outdoor; + } /*public void setAddress(String new_address) { name = new_address; @@ -45,4 +51,5 @@ public class User implements Comparable, Serializable{ { return name.compareTo(otherUser.getName()); } + } \ No newline at end of file