local v finished, connexion GUI and switch b/w co and std improved
This commit is contained in:
parent
d7444342dc
commit
c33e75add7
14 changed files with 230 additions and 144 deletions
|
@ -109,4 +109,12 @@ public class FileTransferUtils {
|
|||
BufferedImage image = gc.createCompatibleImage(w, h);
|
||||
return image;
|
||||
}
|
||||
|
||||
public static void createDownloads() {
|
||||
File downloads = new File(FileTransferUtils.DOWNLOADS_RELATIVE_PATH);
|
||||
|
||||
if(!downloads.exists()) {
|
||||
downloads.mkdir();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,11 +11,14 @@ import observers.ObserverInputMessage;
|
|||
|
||||
public class TCPServer extends Thread {
|
||||
|
||||
//****
|
||||
public static int PORT_SERVER = 7000;
|
||||
|
||||
private ServerSocket sockListenTCP;
|
||||
private ObserverInputMessage obs;
|
||||
|
||||
public TCPServer(int port) throws UnknownHostException, IOException {
|
||||
this.sockListenTCP = new ServerSocket(port, 5, InetAddress.getLocalHost());
|
||||
this.sockListenTCP = new ServerSocket(port, 50, InetAddress.getLocalHost());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -2,6 +2,7 @@ package communication.udp;
|
|||
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
import java.net.SocketException;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.ArrayList;
|
||||
|
||||
|
@ -12,6 +13,11 @@ import observers.ObserverUserList;
|
|||
|
||||
public class CommunicationUDP extends Thread {
|
||||
|
||||
// ****
|
||||
protected static int PORT_SERVEUR = 3000;
|
||||
// ****
|
||||
protected static int PORT_CLIENT = 2000;
|
||||
|
||||
private UDPClient client;
|
||||
private UDPServer server;
|
||||
private int portServer;
|
||||
|
@ -27,6 +33,14 @@ public class CommunicationUDP extends Thread {
|
|||
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[]) {
|
||||
ArrayList<Integer> tmp = new ArrayList<Integer>();
|
||||
for (int port : ports) {
|
||||
|
@ -46,22 +60,22 @@ public class CommunicationUDP extends Thread {
|
|||
protected synchronized void addUser(String idClient, String pseudoClient, InetAddress ipClient, int port)
|
||||
throws IOException {
|
||||
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) {
|
||||
int index = getIndexFromID(idClient);
|
||||
users.get(index).setPseudo(pseudoClient);
|
||||
observer.updateList(this, users);
|
||||
this.sendUpdate();
|
||||
}
|
||||
|
||||
protected synchronized void removeUser(String idClient, String pseudoClient, InetAddress ipClient, int port) {
|
||||
int index = getIndexFromIP(ipClient);
|
||||
int index = getIndexFromID(idClient);
|
||||
if (index != -1) {
|
||||
users.remove(index);
|
||||
}
|
||||
observer.updateList(this, users);
|
||||
this.sendUpdate();
|
||||
}
|
||||
|
||||
public void removeAll() {
|
||||
|
@ -112,17 +126,6 @@ public class CommunicationUDP extends Thread {
|
|||
return -1;
|
||||
}
|
||||
|
||||
private int getIndexFromIP(InetAddress ip) {
|
||||
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 {
|
||||
|
@ -145,15 +148,10 @@ public class CommunicationUDP extends Thread {
|
|||
|
||||
Utilisateur self = Utilisateur.getSelf();
|
||||
|
||||
String pseudoSelf = self.getPseudo();
|
||||
String idSelf = self.getId();
|
||||
int portSelf = self.getPort();
|
||||
|
||||
Message msout = null;
|
||||
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) {
|
||||
this.client.sendMessageUDP_local(msout, port, InetAddress.getLocalHost());
|
||||
this.client.sendMessageUDP_local(msgOut, port, InetAddress.getLocalHost());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
|
@ -167,9 +165,9 @@ public class CommunicationUDP extends Thread {
|
|||
|
||||
Utilisateur self = Utilisateur.getSelf();
|
||||
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());
|
||||
this.client.sendMessageUDP_local(msout, portOther, InetAddress.getLocalHost());
|
||||
this.client.sendMessageUDP_local(msgOut, portOther, InetAddress.getLocalHost());
|
||||
} catch (MauvaisTypeMessageException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
@ -180,31 +178,25 @@ public class CommunicationUDP extends Thread {
|
|||
// This allows the receivers' agent (portOthers) to delete the entry
|
||||
// corresponding to this agent
|
||||
public void sendMessageDelete() throws UnknownHostException, IOException {
|
||||
for (int port : this.portOthers) {
|
||||
Utilisateur self = Utilisateur.getSelf();
|
||||
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());
|
||||
for (int port : this.portOthers) {
|
||||
this.client.sendMessageUDP_local(msgOut, port, InetAddress.getLocalHost());
|
||||
}
|
||||
|
||||
} catch (MauvaisTypeMessageException e) {
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// 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();
|
||||
// }
|
||||
private void sendUpdate() {
|
||||
if(this.observer != null) {
|
||||
this.observer.updateList(this, users);
|
||||
}
|
||||
}
|
||||
|
||||
public void destroyAll() {
|
||||
this.client.destroyAll();
|
||||
|
|
|
@ -21,6 +21,8 @@ public class UDPClient {
|
|||
InetAddress localHost = InetAddress.getLocalHost();
|
||||
NetworkInterface networkInterface = NetworkInterface.getByInetAddress(localHost);
|
||||
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() {
|
||||
this.sockUDP.close();
|
||||
this.sockUDP = null;
|
||||
|
|
|
@ -4,6 +4,7 @@ import java.io.IOException;
|
|||
import java.net.DatagramPacket;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.SocketException;
|
||||
|
||||
import main.Utilisateur;
|
||||
import messages.*;
|
||||
|
||||
|
@ -36,26 +37,23 @@ public class UDPServer extends Thread {
|
|||
case JE_SUIS_CONNECTE:
|
||||
|
||||
if (Utilisateur.getSelf() != null) {
|
||||
// System.out.println("first co");
|
||||
|
||||
int portClient = inPacket.getPort();
|
||||
int portServer = portClient+1;
|
||||
|
||||
this.commUDP.sendMessageInfoPseudo(portServer);
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case INFO_PSEUDO:
|
||||
MessageSysteme m = (MessageSysteme) msg;
|
||||
|
||||
if (this.commUDP.containsUserFromID(((MessageSysteme) msg).getId())) {
|
||||
this.commUDP.changePseudoUser(((MessageSysteme) msg).getId(),
|
||||
((MessageSysteme) msg).getPseudo(), inPacket.getAddress(),
|
||||
((MessageSysteme) msg).getPort());
|
||||
if (this.commUDP.containsUserFromID(m.getId())) {
|
||||
this.commUDP.changePseudoUser(m.getId(), m.getPseudo(), inPacket.getAddress(), m.getPort());
|
||||
} else {
|
||||
|
||||
this.commUDP.addUser(((MessageSysteme) msg).getId(), ((MessageSysteme) msg).getPseudo(),
|
||||
inPacket.getAddress(), ((MessageSysteme) msg).getPort());
|
||||
System.out.println(((MessageSysteme) msg).getId() + ", " + ((MessageSysteme) msg).getPseudo());
|
||||
this.commUDP.addUser(m.getId(), m.getPseudo(), inPacket.getAddress(), m.getPort());
|
||||
System.out.println(m.getId() + ", " + m.getPseudo());
|
||||
}
|
||||
break;
|
||||
|
||||
|
|
|
@ -5,16 +5,13 @@ import java.awt.event.ActionListener;
|
|||
import java.io.IOException;
|
||||
import java.net.UnknownHostException;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import communication.*;
|
||||
import communication.udp.CommunicationUDP;
|
||||
import database.SQLiteManager;
|
||||
import main.Utilisateur;
|
||||
import observers.ObserverUserList;
|
||||
import standard.VueStandard;
|
||||
|
||||
public class ControleurConnexion implements ActionListener, ObserverUserList{
|
||||
public class ControleurConnexion implements ActionListener{
|
||||
|
||||
private enum Etat {DEBUT, ID_OK};
|
||||
|
||||
|
@ -22,37 +19,43 @@ public class ControleurConnexion implements ActionListener, ObserverUserList{
|
|||
private Etat etat;
|
||||
private CommunicationUDP comUDP;
|
||||
private int portTCP;
|
||||
private int num;
|
||||
private String username;
|
||||
private SQLiteManager sqlManager;
|
||||
private VueStandard vueStd;
|
||||
|
||||
public ControleurConnexion(VueConnexion vue, int numtest) {
|
||||
this.vue = vue;
|
||||
this.etat = Etat.DEBUT;
|
||||
this.num = numtest;
|
||||
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
|
||||
|
||||
int[] portServer = {2209, 2309, 2409, 2509};
|
||||
try {
|
||||
switch(numtest) {
|
||||
case 0 :
|
||||
this.comUDP = new CommunicationUDP(2208, 2209, new int[] {2309, 2409});
|
||||
this.comUDP = new CommunicationUDP(2208, 2209, portServer);
|
||||
this.portTCP = 7010;
|
||||
break;
|
||||
case 1 :
|
||||
this.comUDP = new CommunicationUDP(2308, 2309, new int[] {2209, 2409});
|
||||
this.comUDP = new CommunicationUDP(2308, 2309, portServer);
|
||||
this.portTCP = 7020;
|
||||
break;
|
||||
case 2 :
|
||||
this.comUDP = new CommunicationUDP(2408, 2409, new int[] {2209, 2309});
|
||||
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, new int[] {2209, 2309});
|
||||
this.comUDP = new CommunicationUDP(2408, 2409, portServer);
|
||||
this.portTCP = 7040;
|
||||
}
|
||||
//
|
||||
|
||||
this.comUDP.setObserver(this);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
@ -99,11 +102,17 @@ public class ControleurConnexion implements ActionListener, ObserverUserList{
|
|||
|
||||
//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");
|
||||
else {
|
||||
this.vue.setConnexionInfo("Identifiant ou mot de passe invalide, veuillez réessayer");
|
||||
this.vue.resetPasswordField();
|
||||
}
|
||||
|
||||
}
|
||||
else {
|
||||
pseudo = vue.getUsernameValue();
|
||||
|
@ -125,17 +134,16 @@ public class ControleurConnexion implements ActionListener, ObserverUserList{
|
|||
try {
|
||||
this.comUDP.sendMessageInfoPseudo();
|
||||
} catch (UnknownHostException e1) {
|
||||
// TODO Auto-generated catch block
|
||||
e1.printStackTrace();
|
||||
} catch (IOException e1) {
|
||||
// TODO Auto-generated catch block
|
||||
e1.printStackTrace();
|
||||
}
|
||||
|
||||
try {
|
||||
this.vue.close();
|
||||
new VueStandard("Standard", this.comUDP, this.portTCP, this.sqlManager, this.num);
|
||||
this.resetView();
|
||||
this.vue.setVisible(false);
|
||||
this.setVueStandard();
|
||||
} catch (IOException e1) {
|
||||
// TODO Auto-generated catch block
|
||||
e1.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
@ -143,10 +151,25 @@ public class ControleurConnexion implements ActionListener, ObserverUserList{
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateList(Object o, ArrayList<Utilisateur> userList) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
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("");
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ package connexion;
|
|||
import java.awt.*;
|
||||
import javax.swing.*;
|
||||
|
||||
import database.SQLiteManager;
|
||||
import main.Vue;
|
||||
|
||||
public class VueConnexion extends Vue {
|
||||
|
@ -16,6 +17,8 @@ public class VueConnexion extends Vue {
|
|||
private JLabel labelUsername;
|
||||
private JLabel labelPassword;
|
||||
private JLabel connexionInfo;
|
||||
private JPanel main;
|
||||
private JPanel panelPassword;
|
||||
|
||||
//Controleur
|
||||
private ControleurConnexion controle;
|
||||
|
@ -36,6 +39,9 @@ public class VueConnexion extends Vue {
|
|||
//Regle le bouton par défaut
|
||||
this.getRootPane().setDefaultButton(boutonValider);
|
||||
|
||||
this.inputUsername.setText(SQLiteManager.hardcodedNames[numtest]+numtest);
|
||||
this.inputPassword.setText("aze1$"+SQLiteManager.hardcodedNames[numtest].charAt(0)+numtest);
|
||||
|
||||
//Affiche la fenetre
|
||||
this.setVisible(true);
|
||||
}
|
||||
|
@ -43,9 +49,9 @@ public class VueConnexion extends Vue {
|
|||
private void ajouterElements() {
|
||||
|
||||
//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 panelPassword = new JPanel(new GridLayout(1, 2));
|
||||
this.panelPassword = new JPanel(new GridLayout(1, 2));
|
||||
|
||||
//Cree les elements
|
||||
this.connexionInfo = new JLabel("");
|
||||
|
@ -68,15 +74,18 @@ public class VueConnexion extends Vue {
|
|||
boutonValider.addActionListener(controle);
|
||||
|
||||
//Ajoute les elements
|
||||
panelUsername.add(this.inputUsername);
|
||||
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(panelUsername);
|
||||
main.add(panelPassword);
|
||||
main.add(this.panelPassword);
|
||||
main.add(boutonValider);
|
||||
|
||||
this.add(main);
|
||||
|
@ -105,6 +114,14 @@ public class VueConnexion extends Vue {
|
|||
this.inputUsername.setText("");
|
||||
}
|
||||
|
||||
protected void removePasswordPanel() {
|
||||
this.main.remove(2);
|
||||
}
|
||||
|
||||
protected void addPasswordPanel() {
|
||||
this.main.add(this.panelPassword, 2);
|
||||
}
|
||||
|
||||
protected void resetPasswordField() {
|
||||
this.inputPassword.setText("");
|
||||
}
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package database;
|
||||
|
||||
import java.io.File;
|
||||
import java.lang.reflect.Array;
|
||||
import java.security.InvalidAlgorithmParameterException;
|
||||
import java.security.InvalidKeyException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
@ -32,6 +31,9 @@ public class SQLiteManager {
|
|||
|
||||
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 int numDatabase;
|
||||
private SecretKey dbDataKey;
|
||||
|
@ -549,7 +551,7 @@ public class SQLiteManager {
|
|||
|
||||
String pwdPrefix = "aze1$";
|
||||
|
||||
SQLiteManager sqlManager = new SQLiteManager(0);
|
||||
SQLiteManager sqlManager = new SQLiteManager(1);
|
||||
|
||||
for(int i=0; i<hardcodedNames.length; i++) {
|
||||
sqlManager.createNewUserEncrypt(hardcodedNames[i]+i, pwdPrefix+hardcodedNames[i].charAt(0)+i);
|
||||
|
|
|
@ -5,11 +5,17 @@ import javax.swing.JPanel;
|
|||
import javax.swing.UIManager;
|
||||
import javax.swing.UIManager.LookAndFeelInfo;
|
||||
|
||||
import communication.filetransfer.FileTransferUtils;
|
||||
import connexion.VueConnexion;
|
||||
|
||||
public class Main extends JPanel{
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
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.
|
||||
}
|
||||
|
||||
FileTransferUtils.createDownloads();
|
||||
|
||||
new VueConnexion(Integer.parseInt(args[0]));
|
||||
}
|
||||
|
||||
|
|
|
@ -1,12 +1,8 @@
|
|||
package messages;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.lang.instrument.Instrumentation;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Arrays;
|
||||
|
||||
import messages.Message.TypeMessage;
|
||||
|
||||
public abstract class Message implements Serializable {
|
||||
|
||||
|
@ -15,7 +11,6 @@ public abstract class Message implements Serializable {
|
|||
private String dateMessage;
|
||||
private String sender;
|
||||
private static final long serialVersionUID = 1L;
|
||||
private static Instrumentation inst;
|
||||
|
||||
|
||||
public static String getDateAndTime() {
|
||||
|
@ -61,7 +56,7 @@ public abstract class Message implements Serializable {
|
|||
return new MessageSysteme(TypeMessage.JE_SUIS_CONNECTE);
|
||||
|
||||
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" :
|
||||
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
|
||||
public static void main(String[] args) throws MauvaisTypeMessageException {
|
||||
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 m4 = new MessageTexte(TypeMessage.TEXTE, "blablabla");
|
||||
Message m5 = new MessageFichier(TypeMessage.FICHIER, "truc", ".pdf");
|
||||
|
|
|
@ -8,7 +8,7 @@ public class MessageSysteme extends Message {
|
|||
private int port;
|
||||
|
||||
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.pseudo="";
|
||||
this.id="";
|
||||
|
@ -18,7 +18,7 @@ public class MessageSysteme extends Message {
|
|||
}
|
||||
|
||||
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.pseudo=pseudo;
|
||||
this.id=id;
|
||||
|
@ -27,6 +27,7 @@ public class MessageSysteme extends Message {
|
|||
else throw new MauvaisTypeMessageException();
|
||||
}
|
||||
|
||||
|
||||
public String getPseudo() {
|
||||
return this.pseudo;
|
||||
}
|
||||
|
|
|
@ -10,10 +10,12 @@ import java.io.InputStreamReader;
|
|||
import java.io.PrintWriter;
|
||||
import java.net.InetAddress;
|
||||
import java.net.Socket;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JList;
|
||||
import javax.swing.WindowConstants;
|
||||
import javax.swing.event.ListSelectionEvent;
|
||||
import javax.swing.event.ListSelectionListener;
|
||||
|
||||
|
@ -34,7 +36,6 @@ public class ControleurStandard implements ActionListener, ListSelectionListener
|
|||
TERMINE, EN_COURS
|
||||
}
|
||||
|
||||
private int num;
|
||||
private ModifPseudo modifPseudo;
|
||||
private VueStandard vue;
|
||||
private CommunicationUDP commUDP;
|
||||
|
@ -42,26 +43,25 @@ public class ControleurStandard implements ActionListener, ListSelectionListener
|
|||
private TCPServer tcpServ;
|
||||
private ArrayList<String> idsSessionEnCours;
|
||||
private SQLiteManager sqlManager;
|
||||
private VueConnexion vueConnexion;
|
||||
|
||||
public ControleurStandard(VueStandard vue, CommunicationUDP commUDP, int portServerTCP, SQLiteManager sqlManager, int num)
|
||||
throws IOException {
|
||||
public ControleurStandard(VueStandard vue, CommunicationUDP commUDP, int portServerTCP, SQLiteManager sqlManager,
|
||||
VueConnexion vueConnexion) throws IOException {
|
||||
this.vue = vue;
|
||||
this.vue.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
|
||||
|
||||
this.vueConnexion = vueConnexion;
|
||||
|
||||
this.num = num;
|
||||
this.tcpServ = new TCPServer(portServerTCP);
|
||||
|
||||
this.tcpServ.addObserver(this);
|
||||
this.tcpServ.start();
|
||||
|
||||
this.commUDP = commUDP;
|
||||
this.commUDP.setObserver(this);
|
||||
this.commUDP.sendMessageConnecte();
|
||||
this.commUDP.sendMessageInfoPseudo();
|
||||
|
||||
this.idsSessionEnCours = new ArrayList<String>();
|
||||
|
||||
this.sqlManager = sqlManager;
|
||||
|
||||
this.modifPseudo = ModifPseudo.TERMINE;
|
||||
}
|
||||
|
||||
// ---------- LISTSELECTION LISTENER OPERATIONS ----------//
|
||||
|
@ -86,11 +86,16 @@ public class ControleurStandard implements ActionListener, ListSelectionListener
|
|||
System.out.println("choix : " + choix);
|
||||
|
||||
if (choix == 0) {
|
||||
|
||||
|
||||
int port = other.getPort();
|
||||
|
||||
|
||||
System.out.println("port = " + port);
|
||||
try {
|
||||
|
||||
Socket socketComm = new Socket(InetAddress.getLocalHost(), port);
|
||||
|
||||
this.sendMessage(socketComm, Utilisateur.getSelf().getPseudo());
|
||||
String reponse = this.readMessage(socketComm);
|
||||
|
||||
|
@ -145,7 +150,6 @@ public class ControleurStandard implements ActionListener, ListSelectionListener
|
|||
try {
|
||||
this.commUDP.sendMessageInfoPseudo();
|
||||
} catch (IOException e1) {
|
||||
// TODO Auto-generated catch block
|
||||
e1.printStackTrace();
|
||||
}
|
||||
|
||||
|
@ -163,15 +167,7 @@ public class ControleurStandard implements ActionListener, ListSelectionListener
|
|||
// Cas deconnexion
|
||||
else if ((JButton) e.getSource() == this.vue.getButtonDeconnexion()) {
|
||||
try {
|
||||
this.commUDP.sendMessageDelete();
|
||||
this.commUDP.removeAll();
|
||||
this.commUDP.destroyAll();
|
||||
this.vue.removeAllUsers();
|
||||
Utilisateur.resetSelf();
|
||||
|
||||
vue.dispose();
|
||||
|
||||
new VueConnexion(this.num);
|
||||
this.setVueConnexion();
|
||||
} catch (IOException e1) {
|
||||
|
||||
e1.printStackTrace();
|
||||
|
@ -191,9 +187,8 @@ public class ControleurStandard implements ActionListener, ListSelectionListener
|
|||
public void windowClosing(WindowEvent e) {
|
||||
|
||||
try {
|
||||
this.commUDP.sendMessageDelete();
|
||||
this.setVueConnexion();
|
||||
} catch (IOException e1) {
|
||||
// TODO Auto-generated catch block
|
||||
e1.printStackTrace();
|
||||
}
|
||||
|
||||
|
@ -207,7 +202,6 @@ public class ControleurStandard implements ActionListener, ListSelectionListener
|
|||
|
||||
@Override
|
||||
public void windowClosed(WindowEvent e) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
|
@ -308,5 +302,24 @@ public class ControleurStandard implements ActionListener, ListSelectionListener
|
|||
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;
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -7,7 +7,9 @@ import java.awt.event.MouseAdapter;
|
|||
import java.awt.event.MouseEvent;
|
||||
import java.awt.event.MouseListener;
|
||||
import java.io.IOException;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
|
||||
import javax.swing.AbstractButton;
|
||||
import javax.swing.BorderFactory;
|
||||
|
@ -25,6 +27,7 @@ import javax.swing.ScrollPaneConstants;
|
|||
import javax.swing.plaf.basic.BasicButtonUI;
|
||||
|
||||
import communication.udp.CommunicationUDP;
|
||||
import connexion.VueConnexion;
|
||||
import database.SQLiteManager;
|
||||
import main.Utilisateur;
|
||||
import main.Vue;
|
||||
|
@ -48,12 +51,13 @@ public class VueStandard extends Vue {
|
|||
private ArrayList<VueSession> sessions;
|
||||
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);
|
||||
|
||||
this.tabButtons = new ArrayList<JButton>();
|
||||
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());
|
||||
|
||||
|
@ -119,6 +123,11 @@ public class VueStandard extends Vue {
|
|||
this.seDeconnecter.addActionListener(this.c);
|
||||
deconnexion.add(this.seDeconnecter);
|
||||
|
||||
if(Utilisateur.getSelf().getId() == "admin") {
|
||||
JButton addNewUser = new JButton("Ajouter un nouvel utilisateur");
|
||||
deconnexion.add(addNewUser);
|
||||
}
|
||||
|
||||
// --------Ajout à la vue--------//
|
||||
left.add(self, BorderLayout.PAGE_START);
|
||||
left.add(listScroller, BorderLayout.CENTER);
|
||||
|
@ -186,6 +195,10 @@ public class VueStandard extends Vue {
|
|||
this.pseudoSelf.setText(pseudo);
|
||||
}
|
||||
|
||||
public void setPseudoSelf() {
|
||||
this.setDisplayedPseudo(Utilisateur.getSelf().getPseudo());
|
||||
}
|
||||
|
||||
// ------------ JOPTIONS -------------//
|
||||
|
||||
protected int displayJOptionSessionCreation(String pseudo) {
|
||||
|
@ -237,18 +250,6 @@ public class VueStandard extends Vue {
|
|||
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) {
|
||||
JPanel tabTitle = new JPanel();
|
||||
|
||||
|
@ -267,12 +268,41 @@ public class VueStandard extends Vue {
|
|||
|
||||
}
|
||||
|
||||
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 -------------//
|
||||
|
||||
protected void removeAllUsers() {
|
||||
this.userList.removeAllElements();
|
||||
}
|
||||
|
||||
public void initControleur() throws UnknownHostException, IOException {
|
||||
this.c.init();
|
||||
}
|
||||
|
||||
// ------------- PRIVATE CLASSES FOR THE TABS BUTTON -------------//
|
||||
private class TabButton extends JButton {
|
||||
|
||||
|
|
Loading…
Reference in a new issue