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 UDPClient client;
|
||||||
private int portServer;
|
private int portServer;
|
||||||
private ArrayList<Integer> portOthers;
|
private ArrayList<Integer> portOthers;
|
||||||
private static ArrayList<Utilisateur> users = new ArrayList<Utilisateur>();
|
private ArrayList<Utilisateur> users = new ArrayList<Utilisateur>();
|
||||||
private Observer observer;
|
private Observer observer;
|
||||||
|
|
||||||
public CommunicationUDP(int portClient, int portServer, int[] portsOther) throws IOException {
|
public CommunicationUDP(int portClient, int portServer, int[] portsOther) throws IOException {
|
||||||
|
@ -33,7 +33,11 @@ public class CommunicationUDP extends Thread {
|
||||||
this.observer=obs;
|
this.observer=obs;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static boolean containsUserFromID(String id) {
|
public ArrayList<Utilisateur> getListUsers(){
|
||||||
|
return users;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected boolean containsUserFromID(String id) {
|
||||||
for(Utilisateur u : users) {
|
for(Utilisateur u : users) {
|
||||||
if(u.getId().equals(id) ) {
|
if(u.getId().equals(id) ) {
|
||||||
return true;
|
return true;
|
||||||
|
@ -42,7 +46,7 @@ public class CommunicationUDP extends Thread {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean containsUserFromPseudo(String pseudo) {
|
public boolean containsUserFromPseudo(String pseudo) {
|
||||||
for(Utilisateur u : users) {
|
for(Utilisateur u : users) {
|
||||||
if(u.getPseudo().equals(pseudo) ) {
|
if(u.getPseudo().equals(pseudo) ) {
|
||||||
return true;
|
return true;
|
||||||
|
@ -52,16 +56,18 @@ public class CommunicationUDP extends Thread {
|
||||||
return false;
|
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++) {
|
for(int i=0; i < users.size() ; i++) {
|
||||||
if(users.get(i).getId().equals(id) ) {
|
if(users.get(i).getId().contentEquals(id) ) {
|
||||||
return i;
|
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++) {
|
for(int i=0; i < users.size() ; i++) {
|
||||||
if(users.get(i).getIp().equals(ip)) {
|
if(users.get(i).getIp().equals(ip)) {
|
||||||
return i;
|
return i;
|
||||||
|
@ -74,7 +80,6 @@ public class CommunicationUDP extends Thread {
|
||||||
protected synchronized void addUser(String idClient, String pseudoClient, InetAddress ipClient) throws IOException {
|
protected synchronized void addUser(String idClient, String pseudoClient, InetAddress ipClient) throws IOException {
|
||||||
users.add(new Utilisateur(idClient, pseudoClient, ipClient));
|
users.add(new Utilisateur(idClient, pseudoClient, ipClient));
|
||||||
observer.update(this, users);
|
observer.update(this, users);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected synchronized void changePseudoUser(String idClient, String pseudoClient, InetAddress ipClient) {
|
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) {
|
protected synchronized void removeUser(String idClient, String pseudoClient,InetAddress ipClient) {
|
||||||
int index = getIndexFromIP(ipClient);
|
int index = getIndexFromIP(ipClient);
|
||||||
|
//System.out.println("index : "+index);
|
||||||
if( index != -1) {
|
if( index != -1) {
|
||||||
users.remove(index);
|
users.remove(index);
|
||||||
}
|
}
|
||||||
|
@ -164,7 +170,7 @@ public class CommunicationUDP extends Thread {
|
||||||
for(int port : this.portOthers) {
|
for(int port : this.portOthers) {
|
||||||
try {
|
try {
|
||||||
this.client.sendMessageUDP_local(new MessageSysteme(Message.TypeMessage.JE_SUIS_DECONNECTE), port, InetAddress.getLocalHost());
|
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 :
|
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());
|
commUDP.changePseudoUser(((MessageSysteme) msg).getId(), ((MessageSysteme) msg).getPseudo(), inPacket.getAddress());
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
@ -56,7 +56,7 @@ public class UDPServer extends Thread {
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case JE_SUIS_DECONNECTE :
|
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;
|
break;
|
||||||
|
|
||||||
default : //Others types of messages are ignored because they are supposed to be transmitted by TCP and not UDP
|
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.*;
|
import communication.*;
|
||||||
|
|
||||||
public class ControleurConnexion implements ActionListener {
|
public class ControleurConnexion implements ActionListener, Observer{
|
||||||
|
|
||||||
private enum Etat {DEBUT, ID_OK};
|
private enum Etat {DEBUT, ID_OK};
|
||||||
|
|
||||||
|
@ -17,16 +17,29 @@ public class ControleurConnexion implements ActionListener {
|
||||||
private String id;
|
private String id;
|
||||||
private String pseudo;
|
private String pseudo;
|
||||||
|
|
||||||
public ControleurConnexion(VueConnexion vue) {
|
public ControleurConnexion(VueConnexion vue, int numtest) {
|
||||||
this.vue = vue;
|
this.vue = vue;
|
||||||
this.etat = Etat.DEBUT;
|
this.etat = Etat.DEBUT;
|
||||||
this.id="";
|
this.id="";
|
||||||
//Pour les tests, changer pour un truc plus général quand on change CommunicationUDP
|
//Pour les tests, changer pour un truc plus général quand on change CommunicationUDP
|
||||||
try {
|
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) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
comUDP.setObserver(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -36,7 +49,7 @@ public class ControleurConnexion implements ActionListener {
|
||||||
id=vue.getValeurTextField();
|
id=vue.getValeurTextField();
|
||||||
|
|
||||||
//Recherche dans la liste des utilisateurs enregistres, report sur inputOK
|
//Recherche dans la liste des utilisateurs enregistres, report sur inputOK
|
||||||
inputOK = id.contentEquals("idvalide");
|
inputOK = (id.contentEquals("idvalide")||id.contentEquals("idv2"));
|
||||||
|
|
||||||
if (inputOK) {
|
if (inputOK) {
|
||||||
this.etat=Etat.ID_OK;
|
this.etat=Etat.ID_OK;
|
||||||
|
@ -70,7 +83,7 @@ public class ControleurConnexion implements ActionListener {
|
||||||
pseudo=vue.getValeurTextField();
|
pseudo=vue.getValeurTextField();
|
||||||
|
|
||||||
//Recherche dans la liste locale des utilisateurs connectes, report sur inputOK
|
//Recherche dans la liste locale des utilisateurs connectes, report sur inputOK
|
||||||
inputOK = !Communication.containsUserFromPseudo(pseudo);
|
inputOK = !comUDP.containsUserFromPseudo(pseudo);
|
||||||
|
|
||||||
if (inputOK) {
|
if (inputOK) {
|
||||||
//Reglage de l'utilisateur
|
//Reglage de l'utilisateur
|
||||||
|
@ -91,11 +104,23 @@ public class ControleurConnexion implements ActionListener {
|
||||||
// TODO Auto-generated catch block
|
// TODO Auto-generated catch block
|
||||||
e1.printStackTrace();
|
e1.printStackTrace();
|
||||||
}
|
}
|
||||||
//Creation de la vue principale
|
try {
|
||||||
vue.setTexteLabelInput("Congrations you done it");
|
//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");
|
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 VueStandard vue;
|
||||||
private CommunicationUDP commUDP;
|
private CommunicationUDP commUDP;
|
||||||
private String lastPseudo;
|
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.vue = vue;
|
||||||
this.commUDP = new CommunicationUDP(portClient,portServer, portsOther);
|
this.commUDP = commUDP;
|
||||||
this.commUDP.setObserver(this);
|
this.commUDP.setObserver(this);
|
||||||
this.commUDP.sendMessageConnecte();
|
this.commUDP.sendMessageConnecte();
|
||||||
this.commUDP.sendMessageInfoPseudo();
|
this.commUDP.sendMessageInfoPseudo();
|
||||||
|
@ -57,7 +57,7 @@ public class ControleurStandard implements ActionListener, ListSelectionListener
|
||||||
this.etatModif = EtatModif.EN_COURS;
|
this.etatModif = EtatModif.EN_COURS;
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
if (!CommunicationUDP.containsUserFromPseudo(this.vue.getDisplayedPseudo())) {
|
if (!this.commUDP.containsUserFromPseudo(this.vue.getDisplayedPseudo())) {
|
||||||
|
|
||||||
Utilisateur.getSelf().setPseudo(this.vue.getDisplayedPseudo());
|
Utilisateur.getSelf().setPseudo(this.vue.getDisplayedPseudo());
|
||||||
|
|
||||||
|
@ -85,11 +85,10 @@ public class ControleurStandard implements ActionListener, ListSelectionListener
|
||||||
this.commUDP.removeAll();
|
this.commUDP.removeAll();
|
||||||
VueStandard.userList.removeAllElements();
|
VueStandard.userList.removeAllElements();
|
||||||
Utilisateur.getSelf().setPseudo("");
|
Utilisateur.getSelf().setPseudo("");
|
||||||
//Ajouter code pour passer à la vue de connexion
|
vue.dispose();
|
||||||
//
|
new VueConnexion(0);
|
||||||
//
|
/*this.vue.toggleEnableButtonConnexion();
|
||||||
this.vue.toggleEnableButtonConnexion();
|
this.vue.toggleEnableButtonDeconnexion();*/
|
||||||
this.vue.toggleEnableButtonDeconnexion();
|
|
||||||
} catch (IOException e1) {
|
} catch (IOException e1) {
|
||||||
|
|
||||||
e1.printStackTrace();
|
e1.printStackTrace();
|
||||||
|
@ -170,9 +169,9 @@ public class ControleurStandard implements ActionListener, ListSelectionListener
|
||||||
ArrayList<Utilisateur> userList = (ArrayList<Utilisateur>) arg;
|
ArrayList<Utilisateur> userList = (ArrayList<Utilisateur>) arg;
|
||||||
ArrayList<String> listPseudo = new ArrayList<String>();
|
ArrayList<String> listPseudo = new ArrayList<String>();
|
||||||
vue.resetListUsers();
|
vue.resetListUsers();
|
||||||
System.out.println("Updated list :");
|
//System.out.println("Updated list :");
|
||||||
for (Utilisateur user : userList) {
|
for (Utilisateur user : userList) {
|
||||||
System.out.println(user.getPseudo());
|
//System.out.println(user.getPseudo());
|
||||||
listPseudo.add(user.getPseudo());
|
listPseudo.add(user.getPseudo());
|
||||||
}
|
}
|
||||||
vue.addListUsers(listPseudo);
|
vue.addListUsers(listPseudo);
|
||||||
|
|
|
@ -1,54 +1,18 @@
|
||||||
package main;
|
package main;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import javax.swing.JFrame;
|
||||||
|
import javax.swing.JPanel;
|
||||||
|
|
||||||
|
public class Main extends 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) {
|
public static void main(String[] args) {
|
||||||
|
new VueConnexion(Integer.parseInt(args[0]));
|
||||||
|
|
||||||
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();
|
|
||||||
// }
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
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());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,8 +7,6 @@ import javax.swing.*;
|
||||||
|
|
||||||
public class VueConnexion extends Vue {
|
public class VueConnexion extends Vue {
|
||||||
|
|
||||||
//Penser à regler la taille de la fenetre et a la centrer !
|
|
||||||
|
|
||||||
//Elements vue
|
//Elements vue
|
||||||
private JFrame frame;
|
private JFrame frame;
|
||||||
private JPanel panel;
|
private JPanel panel;
|
||||||
|
@ -19,9 +17,9 @@ public class VueConnexion extends Vue {
|
||||||
//Controleur
|
//Controleur
|
||||||
ControleurConnexion controle;
|
ControleurConnexion controle;
|
||||||
|
|
||||||
public VueConnexion() {
|
public VueConnexion(int numtest) {
|
||||||
super();
|
super("Connexion");
|
||||||
controle = new ControleurConnexion(this);
|
controle = new ControleurConnexion(this, numtest);
|
||||||
|
|
||||||
//Creation fenetre
|
//Creation fenetre
|
||||||
frame = new JFrame("Connexion");
|
frame = new JFrame("Connexion");
|
||||||
|
|
|
@ -22,6 +22,8 @@ import javax.swing.JTextField;
|
||||||
import javax.swing.ListSelectionModel;
|
import javax.swing.ListSelectionModel;
|
||||||
import javax.swing.ScrollPaneConstants;
|
import javax.swing.ScrollPaneConstants;
|
||||||
|
|
||||||
|
import communication.CommunicationUDP;
|
||||||
|
|
||||||
public class VueStandard extends Vue {
|
public class VueStandard extends Vue {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -37,8 +39,7 @@ public class VueStandard extends Vue {
|
||||||
private ControleurStandard c;
|
private ControleurStandard c;
|
||||||
public static DefaultListModel<String> userList = new DefaultListModel<String>();
|
public static DefaultListModel<String> userList = new DefaultListModel<String>();
|
||||||
|
|
||||||
|
public VueStandard(String title, CommunicationUDP comUDP) throws IOException {
|
||||||
public VueStandard(String title, int port, int clientPort, int[] portsOther) throws IOException {
|
|
||||||
super(title);
|
super(title);
|
||||||
|
|
||||||
JPanel main = new JPanel(new GridBagLayout());
|
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--------//
|
//--------Panel haut pseudo--------//
|
||||||
JPanel self = new JPanel(new FlowLayout());
|
JPanel self = new JPanel(new FlowLayout());
|
||||||
|
@ -75,11 +76,13 @@ public class VueStandard extends Vue {
|
||||||
self.add(this.modifierPseudo);
|
self.add(this.modifierPseudo);
|
||||||
|
|
||||||
//--------Panel milieu liste utilisateurs--------//
|
//--------Panel milieu liste utilisateurs--------//
|
||||||
|
|
||||||
this.activeUsersList = new JList<String>(VueStandard.userList);
|
this.activeUsersList = new JList<String>(VueStandard.userList);
|
||||||
this.activeUsersList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
this.activeUsersList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
||||||
this.activeUsersList.setLayoutOrientation(JList.VERTICAL);
|
this.activeUsersList.setLayoutOrientation(JList.VERTICAL);
|
||||||
this.activeUsersList.addListSelectionListener(this.c);
|
this.activeUsersList.addListSelectionListener(this.c);
|
||||||
|
|
||||||
|
|
||||||
JScrollPane listScroller = new JScrollPane(this.activeUsersList);
|
JScrollPane listScroller = new JScrollPane(this.activeUsersList);
|
||||||
listScroller.setPreferredSize(new Dimension(50,50));
|
listScroller.setPreferredSize(new Dimension(50,50));
|
||||||
listScroller.setAlignmentX(LEFT_ALIGNMENT);
|
listScroller.setAlignmentX(LEFT_ALIGNMENT);
|
||||||
|
|
Loading…
Reference in a new issue