From 7b107a758f4730b0bd13ac3a8e64d4ea458796ca Mon Sep 17 00:00:00 2001 From: m-gues Date: Mon, 7 Dec 2020 18:55:31 +0100 Subject: [PATCH] =?UTF-8?q?passage=20liste=20utilisateurs=20en=20priv?= =?UTF-8?q?=C3=A9=20:=20pas=20encore=20fonctionnel?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- POO/src/communication/Communication.java | 79 --------------------- POO/src/communication/CommunicationUDP.java | 76 +++++++++++++++++++- POO/src/communication/UDPServer.java | 8 +-- POO/src/main/ControleurStandard.java | 21 ++++-- POO/src/main/Observer.java | 5 ++ POO/src/main/VueStandard.java | 11 +++ 6 files changed, 112 insertions(+), 88 deletions(-) delete mode 100644 POO/src/communication/Communication.java create mode 100644 POO/src/main/Observer.java diff --git a/POO/src/communication/Communication.java b/POO/src/communication/Communication.java deleted file mode 100644 index 55fb9a4..0000000 --- a/POO/src/communication/Communication.java +++ /dev/null @@ -1,79 +0,0 @@ -package communication; - -import java.net.InetAddress; -import java.net.UnknownHostException; -import java.util.ArrayList; -import java.util.List; - -import main.Utilisateur; -import main.VueStandard; - -public class Communication extends Thread{ - protected static ArrayList users = new ArrayList(); - - protected static boolean containsUserFromID(String id) { - for(Utilisateur u : Communication.users) { - if(u.getId().equals(id) ) { - return true; - } - } - - return false; - } - - public static boolean containsUserFromPseudo(String pseudo) { - for(Utilisateur u : Communication.users) { - if(u.getPseudo().equals(pseudo) ) { - return true; - } - } - - return false; - } - - protected static int getIndexFromID(String id) { - for(int i=0; i < Communication.users.size() ; i++) { - if(Communication.users.get(i).getId().equals(id) ) { - return i; - } - } - return -1; - } - - protected static int getIndexFromIP(InetAddress ip) { - for(int i=0; i < Communication.users.size() ; i++) { - if(Communication.users.get(i).getIp().equals(ip)) { - return i; - } - } - return -1; - } - - - protected static synchronized void addUser(String idClient, String pseudoClient, InetAddress ipClient) throws UnknownHostException { - Communication.users.add(new Utilisateur(idClient, pseudoClient, ipClient)); - VueStandard.userList.addElement(pseudoClient); - } - - protected static synchronized void changePseudoUser(String idClient, String pseudoClient, InetAddress ipClient) { - int index = Communication.getIndexFromID(idClient); - Communication.users.get(index).setPseudo(pseudoClient); - VueStandard.userList.set(index, pseudoClient); - } - - - protected static synchronized void removeUser(String idClient, String pseudoClient,InetAddress ipClient) { - int index = Communication.getIndexFromIP(ipClient); - if( index != -1) { - Communication.users.remove(index); - VueStandard.userList.remove(index); - } - } - - public static void removeAll(){ - int oSize = Communication.users.size(); - for(int i=0; i portOthers; + private static ArrayList users = new ArrayList(); + private Observer observer; public CommunicationUDP(int portClient, int portServer, int[] portsOther) throws IOException { this.portServer = portServer; @@ -24,6 +28,76 @@ public class CommunicationUDP extends Communication { new UDPServer(portServer, this).start(); this.client = new UDPClient(portClient); } + + public void setObserver (Observer obs) { + this.observer=obs; + } + + protected static boolean containsUserFromID(String id) { + for(Utilisateur u : users) { + if(u.getId().equals(id) ) { + return true; + } + } + return false; + } + + public static boolean containsUserFromPseudo(String pseudo) { + for(Utilisateur u : users) { + if(u.getPseudo().equals(pseudo) ) { + return true; + } + } + + return false; + } + + private static int getIndexFromID(String id) { + for(int i=0; i < users.size() ; i++) { + if(users.get(i).getId().equals(id) ) { + return i; + } + } + return -1; + } + + private static int getIndexFromIP(InetAddress ip) { + for(int i=0; i < users.size() ; i++) { + if(users.get(i).getIp().equals(ip)) { + return i; + } + } + return -1; + } + + + protected synchronized void addUser(String idClient, String pseudoClient, InetAddress ipClient) throws IOException { + users.add(new Utilisateur(idClient, pseudoClient, ipClient)); + observer.update(this, users); + + } + + protected synchronized void changePseudoUser(String idClient, String pseudoClient, InetAddress ipClient) { + int index = getIndexFromID(idClient); + users.get(index).setPseudo(pseudoClient); + observer.update(this, users); + } + + + protected synchronized void removeUser(String idClient, String pseudoClient,InetAddress ipClient) { + int index = getIndexFromIP(ipClient); + if( index != -1) { + users.remove(index); + } + observer.update(this, users); + } + + public void removeAll(){ + int oSize = users.size(); + for(int i=0; i getArrayListFromArray(int ports[]) { ArrayList tmp = new ArrayList(); diff --git a/POO/src/communication/UDPServer.java b/POO/src/communication/UDPServer.java index cfe4cb8..6ae4674 100644 --- a/POO/src/communication/UDPServer.java +++ b/POO/src/communication/UDPServer.java @@ -45,18 +45,18 @@ public class UDPServer extends Thread { case INFO_PSEUDO : - if (Communication.containsUserFromID(((MessageSysteme) msg).getId())) { - Communication.changePseudoUser(((MessageSysteme) msg).getId(), ((MessageSysteme) msg).getPseudo(), inPacket.getAddress()); + if (CommunicationUDP.containsUserFromID(((MessageSysteme) msg).getId())) { + commUDP.changePseudoUser(((MessageSysteme) msg).getId(), ((MessageSysteme) msg).getPseudo(), inPacket.getAddress()); } else { - Communication.addUser(((MessageSysteme) msg).getId(), ((MessageSysteme) msg).getPseudo(), inPacket.getAddress()); + commUDP.addUser(((MessageSysteme) msg).getId(), ((MessageSysteme) msg).getPseudo(), inPacket.getAddress()); System.out.println(((MessageSysteme) msg).getId()+", "+((MessageSysteme) msg).getPseudo()); } break; case JE_SUIS_DECONNECTE : - Communication.removeUser( ((MessageSysteme) msg).getId() , ((MessageSysteme) msg).getPseudo(), inPacket.getAddress() ); + commUDP.removeUser( ((MessageSysteme) msg).getId() , ((MessageSysteme) msg).getPseudo(), inPacket.getAddress() ); break; default : //Others types of messages are ignored because they are supposed to be transmitted by TCP and not UDP diff --git a/POO/src/main/ControleurStandard.java b/POO/src/main/ControleurStandard.java index 54e8587..da1c410 100644 --- a/POO/src/main/ControleurStandard.java +++ b/POO/src/main/ControleurStandard.java @@ -5,15 +5,16 @@ import java.awt.event.ActionListener; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; import java.io.IOException; +import java.util.ArrayList; + import javax.swing.JButton; import javax.swing.JList; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; -import communication.Communication; import communication.CommunicationUDP; -public class ControleurStandard implements ActionListener, ListSelectionListener, WindowListener { +public class ControleurStandard implements ActionListener, ListSelectionListener, WindowListener, Observer { private enum EtatModif { TERMINE, EN_COURS @@ -28,6 +29,7 @@ public class ControleurStandard implements ActionListener, ListSelectionListener public ControleurStandard(VueStandard vue, int portClient, int portServer, int[] portsOther) throws IOException { this.vue = vue; this.commUDP = new CommunicationUDP(portClient,portServer, portsOther); + this.commUDP.setObserver(this); this.commUDP.sendMessageConnecte(); this.commUDP.sendMessageInfoPseudo(); this.etatModif = EtatModif.TERMINE; @@ -55,7 +57,7 @@ public class ControleurStandard implements ActionListener, ListSelectionListener this.etatModif = EtatModif.EN_COURS; } else { - if (!Communication.containsUserFromPseudo(this.vue.getDisplayedPseudo())) { + if (!CommunicationUDP.containsUserFromPseudo(this.vue.getDisplayedPseudo())) { Utilisateur.getSelf().setPseudo(this.vue.getDisplayedPseudo()); @@ -80,7 +82,7 @@ public class ControleurStandard implements ActionListener, ListSelectionListener if((JButton) e.getSource() == this.vue.getButtonDeconnexion() ) { try { this.commUDP.sendMessageDelete(); - Communication.removeAll(); + this.commUDP.removeAll(); VueStandard.userList.removeAllElements(); Utilisateur.getSelf().setPseudo(""); //Ajouter code pour passer à la vue de connexion @@ -161,4 +163,15 @@ public class ControleurStandard implements ActionListener, ListSelectionListener } + //---------- OBSERVER OPERATIONS ----------// + @Override + public void update(Object o, Object arg) { + //entre dans la fonction mais affichage pas systematique : voir si pb d'affichage ou d'argument + ArrayList userList = (ArrayList) arg; + vue.resetListUsers(); + for (Utilisateur user : userList) { + vue.addListUsers(user.getPseudo()); + } + } + } diff --git a/POO/src/main/Observer.java b/POO/src/main/Observer.java new file mode 100644 index 0000000..0017f52 --- /dev/null +++ b/POO/src/main/Observer.java @@ -0,0 +1,5 @@ +package main; + +public interface Observer { + public void update(Object o, Object arg); +} diff --git a/POO/src/main/VueStandard.java b/POO/src/main/VueStandard.java index 7e6b06c..0faa9b5 100644 --- a/POO/src/main/VueStandard.java +++ b/POO/src/main/VueStandard.java @@ -186,4 +186,15 @@ public class VueStandard extends Vue { protected void toggleEnableButtonConnexion() { this.seConnecter.setEnabled(!this.seConnecter.isEnabled()); } + + //Update de la liste des utilisateurs// + protected void resetListUsers() { + VueStandard.userList = new DefaultListModel(); + this.activeUsersList = new JList(VueStandard.userList); + } + + protected void addListUsers (String newUser) { + VueStandard.userList.addElement(newUser); + } + }