Browse Source

outdoor users(13)

Louis Farina 3 years ago
parent
commit
748ab397cd
2 changed files with 41 additions and 45 deletions
  1. 35
    43
      src/chat/ReceiveThread.java
  2. 6
    2
      src/chat/User.java

+ 35
- 43
src/chat/ReceiveThread.java View File

@@ -44,6 +44,21 @@ class ReceiveThread extends Thread {
44 44
         			ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
45 45
 			        Notification notif = (Notification) in.readObject();
46 46
 			        
47
+			        if(!(notif.getAuthor().equals(user)) && !notif.isRedirected())
48
+	            	{
49
+	            		System.out.println("Redirecting message");
50
+	            		for(Socket s:outdoor_dest_sockets)
51
+				    	{
52
+				    		try
53
+				    		{
54
+				    			ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
55
+				    			notif.setRedirected(true);
56
+				    		    out.writeObject(notif);
57
+				    		}
58
+				    		catch(IOException e){}
59
+				    	}
60
+	            	}
61
+			        
47 62
 	            	if(isOutdoor)
48 63
 		            {
49 64
 		            	for(Socket s:dest_sockets)
@@ -59,36 +74,21 @@ class ReceiveThread extends Thread {
59 74
 		            }
60 75
 	            	else
61 76
 	            	{
62
-			        	if(notif instanceof Message)
63
-			        	{
64
-			        		Message m = (Message) notif;
65
-				            displayArea.append(m.getAuthor() + " : " + m.getText() + "\n");
66
-			        	}
77
+	            		if(notif instanceof Message)
78
+				        {
79
+				        	Message m = (Message) notif;
80
+					        displayArea.append(m.getAuthor().getName() + " : " + m.getText() + "\n");
81
+				        }
67 82
 			        	else if (notif instanceof DisconnectNotification)
68 83
 			        	{
69 84
 			        		DisconnectNotification dn = (DisconnectNotification) notif;
70 85
 			        		
71 86
 				            displayArea.append(dn.getAuthor().getName() + " has left the chat.\n");
72
-				            for(int i = 0;i < known_users.size();i ++)
73
-				            {
74
-				            	if (known_users.get(i).getName().equals(dn.getAuthor()))
75
-				            	{
76
-				            		known_users.remove(i);
77
-				            		break;
78
-				            		//System.out.println("Removing " + message.getAuthor());
79
-				            	}
80
-				            }
81
-				            for(int i = 0;i < known_outdoor_users.size();i ++)
82
-				            {
83
-				            	if (known_outdoor_users.get(i).getName().equals(dn.getAuthor()))
84
-				            	{
85
-				            		known_outdoor_users.remove(i);
86
-				            		break;
87
-				            		//System.out.println("Removing " + message.getAuthor());
88
-				            	}
89
-				            }
90 87
 				            
91
-				            if(!dn.isRedirected() && !dn.getAuthor().equals(user.getName()))
88
+				            known_users.remove(dn.getAuthor());
89
+				            known_outdoor_users.remove(dn.getAuthor());
90
+				            
91
+				            if(!dn.isRedirected() && !dn.getAuthor().equals(user))
92 92
 				            {
93 93
 				            	socket.close();
94 94
 				            	if(!isOutdoor)
@@ -105,17 +105,22 @@ class ReceiveThread extends Thread {
105 105
 				            displayArea.append(cn.getAuthor().getName() + " has joined the chat.\n");
106 106
 				            if(cn.isOutdoor())
107 107
 				            {
108
-				            	known_outdoor_users.add(cn.getAuthor());
109
-					            Collections.sort(known_outdoor_users);
108
+				            	if(!known_outdoor_users.contains(cn.getAuthor()))
109
+				            	{
110
+				            		known_outdoor_users.add(cn.getAuthor());
111
+						            Collections.sort(known_outdoor_users);
112
+				            	}
110 113
 				            }
111 114
 				            else
112 115
 				            {
113
-				            	known_users.add(cn.getAuthor());
114
-					            Collections.sort(known_users);
116
+				            	if(!known_users.contains(cn.getAuthor()))
117
+				            	{
118
+				            		known_users.add(cn.getAuthor());
119
+						            Collections.sort(known_users);
120
+				            	}
115 121
 				            }
116 122
 			        	}
117
-			        		
118
-			        	knownUsersPanel.setText("");
123
+	            		knownUsersPanel.setText("");
119 124
 			            knownUsersPanel.append("Indoor users:\n");
120 125
 			            for(User a:known_users)
121 126
 			            {
@@ -128,19 +133,6 @@ class ReceiveThread extends Thread {
128 133
 			            }
129 134
 			            displayArea.setCaretPosition(displayArea.getDocument().getLength());
130 135
 	            	}
131
-	            	if(!notif.getAuthor().equals(user.getName()) && !notif.isRedirected())
132
-	            	{
133
-	            		for(Socket s:outdoor_dest_sockets)
134
-				    	{
135
-				    		try
136
-				    		{
137
-				    			ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
138
-				    			notif.setRedirected(true);
139
-				    		    out.writeObject(notif);
140
-				    		}
141
-				    		catch(IOException e){}
142
-				    	}
143
-	            	}
144 136
 		      }
145 137
     	}
146 138
 		catch(EOFException e) {}

+ 6
- 2
src/chat/User.java View File

@@ -32,9 +32,13 @@ public class User implements Comparable<User>, Serializable{
32 32
 		return address;
33 33
 	}*/
34 34
 	
35
-	boolean equals(User b)
35
+	public boolean equals(Object b)
36 36
 	{
37
-		return (b.getName() == name);
37
+		if(!(b instanceof User))
38
+			return false;
39
+		
40
+		User u = (User) b;
41
+		return u.getName().equals(name);
38 42
 	}
39 43
 	
40 44
 	public int compareTo (User otherUser)

Loading…
Cancel
Save