Compare commits
3 commits
Author | SHA1 | Date | |
---|---|---|---|
|
566b89c382 | ||
|
61e069d6ec | ||
|
16204aa9ac |
|
@ -1,188 +0,0 @@
|
||||||
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 static 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected static boolean containsUserFromID(String id) {
|
|
||||||
for(Utilisateur u : users) {
|
|
||||||
if(u.getId().equals(id) ) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static boolean containsUserFromPseudo(String pseudo) {
|
|
||||||
for(Utilisateur u : users) {
|
|
||||||
if(u.getPseudo().equals(pseudo) ) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static int getIndexFromID(String id) {
|
|
||||||
for(int i=0; i < users.size() ; i++) {
|
|
||||||
if(users.get(i).getId().equals(id) ) {
|
|
||||||
return i;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static 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);
|
|
||||||
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) {/*Si ça marche pas essayer là*/}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//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();
|
|
||||||
// }
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,41 +0,0 @@
|
||||||
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);
|
|
||||||
// }
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,71 +0,0 @@
|
||||||
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 (CommunicationUDP.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");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,181 +0,0 @@
|
||||||
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;
|
|
||||||
private int clientPort;
|
|
||||||
|
|
||||||
public ControleurStandard(VueStandard vue, int portClient, int portServer, int[] portsOther) throws IOException {
|
|
||||||
this.vue = vue;
|
|
||||||
this.commUDP = new CommunicationUDP(portClient,portServer, portsOther);
|
|
||||||
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 (!CommunicationUDP.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("");
|
|
||||||
//Ajouter code pour passer à la vue de connexion
|
|
||||||
//
|
|
||||||
//
|
|
||||||
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,54 +0,0 @@
|
||||||
package main;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public class Main {
|
|
||||||
|
|
||||||
private static int portServers[] = {1526,1501,1551,1561};
|
|
||||||
private static String ids[] = {"Raijila", "titi33", "Semtexx", "Salam"};
|
|
||||||
private static String pseudo[] = {"Raijila", "Mirasio", "Semtexx", "Xaegon"};
|
|
||||||
public static void main(String[] args) {
|
|
||||||
|
|
||||||
|
|
||||||
switch(args[0]) {
|
|
||||||
case "0":
|
|
||||||
Main.createApp(0);
|
|
||||||
break;
|
|
||||||
case "1":
|
|
||||||
Main.createApp(1);
|
|
||||||
break;
|
|
||||||
case "2":
|
|
||||||
Main.createApp(2);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
Main.createApp(3);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// VueStandard.userList.addElement("Mirasio");
|
|
||||||
//
|
|
||||||
// try {
|
|
||||||
// Thread.sleep(2000);
|
|
||||||
// VueStandard.userList.addElement("Semtexx");
|
|
||||||
// } catch (InterruptedException e) {
|
|
||||||
// // TODO Auto-generated catch block
|
|
||||||
// e.printStackTrace();
|
|
||||||
// }
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void createApp(int i) {
|
|
||||||
try {
|
|
||||||
Utilisateur.setSelf(Main.ids[i], Main.pseudo[i], "localhost");
|
|
||||||
new VueStandard("Application", Main.portServers[i]-1, Main.portServers[i], Main.portServers);
|
|
||||||
} catch (IOException e) {
|
|
||||||
System.out.println(e.toString());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,13 +0,0 @@
|
||||||
package main;
|
|
||||||
|
|
||||||
import java.io.Serializable;
|
|
||||||
|
|
||||||
public class Message implements Serializable {
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
private static final long serialVersionUID = 1L;
|
|
||||||
private String contenu;
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,5 +0,0 @@
|
||||||
package main;
|
|
||||||
|
|
||||||
public interface Observer {
|
|
||||||
public void update(Object o, Object arg);
|
|
||||||
}
|
|
|
@ -1,51 +0,0 @@
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,15 +0,0 @@
|
||||||
package main;
|
|
||||||
|
|
||||||
import javax.swing.JFrame;
|
|
||||||
|
|
||||||
public class Vue extends JFrame{
|
|
||||||
|
|
||||||
public Vue(String title) {
|
|
||||||
super(title);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void reduireAgent() {}
|
|
||||||
|
|
||||||
public void fermerAgent() {}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,199 +0,0 @@
|
||||||
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;
|
|
||||||
|
|
||||||
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, int port, int clientPort, int[] portsOther) 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, port, clientPort, portsOther);
|
|
||||||
|
|
||||||
//--------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);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,8 +0,0 @@
|
||||||
package messages;
|
|
||||||
|
|
||||||
public class MauvaisTypeMessageException extends Exception {
|
|
||||||
|
|
||||||
private static final long serialVersionUID = 1L;
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,69 +0,0 @@
|
||||||
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()));
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,33 +0,0 @@
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,39 +0,0 @@
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,27 +0,0 @@
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
17
README.md
|
@ -1,3 +1,20 @@
|
||||||
# Projet_COO_POO
|
# Projet_COO_POO
|
||||||
|
|
||||||
Projet de 4ème année : Conception et programmation orientée objet d'un systeme de clavardage distribué interactif multi-utilisateur temps réel
|
Projet de 4ème année : Conception et programmation orientée objet d'un systeme de clavardage distribué interactif multi-utilisateur temps réel
|
||||||
|
|
||||||
|
Contenu (branche master):
|
||||||
|
|
||||||
|
Dossier rapports : rapports de conception et de projet
|
||||||
|
|
||||||
|
Dossier application : archives .jar permettant d'exécuter l'application (version classique)
|
||||||
|
|
||||||
|
Dossier serveur_presence : archives .jar et .war permettant d'exécuter respectivement l'application (version compatible avec le serveur) et le serveur de présence
|
||||||
|
|
||||||
|
|
||||||
|
Pour récupérer les codes sources :
|
||||||
|
|
||||||
|
- De l'application classique : branche application
|
||||||
|
|
||||||
|
- Du serveur et de l'application modifiée : branche serveur_presence (l'application est dans le projet POO, le servlet dans le projet POO_Server)
|
||||||
|
|
||||||
|
Toutes les autres branches concernent des versions obsolètes de l'application, merci de ne pas en tenir compte.
|
BIN
application/chat.jar
Normal file
BIN
application/chat_lib/sqlite-jdbc-3.32.3.2.jar
Normal file
BIN
application/create_users.jar
Normal file
Before Width: | Height: | Size: 57 KiB |
Before Width: | Height: | Size: 46 KiB |
Before Width: | Height: | Size: 77 KiB |
Before Width: | Height: | Size: 38 KiB |
Before Width: | Height: | Size: 45 KiB |
Before Width: | Height: | Size: 34 KiB |
Before Width: | Height: | Size: 65 KiB |
Before Width: | Height: | Size: 99 KiB |