intégration standard+connexion presque terminée A UNE JFRAME QUI SE FERME PAS PRES
This commit is contained in:
parent
f6b86d7aea
commit
a1531eebb5
7 changed files with 79 additions and 84 deletions
|
@ -19,7 +19,7 @@ public class CommunicationUDP extends Thread {
|
|||
private UDPClient client;
|
||||
private int portServer;
|
||||
private ArrayList<Integer> portOthers;
|
||||
private static ArrayList<Utilisateur> users = new ArrayList<Utilisateur>();
|
||||
private ArrayList<Utilisateur> users = new ArrayList<Utilisateur>();
|
||||
private Observer observer;
|
||||
|
||||
public CommunicationUDP(int portClient, int portServer, int[] portsOther) throws IOException {
|
||||
|
@ -33,7 +33,11 @@ public class CommunicationUDP extends Thread {
|
|||
this.observer=obs;
|
||||
}
|
||||
|
||||
protected static boolean containsUserFromID(String id) {
|
||||
public ArrayList<Utilisateur> getListUsers(){
|
||||
return users;
|
||||
}
|
||||
|
||||
protected boolean containsUserFromID(String id) {
|
||||
for(Utilisateur u : users) {
|
||||
if(u.getId().equals(id) ) {
|
||||
return true;
|
||||
|
@ -42,7 +46,7 @@ public class CommunicationUDP extends Thread {
|
|||
return false;
|
||||
}
|
||||
|
||||
public static boolean containsUserFromPseudo(String pseudo) {
|
||||
public boolean containsUserFromPseudo(String pseudo) {
|
||||
for(Utilisateur u : users) {
|
||||
if(u.getPseudo().equals(pseudo) ) {
|
||||
return true;
|
||||
|
@ -52,16 +56,18 @@ public class CommunicationUDP extends Thread {
|
|||
return false;
|
||||
}
|
||||
|
||||
private static int getIndexFromID(String id) {
|
||||
//Marche pas
|
||||
private int getIndexFromID(String id) {
|
||||
int index = -1;
|
||||
for(int i=0; i < users.size() ; i++) {
|
||||
if(users.get(i).getId().equals(id) ) {
|
||||
return i;
|
||||
if(users.get(i).getId().contentEquals(id) ) {
|
||||
index=i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
return index;
|
||||
}
|
||||
|
||||
private static int getIndexFromIP(InetAddress ip) {
|
||||
private int getIndexFromIP(InetAddress ip) {
|
||||
for(int i=0; i < users.size() ; i++) {
|
||||
if(users.get(i).getIp().equals(ip)) {
|
||||
return i;
|
||||
|
@ -74,7 +80,6 @@ public class CommunicationUDP extends Thread {
|
|||
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) {
|
||||
|
@ -86,6 +91,7 @@ public class CommunicationUDP extends Thread {
|
|||
|
||||
protected synchronized void removeUser(String idClient, String pseudoClient,InetAddress ipClient) {
|
||||
int index = getIndexFromIP(ipClient);
|
||||
//System.out.println("index : "+index);
|
||||
if( index != -1) {
|
||||
users.remove(index);
|
||||
}
|
||||
|
@ -164,7 +170,7 @@ public class CommunicationUDP extends Thread {
|
|||
for(int port : this.portOthers) {
|
||||
try {
|
||||
this.client.sendMessageUDP_local(new MessageSysteme(Message.TypeMessage.JE_SUIS_DECONNECTE), port, InetAddress.getLocalHost());
|
||||
} catch (MauvaisTypeMessageException e) {/*Si ça marche pas essayer là*/}
|
||||
} catch (MauvaisTypeMessageException e) {}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -45,7 +45,7 @@ public class UDPServer extends Thread {
|
|||
|
||||
case INFO_PSEUDO :
|
||||
|
||||
if (CommunicationUDP.containsUserFromID(((MessageSysteme) msg).getId())) {
|
||||
if (commUDP.containsUserFromID(((MessageSysteme) msg).getId())) {
|
||||
commUDP.changePseudoUser(((MessageSysteme) msg).getId(), ((MessageSysteme) msg).getPseudo(), inPacket.getAddress());
|
||||
}
|
||||
else {
|
||||
|
@ -56,7 +56,7 @@ public class UDPServer extends Thread {
|
|||
break;
|
||||
|
||||
case JE_SUIS_DECONNECTE :
|
||||
commUDP.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
|
||||
|
|
|
@ -7,7 +7,7 @@ import java.net.UnknownHostException;
|
|||
|
||||
import communication.*;
|
||||
|
||||
public class ControleurConnexion implements ActionListener {
|
||||
public class ControleurConnexion implements ActionListener, Observer{
|
||||
|
||||
private enum Etat {DEBUT, ID_OK};
|
||||
|
||||
|
@ -17,16 +17,29 @@ public class ControleurConnexion implements ActionListener {
|
|||
private String id;
|
||||
private String pseudo;
|
||||
|
||||
public ControleurConnexion(VueConnexion vue) {
|
||||
public ControleurConnexion(VueConnexion vue, int numtest) {
|
||||
this.vue = vue;
|
||||
this.etat = Etat.DEBUT;
|
||||
this.id="";
|
||||
//Pour les tests, changer pour un truc plus général quand on change CommunicationUDP
|
||||
try {
|
||||
this.comUDP = new CommunicationUDP(2208, 2209, new int[] {2906});
|
||||
switch(numtest) {
|
||||
case 0 :
|
||||
this.comUDP = new CommunicationUDP(2208, 2209, new int[] {2309, 2409});
|
||||
break;
|
||||
case 1 :
|
||||
this.comUDP = new CommunicationUDP(2308, 2309, new int[] {2209, 2409});
|
||||
break;
|
||||
case 2 :
|
||||
this.comUDP = new CommunicationUDP(2408, 2409, new int[] {2209, 2309});
|
||||
break;
|
||||
default :
|
||||
this.comUDP = new CommunicationUDP(2308, 2309, new int[] {2209, 2409});
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
comUDP.setObserver(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -36,7 +49,7 @@ public class ControleurConnexion implements ActionListener {
|
|||
id=vue.getValeurTextField();
|
||||
|
||||
//Recherche dans la liste des utilisateurs enregistres, report sur inputOK
|
||||
inputOK = id.contentEquals("idvalide");
|
||||
inputOK = (id.contentEquals("idvalide")||id.contentEquals("idv2"));
|
||||
|
||||
if (inputOK) {
|
||||
this.etat=Etat.ID_OK;
|
||||
|
@ -70,7 +83,7 @@ public class ControleurConnexion implements ActionListener {
|
|||
pseudo=vue.getValeurTextField();
|
||||
|
||||
//Recherche dans la liste locale des utilisateurs connectes, report sur inputOK
|
||||
inputOK = !Communication.containsUserFromPseudo(pseudo);
|
||||
inputOK = !comUDP.containsUserFromPseudo(pseudo);
|
||||
|
||||
if (inputOK) {
|
||||
//Reglage de l'utilisateur
|
||||
|
@ -91,11 +104,23 @@ public class ControleurConnexion implements ActionListener {
|
|||
// TODO Auto-generated catch block
|
||||
e1.printStackTrace();
|
||||
}
|
||||
//Creation de la vue principale
|
||||
vue.setTexteLabelInput("Congrations you done it");
|
||||
try {
|
||||
//Ne se ferme pas ???????
|
||||
vue.dispose();
|
||||
new VueStandard("Standard", comUDP);
|
||||
} catch (IOException e1) {
|
||||
// TODO Auto-generated catch block
|
||||
e1.printStackTrace();
|
||||
}
|
||||
}
|
||||
else vue.setTexteLabelInput("Ce nom est déjà utilisé, veuillez en choisir un autre");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(Object o, Object arg) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -24,11 +24,11 @@ public class ControleurStandard implements ActionListener, ListSelectionListener
|
|||
private VueStandard vue;
|
||||
private CommunicationUDP commUDP;
|
||||
private String lastPseudo;
|
||||
private int clientPort;
|
||||
|
||||
public ControleurStandard(VueStandard vue, int portClient, int portServer, int[] portsOther) throws IOException {
|
||||
|
||||
|
||||
public ControleurStandard(VueStandard vue, CommunicationUDP commUDP) throws IOException {
|
||||
this.vue = vue;
|
||||
this.commUDP = new CommunicationUDP(portClient,portServer, portsOther);
|
||||
this.commUDP = commUDP;
|
||||
this.commUDP.setObserver(this);
|
||||
this.commUDP.sendMessageConnecte();
|
||||
this.commUDP.sendMessageInfoPseudo();
|
||||
|
@ -57,7 +57,7 @@ public class ControleurStandard implements ActionListener, ListSelectionListener
|
|||
this.etatModif = EtatModif.EN_COURS;
|
||||
} else {
|
||||
|
||||
if (!CommunicationUDP.containsUserFromPseudo(this.vue.getDisplayedPseudo())) {
|
||||
if (!this.commUDP.containsUserFromPseudo(this.vue.getDisplayedPseudo())) {
|
||||
|
||||
Utilisateur.getSelf().setPseudo(this.vue.getDisplayedPseudo());
|
||||
|
||||
|
@ -85,11 +85,10 @@ public class ControleurStandard implements ActionListener, ListSelectionListener
|
|||
this.commUDP.removeAll();
|
||||
VueStandard.userList.removeAllElements();
|
||||
Utilisateur.getSelf().setPseudo("");
|
||||
//Ajouter code pour passer à la vue de connexion
|
||||
//
|
||||
//
|
||||
this.vue.toggleEnableButtonConnexion();
|
||||
this.vue.toggleEnableButtonDeconnexion();
|
||||
vue.dispose();
|
||||
new VueConnexion(0);
|
||||
/*this.vue.toggleEnableButtonConnexion();
|
||||
this.vue.toggleEnableButtonDeconnexion();*/
|
||||
} catch (IOException e1) {
|
||||
|
||||
e1.printStackTrace();
|
||||
|
@ -170,9 +169,9 @@ public class ControleurStandard implements ActionListener, ListSelectionListener
|
|||
ArrayList<Utilisateur> userList = (ArrayList<Utilisateur>) arg;
|
||||
ArrayList<String> listPseudo = new ArrayList<String>();
|
||||
vue.resetListUsers();
|
||||
System.out.println("Updated list :");
|
||||
//System.out.println("Updated list :");
|
||||
for (Utilisateur user : userList) {
|
||||
System.out.println(user.getPseudo());
|
||||
//System.out.println(user.getPseudo());
|
||||
listPseudo.add(user.getPseudo());
|
||||
}
|
||||
vue.addListUsers(listPseudo);
|
||||
|
|
|
@ -1,54 +1,18 @@
|
|||
package main;
|
||||
|
||||
|
||||
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JPanel;
|
||||
|
||||
|
||||
public class Main {
|
||||
|
||||
private static int portServers[] = {1526,1501,1551,1561};
|
||||
private static String ids[] = {"Raijila", "titi33", "Semtexx", "Salam"};
|
||||
private static String pseudo[] = {"Raijila", "Mirasio", "Semtexx", "Xaegon"};
|
||||
public static void main(String[] args) {
|
||||
|
||||
|
||||
switch(args[0]) {
|
||||
case "0":
|
||||
Main.createApp(0);
|
||||
break;
|
||||
case "1":
|
||||
Main.createApp(1);
|
||||
break;
|
||||
case "2":
|
||||
Main.createApp(2);
|
||||
break;
|
||||
default:
|
||||
Main.createApp(3);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// VueStandard.userList.addElement("Mirasio");
|
||||
//
|
||||
// try {
|
||||
// Thread.sleep(2000);
|
||||
// VueStandard.userList.addElement("Semtexx");
|
||||
// } catch (InterruptedException e) {
|
||||
// // TODO Auto-generated catch block
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
|
||||
|
||||
}
|
||||
public class Main extends JPanel{
|
||||
|
||||
private static void createApp(int i) {
|
||||
try {
|
||||
Utilisateur.setSelf(Main.ids[i], Main.pseudo[i], "localhost");
|
||||
new VueStandard("Application", Main.portServers[i]-1, Main.portServers[i], Main.portServers);
|
||||
} catch (IOException e) {
|
||||
System.out.println(e.toString());
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
new VueConnexion(Integer.parseInt(args[0]));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -7,8 +7,6 @@ import javax.swing.*;
|
|||
|
||||
public class VueConnexion extends Vue {
|
||||
|
||||
//Penser à regler la taille de la fenetre et a la centrer !
|
||||
|
||||
//Elements vue
|
||||
private JFrame frame;
|
||||
private JPanel panel;
|
||||
|
@ -19,9 +17,9 @@ public class VueConnexion extends Vue {
|
|||
//Controleur
|
||||
ControleurConnexion controle;
|
||||
|
||||
public VueConnexion() {
|
||||
super();
|
||||
controle = new ControleurConnexion(this);
|
||||
public VueConnexion(int numtest) {
|
||||
super("Connexion");
|
||||
controle = new ControleurConnexion(this, numtest);
|
||||
|
||||
//Creation fenetre
|
||||
frame = new JFrame("Connexion");
|
||||
|
|
|
@ -22,6 +22,8 @@ import javax.swing.JTextField;
|
|||
import javax.swing.ListSelectionModel;
|
||||
import javax.swing.ScrollPaneConstants;
|
||||
|
||||
import communication.CommunicationUDP;
|
||||
|
||||
public class VueStandard extends Vue {
|
||||
|
||||
/**
|
||||
|
@ -37,8 +39,7 @@ public class VueStandard extends Vue {
|
|||
private ControleurStandard c;
|
||||
public static DefaultListModel<String> userList = new DefaultListModel<String>();
|
||||
|
||||
|
||||
public VueStandard(String title, int port, int clientPort, int[] portsOther) throws IOException {
|
||||
public VueStandard(String title, CommunicationUDP comUDP) throws IOException {
|
||||
super(title);
|
||||
|
||||
JPanel main = new JPanel(new GridBagLayout());
|
||||
|
@ -58,7 +59,7 @@ public class VueStandard extends Vue {
|
|||
|
||||
|
||||
|
||||
this.c = new ControleurStandard(this, port, clientPort, portsOther);
|
||||
this.c = new ControleurStandard(this, comUDP);
|
||||
|
||||
//--------Panel haut pseudo--------//
|
||||
JPanel self = new JPanel(new FlowLayout());
|
||||
|
@ -75,11 +76,13 @@ public class VueStandard extends Vue {
|
|||
self.add(this.modifierPseudo);
|
||||
|
||||
//--------Panel milieu liste utilisateurs--------//
|
||||
|
||||
this.activeUsersList = new JList<String>(VueStandard.userList);
|
||||
this.activeUsersList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
||||
this.activeUsersList.setLayoutOrientation(JList.VERTICAL);
|
||||
this.activeUsersList.addListSelectionListener(this.c);
|
||||
|
||||
|
||||
JScrollPane listScroller = new JScrollPane(this.activeUsersList);
|
||||
listScroller.setPreferredSize(new Dimension(50,50));
|
||||
listScroller.setAlignmentX(LEFT_ALIGNMENT);
|
||||
|
|
Loading…
Reference in a new issue