parent
18321eb1ef
commit
3ce1752ddd
9 changed files with 140 additions and 142 deletions
BIN
Projet.jar
BIN
Projet.jar
Binary file not shown.
|
@ -137,7 +137,7 @@ public class ClientWindow implements ActionListener {
|
||||||
if("".compareTo(message) != 0)
|
if("".compareTo(message) != 0)
|
||||||
{
|
{
|
||||||
messageField.setText("");
|
messageField.setText("");
|
||||||
network.send(new Message(network.getUser(), message));
|
network.send(new Message(network.getUser(), message, false));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,16 @@
|
||||||
package chat;
|
package chat;
|
||||||
|
|
||||||
public class ConnectNotification extends Notification {
|
public class ConnectNotification extends Notification {
|
||||||
ConnectNotification (User in_author)
|
private Boolean outdoor;
|
||||||
|
ConnectNotification (User in_author, Boolean in_redirected, Boolean in_outdoor)
|
||||||
{
|
{
|
||||||
super(in_author);
|
author = in_author;
|
||||||
|
redirected = in_redirected;
|
||||||
|
outdoor = in_outdoor;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Boolean isOutdoor()
|
||||||
|
{
|
||||||
|
return outdoor;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
package chat;
|
package chat;
|
||||||
|
|
||||||
public class DisconnectNotification extends Notification {
|
public class DisconnectNotification extends Notification {
|
||||||
DisconnectNotification (User in_author)
|
DisconnectNotification (User in_author, Boolean in_redirected)
|
||||||
{
|
{
|
||||||
super(in_author);
|
author = in_author;
|
||||||
|
redirected = in_redirected;
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -6,10 +6,11 @@ import java.util.*;
|
||||||
public class Message extends Notification {
|
public class Message extends Notification {
|
||||||
private String text;
|
private String text;
|
||||||
|
|
||||||
Message (User in_author, String in_text)
|
Message (User in_author, String in_text, Boolean in_redirected)
|
||||||
{
|
{
|
||||||
super(in_author);
|
author = in_author;
|
||||||
text = in_text;
|
text = in_text;
|
||||||
|
redirected = in_redirected;
|
||||||
}
|
}
|
||||||
public void setText(String new_text)
|
public void setText(String new_text)
|
||||||
{
|
{
|
||||||
|
|
|
@ -38,7 +38,7 @@ public class NetworkClient {
|
||||||
|
|
||||||
NetworkClient(JTextArea in_chatText, JTextArea in_knownUsersPanel)
|
NetworkClient(JTextArea in_chatText, JTextArea in_knownUsersPanel)
|
||||||
{
|
{
|
||||||
user = null;
|
user = new User("");
|
||||||
chatText = in_chatText;
|
chatText = in_chatText;
|
||||||
knownUsersPanel = in_knownUsersPanel;
|
knownUsersPanel = in_knownUsersPanel;
|
||||||
known_users = new ArrayList<User>();
|
known_users = new ArrayList<User>();
|
||||||
|
@ -123,11 +123,11 @@ public class NetworkClient {
|
||||||
System.out.println(usernameList.length + " users currently connected");
|
System.out.println(usernameList.length + " users currently connected");
|
||||||
for(String u:usernameList)
|
for(String u:usernameList)
|
||||||
{
|
{
|
||||||
known_users.add(new User (u, false));
|
known_users.add(new User (u));
|
||||||
}
|
}
|
||||||
for(String u:outdoorUsernameList)
|
for(String u:outdoorUsernameList)
|
||||||
{
|
{
|
||||||
known_outdoor_users.add(new User (u, true));
|
known_outdoor_users.add(new User (u));
|
||||||
}
|
}
|
||||||
for(String a:addressList)
|
for(String a:addressList)
|
||||||
{
|
{
|
||||||
|
@ -193,9 +193,9 @@ public class NetworkClient {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
user = new User(username, outdoor);
|
user.setName(username);
|
||||||
|
|
||||||
send(new ConnectNotification(user));
|
send(new ConnectNotification(user, false, outdoor));
|
||||||
}
|
}
|
||||||
|
|
||||||
return connected;
|
return connected;
|
||||||
|
@ -229,7 +229,7 @@ public class NetworkClient {
|
||||||
|
|
||||||
void disconnect()
|
void disconnect()
|
||||||
{
|
{
|
||||||
send(new DisconnectNotification(user));
|
send(new DisconnectNotification(user, false));
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Thread.sleep(1000);
|
Thread.sleep(1000);
|
||||||
|
@ -244,16 +244,6 @@ public class NetworkClient {
|
||||||
}
|
}
|
||||||
catch(IOException e){}
|
catch(IOException e){}
|
||||||
}
|
}
|
||||||
for(Socket s:outdoor_dest_sockets)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
s.close();
|
|
||||||
}
|
|
||||||
catch(IOException e){}
|
|
||||||
}
|
|
||||||
dest_sockets.clear();
|
|
||||||
outdoor_dest_sockets.clear();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
User getUser()
|
User getUser()
|
||||||
|
|
|
@ -4,12 +4,8 @@ import java.io.Serializable;
|
||||||
|
|
||||||
abstract class Notification implements Serializable{
|
abstract class Notification implements Serializable{
|
||||||
|
|
||||||
private User author;
|
protected User author;
|
||||||
|
protected Boolean redirected;
|
||||||
Notification(User in_author)
|
|
||||||
{
|
|
||||||
author = in_author;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setAuthor(User new_author)
|
public void setAuthor(User new_author)
|
||||||
{
|
{
|
||||||
|
@ -19,4 +15,12 @@ abstract class Notification implements Serializable{
|
||||||
{
|
{
|
||||||
return author;
|
return author;
|
||||||
}
|
}
|
||||||
|
public Boolean isRedirected()
|
||||||
|
{
|
||||||
|
return redirected;
|
||||||
|
}
|
||||||
|
public void setRedirected(Boolean in_redirected)
|
||||||
|
{
|
||||||
|
redirected = in_redirected;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,25 +37,24 @@ class ReceiveThread extends Thread {
|
||||||
}
|
}
|
||||||
public void run()
|
public void run()
|
||||||
{
|
{
|
||||||
ObjectInputStream in;
|
|
||||||
boolean exit = false;
|
boolean exit = false;
|
||||||
while(!exit)
|
|
||||||
{
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
in = new ObjectInputStream(socket.getInputStream());
|
while(!exit)
|
||||||
|
{
|
||||||
|
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
|
||||||
Notification notif = (Notification) in.readObject();
|
Notification notif = (Notification) in.readObject();
|
||||||
|
|
||||||
if(!(notif.getAuthor().equals(user)) && !(notif.getAuthor().isOutdoor() && !isOutdoor))
|
if(!(notif.getAuthor().equals(user)) && !notif.isRedirected())
|
||||||
{
|
{
|
||||||
for(Socket s:outdoor_dest_sockets)
|
for(Socket s:outdoor_dest_sockets)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
|
ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
|
||||||
|
notif.setRedirected(true);
|
||||||
out.writeObject(notif);
|
out.writeObject(notif);
|
||||||
|
notif.setRedirected(false);
|
||||||
}
|
}
|
||||||
catch(IOException e){}
|
catch(IOException e){}
|
||||||
}
|
}
|
||||||
|
@ -68,12 +67,11 @@ class ReceiveThread extends Thread {
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
|
ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
|
||||||
|
notif.setRedirected(true);
|
||||||
out.writeObject(notif);
|
out.writeObject(notif);
|
||||||
}
|
}
|
||||||
catch(IOException e){}
|
catch(IOException e){}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -91,7 +89,7 @@ class ReceiveThread extends Thread {
|
||||||
known_users.remove(dn.getAuthor());
|
known_users.remove(dn.getAuthor());
|
||||||
known_outdoor_users.remove(dn.getAuthor());
|
known_outdoor_users.remove(dn.getAuthor());
|
||||||
|
|
||||||
if(!dn.getAuthor().equals(user) && !(dn.getAuthor().isOutdoor() && !isOutdoor))
|
if(!dn.isRedirected() && !dn.getAuthor().equals(user))
|
||||||
{
|
{
|
||||||
exit = true;
|
exit = true;
|
||||||
|
|
||||||
|
@ -108,7 +106,7 @@ class ReceiveThread extends Thread {
|
||||||
ConnectNotification cn = (ConnectNotification) notif;
|
ConnectNotification cn = (ConnectNotification) notif;
|
||||||
|
|
||||||
displayArea.append(cn.getAuthor().getName() + " has joined the chat.\n");
|
displayArea.append(cn.getAuthor().getName() + " has joined the chat.\n");
|
||||||
if(cn.getAuthor().isOutdoor())
|
if(cn.isOutdoor())
|
||||||
{
|
{
|
||||||
if(!known_outdoor_users.contains(cn.getAuthor()))
|
if(!known_outdoor_users.contains(cn.getAuthor()))
|
||||||
{
|
{
|
||||||
|
@ -139,14 +137,18 @@ class ReceiveThread extends Thread {
|
||||||
displayArea.setCaretPosition(displayArea.getDocument().getLength());
|
displayArea.setCaretPosition(displayArea.getDocument().getLength());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
catch(EOFException e) {}
|
||||||
|
catch(SocketException e)
|
||||||
|
{
|
||||||
|
if(!isOutdoor)
|
||||||
|
dest_sockets.remove(socket);
|
||||||
|
else
|
||||||
|
outdoor_dest_sockets.remove(socket);
|
||||||
|
}
|
||||||
catch(Exception e)
|
catch(Exception e)
|
||||||
{
|
{
|
||||||
in.reset();
|
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
/*System.out.println("Socket closed");
|
|
||||||
dest_sockets.remove(socket);
|
|
||||||
outdoor_dest_sockets.remove(socket);*/
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,14 +6,12 @@ import java.util.*;
|
||||||
public class User implements Comparable<User>, Serializable{
|
public class User implements Comparable<User>, Serializable{
|
||||||
|
|
||||||
private String name;
|
private String name;
|
||||||
private Boolean outdoor;
|
|
||||||
// private String address;
|
// private String address;
|
||||||
|
|
||||||
|
|
||||||
public User(String in_name, Boolean in_outdoor)
|
public User(String in_name)
|
||||||
{
|
{
|
||||||
name = in_name;
|
name = in_name;
|
||||||
outdoor = in_outdoor;
|
|
||||||
// address = in_address;
|
// address = in_address;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -25,10 +23,6 @@ public class User implements Comparable<User>, Serializable{
|
||||||
{
|
{
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
public Boolean isOutdoor()
|
|
||||||
{
|
|
||||||
return outdoor;
|
|
||||||
}
|
|
||||||
/*public void setAddress(String new_address)
|
/*public void setAddress(String new_address)
|
||||||
{
|
{
|
||||||
name = new_address;
|
name = new_address;
|
||||||
|
@ -51,5 +45,4 @@ public class User implements Comparable<User>, Serializable{
|
||||||
{
|
{
|
||||||
return name.compareTo(otherUser.getName());
|
return name.compareTo(otherUser.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in a new issue