modif package comm + boutons connexion/deconnexion
This commit is contained in:
parent
539a0f0438
commit
94e1c6165b
7 changed files with 176 additions and 40 deletions
|
@ -39,6 +39,8 @@ public class Communication extends Thread{
|
|||
return -1;
|
||||
}
|
||||
|
||||
//TODO
|
||||
//Combiner add et change
|
||||
protected static synchronized void addUser(List<String> datas) throws UnknownHostException {
|
||||
|
||||
String idClient = datas.get(0);
|
||||
|
@ -71,4 +73,11 @@ public class Communication extends Thread{
|
|||
VueStandard.userList.remove(index);
|
||||
}
|
||||
}
|
||||
|
||||
public static void removeAll(){
|
||||
int oSize = Communication.users.size();
|
||||
for(int i=0; i<oSize;i++) {
|
||||
Communication.users.remove(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
package communication;
|
||||
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
|
@ -15,16 +14,15 @@ public class CommunicationUDP extends Communication {
|
|||
|
||||
private UDPClient client;
|
||||
private int portServer;
|
||||
private ArrayList<Integer> portsOther;
|
||||
private ArrayList<Integer> portOthers;
|
||||
|
||||
public CommunicationUDP(int portClient, int portServer, int[] portsOther) throws IOException {
|
||||
this.portServer = portServer;
|
||||
this.portsOther = this.getArrayListFromArray(portsOther);
|
||||
this.portOthers = this.getArrayListFromArray(portsOther);
|
||||
new UDPServer(portServer, this).start();
|
||||
this.client = new UDPClient(portClient);
|
||||
}
|
||||
|
||||
|
||||
private ArrayList<Integer> getArrayListFromArray(int ports[]) {
|
||||
ArrayList<Integer> tmp = new ArrayList<Integer>();
|
||||
for (int port : ports) {
|
||||
|
@ -35,30 +33,78 @@ public class CommunicationUDP extends Communication {
|
|||
return tmp;
|
||||
}
|
||||
|
||||
|
||||
public void sendMessageConnecte() throws UnknownHostException, IOException {
|
||||
|
||||
for(int port : this.portsOther) {
|
||||
this.client.sendMessageUDP("first_connection", port, InetAddress.getLocalHost());
|
||||
for(int port : this.portOthers) {
|
||||
this.client.sendMessageUDP_local("first_connection", port, InetAddress.getLocalHost());
|
||||
}
|
||||
}
|
||||
|
||||
public void sendMessageAdd() throws UnknownHostException, IOException { this.sendIDPseudo("add"); }
|
||||
public void sendMessageModify() throws UnknownHostException, IOException{ this.sendIDPseudo("modify"); }
|
||||
public void sendMessageDelete() throws UnknownHostException, IOException{ this.sendIDPseudo("del"); }
|
||||
|
||||
// Send the message "add,id,pseudo" to localhost on all the ports in
|
||||
// "portOthers"
|
||||
// This allows the receivers' agent (portOthers) to create an entry with the
|
||||
// data of this agent
|
||||
public void sendMessageAdd() throws UnknownHostException, IOException {
|
||||
this.sendIDPseudo_local("add");
|
||||
}
|
||||
|
||||
private void sendIDPseudo(String prefixe) throws UnknownHostException, IOException {
|
||||
public void sendMessageAdd(ArrayList<Integer> portServers) throws UnknownHostException, IOException {
|
||||
this.sendIDPseudo_local("add", portServers);
|
||||
}
|
||||
|
||||
// Send the message "modify,id,pseudo" to localhost on all the ports in
|
||||
// "portOthers"
|
||||
// This allows the receivers' agent (portOthers) to update the entry
|
||||
// corresponding to this agent
|
||||
public void sendMessageModify() throws UnknownHostException, IOException {
|
||||
this.sendIDPseudo_local("modify");
|
||||
}
|
||||
|
||||
// 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 {
|
||||
this.sendIDPseudo_local("del");
|
||||
}
|
||||
|
||||
// Private function to create the message "[prefix],id,pseudo"
|
||||
// and send it to localhost on all the ports in "portOthers"
|
||||
private void sendIDPseudo_local(String prefixe, ArrayList<Integer> portServers) throws UnknownHostException, IOException {
|
||||
Utilisateur self = Utilisateur.getSelf();
|
||||
String idSelf = self.getId();
|
||||
String pseudoSelf = self.getPseudo();
|
||||
|
||||
if (!pseudoSelf.equals("")) {
|
||||
|
||||
String message = prefixe + "," + idSelf + "," + pseudoSelf;
|
||||
// A modifier pour créer un objet de type Message
|
||||
//
|
||||
//
|
||||
|
||||
for(int port : this.portsOther) {
|
||||
this.client.sendMessageUDP(message, port, InetAddress.getLocalHost());
|
||||
for (int port : portServers) {
|
||||
this.client.sendMessageUDP_local(message, port, InetAddress.getLocalHost());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void sendIDPseudo_local(String prefixe) throws UnknownHostException, IOException {
|
||||
this.sendIDPseudo_local(prefixe, this.portOthers);
|
||||
}
|
||||
|
||||
// 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();
|
||||
|
|
|
@ -4,21 +4,38 @@ 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;
|
||||
|
||||
public class UDPClient {
|
||||
|
||||
private DatagramSocket sockUDP;
|
||||
private byte[] buffer;
|
||||
private InetAddress broadcast;
|
||||
|
||||
public UDPClient(int port) throws SocketException {
|
||||
public UDPClient(int port) throws SocketException, UnknownHostException {
|
||||
this.sockUDP = new DatagramSocket(port);
|
||||
this.buffer = new byte[256];
|
||||
|
||||
InetAddress localHost = InetAddress.getLocalHost();
|
||||
NetworkInterface networkInterface = NetworkInterface.getByInetAddress(localHost);
|
||||
this.broadcast = networkInterface.getInterfaceAddresses().get(0).getBroadcast();
|
||||
}
|
||||
|
||||
|
||||
protected void sendMessageUDP(String message, int port, InetAddress clientAddress) throws IOException {
|
||||
//Send a string message to the specified port on localhost
|
||||
protected void sendMessageUDP_local(String message, int port, InetAddress clientAddress) throws IOException {
|
||||
|
||||
//A modifier, faire passer un type Message en paramètre
|
||||
//puis écrire les instructions pour envoyer un Message à traver la socket
|
||||
|
||||
DatagramPacket outpacket = new DatagramPacket(message.getBytes(), message.length(), clientAddress, port);
|
||||
this.sockUDP.send(outpacket);
|
||||
|
||||
}
|
||||
|
||||
// protected void sendMessageUDP_broadcast(String message, int port) throws IOException{
|
||||
// DatagramPacket outpacket = new DatagramPacket(message.getBytes(), message.length(), this.broadcast, port);
|
||||
// this.sockUDP.send(outpacket);
|
||||
// }
|
||||
|
||||
}
|
||||
|
|
|
@ -29,17 +29,22 @@ public class UDPServer extends Thread {
|
|||
DatagramPacket inPacket = new DatagramPacket(buffer, buffer.length);
|
||||
this.sockUDP.receive(inPacket);
|
||||
String msg = new String(inPacket.getData(), 0, inPacket.getLength());
|
||||
|
||||
if (msg.equals("first_connection")) {
|
||||
System.out.println("first co");
|
||||
this.commUDP.sendMessageAdd();
|
||||
//new ReceiverUDP(Mode.PREMIERE_CONNEXION, inPacket).start();
|
||||
//System.out.println("first co");
|
||||
ArrayList<Integer> portClient = new ArrayList<Integer>();
|
||||
portClient.add(inPacket.getPort()+1);
|
||||
this.commUDP.sendMessageAdd(portClient);
|
||||
|
||||
} else if (msg.contains("add,")) {
|
||||
System.out.println("add");
|
||||
//System.out.println("add");
|
||||
ArrayList<String> datas = this.getDatas(inPacket);
|
||||
Communication.addUser(datas);
|
||||
|
||||
} else if (msg.contains("modify,")) {
|
||||
ArrayList<String> datas = this.getDatas(inPacket);
|
||||
Communication.changePseudoUser(datas);
|
||||
|
||||
} else if (msg.contains("del,")) {
|
||||
ArrayList<String> datas = this.getDatas(inPacket);
|
||||
Communication.removeUser(datas);
|
||||
|
@ -54,9 +59,14 @@ public class UDPServer extends Thread {
|
|||
}
|
||||
|
||||
protected ArrayList<String> getDatas(DatagramPacket inPacket) {
|
||||
//Message
|
||||
//
|
||||
|
||||
String msg = new String(inPacket.getData(), 0, inPacket.getLength());
|
||||
String tmp[] = msg.split(",");
|
||||
|
||||
|
||||
|
||||
ArrayList<String> datas = new ArrayList<String>(Arrays.asList(tmp));
|
||||
datas.remove(0);
|
||||
datas.add(inPacket.getAddress().toString());
|
||||
|
|
|
@ -75,7 +75,36 @@ public class ControleurStandard implements ActionListener, ListSelectionListener
|
|||
}
|
||||
|
||||
this.vue.toggleEditPseudo();
|
||||
}
|
||||
|
||||
if((JButton) e.getSource() == this.vue.getButtonDeconnexion() ) {
|
||||
try {
|
||||
this.commUDP.sendMessageDelete();
|
||||
Communication.removeAll();
|
||||
VueStandard.userList.removeAllElements();
|
||||
Utilisateur.getSelf().setPseudo("");
|
||||
//Ajouter code pour passer à la vue de connexion
|
||||
//
|
||||
//
|
||||
} catch (IOException e1) {
|
||||
|
||||
e1.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if((JButton) e.getSource() == this.vue.getButtonConnexion() ) {
|
||||
try {
|
||||
Utilisateur.getSelf().setPseudo(this.vue.getDisplayedPseudo());
|
||||
this.commUDP.sendMessageConnecte();
|
||||
this.commUDP.sendMessageAdd();
|
||||
//Ajouter code pour passer à la vue de connexion
|
||||
//
|
||||
//
|
||||
} catch (IOException e1) {
|
||||
|
||||
e1.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -32,6 +32,8 @@ public class VueStandard extends Vue {
|
|||
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>();
|
||||
|
||||
|
@ -43,20 +45,20 @@ public class VueStandard extends Vue {
|
|||
|
||||
this.c = new ControleurStandard(this, port, clientPort, portsOther);
|
||||
|
||||
|
||||
//--------Panel haut pseudo--------//
|
||||
JPanel self = new JPanel(new GridLayout(1, 3));
|
||||
|
||||
this.pseudoSelf = new JTextField(Utilisateur.getSelf().getPseudo());
|
||||
this.pseudoSelf.setEditable(false);
|
||||
|
||||
this.modifierPseudo = new JButton("Modifier");
|
||||
this.modifierPseudo.addActionListener(c);
|
||||
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);
|
||||
|
@ -70,8 +72,23 @@ public class VueStandard extends Vue {
|
|||
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.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--------//
|
||||
main.add(self);
|
||||
main.add(listScroller);
|
||||
main.add(deconnexion);
|
||||
|
||||
this.add(main);
|
||||
|
||||
|
@ -90,6 +107,14 @@ public class VueStandard extends Vue {
|
|||
return this.modifierPseudo;
|
||||
}
|
||||
|
||||
protected JButton getButtonDeconnexion() {
|
||||
return this.seDeconnecter;
|
||||
}
|
||||
|
||||
protected JButton getButtonConnexion() {
|
||||
return this.seConnecter;
|
||||
}
|
||||
|
||||
protected String getDisplayedPseudo() {
|
||||
return this.pseudoSelf.getText();
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue