local v finished, connexion GUI and switch b/w co and std improved

This commit is contained in:
Cavailles Kevin 2021-01-31 12:52:49 +01:00
parent d7444342dc
commit c33e75add7
14 changed files with 230 additions and 144 deletions

View file

@ -109,4 +109,12 @@ public class FileTransferUtils {
BufferedImage image = gc.createCompatibleImage(w, h); BufferedImage image = gc.createCompatibleImage(w, h);
return image; return image;
} }
public static void createDownloads() {
File downloads = new File(FileTransferUtils.DOWNLOADS_RELATIVE_PATH);
if(!downloads.exists()) {
downloads.mkdir();
}
}
} }

View file

@ -10,12 +10,15 @@ import observers.ObserverInputMessage;
public class TCPServer extends Thread { public class TCPServer extends Thread {
//****
public static int PORT_SERVER = 7000;
private ServerSocket sockListenTCP; private ServerSocket sockListenTCP;
private ObserverInputMessage obs; private ObserverInputMessage obs;
public TCPServer(int port) throws UnknownHostException, IOException { public TCPServer(int port) throws UnknownHostException, IOException {
this.sockListenTCP = new ServerSocket(port, 5, InetAddress.getLocalHost()); this.sockListenTCP = new ServerSocket(port, 50, InetAddress.getLocalHost());
} }
@Override @Override

View file

@ -2,6 +2,7 @@ package communication.udp;
import java.io.IOException; import java.io.IOException;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.util.ArrayList; import java.util.ArrayList;
@ -12,6 +13,11 @@ import observers.ObserverUserList;
public class CommunicationUDP extends Thread { public class CommunicationUDP extends Thread {
// ****
protected static int PORT_SERVEUR = 3000;
// ****
protected static int PORT_CLIENT = 2000;
private UDPClient client; private UDPClient client;
private UDPServer server; private UDPServer server;
private int portServer; private int portServer;
@ -27,6 +33,14 @@ public class CommunicationUDP extends Thread {
this.client = new UDPClient(portClient); this.client = new UDPClient(portClient);
} }
// ****
public CommunicationUDP() throws SocketException, UnknownHostException {
this.portServer = PORT_SERVEUR;
this.server = new UDPServer(portServer, this);
this.server.start();
this.client = new UDPClient(PORT_CLIENT);
}
private ArrayList<Integer> getArrayListFromArray(int ports[]) { private ArrayList<Integer> getArrayListFromArray(int ports[]) {
ArrayList<Integer> tmp = new ArrayList<Integer>(); ArrayList<Integer> tmp = new ArrayList<Integer>();
for (int port : ports) { for (int port : ports) {
@ -41,27 +55,27 @@ public class CommunicationUDP extends Thread {
this.observer = obs; this.observer = obs;
} }
//-------------- USER LIST UPDATE FUNCTION --------------// // -------------- USER LIST UPDATE FUNCTION --------------//
protected synchronized void addUser(String idClient, String pseudoClient, InetAddress ipClient, int port) protected synchronized void addUser(String idClient, String pseudoClient, InetAddress ipClient, int port)
throws IOException { throws IOException {
users.add(new Utilisateur(idClient, pseudoClient, ipClient, port)); users.add(new Utilisateur(idClient, pseudoClient, ipClient, port));
observer.updateList(this, users); this.sendUpdate();
} }
protected synchronized void changePseudoUser(String idClient, String pseudoClient, InetAddress ipClient, int port) { protected synchronized void changePseudoUser(String idClient, String pseudoClient, InetAddress ipClient, int port) {
int index = getIndexFromID(idClient); int index = getIndexFromID(idClient);
users.get(index).setPseudo(pseudoClient); users.get(index).setPseudo(pseudoClient);
observer.updateList(this, users); this.sendUpdate();
} }
protected synchronized void removeUser(String idClient, String pseudoClient, InetAddress ipClient, int port) { protected synchronized void removeUser(String idClient, String pseudoClient, InetAddress ipClient, int port) {
int index = getIndexFromIP(ipClient); int index = getIndexFromID(idClient);
if (index != -1) { if (index != -1) {
users.remove(index); users.remove(index);
} }
observer.updateList(this, users); this.sendUpdate();
} }
public void removeAll() { public void removeAll() {
@ -71,7 +85,7 @@ public class CommunicationUDP extends Thread {
} }
} }
//-------------- CHECKERS --------------// // -------------- CHECKERS --------------//
protected boolean containsUserFromID(String id) { protected boolean containsUserFromID(String id) {
for (Utilisateur u : users) { for (Utilisateur u : users) {
@ -92,7 +106,7 @@ public class CommunicationUDP extends Thread {
return false; return false;
} }
//-------------- GETTERS --------------// // -------------- GETTERS --------------//
public Utilisateur getUserFromPseudo(String pseudo) { public Utilisateur getUserFromPseudo(String pseudo) {
for (int i = 0; i < users.size(); i++) { for (int i = 0; i < users.size(); i++) {
@ -112,19 +126,8 @@ public class CommunicationUDP extends Thread {
return -1; return -1;
} }
private int getIndexFromIP(InetAddress ip) { // -------------- SEND MESSAGES --------------//
for (int i = 0; i < users.size(); i++) {
if (users.get(i).getIp().equals(ip)) {
return i;
}
}
return -1;
}
//-------------- SEND MESSAGES --------------//
public void sendMessageConnecte() throws UnknownHostException, IOException { public void sendMessageConnecte() throws UnknownHostException, IOException {
for (int port : this.portOthers) { for (int port : this.portOthers) {
try { try {
@ -145,15 +148,10 @@ public class CommunicationUDP extends Thread {
Utilisateur self = Utilisateur.getSelf(); Utilisateur self = Utilisateur.getSelf();
String pseudoSelf = self.getPseudo();
String idSelf = self.getId();
int portSelf = self.getPort();
Message msout = null;
try { try {
msout = new MessageSysteme(Message.TypeMessage.INFO_PSEUDO, pseudoSelf, idSelf, portSelf); Message msgOut = new MessageSysteme(Message.TypeMessage.INFO_PSEUDO, self.getPseudo(), self.getId(), self.getPort());
for (int port : this.portOthers) { for (int port : this.portOthers) {
this.client.sendMessageUDP_local(msout, port, InetAddress.getLocalHost()); this.client.sendMessageUDP_local(msgOut, port, InetAddress.getLocalHost());
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
@ -167,9 +165,9 @@ public class CommunicationUDP extends Thread {
Utilisateur self = Utilisateur.getSelf(); Utilisateur self = Utilisateur.getSelf();
try { try {
Message msout = new MessageSysteme(Message.TypeMessage.INFO_PSEUDO, self.getPseudo(), self.getId(), Message msgOut = new MessageSysteme(Message.TypeMessage.INFO_PSEUDO, self.getPseudo(), self.getId(),
self.getPort()); self.getPort());
this.client.sendMessageUDP_local(msout, portOther, InetAddress.getLocalHost()); this.client.sendMessageUDP_local(msgOut, portOther, InetAddress.getLocalHost());
} catch (MauvaisTypeMessageException e) { } catch (MauvaisTypeMessageException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -180,32 +178,26 @@ public class CommunicationUDP extends Thread {
// This allows the receivers' agent (portOthers) to delete the entry // This allows the receivers' agent (portOthers) to delete the entry
// corresponding to this agent // corresponding to this agent
public void sendMessageDelete() throws UnknownHostException, IOException { public void sendMessageDelete() throws UnknownHostException, IOException {
for (int port : this.portOthers) { Utilisateur self = Utilisateur.getSelf();
try { try {
this.client.sendMessageUDP_local(new MessageSysteme(Message.TypeMessage.JE_SUIS_DECONNECTE), port,
InetAddress.getLocalHost()); Message msgOut = new MessageSysteme(Message.TypeMessage.JE_SUIS_DECONNECTE, self.getPseudo(), self.getId(), self.getPort());
} catch (MauvaisTypeMessageException e) { for (int port : this.portOthers) {
this.client.sendMessageUDP_local(msgOut, port, InetAddress.getLocalHost());
} }
} catch (MauvaisTypeMessageException e) {
}
}
private void sendUpdate() {
if(this.observer != null) {
this.observer.updateList(this, users);
} }
} }
// Pas encore adapte message
// private void sendIDPseudo_broadcast(String prefixe) throws UnknownHostException, IOException {
// Utilisateur self = Utilisateur.getSelf();
// String idSelf = self.getId();
// String pseudoSelf = self.getPseudo();
//
// String message = prefixe+","+idSelf + "," + pseudoSelf;
//
//
// this.client.sendMessageUDP_broadcast(message, this.portServer);
//
// }
// public synchronized void createSenderUDP(int port, Mode mode) throws SocketException {
// new SenderUDP(mode, port).start();
// }
public void destroyAll() { public void destroyAll() {
this.client.destroyAll(); this.client.destroyAll();
this.server.interrupt(); this.server.interrupt();

View file

@ -21,6 +21,8 @@ public class UDPClient {
InetAddress localHost = InetAddress.getLocalHost(); InetAddress localHost = InetAddress.getLocalHost();
NetworkInterface networkInterface = NetworkInterface.getByInetAddress(localHost); NetworkInterface networkInterface = NetworkInterface.getByInetAddress(localHost);
this.broadcast = networkInterface.getInterfaceAddresses().get(0).getBroadcast(); this.broadcast = networkInterface.getInterfaceAddresses().get(0).getBroadcast();
System.out.println(this.broadcast);
System.out.println(InetAddress.getLocalHost());
} }
@ -32,12 +34,6 @@ public class UDPClient {
} }
// protected void sendMessageUDP_broadcast(String message, int port) throws IOException{
// String messageString=message.toString();
// DatagramPacket outpacket = new DatagramPacket(messageString.getBytes(), messageString.length(), this.broadcast, port);
// this.sockUDP.send(outpacket);
// }
protected void destroyAll() { protected void destroyAll() {
this.sockUDP.close(); this.sockUDP.close();
this.sockUDP = null; this.sockUDP = null;

View file

@ -4,6 +4,7 @@ import java.io.IOException;
import java.net.DatagramPacket; import java.net.DatagramPacket;
import java.net.DatagramSocket; import java.net.DatagramSocket;
import java.net.SocketException; import java.net.SocketException;
import main.Utilisateur; import main.Utilisateur;
import messages.*; import messages.*;
@ -36,26 +37,23 @@ public class UDPServer extends Thread {
case JE_SUIS_CONNECTE: case JE_SUIS_CONNECTE:
if (Utilisateur.getSelf() != null) { if (Utilisateur.getSelf() != null) {
// System.out.println("first co");
int portClient = inPacket.getPort(); int portClient = inPacket.getPort();
int portServer = portClient + 1; int portServer = portClient+1;
this.commUDP.sendMessageInfoPseudo(portServer); this.commUDP.sendMessageInfoPseudo(portServer);
} }
break; break;
case INFO_PSEUDO: case INFO_PSEUDO:
MessageSysteme m = (MessageSysteme) msg;
if (this.commUDP.containsUserFromID(((MessageSysteme) msg).getId())) { if (this.commUDP.containsUserFromID(m.getId())) {
this.commUDP.changePseudoUser(((MessageSysteme) msg).getId(), this.commUDP.changePseudoUser(m.getId(), m.getPseudo(), inPacket.getAddress(), m.getPort());
((MessageSysteme) msg).getPseudo(), inPacket.getAddress(),
((MessageSysteme) msg).getPort());
} else { } else {
this.commUDP.addUser(((MessageSysteme) msg).getId(), ((MessageSysteme) msg).getPseudo(), this.commUDP.addUser(m.getId(), m.getPseudo(), inPacket.getAddress(), m.getPort());
inPacket.getAddress(), ((MessageSysteme) msg).getPort()); System.out.println(m.getId() + ", " + m.getPseudo());
System.out.println(((MessageSysteme) msg).getId() + ", " + ((MessageSysteme) msg).getPseudo());
} }
break; break;

View file

@ -5,16 +5,13 @@ import java.awt.event.ActionListener;
import java.io.IOException; import java.io.IOException;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList;
import communication.*;
import communication.udp.CommunicationUDP; import communication.udp.CommunicationUDP;
import database.SQLiteManager; import database.SQLiteManager;
import main.Utilisateur; import main.Utilisateur;
import observers.ObserverUserList;
import standard.VueStandard; import standard.VueStandard;
public class ControleurConnexion implements ActionListener, ObserverUserList{ public class ControleurConnexion implements ActionListener{
private enum Etat {DEBUT, ID_OK}; private enum Etat {DEBUT, ID_OK};
@ -22,37 +19,43 @@ public class ControleurConnexion implements ActionListener, ObserverUserList{
private Etat etat; private Etat etat;
private CommunicationUDP comUDP; private CommunicationUDP comUDP;
private int portTCP; private int portTCP;
private int num;
private String username; private String username;
private SQLiteManager sqlManager; private SQLiteManager sqlManager;
private VueStandard vueStd;
public ControleurConnexion(VueConnexion vue, int numtest) { public ControleurConnexion(VueConnexion vue, int numtest) {
this.vue = vue; this.vue = vue;
this.etat = Etat.DEBUT; this.etat = Etat.DEBUT;
this.num = numtest;
this.username = ""; this.username = "";
this.sqlManager = new SQLiteManager(0); this.sqlManager = new SQLiteManager(0);
this.vueStd = null;
//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
int[] portServer = {2209, 2309, 2409, 2509};
try { try {
switch(numtest) { switch(numtest) {
case 0 : case 0 :
this.comUDP = new CommunicationUDP(2208, 2209, new int[] {2309, 2409}); this.comUDP = new CommunicationUDP(2208, 2209, portServer);
this.portTCP = 7010; this.portTCP = 7010;
break; break;
case 1 : case 1 :
this.comUDP = new CommunicationUDP(2308, 2309, new int[] {2209, 2409}); this.comUDP = new CommunicationUDP(2308, 2309, portServer);
this.portTCP = 7020; this.portTCP = 7020;
break; break;
case 2 : case 2 :
this.comUDP = new CommunicationUDP(2408, 2409, new int[] {2209, 2309}); this.comUDP = new CommunicationUDP(2408, 2409, portServer);
this.portTCP = 7030; this.portTCP = 7030;
break; break;
case 3 :
this.comUDP = new CommunicationUDP(2508, 2509, portServer);
this.portTCP = 7040;
break;
default : default :
this.comUDP = new CommunicationUDP(2408, 2409, new int[] {2209, 2309}); this.comUDP = new CommunicationUDP(2408, 2409, portServer);
this.portTCP = 7040; this.portTCP = 7040;
} }
//
this.comUDP.setObserver(this);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -99,11 +102,17 @@ public class ControleurConnexion implements ActionListener, ObserverUserList{
//Mise en place de la demande du pseudo //Mise en place de la demande du pseudo
this.vue.setConnexionInfo(""); this.vue.setConnexionInfo("");
this.vue.removePasswordPanel();
this.vue.setTextUsernameField("Veuillez entrer votre pseudonyme"); this.vue.setTextUsernameField("Veuillez entrer votre pseudonyme");
this.vue.resetUsernameField(); this.vue.resetUsernameField();
inputOK=false; inputOK=false;
} }
else this.vue.setConnexionInfo("Identifiant ou mot de passe invalide, veuillez réessayer"); else {
this.vue.setConnexionInfo("Identifiant ou mot de passe invalide, veuillez réessayer");
this.vue.resetPasswordField();
}
} }
else { else {
pseudo = vue.getUsernameValue(); pseudo = vue.getUsernameValue();
@ -125,17 +134,16 @@ public class ControleurConnexion implements ActionListener, ObserverUserList{
try { try {
this.comUDP.sendMessageInfoPseudo(); this.comUDP.sendMessageInfoPseudo();
} catch (UnknownHostException e1) { } catch (UnknownHostException e1) {
// TODO Auto-generated catch block
e1.printStackTrace(); e1.printStackTrace();
} catch (IOException e1) { } catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace(); e1.printStackTrace();
} }
try { try {
this.vue.close(); this.resetView();
new VueStandard("Standard", this.comUDP, this.portTCP, this.sqlManager, this.num); this.vue.setVisible(false);
this.setVueStandard();
} catch (IOException e1) { } catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace(); e1.printStackTrace();
} }
} }
@ -143,10 +151,25 @@ public class ControleurConnexion implements ActionListener, ObserverUserList{
} }
} }
@Override
public void updateList(Object o, ArrayList<Utilisateur> userList) { private void setVueStandard() throws IOException {
// TODO Auto-generated method stub 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("");
} }
} }

View file

@ -4,6 +4,7 @@ package connexion;
import java.awt.*; import java.awt.*;
import javax.swing.*; import javax.swing.*;
import database.SQLiteManager;
import main.Vue; import main.Vue;
public class VueConnexion extends Vue { public class VueConnexion extends Vue {
@ -16,6 +17,8 @@ public class VueConnexion extends Vue {
private JLabel labelUsername; private JLabel labelUsername;
private JLabel labelPassword; private JLabel labelPassword;
private JLabel connexionInfo; private JLabel connexionInfo;
private JPanel main;
private JPanel panelPassword;
//Controleur //Controleur
private ControleurConnexion controle; private ControleurConnexion controle;
@ -36,6 +39,9 @@ public class VueConnexion extends Vue {
//Regle le bouton par défaut //Regle le bouton par défaut
this.getRootPane().setDefaultButton(boutonValider); this.getRootPane().setDefaultButton(boutonValider);
this.inputUsername.setText(SQLiteManager.hardcodedNames[numtest]+numtest);
this.inputPassword.setText("aze1$"+SQLiteManager.hardcodedNames[numtest].charAt(0)+numtest);
//Affiche la fenetre //Affiche la fenetre
this.setVisible(true); this.setVisible(true);
} }
@ -43,9 +49,9 @@ public class VueConnexion extends Vue {
private void ajouterElements() { private void ajouterElements() {
//Creation panel //Creation panel
JPanel main = new JPanel(new GridLayout(4,1)); main = new JPanel(new GridLayout(4,1));
JPanel panelUsername = new JPanel(new GridLayout(1, 2)); JPanel panelUsername = new JPanel(new GridLayout(1, 2));
JPanel panelPassword = new JPanel(new GridLayout(1, 2)); this.panelPassword = new JPanel(new GridLayout(1, 2));
//Cree les elements //Cree les elements
this.connexionInfo = new JLabel(""); this.connexionInfo = new JLabel("");
@ -68,15 +74,18 @@ public class VueConnexion extends Vue {
boutonValider.addActionListener(controle); boutonValider.addActionListener(controle);
//Ajoute les elements //Ajoute les elements
panelUsername.add(this.inputUsername);
panelUsername.add(this.labelUsername); panelUsername.add(this.labelUsername);
panelUsername.add(this.inputUsername);
this.panelPassword.add(this.labelPassword);
this.panelPassword.add(this.inputPassword);
panelPassword.add(this.inputPassword);
panelPassword.add(this.labelPassword);
main.add(connexionInfo); main.add(connexionInfo);
main.add(panelUsername); main.add(panelUsername);
main.add(panelPassword); main.add(this.panelPassword);
main.add(boutonValider); main.add(boutonValider);
this.add(main); this.add(main);
@ -104,6 +113,14 @@ public class VueConnexion extends Vue {
protected void resetUsernameField() { protected void resetUsernameField() {
this.inputUsername.setText(""); this.inputUsername.setText("");
} }
protected void removePasswordPanel() {
this.main.remove(2);
}
protected void addPasswordPanel() {
this.main.add(this.panelPassword, 2);
}
protected void resetPasswordField() { protected void resetPasswordField() {
this.inputPassword.setText(""); this.inputPassword.setText("");

View file

@ -1,7 +1,6 @@
package database; package database;
import java.io.File; import java.io.File;
import java.lang.reflect.Array;
import java.security.InvalidAlgorithmParameterException; import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException; import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
@ -32,6 +31,9 @@ public class SQLiteManager {
private static final String DATABASE_RELATIVE_PATH = "../database"; private static final String DATABASE_RELATIVE_PATH = "../database";
public static String[] hardcodedNames = {"Olivia","Liam","Benjamin","Sophia","Charlotte","Noah","Elijah","Isabella",
"Oliver","Emma","William","Amelia","Evelyn","James","Mia","Ava","Lucas","Mason","Ethan","Harper"};
private Connection connec; private Connection connec;
private int numDatabase; private int numDatabase;
private SecretKey dbDataKey; private SecretKey dbDataKey;
@ -549,7 +551,7 @@ public class SQLiteManager {
String pwdPrefix = "aze1$"; String pwdPrefix = "aze1$";
SQLiteManager sqlManager = new SQLiteManager(0); SQLiteManager sqlManager = new SQLiteManager(1);
for(int i=0; i<hardcodedNames.length; i++) { for(int i=0; i<hardcodedNames.length; i++) {
sqlManager.createNewUserEncrypt(hardcodedNames[i]+i, pwdPrefix+hardcodedNames[i].charAt(0)+i); sqlManager.createNewUserEncrypt(hardcodedNames[i]+i, pwdPrefix+hardcodedNames[i].charAt(0)+i);

View file

@ -5,11 +5,17 @@ import javax.swing.JPanel;
import javax.swing.UIManager; import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo; import javax.swing.UIManager.LookAndFeelInfo;
import communication.filetransfer.FileTransferUtils;
import connexion.VueConnexion; import connexion.VueConnexion;
public class Main extends JPanel{ public class Main extends JPanel{
/**
*
*/
private static final long serialVersionUID = 1L;
public static void main(String[] args) { public static void main(String[] args) {
try { try {
@ -23,6 +29,8 @@ public class Main extends JPanel{
// If Nimbus is not available, you can set the GUI to another look and feel. // If Nimbus is not available, you can set the GUI to another look and feel.
} }
FileTransferUtils.createDownloads();
new VueConnexion(Integer.parseInt(args[0])); new VueConnexion(Integer.parseInt(args[0]));
} }

View file

@ -24,7 +24,7 @@ public class Utilisateur implements Serializable{
this.port = port; this.port = port;
} }
public String getId() { public String getId() {
return id; return id;

View file

@ -1,12 +1,8 @@
package messages; package messages;
import java.io.Serializable; import java.io.Serializable;
import java.lang.instrument.Instrumentation;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import messages.Message.TypeMessage;
public abstract class Message implements Serializable { public abstract class Message implements Serializable {
@ -15,7 +11,6 @@ public abstract class Message implements Serializable {
private String dateMessage; private String dateMessage;
private String sender; private String sender;
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private static Instrumentation inst;
public static String getDateAndTime() { public static String getDateAndTime() {
@ -61,7 +56,7 @@ public abstract class Message implements Serializable {
return new MessageSysteme(TypeMessage.JE_SUIS_CONNECTE); return new MessageSysteme(TypeMessage.JE_SUIS_CONNECTE);
case "JE_SUIS_DECONNECTE" : case "JE_SUIS_DECONNECTE" :
return new MessageSysteme(TypeMessage.JE_SUIS_DECONNECTE); return new MessageSysteme(TypeMessage.JE_SUIS_DECONNECTE, parts[1], parts[2], Integer.parseInt(parts[3]) );
case "INFO_PSEUDO" : case "INFO_PSEUDO" :
return new MessageSysteme(TypeMessage.INFO_PSEUDO, parts[1], parts[2], Integer.parseInt(parts[3]) ); return new MessageSysteme(TypeMessage.INFO_PSEUDO, parts[1], parts[2], Integer.parseInt(parts[3]) );
@ -82,7 +77,7 @@ public abstract class Message implements Serializable {
//tests ici //tests ici
public static void main(String[] args) throws MauvaisTypeMessageException { public static void main(String[] args) throws MauvaisTypeMessageException {
Message m1 = new MessageSysteme(TypeMessage.JE_SUIS_CONNECTE); Message m1 = new MessageSysteme(TypeMessage.JE_SUIS_CONNECTE);
Message m2 = new MessageSysteme(TypeMessage.JE_SUIS_DECONNECTE); Message m2 = new MessageSysteme(TypeMessage.JE_SUIS_DECONNECTE,"aker", "man", 5000);
Message m3 = new MessageSysteme(TypeMessage.INFO_PSEUDO, "pseudo156434518", "id236", 1500); Message m3 = new MessageSysteme(TypeMessage.INFO_PSEUDO, "pseudo156434518", "id236", 1500);
Message m4 = new MessageTexte(TypeMessage.TEXTE, "blablabla"); Message m4 = new MessageTexte(TypeMessage.TEXTE, "blablabla");
Message m5 = new MessageFichier(TypeMessage.FICHIER, "truc", ".pdf"); Message m5 = new MessageFichier(TypeMessage.FICHIER, "truc", ".pdf");

View file

@ -8,7 +8,7 @@ public class MessageSysteme extends Message {
private int port; private int port;
public MessageSysteme(TypeMessage type) throws MauvaisTypeMessageException{ public MessageSysteme(TypeMessage type) throws MauvaisTypeMessageException{
if ((type==TypeMessage.JE_SUIS_CONNECTE)||(type==TypeMessage.JE_SUIS_DECONNECTE)||(type==TypeMessage.MESSAGE_NUL)) { if ((type==TypeMessage.JE_SUIS_CONNECTE)||(type==TypeMessage.MESSAGE_NUL)) {
this.type=type; this.type=type;
this.pseudo=""; this.pseudo="";
this.id=""; this.id="";
@ -18,7 +18,7 @@ public class MessageSysteme extends Message {
} }
public MessageSysteme(TypeMessage type, String pseudo, String id, int port) throws MauvaisTypeMessageException { public MessageSysteme(TypeMessage type, String pseudo, String id, int port) throws MauvaisTypeMessageException {
if (type==TypeMessage.INFO_PSEUDO) { if (type==TypeMessage.INFO_PSEUDO ||(type==TypeMessage.JE_SUIS_DECONNECTE)) {
this.type=type; this.type=type;
this.pseudo=pseudo; this.pseudo=pseudo;
this.id=id; this.id=id;
@ -27,6 +27,7 @@ public class MessageSysteme extends Message {
else throw new MauvaisTypeMessageException(); else throw new MauvaisTypeMessageException();
} }
public String getPseudo() { public String getPseudo() {
return this.pseudo; return this.pseudo;
} }

View file

@ -10,10 +10,12 @@ import java.io.InputStreamReader;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.Socket; import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList; import java.util.ArrayList;
import javax.swing.JButton; import javax.swing.JButton;
import javax.swing.JList; import javax.swing.JList;
import javax.swing.WindowConstants;
import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener; import javax.swing.event.ListSelectionListener;
@ -34,7 +36,6 @@ public class ControleurStandard implements ActionListener, ListSelectionListener
TERMINE, EN_COURS TERMINE, EN_COURS
} }
private int num;
private ModifPseudo modifPseudo; private ModifPseudo modifPseudo;
private VueStandard vue; private VueStandard vue;
private CommunicationUDP commUDP; private CommunicationUDP commUDP;
@ -42,26 +43,25 @@ public class ControleurStandard implements ActionListener, ListSelectionListener
private TCPServer tcpServ; private TCPServer tcpServ;
private ArrayList<String> idsSessionEnCours; private ArrayList<String> idsSessionEnCours;
private SQLiteManager sqlManager; private SQLiteManager sqlManager;
private VueConnexion vueConnexion;
public ControleurStandard(VueStandard vue, CommunicationUDP commUDP, int portServerTCP, SQLiteManager sqlManager, int num) public ControleurStandard(VueStandard vue, CommunicationUDP commUDP, int portServerTCP, SQLiteManager sqlManager,
throws IOException { VueConnexion vueConnexion) throws IOException {
this.vue = vue; this.vue = vue;
this.vue.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
this.num = num; this.vueConnexion = vueConnexion;
this.tcpServ = new TCPServer(portServerTCP); this.tcpServ = new TCPServer(portServerTCP);
this.tcpServ.addObserver(this); this.tcpServ.addObserver(this);
this.tcpServ.start(); this.tcpServ.start();
this.commUDP = commUDP; this.commUDP = commUDP;
this.commUDP.setObserver(this);
this.commUDP.sendMessageConnecte();
this.commUDP.sendMessageInfoPseudo();
this.idsSessionEnCours = new ArrayList<String>(); this.idsSessionEnCours = new ArrayList<String>();
this.sqlManager = sqlManager; this.sqlManager = sqlManager;
this.modifPseudo = ModifPseudo.TERMINE;
} }
// ---------- LISTSELECTION LISTENER OPERATIONS ----------// // ---------- LISTSELECTION LISTENER OPERATIONS ----------//
@ -86,11 +86,16 @@ public class ControleurStandard implements ActionListener, ListSelectionListener
System.out.println("choix : " + choix); System.out.println("choix : " + choix);
if (choix == 0) { if (choix == 0) {
int port = other.getPort(); int port = other.getPort();
System.out.println("port = " + port); System.out.println("port = " + port);
try { try {
Socket socketComm = new Socket(InetAddress.getLocalHost(), port); Socket socketComm = new Socket(InetAddress.getLocalHost(), port);
this.sendMessage(socketComm, Utilisateur.getSelf().getPseudo()); this.sendMessage(socketComm, Utilisateur.getSelf().getPseudo());
String reponse = this.readMessage(socketComm); String reponse = this.readMessage(socketComm);
@ -145,7 +150,6 @@ public class ControleurStandard implements ActionListener, ListSelectionListener
try { try {
this.commUDP.sendMessageInfoPseudo(); this.commUDP.sendMessageInfoPseudo();
} catch (IOException e1) { } catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace(); e1.printStackTrace();
} }
@ -163,15 +167,7 @@ public class ControleurStandard implements ActionListener, ListSelectionListener
// Cas deconnexion // Cas deconnexion
else if ((JButton) e.getSource() == this.vue.getButtonDeconnexion()) { else if ((JButton) e.getSource() == this.vue.getButtonDeconnexion()) {
try { try {
this.commUDP.sendMessageDelete(); this.setVueConnexion();
this.commUDP.removeAll();
this.commUDP.destroyAll();
this.vue.removeAllUsers();
Utilisateur.resetSelf();
vue.dispose();
new VueConnexion(this.num);
} catch (IOException e1) { } catch (IOException e1) {
e1.printStackTrace(); e1.printStackTrace();
@ -191,9 +187,8 @@ public class ControleurStandard implements ActionListener, ListSelectionListener
public void windowClosing(WindowEvent e) { public void windowClosing(WindowEvent e) {
try { try {
this.commUDP.sendMessageDelete(); this.setVueConnexion();
} catch (IOException e1) { } catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace(); e1.printStackTrace();
} }
@ -207,7 +202,6 @@ public class ControleurStandard implements ActionListener, ListSelectionListener
@Override @Override
public void windowClosed(WindowEvent e) { public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub
} }
@ -307,6 +301,25 @@ public class ControleurStandard implements ActionListener, ListSelectionListener
int index = this.vue.removeSession(session); int index = this.vue.removeSession(session);
this.idsSessionEnCours.remove(index); this.idsSessionEnCours.remove(index);
} }
private void setVueConnexion() throws UnknownHostException, IOException {
this.commUDP.sendMessageDelete();
this.vue.removeAllUsers();
this.vue.closeAllSession();
this.idsSessionEnCours.clear();
Utilisateur.resetSelf();
this.commUDP.setObserver(null);
this.vue.setVisible(false);
this.vueConnexion.setVisible(true);
}
protected void init() throws UnknownHostException, IOException {
this.commUDP.setObserver(this);
this.commUDP.sendMessageConnecte();
this.commUDP.sendMessageInfoPseudo();
this.modifPseudo = ModifPseudo.TERMINE;
}
} }

View file

@ -7,7 +7,9 @@ import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent; import java.awt.event.MouseEvent;
import java.awt.event.MouseListener; import java.awt.event.MouseListener;
import java.io.IOException; import java.io.IOException;
import java.net.UnknownHostException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.AbstractButton; import javax.swing.AbstractButton;
import javax.swing.BorderFactory; import javax.swing.BorderFactory;
@ -25,6 +27,7 @@ import javax.swing.ScrollPaneConstants;
import javax.swing.plaf.basic.BasicButtonUI; import javax.swing.plaf.basic.BasicButtonUI;
import communication.udp.CommunicationUDP; import communication.udp.CommunicationUDP;
import connexion.VueConnexion;
import database.SQLiteManager; import database.SQLiteManager;
import main.Utilisateur; import main.Utilisateur;
import main.Vue; import main.Vue;
@ -48,12 +51,13 @@ public class VueStandard extends Vue {
private ArrayList<VueSession> sessions; private ArrayList<VueSession> sessions;
private DefaultListModel<String> userList = new DefaultListModel<String>(); private DefaultListModel<String> userList = new DefaultListModel<String>();
public VueStandard(String title, CommunicationUDP commUDP, int portServerTCP, SQLiteManager sqlManager, int num) throws IOException { public VueStandard(String title, CommunicationUDP commUDP, int portServerTCP, SQLiteManager sqlManager, VueConnexion vueConnexion) throws IOException {
super(title); super(title);
this.tabButtons = new ArrayList<JButton>(); this.tabButtons = new ArrayList<JButton>();
this.sessions = new ArrayList<VueSession>(); this.sessions = new ArrayList<VueSession>();
this.c = new ControleurStandard(this, commUDP, portServerTCP, sqlManager, num); this.c = new ControleurStandard(this, commUDP, portServerTCP, sqlManager, vueConnexion);
this.c.init();
getContentPane().setLayout(new GridBagLayout()); getContentPane().setLayout(new GridBagLayout());
@ -118,6 +122,11 @@ public class VueStandard extends Vue {
this.seDeconnecter = new JButton("Se Déconnecter"); this.seDeconnecter = new JButton("Se Déconnecter");
this.seDeconnecter.addActionListener(this.c); this.seDeconnecter.addActionListener(this.c);
deconnexion.add(this.seDeconnecter); deconnexion.add(this.seDeconnecter);
if(Utilisateur.getSelf().getId() == "admin") {
JButton addNewUser = new JButton("Ajouter un nouvel utilisateur");
deconnexion.add(addNewUser);
}
// --------Ajout à la vue--------// // --------Ajout à la vue--------//
left.add(self, BorderLayout.PAGE_START); left.add(self, BorderLayout.PAGE_START);
@ -185,6 +194,10 @@ public class VueStandard extends Vue {
protected void setDisplayedPseudo(String pseudo) { protected void setDisplayedPseudo(String pseudo) {
this.pseudoSelf.setText(pseudo); this.pseudoSelf.setText(pseudo);
} }
public void setPseudoSelf() {
this.setDisplayedPseudo(Utilisateur.getSelf().getPseudo());
}
// ------------ JOPTIONS -------------// // ------------ JOPTIONS -------------//
@ -237,18 +250,6 @@ public class VueStandard extends Vue {
return index; return index;
} }
protected int removeSession(VueSession vue) {
int index = this.sessions.indexOf(vue);
vue.destroyAll();
this.zoneSessions.remove(vue);
this.sessions.remove(index);
this.tabButtons.remove(index);
return index;
}
protected void addSession(String pseudo, VueSession session) { protected void addSession(String pseudo, VueSession session) {
JPanel tabTitle = new JPanel(); JPanel tabTitle = new JPanel();
@ -266,12 +267,41 @@ public class VueStandard extends Vue {
session.requestFocus(); session.requestFocus();
} }
protected synchronized int removeSession(VueSession vue) {
int index = this.sessions.indexOf(vue);
vue.destroyAll();
this.zoneSessions.remove(vue);
this.sessions.remove(index);
this.tabButtons.remove(index);
return index;
}
protected void closeAllSession() {
Iterator<VueSession> it = this.sessions.iterator();
while(it.hasNext()) {
VueSession session = it.next();
this.zoneSessions.remove(session);
session.destroyAll();
it.remove();
}
this.tabButtons.clear();
}
// ------------ OTHERS -------------// // ------------ OTHERS -------------//
protected void removeAllUsers() { protected void removeAllUsers() {
this.userList.removeAllElements(); this.userList.removeAllElements();
} }
public void initControleur() throws UnknownHostException, IOException {
this.c.init();
}
// ------------- PRIVATE CLASSES FOR THE TABS BUTTON -------------// // ------------- PRIVATE CLASSES FOR THE TABS BUTTON -------------//
private class TabButton extends JButton { private class TabButton extends JButton {