Projet_COO_POO/POO/src/connexion/ControleurConnexion.java
2021-02-07 17:48:34 +01:00

176 lines
4.5 KiB
Java

package connexion;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.UnknownHostException;
import java.sql.SQLException;
import communication.udp.CommunicationUDP;
import database.SQLiteManager;
import main.Utilisateur;
import standard.VueStandard;
public class ControleurConnexion implements ActionListener{
private enum Etat {DEBUT, ID_OK};
private VueConnexion vue;
private Etat etat;
private CommunicationUDP comUDP;
private int portTCP;
private String username;
private SQLiteManager sqlManager;
private VueStandard vueStd;
public ControleurConnexion(VueConnexion vue, int numtest) {
this.vue = vue;
this.etat = Etat.DEBUT;
this.username = "";
this.sqlManager = new SQLiteManager(0);
this.vueStd = null;
//Pour les tests, changer pour un truc plus général quand on change CommunicationUDP
//Note : 3334 est le port du serveur de présence
int[] portServer = {2209, 2309, 2409, 2509, 3334};
try {
switch(numtest) {
case 0 :
this.comUDP = new CommunicationUDP(2208, 2209, portServer);
this.portTCP = 7010;
break;
case 1 :
this.comUDP = new CommunicationUDP(2308, 2309, portServer);
this.portTCP = 7020;
break;
case 2 :
this.comUDP = new CommunicationUDP(2408, 2409, portServer);
this.portTCP = 7030;
break;
case 3 :
this.comUDP = new CommunicationUDP(2508, 2509, portServer);
this.portTCP = 7040;
break;
default :
this.comUDP = new CommunicationUDP(2408, 2409, portServer);
this.portTCP = 7040;
}
//
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void actionPerformed(ActionEvent e) {
String pseudo;
boolean inputOK = false;
if (this.etat == Etat.DEBUT) {
this.username = this.vue.getUsernameValue();
char[] password = this.vue.getPasswordValue();
try {
int res = this.sqlManager.checkPwd(this.username, password);
inputOK = (res == 1);
} catch (SQLException e2) {
e2.printStackTrace();
}
if (inputOK) {
this.etat=Etat.ID_OK;
//Envoi broadcast du message "JeSuisActif" et, attente du retour de la liste des utilisateurs actifs
try {
comUDP.sendMessageConnecte();
} catch (UnknownHostException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
Thread.sleep(2);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//Mise en place de la demande du pseudo
this.vue.setConnexionInfo("");
this.vue.removePasswordPanel();
this.vue.setTextUsernameField("Veuillez entrer votre pseudonyme");
this.vue.resetUsernameField();
inputOK=false;
}
else {
this.vue.setConnexionInfo("Identifiant ou mot de passe invalide, veuillez réessayer");
this.vue.resetPasswordField();
}
}
else {
pseudo = vue.getUsernameValue();
//Recherche dans la liste locale des utilisateurs connectes, report sur inputOK
inputOK = !this.comUDP.containsUserFromPseudo(pseudo);
if(pseudo.equals("")) {
this.vue.setConnexionInfo("Votre pseudonyme doit contenir au moins 1 caratère");
}else if (inputOK) {
//Reglage de l'utilisateur
try {
Utilisateur.setSelf(this.username, pseudo, "localhost", this.portTCP);
} catch (UnknownHostException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
//Broadcast du pseudo
try {
this.comUDP.sendMessageInfoPseudo();
} catch (UnknownHostException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
try {
this.resetView();
this.vue.setVisible(false);
this.setVueStandard();
} catch (IOException e1) {
e1.printStackTrace();
}
}
else this.vue.setConnexionInfo("Ce nom est déjà utilisé, veuillez en choisir un autre");
}
}
private void setVueStandard() throws IOException {
if(this.vueStd == null) {
this.vueStd = new VueStandard("Standard", this.comUDP, this.portTCP, this.sqlManager, this.vue);
}else {
this.vueStd.initControleur();
this.vueStd.setPseudoSelf();
this.vueStd.setVisible(true);
}
}
private void resetView() {
this.etat = Etat.DEBUT;
this.vue.addPasswordPanel();
this.vue.resetPasswordField();
this.vue.resetUsernameField();
this.vue.setTextUsernameField("Nom d'utilisateur");
this.vue.setConnexionInfo("");
}
}