communication static -> private
This commit is contained in:
parent
bed2d47efa
commit
08a801f077
5 changed files with 120 additions and 28 deletions
|
@ -6,17 +6,21 @@ import java.net.UnknownHostException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
import main.Observer;
|
||||||
import main.Utilisateur;
|
import main.Utilisateur;
|
||||||
|
import standard.VueStandard;
|
||||||
import messages.*;
|
import messages.*;
|
||||||
|
|
||||||
|
|
||||||
public class CommunicationUDP extends Communication {
|
public class CommunicationUDP extends Thread {
|
||||||
|
|
||||||
// public enum Mode {PREMIERE_CONNEXION, CHANGEMENT_PSEUDO, DECONNEXION};
|
// public enum Mode {PREMIERE_CONNEXION, CHANGEMENT_PSEUDO, DECONNEXION};
|
||||||
|
|
||||||
private UDPClient client;
|
private UDPClient client;
|
||||||
private int portServer;
|
private int portServer;
|
||||||
private ArrayList<Integer> portOthers;
|
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 {
|
public CommunicationUDP(int portClient, int portServer, int[] portsOther) throws IOException {
|
||||||
this.portServer = portServer;
|
this.portServer = portServer;
|
||||||
|
@ -24,7 +28,8 @@ public class CommunicationUDP extends Communication {
|
||||||
new UDPServer(portServer, this).start();
|
new UDPServer(portServer, this).start();
|
||||||
this.client = new UDPClient(portClient);
|
this.client = new UDPClient(portClient);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private ArrayList<Integer> getArrayListFromArray(int ports[]) {
|
private ArrayList<Integer> getArrayListFromArray(int ports[]) {
|
||||||
ArrayList<Integer> tmp = new ArrayList<Integer>();
|
ArrayList<Integer> tmp = new ArrayList<Integer>();
|
||||||
for (int port : ports) {
|
for (int port : ports) {
|
||||||
|
@ -34,6 +39,86 @@ public class CommunicationUDP extends Communication {
|
||||||
|
|
||||||
return tmp;
|
return tmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int getPortFromPseudo(String pseudo) {
|
||||||
|
for(int i=0; i < users.size() ; i++) {
|
||||||
|
|
||||||
|
if(users.get(i).getPseudo().equals(pseudo) ) {
|
||||||
|
return users.get(i).getPort();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
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, int port) throws IOException {
|
||||||
|
users.add(new Utilisateur(idClient, pseudoClient, ipClient, port));
|
||||||
|
observer.update(this, users);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
protected synchronized void changePseudoUser(String idClient, String pseudoClient, InetAddress ipClient, int port) {
|
||||||
|
int index = getIndexFromID(idClient);
|
||||||
|
users.get(index).setPseudo(pseudoClient);
|
||||||
|
observer.update(this, users);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
protected synchronized void removeUser(String idClient, String pseudoClient,InetAddress ipClient, int port) {
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public void sendMessageConnecte() throws UnknownHostException, IOException {
|
public void sendMessageConnecte() throws UnknownHostException, IOException {
|
||||||
|
|
|
@ -45,18 +45,18 @@ public class UDPServer extends Thread {
|
||||||
|
|
||||||
case INFO_PSEUDO :
|
case INFO_PSEUDO :
|
||||||
|
|
||||||
if (Communication.containsUserFromID(((MessageSysteme) msg).getId())) {
|
if (CommunicationUDP.containsUserFromID(((MessageSysteme) msg).getId())) {
|
||||||
Communication.changePseudoUser(((MessageSysteme) msg).getId(), ((MessageSysteme) msg).getPseudo(), inPacket.getAddress(),((MessageSysteme) msg).getPort());
|
this.commUDP.changePseudoUser(((MessageSysteme) msg).getId(), ((MessageSysteme) msg).getPseudo(), inPacket.getAddress(),((MessageSysteme) msg).getPort());
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
||||||
Communication.addUser(((MessageSysteme) msg).getId(), ((MessageSysteme) msg).getPseudo(), inPacket.getAddress(), ((MessageSysteme) msg).getPort() );
|
this.commUDP.addUser(((MessageSysteme) msg).getId(), ((MessageSysteme) msg).getPseudo(), inPacket.getAddress(), ((MessageSysteme) msg).getPort() );
|
||||||
System.out.println(((MessageSysteme) msg).getId()+", "+((MessageSysteme) msg).getPseudo());
|
System.out.println(((MessageSysteme) msg).getId()+", "+((MessageSysteme) msg).getPseudo());
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case JE_SUIS_DECONNECTE :
|
case JE_SUIS_DECONNECTE :
|
||||||
Communication.removeUser( ((MessageSysteme) msg).getId() , ((MessageSysteme) msg).getPseudo(), inPacket.getAddress(), inPacket.getPort());
|
this.commUDP.removeUser( ((MessageSysteme) msg).getId() , ((MessageSysteme) msg).getPseudo(), inPacket.getAddress(), inPacket.getPort());
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default : //Others types of messages are ignored because they are supposed to be transmitted by TCP and not UDP
|
default : //Others types of messages are ignored because they are supposed to be transmitted by TCP and not UDP
|
||||||
|
|
|
@ -1,13 +0,0 @@
|
||||||
package main;
|
|
||||||
|
|
||||||
import java.io.Serializable;
|
|
||||||
|
|
||||||
public class Message implements Serializable {
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
private static final long serialVersionUID = 1L;
|
|
||||||
private String contenu;
|
|
||||||
|
|
||||||
}
|
|
|
@ -7,17 +7,16 @@ import java.awt.event.WindowListener;
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.io.OutputStreamWriter;
|
|
||||||
import java.io.PrintWriter;
|
import java.io.PrintWriter;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
import javax.swing.JButton;
|
import javax.swing.JButton;
|
||||||
import javax.swing.JList;
|
import javax.swing.JList;
|
||||||
import javax.swing.event.ListSelectionEvent;
|
import javax.swing.event.ListSelectionEvent;
|
||||||
import javax.swing.event.ListSelectionListener;
|
import javax.swing.event.ListSelectionListener;
|
||||||
|
|
||||||
import communication.Communication;
|
|
||||||
import communication.CommunicationUDP;
|
import communication.CommunicationUDP;
|
||||||
import communication.TCPServer;
|
import communication.TCPServer;
|
||||||
import main.Observer;
|
import main.Observer;
|
||||||
|
@ -44,6 +43,7 @@ public class ControleurStandard implements ActionListener, ListSelectionListener
|
||||||
this.tcpServ.start();
|
this.tcpServ.start();
|
||||||
|
|
||||||
this.commUDP = new CommunicationUDP(portClientUDP,portServerUDP, portsOther);
|
this.commUDP = new CommunicationUDP(portClientUDP,portServerUDP, portsOther);
|
||||||
|
this.commUDP.setObserver(this);
|
||||||
this.commUDP.sendMessageConnecte();
|
this.commUDP.sendMessageConnecte();
|
||||||
this.commUDP.sendMessageInfoPseudo();
|
this.commUDP.sendMessageInfoPseudo();
|
||||||
|
|
||||||
|
@ -63,7 +63,8 @@ public class ControleurStandard implements ActionListener, ListSelectionListener
|
||||||
int choix = this.vue.displayJOptionCreation(pseudo);
|
int choix = this.vue.displayJOptionCreation(pseudo);
|
||||||
System.out.println("choix : "+choix);
|
System.out.println("choix : "+choix);
|
||||||
if(choix == 0) {
|
if(choix == 0) {
|
||||||
int port = Communication.getPortFromPseudo(pseudo);
|
int port = CommunicationUDP.getPortFromPseudo(pseudo);
|
||||||
|
System.out.println("port = "+port);
|
||||||
try {
|
try {
|
||||||
|
|
||||||
Socket socketComm = new Socket(InetAddress.getLocalHost(), port);
|
Socket socketComm = new Socket(InetAddress.getLocalHost(), port);
|
||||||
|
@ -114,7 +115,7 @@ public class ControleurStandard implements ActionListener, ListSelectionListener
|
||||||
this.etatModif = EtatModif.EN_COURS;
|
this.etatModif = EtatModif.EN_COURS;
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
if (!Communication.containsUserFromPseudo(this.vue.getDisplayedPseudo())) {
|
if (!CommunicationUDP.containsUserFromPseudo(this.vue.getDisplayedPseudo())) {
|
||||||
|
|
||||||
Utilisateur.getSelf().setPseudo(this.vue.getDisplayedPseudo());
|
Utilisateur.getSelf().setPseudo(this.vue.getDisplayedPseudo());
|
||||||
|
|
||||||
|
@ -142,8 +143,8 @@ public class ControleurStandard implements ActionListener, ListSelectionListener
|
||||||
if((JButton) e.getSource() == this.vue.getButtonDeconnexion() ) {
|
if((JButton) e.getSource() == this.vue.getButtonDeconnexion() ) {
|
||||||
try {
|
try {
|
||||||
this.commUDP.sendMessageDelete();
|
this.commUDP.sendMessageDelete();
|
||||||
Communication.removeAll();
|
this.commUDP.removeAll();
|
||||||
VueStandard.userList.removeAllElements();
|
this.vue.removeAllUsers();
|
||||||
Utilisateur.getSelf().setPseudo("");
|
Utilisateur.getSelf().setPseudo("");
|
||||||
//Ajouter code pour passer à la vue de connexion
|
//Ajouter code pour passer à la vue de connexion
|
||||||
//
|
//
|
||||||
|
@ -254,6 +255,15 @@ public class ControleurStandard implements ActionListener, ListSelectionListener
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(o == this.commUDP) {
|
||||||
|
ArrayList<Utilisateur> users = (ArrayList<Utilisateur>) arg;
|
||||||
|
ArrayList<String> pseudos = new ArrayList<String>();
|
||||||
|
for (Utilisateur u : users) {
|
||||||
|
pseudos.add(u.getPseudo());
|
||||||
|
}
|
||||||
|
this.vue.setActiveUsersList(pseudos);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -44,7 +44,7 @@ public class VueStandard extends Vue {
|
||||||
private JButton seDeconnecter;
|
private JButton seDeconnecter;
|
||||||
private ControleurStandard c;
|
private ControleurStandard c;
|
||||||
private HashMap<JButton,VueSession> sessions;
|
private HashMap<JButton,VueSession> sessions;
|
||||||
public static DefaultListModel<String> userList = new DefaultListModel<String>();
|
private DefaultListModel<String> userList = new DefaultListModel<String>();
|
||||||
|
|
||||||
|
|
||||||
public VueStandard(String title, int portClientUDP, int portServerUDP, int[] portsOther, int portServerTCP) throws IOException {
|
public VueStandard(String title, int portClientUDP, int portServerUDP, int[] portsOther, int portServerTCP) throws IOException {
|
||||||
|
@ -114,7 +114,7 @@ public class VueStandard extends Vue {
|
||||||
});
|
});
|
||||||
|
|
||||||
//--------Panel milieu liste utilisateurs--------//
|
//--------Panel milieu liste utilisateurs--------//
|
||||||
this.activeUsersList = new JList<String>(VueStandard.userList);
|
this.activeUsersList = new JList<String>(this.userList);
|
||||||
this.activeUsersList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
this.activeUsersList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
||||||
this.activeUsersList.setLayoutOrientation(JList.VERTICAL);
|
this.activeUsersList.setLayoutOrientation(JList.VERTICAL);
|
||||||
this.activeUsersList.addListSelectionListener(this.c);
|
this.activeUsersList.addListSelectionListener(this.c);
|
||||||
|
@ -192,10 +192,20 @@ public class VueStandard extends Vue {
|
||||||
this.addWindowListener(c);
|
this.addWindowListener(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
public JList<String> getActiveUsersList(){
|
protected JList<String> getActiveUsersList(){
|
||||||
return this.activeUsersList;
|
return this.activeUsersList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected void setActiveUsersList(ArrayList<String> users) {
|
||||||
|
this.removeAllUsers();
|
||||||
|
this.userList.addAll(users);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
protected void removeAllUsers() {
|
||||||
|
this.userList.removeAllElements();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
protected JButton getButtonModifierPseudo() {
|
protected JButton getButtonModifierPseudo() {
|
||||||
return this.modifierPseudo;
|
return this.modifierPseudo;
|
||||||
|
|
Loading…
Reference in a new issue