Compare commits
10 commits
master
...
standard+c
Author | SHA1 | Date | |
---|---|---|---|
|
79d50ea36d | ||
|
a1531eebb5 | ||
|
f6b86d7aea | ||
|
5fd722d95b | ||
|
7b107a758f | ||
|
7a4d831d56 | ||
|
13ca8bb0c0 | ||
|
94e1c6165b | ||
|
539a0f0438 | ||
|
1660e85004 |
17 changed files with 1162 additions and 3 deletions
194
POO/src/communication/CommunicationUDP.java
Normal file
194
POO/src/communication/CommunicationUDP.java
Normal file
|
@ -0,0 +1,194 @@
|
|||
package communication;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
||||
import main.Observer;
|
||||
import main.Utilisateur;
|
||||
import main.VueStandard;
|
||||
import messages.*;
|
||||
|
||||
|
||||
public class CommunicationUDP extends Thread {
|
||||
|
||||
// public enum Mode {PREMIERE_CONNEXION, CHANGEMENT_PSEUDO, DECONNEXION};
|
||||
|
||||
private UDPClient client;
|
||||
private int portServer;
|
||||
private ArrayList<Integer> portOthers;
|
||||
private ArrayList<Utilisateur> users = new ArrayList<Utilisateur>();
|
||||
private Observer observer;
|
||||
|
||||
public CommunicationUDP(int portClient, int portServer, int[] portsOther) throws IOException {
|
||||
this.portServer = portServer;
|
||||
this.portOthers = this.getArrayListFromArray(portsOther);
|
||||
new UDPServer(portServer, this).start();
|
||||
this.client = new UDPClient(portClient);
|
||||
}
|
||||
|
||||
public void setObserver (Observer obs) {
|
||||
this.observer=obs;
|
||||
}
|
||||
|
||||
public ArrayList<Utilisateur> getListUsers(){
|
||||
return users;
|
||||
}
|
||||
|
||||
protected boolean containsUserFromID(String id) {
|
||||
for(Utilisateur u : users) {
|
||||
if(u.getId().equals(id) ) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean containsUserFromPseudo(String pseudo) {
|
||||
for(Utilisateur u : users) {
|
||||
if(u.getPseudo().equals(pseudo) ) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
//Marche pas
|
||||
private int getIndexFromID(String id) {
|
||||
int index = -1;
|
||||
for(int i=0; i < users.size() ; i++) {
|
||||
if(users.get(i).getId().contentEquals(id) ) {
|
||||
index=i;
|
||||
}
|
||||
}
|
||||
return index;
|
||||
}
|
||||
|
||||
private int getIndexFromIP(InetAddress ip) {
|
||||
for(int i=0; i < users.size() ; i++) {
|
||||
if(users.get(i).getIp().equals(ip)) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
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) {
|
||||
int index = getIndexFromID(idClient);
|
||||
users.get(index).setPseudo(pseudoClient);
|
||||
observer.update(this, users);
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
}
|
||||
observer.update(this, users);
|
||||
}
|
||||
|
||||
public void removeAll(){
|
||||
int oSize = users.size();
|
||||
for(int i=0; i<oSize;i++) {
|
||||
users.remove(0);
|
||||
}
|
||||
}
|
||||
|
||||
private ArrayList<Integer> getArrayListFromArray(int ports[]) {
|
||||
ArrayList<Integer> tmp = new ArrayList<Integer>();
|
||||
for (int port : ports) {
|
||||
tmp.add(port);
|
||||
}
|
||||
tmp.remove(Integer.valueOf(portServer));
|
||||
|
||||
return tmp;
|
||||
}
|
||||
|
||||
|
||||
public void sendMessageConnecte() throws UnknownHostException, IOException {
|
||||
for(int port : this.portOthers) {
|
||||
try {
|
||||
this.client.sendMessageUDP_local(new MessageSysteme(Message.TypeMessage.JE_SUIS_CONNECTE), port, InetAddress.getLocalHost());
|
||||
} catch (MauvaisTypeMessageException e) {/*Si ça marche pas essayer là*/}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Send the message "add,id,pseudo" to localhost on all the ports in
|
||||
// "portOthers"
|
||||
// This allows the receivers' agent (portOthers) to create or modify an entry with the
|
||||
// data of this agent
|
||||
//Typically used to notify of a name change
|
||||
public void sendMessageInfoPseudo() throws UnknownHostException, IOException {
|
||||
|
||||
Utilisateur self = Utilisateur.getSelf();
|
||||
|
||||
String pseudoSelf =self.getPseudo();
|
||||
String idSelf = self.getId();
|
||||
|
||||
Message msout = null;
|
||||
try {
|
||||
msout = new MessageSysteme(Message.TypeMessage.INFO_PSEUDO, pseudoSelf, idSelf);
|
||||
for(int port : this.portOthers) {
|
||||
this.client.sendMessageUDP_local(msout, port, InetAddress.getLocalHost());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
//Same, but on only one port
|
||||
//Typically used to give your current name and id to a newly arrived host
|
||||
public void sendMessageInfoPseudo(int portOther) throws UnknownHostException, IOException {
|
||||
|
||||
Utilisateur self = Utilisateur.getSelf();
|
||||
try {
|
||||
Message msout = new MessageSysteme(Message.TypeMessage.INFO_PSEUDO, self.getPseudo(), self.getId());
|
||||
this.client.sendMessageUDP_local(msout, portOther, InetAddress.getLocalHost());
|
||||
} catch (MauvaisTypeMessageException e) {e.printStackTrace();}
|
||||
}
|
||||
|
||||
|
||||
// Send the message "del,id,pseudo" to localhost on all the ports in
|
||||
// "portOthers"
|
||||
// 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) {
|
||||
try {
|
||||
this.client.sendMessageUDP_local(new MessageSysteme(Message.TypeMessage.JE_SUIS_DECONNECTE), 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();
|
||||
// }
|
||||
|
||||
}
|
41
POO/src/communication/UDPClient.java
Normal file
41
POO/src/communication/UDPClient.java
Normal file
|
@ -0,0 +1,41 @@
|
|||
package communication;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.InetAddress;
|
||||
import java.net.NetworkInterface;
|
||||
import java.net.SocketException;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
import messages.*;
|
||||
|
||||
public class UDPClient {
|
||||
|
||||
private DatagramSocket sockUDP;
|
||||
private InetAddress broadcast;
|
||||
|
||||
public UDPClient(int port) throws SocketException, UnknownHostException {
|
||||
this.sockUDP = new DatagramSocket(port);
|
||||
|
||||
InetAddress localHost = InetAddress.getLocalHost();
|
||||
NetworkInterface networkInterface = NetworkInterface.getByInetAddress(localHost);
|
||||
this.broadcast = networkInterface.getInterfaceAddresses().get(0).getBroadcast();
|
||||
}
|
||||
|
||||
|
||||
//Send a message casted as string to the specified port on localhost
|
||||
protected void sendMessageUDP_local(Message message, int port, InetAddress clientAddress) throws IOException {
|
||||
String messageString=message.toString();
|
||||
DatagramPacket outpacket = new DatagramPacket(messageString.getBytes(), messageString.length(), clientAddress, port);
|
||||
this.sockUDP.send(outpacket);
|
||||
|
||||
}
|
||||
|
||||
// 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);
|
||||
// }
|
||||
|
||||
}
|
71
POO/src/communication/UDPServer.java
Normal file
71
POO/src/communication/UDPServer.java
Normal file
|
@ -0,0 +1,71 @@
|
|||
package communication;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.SocketException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
||||
import messages.*;
|
||||
|
||||
|
||||
|
||||
public class UDPServer extends Thread {
|
||||
|
||||
private DatagramSocket sockUDP;
|
||||
private CommunicationUDP commUDP;
|
||||
private byte[] buffer;
|
||||
|
||||
public UDPServer(int port, CommunicationUDP commUDP) throws SocketException {
|
||||
this.commUDP = commUDP;
|
||||
this.sockUDP = new DatagramSocket(port);
|
||||
this.buffer = new byte[256];
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
while (true) {
|
||||
|
||||
try {
|
||||
|
||||
DatagramPacket inPacket = new DatagramPacket(buffer, buffer.length);
|
||||
this.sockUDP.receive(inPacket);
|
||||
String msgString = new String(inPacket.getData(), 0, inPacket.getLength());
|
||||
Message msg = Message.stringToMessage(msgString);
|
||||
|
||||
switch(msg.getTypeMessage()) {
|
||||
case JE_SUIS_CONNECTE :
|
||||
//System.out.println("first co");
|
||||
int portClient = inPacket.getPort();
|
||||
int portServer = portClient+1;
|
||||
|
||||
this.commUDP.sendMessageInfoPseudo(portServer);
|
||||
break;
|
||||
|
||||
case INFO_PSEUDO :
|
||||
|
||||
if (commUDP.containsUserFromID(((MessageSysteme) msg).getId())) {
|
||||
commUDP.changePseudoUser(((MessageSysteme) msg).getId(), ((MessageSysteme) msg).getPseudo(), inPacket.getAddress());
|
||||
}
|
||||
else {
|
||||
|
||||
commUDP.addUser(((MessageSysteme) msg).getId(), ((MessageSysteme) msg).getPseudo(), inPacket.getAddress());
|
||||
//System.out.println(((MessageSysteme) msg).getId()+", "+((MessageSysteme) msg).getPseudo());
|
||||
}
|
||||
break;
|
||||
|
||||
case JE_SUIS_DECONNECTE :
|
||||
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
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
System.out.println("receive exception");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
126
POO/src/main/ControleurConnexion.java
Normal file
126
POO/src/main/ControleurConnexion.java
Normal file
|
@ -0,0 +1,126 @@
|
|||
package main;
|
||||
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.io.IOException;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
|
||||
import communication.*;
|
||||
|
||||
public class ControleurConnexion implements ActionListener, Observer{
|
||||
|
||||
private enum Etat {DEBUT, ID_OK};
|
||||
|
||||
private VueConnexion vue;
|
||||
private Etat etat;
|
||||
private CommunicationUDP comUDP;
|
||||
private String id;
|
||||
private String pseudo;
|
||||
|
||||
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 {
|
||||
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(2408, 2409, new int[] {2209, 2309});
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
comUDP.setObserver(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
boolean inputOK = false;
|
||||
if (this.etat == Etat.DEBUT) {
|
||||
id=vue.getValeurTextField();
|
||||
|
||||
//Recherche dans la liste des utilisateurs enregistres, report sur inputOK
|
||||
inputOK = (id.contentEquals("idvalide")||id.contentEquals("idv2"));
|
||||
|
||||
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
|
||||
vue.setTexteLabelInput("Veuillez entrer votre nom");
|
||||
vue.resetValeurTextField();
|
||||
inputOK=false;
|
||||
}
|
||||
else vue.setTexteLabelInput("Identifiant invalide, veuillez réessayer");
|
||||
}
|
||||
else {
|
||||
pseudo=vue.getValeurTextField();
|
||||
|
||||
//Recherche dans la liste locale des utilisateurs connectes, report sur inputOK
|
||||
inputOK = !comUDP.containsUserFromPseudo(pseudo);
|
||||
|
||||
if (inputOK) {
|
||||
//Reglage de l'utilisateur
|
||||
try {
|
||||
Utilisateur.setSelf(id, pseudo, "localhost");
|
||||
} catch (UnknownHostException e2) {
|
||||
// TODO Auto-generated catch block
|
||||
e2.printStackTrace();
|
||||
}
|
||||
|
||||
//Broadcast du pseudo
|
||||
try {
|
||||
comUDP.sendMessageInfoPseudo();
|
||||
} catch (UnknownHostException e1) {
|
||||
// TODO Auto-generated catch block
|
||||
e1.printStackTrace();
|
||||
} catch (IOException e1) {
|
||||
// TODO Auto-generated catch block
|
||||
e1.printStackTrace();
|
||||
}
|
||||
try {
|
||||
vue.close();
|
||||
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
|
||||
|
||||
}
|
||||
|
||||
}
|
180
POO/src/main/ControleurStandard.java
Normal file
180
POO/src/main/ControleurStandard.java
Normal file
|
@ -0,0 +1,180 @@
|
|||
package main;
|
||||
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.awt.event.WindowEvent;
|
||||
import java.awt.event.WindowListener;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JList;
|
||||
import javax.swing.event.ListSelectionEvent;
|
||||
import javax.swing.event.ListSelectionListener;
|
||||
|
||||
import communication.CommunicationUDP;
|
||||
|
||||
public class ControleurStandard implements ActionListener, ListSelectionListener, WindowListener, Observer {
|
||||
|
||||
private enum EtatModif {
|
||||
TERMINE, EN_COURS
|
||||
}
|
||||
|
||||
private EtatModif etatModif;
|
||||
private VueStandard vue;
|
||||
private CommunicationUDP commUDP;
|
||||
private String lastPseudo;
|
||||
|
||||
|
||||
public ControleurStandard(VueStandard vue, CommunicationUDP commUDP) throws IOException {
|
||||
this.vue = vue;
|
||||
this.commUDP = commUDP;
|
||||
this.commUDP.setObserver(this);
|
||||
this.commUDP.sendMessageConnecte();
|
||||
this.commUDP.sendMessageInfoPseudo();
|
||||
this.etatModif = EtatModif.TERMINE;
|
||||
}
|
||||
|
||||
//---------- LISTSELECTION LISTENER OPERATIONS ----------//
|
||||
@Override
|
||||
public void valueChanged(ListSelectionEvent e) {
|
||||
if (!e.getValueIsAdjusting()) {
|
||||
JList<String> list = vue.getActiveUsersList();
|
||||
System.out.println(list.getSelectedValue());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//---------- ACTION LISTENER OPERATIONS ----------//
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if ((JButton) e.getSource() == this.vue.getButtonModifierPseudo()) {
|
||||
JButton modifierPseudo = (JButton) e.getSource();
|
||||
|
||||
if (this.etatModif == EtatModif.TERMINE) {
|
||||
this.lastPseudo = Utilisateur.getSelf().getPseudo();
|
||||
modifierPseudo.setText("OK");
|
||||
this.etatModif = EtatModif.EN_COURS;
|
||||
} else {
|
||||
|
||||
if (!this.commUDP.containsUserFromPseudo(this.vue.getDisplayedPseudo())) {
|
||||
|
||||
Utilisateur.getSelf().setPseudo(this.vue.getDisplayedPseudo());
|
||||
|
||||
try {
|
||||
this.commUDP.sendMessageInfoPseudo();
|
||||
} catch (IOException e1) {
|
||||
// TODO Auto-generated catch block
|
||||
e1.printStackTrace();
|
||||
}
|
||||
|
||||
} else {
|
||||
this.vue.setDisplayedPseudo(this.lastPseudo);
|
||||
}
|
||||
|
||||
modifierPseudo.setText("Modifier");
|
||||
this.etatModif = EtatModif.TERMINE;
|
||||
}
|
||||
|
||||
this.vue.toggleEditPseudo();
|
||||
}
|
||||
|
||||
if((JButton) e.getSource() == this.vue.getButtonDeconnexion() ) {
|
||||
try {
|
||||
this.commUDP.sendMessageDelete();
|
||||
this.commUDP.removeAll();
|
||||
VueStandard.userList.removeAllElements();
|
||||
Utilisateur.getSelf().setPseudo("");
|
||||
vue.dispose();
|
||||
new VueConnexion(5);
|
||||
/*this.vue.toggleEnableButtonConnexion();
|
||||
this.vue.toggleEnableButtonDeconnexion();*/
|
||||
} catch (IOException e1) {
|
||||
|
||||
e1.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if((JButton) e.getSource() == this.vue.getButtonConnexion() ) {
|
||||
try {
|
||||
Utilisateur.getSelf().setPseudo(this.vue.getDisplayedPseudo());
|
||||
this.commUDP.sendMessageConnecte();
|
||||
this.commUDP.sendMessageInfoPseudo();
|
||||
|
||||
this.vue.toggleEnableButtonConnexion();
|
||||
this.vue.toggleEnableButtonDeconnexion();
|
||||
} catch (IOException e1) {
|
||||
|
||||
e1.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//---------- WINDOW LISTENER OPERATIONS ----------//
|
||||
|
||||
@Override
|
||||
public void windowClosing(WindowEvent e) {
|
||||
|
||||
try {
|
||||
this.commUDP.sendMessageDelete();
|
||||
} catch (IOException e1) {
|
||||
// TODO Auto-generated catch block
|
||||
e1.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void windowOpened(WindowEvent e) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void windowClosed(WindowEvent e) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void windowIconified(WindowEvent e) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void windowDeiconified(WindowEvent e) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void windowActivated(WindowEvent e) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void windowDeactivated(WindowEvent e) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
//---------- OBSERVER OPERATIONS ----------//
|
||||
@Override
|
||||
public void update(Object o, Object arg) {
|
||||
//entre dans la fonction mais affichage pas systematique : voir si pb d'affichage ou d'argument
|
||||
ArrayList<Utilisateur> userList = (ArrayList<Utilisateur>) arg;
|
||||
ArrayList<String> listPseudo = new ArrayList<String>();
|
||||
vue.resetListUsers();
|
||||
//System.out.println("Updated list :");
|
||||
for (Utilisateur user : userList) {
|
||||
//System.out.println(user.getPseudo());
|
||||
listPseudo.add(user.getPseudo());
|
||||
}
|
||||
vue.addListUsers(listPseudo);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,10 +1,18 @@
|
|||
package main;
|
||||
|
||||
public class Main {
|
||||
|
||||
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JPanel;
|
||||
|
||||
public class Main extends JPanel{
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
new VueConnexion(Integer.parseInt(args[0]));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
13
POO/src/main/Message.java
Normal file
13
POO/src/main/Message.java
Normal file
|
@ -0,0 +1,13 @@
|
|||
package main;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class Message implements Serializable {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
private String contenu;
|
||||
|
||||
}
|
5
POO/src/main/Observer.java
Normal file
5
POO/src/main/Observer.java
Normal file
|
@ -0,0 +1,5 @@
|
|||
package main;
|
||||
|
||||
public interface Observer {
|
||||
public void update(Object o, Object arg);
|
||||
}
|
51
POO/src/main/Utilisateur.java
Normal file
51
POO/src/main/Utilisateur.java
Normal file
|
@ -0,0 +1,51 @@
|
|||
package main;
|
||||
import java.io.Serializable;
|
||||
import java.net.*;
|
||||
|
||||
public class Utilisateur implements Serializable{
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private String id;
|
||||
private String pseudo;
|
||||
private InetAddress ip;
|
||||
|
||||
private static Utilisateur self;
|
||||
|
||||
public Utilisateur(String id, String pseudo, InetAddress ip) throws UnknownHostException {
|
||||
this.id = id;
|
||||
this.pseudo = pseudo;
|
||||
this.ip = ip;
|
||||
/*System.out.println(InetAddress.getLocalHost())*/;
|
||||
}
|
||||
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getPseudo() {
|
||||
return pseudo;
|
||||
}
|
||||
|
||||
public void setPseudo(String pseudo) {
|
||||
this.pseudo = pseudo;
|
||||
}
|
||||
|
||||
public InetAddress getIp() {
|
||||
return ip;
|
||||
}
|
||||
|
||||
public static void setSelf(String id, String pseudo,String host) throws UnknownHostException {
|
||||
if(Utilisateur.self == null) {
|
||||
Utilisateur.self = new Utilisateur(id, pseudo, InetAddress.getByName(host));
|
||||
}
|
||||
}
|
||||
|
||||
public static Utilisateur getSelf() {
|
||||
return Utilisateur.self;
|
||||
}
|
||||
}
|
16
POO/src/main/Vue.java
Normal file
16
POO/src/main/Vue.java
Normal file
|
@ -0,0 +1,16 @@
|
|||
package main;
|
||||
|
||||
import javax.swing.JFrame;
|
||||
|
||||
public class Vue extends JFrame{
|
||||
|
||||
public Vue(String title) {
|
||||
super(title);
|
||||
}
|
||||
|
||||
public void reduireAgent() {}
|
||||
|
||||
public void close() {
|
||||
this.dispose();
|
||||
}
|
||||
}
|
76
POO/src/main/VueConnexion.java
Normal file
76
POO/src/main/VueConnexion.java
Normal file
|
@ -0,0 +1,76 @@
|
|||
package main;
|
||||
|
||||
//Importe les librairies
|
||||
import java.awt.*;
|
||||
import java.awt.event.*;
|
||||
import javax.swing.*;
|
||||
|
||||
public class VueConnexion extends Vue {
|
||||
|
||||
//Elements vue
|
||||
private JPanel panel;
|
||||
private JButton boutonValider;
|
||||
private JTextField input;
|
||||
private JLabel labelInput;
|
||||
|
||||
//Controleur
|
||||
ControleurConnexion controle;
|
||||
|
||||
//penser à enlever le numtest
|
||||
public VueConnexion(int numtest) {
|
||||
super("Connexion");
|
||||
controle = new ControleurConnexion(this, numtest);
|
||||
|
||||
//Creation fenetre
|
||||
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
this.setSize(400, 100);
|
||||
this.setLocationRelativeTo(null);
|
||||
|
||||
//Creation panel
|
||||
panel = new JPanel(new GridLayout(3,1));
|
||||
|
||||
//Ajout elements
|
||||
ajouterElements();
|
||||
|
||||
//Regle le bouton par défaut
|
||||
this.getRootPane().setDefaultButton(boutonValider);
|
||||
|
||||
//Ajoute le panel a la fenetre
|
||||
this.getContentPane().add(panel, BorderLayout.CENTER);
|
||||
|
||||
//Affiche la fenetre
|
||||
this.setVisible(true);
|
||||
}
|
||||
|
||||
private void ajouterElements() {
|
||||
|
||||
//Cree les elements
|
||||
input = new JTextField();
|
||||
labelInput = new JLabel("Veuillez entrer votre identifiant unique");
|
||||
boutonValider = new JButton("Valider");
|
||||
|
||||
//Le controleur guette les evenements du bouton
|
||||
boutonValider.addActionListener(controle);
|
||||
|
||||
//Ajoute les elements
|
||||
panel.add(labelInput);
|
||||
panel.add(input);
|
||||
panel.add(boutonValider);
|
||||
|
||||
labelInput.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
|
||||
}
|
||||
|
||||
|
||||
//Getters et setters
|
||||
public void setTexteLabelInput(String text) {
|
||||
labelInput.setText(text);
|
||||
}
|
||||
|
||||
public String getValeurTextField() {
|
||||
return input.getText();
|
||||
}
|
||||
|
||||
public void resetValeurTextField() {
|
||||
input.setText("");
|
||||
}
|
||||
}
|
202
POO/src/main/VueStandard.java
Normal file
202
POO/src/main/VueStandard.java
Normal file
|
@ -0,0 +1,202 @@
|
|||
package main;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.event.WindowAdapter;
|
||||
import java.awt.event.WindowEvent;
|
||||
import java.io.IOException;
|
||||
import java.net.SocketException;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Vector;
|
||||
|
||||
import javax.swing.BorderFactory;
|
||||
import javax.swing.DefaultListModel;
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JList;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.JScrollPane;
|
||||
import javax.swing.JTabbedPane;
|
||||
import javax.swing.JTextField;
|
||||
import javax.swing.ListSelectionModel;
|
||||
import javax.swing.ScrollPaneConstants;
|
||||
|
||||
import communication.CommunicationUDP;
|
||||
|
||||
public class VueStandard extends Vue {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private JList<String> activeUsersList;
|
||||
private JTextField pseudoSelf;
|
||||
private JButton modifierPseudo;
|
||||
private JButton seConnecter;
|
||||
private JButton seDeconnecter;
|
||||
private ControleurStandard c;
|
||||
public static DefaultListModel<String> userList = new DefaultListModel<String>();
|
||||
|
||||
public VueStandard(String title, CommunicationUDP comUDP) throws IOException {
|
||||
super(title);
|
||||
|
||||
JPanel main = new JPanel(new GridBagLayout());
|
||||
main.setBackground(Color.green);
|
||||
|
||||
JPanel left = new JPanel(new BorderLayout());
|
||||
left.setBackground(Color.red);
|
||||
left.setPreferredSize(new Dimension(200, 200));
|
||||
|
||||
JPanel chat = new JPanel();
|
||||
chat.setBackground(Color.blue);
|
||||
chat.setPreferredSize(new Dimension(575, 600));
|
||||
|
||||
JPanel bottom = new JPanel(new GridLayout(1, 2));
|
||||
bottom.setBackground(Color.yellow);
|
||||
bottom.setPreferredSize(new Dimension(575, 150));
|
||||
|
||||
|
||||
|
||||
this.c = new ControleurStandard(this, comUDP);
|
||||
|
||||
//--------Panel haut pseudo--------//
|
||||
JPanel self = new JPanel(new FlowLayout());
|
||||
|
||||
this.pseudoSelf = new JTextField(Utilisateur.getSelf().getPseudo());
|
||||
this.pseudoSelf.setPreferredSize(new Dimension(100, 20));
|
||||
this.pseudoSelf.setEditable(false);
|
||||
|
||||
this.modifierPseudo = new JButton("Modifier");
|
||||
this.modifierPseudo.addActionListener(this.c);
|
||||
|
||||
self.add(new JLabel("Moi : "));
|
||||
self.add(this.pseudoSelf);
|
||||
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);
|
||||
listScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
|
||||
listScroller.setBorder(BorderFactory.createCompoundBorder(
|
||||
BorderFactory.createTitledBorder("Utilisateurs Actifs"),
|
||||
BorderFactory.createEmptyBorder(5,2,2,2)));
|
||||
|
||||
|
||||
//--------Panel bas deconnexion--------//
|
||||
JPanel deconnexion = new JPanel(new GridLayout(1, 2));
|
||||
|
||||
this.seConnecter = new JButton("Se Connecter");
|
||||
this.seConnecter.setEnabled(false);
|
||||
this.seConnecter.addActionListener(this.c);
|
||||
|
||||
this.seDeconnecter = new JButton("Se Déconnecter");
|
||||
this.seDeconnecter.addActionListener(this.c);
|
||||
|
||||
deconnexion.add(this.seConnecter);
|
||||
deconnexion.add(this.seDeconnecter);
|
||||
|
||||
//--------Ajout à la vue--------//
|
||||
left.add(self, BorderLayout.PAGE_START);
|
||||
left.add(listScroller, BorderLayout.CENTER);
|
||||
left.add(deconnexion, BorderLayout.PAGE_END);
|
||||
|
||||
|
||||
|
||||
GridBagConstraints gridBagConstraint = new GridBagConstraints();
|
||||
|
||||
gridBagConstraint.fill = GridBagConstraints.BOTH;
|
||||
gridBagConstraint.gridx = 0;
|
||||
gridBagConstraint.gridy = 0;
|
||||
gridBagConstraint.gridwidth = 1;
|
||||
gridBagConstraint.gridheight = 4;
|
||||
gridBagConstraint.weightx = 0.33;
|
||||
gridBagConstraint.weighty = 1;
|
||||
|
||||
main.add(left,gridBagConstraint);
|
||||
|
||||
gridBagConstraint.fill = GridBagConstraints.BOTH;
|
||||
gridBagConstraint.gridx = 1;
|
||||
gridBagConstraint.gridy = 0;
|
||||
gridBagConstraint.gridwidth = 2;
|
||||
gridBagConstraint.gridheight = 3;
|
||||
gridBagConstraint.weightx = 0.66;
|
||||
gridBagConstraint.weighty = 0.66;
|
||||
|
||||
main.add(chat,gridBagConstraint);
|
||||
|
||||
gridBagConstraint.fill = GridBagConstraints.BOTH;
|
||||
gridBagConstraint.gridx = 1;
|
||||
gridBagConstraint.gridy = 3;
|
||||
gridBagConstraint.gridwidth = 2;
|
||||
gridBagConstraint.gridheight = 1;
|
||||
gridBagConstraint.weightx = 0.66;
|
||||
gridBagConstraint.weighty = 0.33;
|
||||
|
||||
|
||||
main.add(bottom,gridBagConstraint);
|
||||
|
||||
this.add(main);
|
||||
|
||||
this.setSize(900,900);
|
||||
this.setVisible(true);
|
||||
|
||||
this.addWindowListener(c);
|
||||
}
|
||||
|
||||
public JList<String> getActiveUsersList(){
|
||||
return this.activeUsersList;
|
||||
}
|
||||
|
||||
|
||||
protected JButton getButtonModifierPseudo() {
|
||||
return this.modifierPseudo;
|
||||
}
|
||||
|
||||
protected JButton getButtonDeconnexion() {
|
||||
return this.seDeconnecter;
|
||||
}
|
||||
|
||||
protected JButton getButtonConnexion() {
|
||||
return this.seConnecter;
|
||||
}
|
||||
|
||||
protected String getDisplayedPseudo() {
|
||||
return this.pseudoSelf.getText();
|
||||
}
|
||||
|
||||
protected void setDisplayedPseudo(String pseudo) {
|
||||
this.pseudoSelf.setText(pseudo);
|
||||
}
|
||||
|
||||
protected void toggleEditPseudo() {
|
||||
this.pseudoSelf.setEditable(!this.pseudoSelf.isEditable());
|
||||
}
|
||||
|
||||
protected void toggleEnableButtonDeconnexion() {
|
||||
this.seDeconnecter.setEnabled(!this.seDeconnecter.isEnabled());
|
||||
}
|
||||
|
||||
protected void toggleEnableButtonConnexion() {
|
||||
this.seConnecter.setEnabled(!this.seConnecter.isEnabled());
|
||||
}
|
||||
|
||||
//Update de la liste des utilisateurs//
|
||||
protected void resetListUsers() {
|
||||
VueStandard.userList.removeAllElements();
|
||||
}
|
||||
|
||||
protected void addListUsers (ArrayList<String> listPseudo) {
|
||||
VueStandard.userList.addAll(listPseudo);
|
||||
}
|
||||
|
||||
}
|
8
POO/src/messages/MauvaisTypeMessageException.java
Normal file
8
POO/src/messages/MauvaisTypeMessageException.java
Normal file
|
@ -0,0 +1,8 @@
|
|||
package messages;
|
||||
|
||||
public class MauvaisTypeMessageException extends Exception {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
|
||||
}
|
69
POO/src/messages/Message.java
Normal file
69
POO/src/messages/Message.java
Normal file
|
@ -0,0 +1,69 @@
|
|||
package messages;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.lang.instrument.Instrumentation;
|
||||
import java.util.Arrays;
|
||||
|
||||
import messages.Message.TypeMessage;
|
||||
|
||||
public abstract class Message implements Serializable {
|
||||
|
||||
public enum TypeMessage {JE_SUIS_CONNECTE, JE_SUIS_DECONNECTE, INFO_PSEUDO, TEXTE, IMAGE, FICHIER, MESSAGE_NUL}
|
||||
protected TypeMessage type;
|
||||
private static final long serialVersionUID = 1L;
|
||||
private static Instrumentation inst;
|
||||
|
||||
public TypeMessage getTypeMessage() {
|
||||
return this.type;
|
||||
}
|
||||
|
||||
protected abstract String attributsToString();
|
||||
|
||||
public String toString() {
|
||||
return this.type+"###"+this.attributsToString();
|
||||
}
|
||||
|
||||
public static Message stringToMessage(String messageString) {
|
||||
try {
|
||||
String[] parts = messageString.split("###");
|
||||
switch (parts[0]) {
|
||||
case "JE_SUIS_CONNECTE" :
|
||||
return new MessageSysteme(TypeMessage.JE_SUIS_CONNECTE);
|
||||
|
||||
case "JE_SUIS_DECONNECTE" :
|
||||
return new MessageSysteme(TypeMessage.JE_SUIS_DECONNECTE);
|
||||
|
||||
case "INFO_PSEUDO" :
|
||||
return new MessageSysteme(TypeMessage.INFO_PSEUDO, parts[1], parts[2]);
|
||||
|
||||
case "TEXTE" :
|
||||
return new MessageTexte(TypeMessage.TEXTE, parts[1]);
|
||||
|
||||
case "IMAGE" :
|
||||
return new MessageFichier(TypeMessage.IMAGE, parts[1], parts[2]);
|
||||
|
||||
case "FICHIER" :
|
||||
return new MessageFichier(TypeMessage.FICHIER, parts[1], parts[2]);
|
||||
}
|
||||
} catch (MauvaisTypeMessageException e) {}
|
||||
return null;
|
||||
}
|
||||
|
||||
//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 m3 = new MessageSysteme(TypeMessage.INFO_PSEUDO, "pseudo156434518", "id236");
|
||||
Message m4 = new MessageTexte(TypeMessage.TEXTE, "blablabla");
|
||||
Message m5 = new MessageFichier(TypeMessage.FICHIER, "truc", ".pdf");
|
||||
|
||||
|
||||
System.out.println(Message.stringToMessage(m1.toString()));
|
||||
System.out.println(Message.stringToMessage(m2.toString()));
|
||||
System.out.println(Message.stringToMessage(m3.toString()));
|
||||
System.out.println(Message.stringToMessage(m4.toString()));
|
||||
System.out.println(Message.stringToMessage(m5.toString()));
|
||||
|
||||
}
|
||||
|
||||
}
|
33
POO/src/messages/MessageFichier.java
Normal file
33
POO/src/messages/MessageFichier.java
Normal file
|
@ -0,0 +1,33 @@
|
|||
package messages;
|
||||
|
||||
import messages.Message.TypeMessage;
|
||||
|
||||
public class MessageFichier extends Message {
|
||||
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
private String contenu;
|
||||
private String extension;
|
||||
|
||||
public MessageFichier(TypeMessage type, String contenu, String extension) throws MauvaisTypeMessageException{
|
||||
if ((type==TypeMessage.IMAGE)||(type==TypeMessage.FICHIER)) {
|
||||
this.type=type;
|
||||
this.contenu=contenu;
|
||||
this.extension=extension;
|
||||
}
|
||||
else throw new MauvaisTypeMessageException();
|
||||
}
|
||||
|
||||
public String getContenu() {
|
||||
return this.contenu;
|
||||
}
|
||||
|
||||
public String getExtension() {
|
||||
return this.extension;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String attributsToString() {
|
||||
return this.contenu+"###"+this.extension;
|
||||
}
|
||||
}
|
39
POO/src/messages/MessageSysteme.java
Normal file
39
POO/src/messages/MessageSysteme.java
Normal file
|
@ -0,0 +1,39 @@
|
|||
package messages;
|
||||
|
||||
public class MessageSysteme extends Message {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
private String pseudo;
|
||||
private String id;
|
||||
|
||||
public MessageSysteme(TypeMessage type) throws MauvaisTypeMessageException{
|
||||
if ((type==TypeMessage.JE_SUIS_CONNECTE)||(type==TypeMessage.JE_SUIS_DECONNECTE)||(type==TypeMessage.MESSAGE_NUL)) {
|
||||
this.type=type;
|
||||
this.pseudo="";
|
||||
this.id="";
|
||||
}
|
||||
else throw new MauvaisTypeMessageException();
|
||||
}
|
||||
|
||||
public MessageSysteme(TypeMessage type, String pseudo, String id) throws MauvaisTypeMessageException {
|
||||
if (type==TypeMessage.INFO_PSEUDO) {
|
||||
this.type=type;
|
||||
this.pseudo=pseudo;
|
||||
this.id=id;
|
||||
}
|
||||
else throw new MauvaisTypeMessageException();
|
||||
}
|
||||
|
||||
public String getPseudo() {
|
||||
return this.pseudo;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String attributsToString() {
|
||||
return this.pseudo+"###"+this.id;
|
||||
}
|
||||
}
|
27
POO/src/messages/MessageTexte.java
Normal file
27
POO/src/messages/MessageTexte.java
Normal file
|
@ -0,0 +1,27 @@
|
|||
package messages;
|
||||
|
||||
import messages.Message.TypeMessage;
|
||||
|
||||
public class MessageTexte extends Message {
|
||||
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
private String contenu;
|
||||
|
||||
public MessageTexte(TypeMessage type, String contenu) throws MauvaisTypeMessageException{
|
||||
if (type==TypeMessage.TEXTE) {
|
||||
this.type=type;
|
||||
this.contenu=contenu;
|
||||
}
|
||||
else throw new MauvaisTypeMessageException();
|
||||
}
|
||||
|
||||
public String getContenu() {
|
||||
return this.contenu;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String attributsToString() {
|
||||
return this.contenu;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue