passage liste utilisateurs en privé : pas encore fonctionnel
This commit is contained in:
parent
7a4d831d56
commit
7b107a758f
6 changed files with 112 additions and 88 deletions
|
@ -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<Utilisateur> users = new ArrayList<Utilisateur>();
|
||||
|
||||
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<oSize;i++) {
|
||||
Communication.users.remove(0);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -6,17 +6,21 @@ import java.net.UnknownHostException;
|
|||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
||||
import main.Observer;
|
||||
import main.Utilisateur;
|
||||
import main.VueStandard;
|
||||
import messages.*;
|
||||
|
||||
|
||||
public class CommunicationUDP extends Communication {
|
||||
public class CommunicationUDP extends Thread {
|
||||
|
||||
// public enum Mode {PREMIERE_CONNEXION, CHANGEMENT_PSEUDO, DECONNEXION};
|
||||
|
||||
private UDPClient client;
|
||||
private int portServer;
|
||||
private ArrayList<Integer> portOthers;
|
||||
private static ArrayList<Utilisateur> users = new ArrayList<Utilisateur>();
|
||||
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<oSize;i++) {
|
||||
users.remove(0);
|
||||
}
|
||||
}
|
||||
|
||||
private ArrayList<Integer> getArrayListFromArray(int ports[]) {
|
||||
ArrayList<Integer> tmp = new ArrayList<Integer>();
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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<Utilisateur> userList = (ArrayList<Utilisateur>) arg;
|
||||
vue.resetListUsers();
|
||||
for (Utilisateur user : userList) {
|
||||
vue.addListUsers(user.getPseudo());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
5
POO/src/main/Observer.java
Normal file
5
POO/src/main/Observer.java
Normal file
|
@ -0,0 +1,5 @@
|
|||
package main;
|
||||
|
||||
public interface Observer {
|
||||
public void update(Object o, Object arg);
|
||||
}
|
|
@ -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<String>();
|
||||
this.activeUsersList = new JList<String>(VueStandard.userList);
|
||||
}
|
||||
|
||||
protected void addListUsers (String newUser) {
|
||||
VueStandard.userList.addElement(newUser);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue