diff --git a/POO/src/communication/Communication.java b/POO/src/communication/Communication.java index a0412f5..8e95cf5 100644 --- a/POO/src/communication/Communication.java +++ b/POO/src/communication/Communication.java @@ -39,6 +39,8 @@ public class Communication extends Thread{ return -1; } + //TODO + //Combiner add et change protected static synchronized void addUser(List datas) throws UnknownHostException { String idClient = datas.get(0); @@ -56,7 +58,7 @@ public class Communication extends Thread{ String pseudoClient = datas.get(1); int index = Communication.getIndexFromID(idClient); System.out.println(index); - if( index != -1) { + if(index != -1) { Communication.users.get(index).setPseudo(pseudoClient); VueStandard.userList.set(index, pseudoClient); } @@ -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 portsOther; + private ArrayList 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 getArrayListFromArray(int ports[]){ + private ArrayList getArrayListFromArray(int ports[]) { ArrayList tmp = new ArrayList(); - for(int port : ports) { + for (int port : ports) { tmp.add(port); } - tmp.remove( Integer.valueOf(portServer) ); - + tmp.remove(Integer.valueOf(portServer)); + 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"); } - - private void sendIDPseudo(String prefixe) throws UnknownHostException, IOException { + // 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"); + } + + public void sendMessageAdd(ArrayList 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 portServers) throws UnknownHostException, IOException { Utilisateur self = Utilisateur.getSelf(); String idSelf = self.getId(); String pseudoSelf = self.getPseudo(); - String message = prefixe+","+idSelf + "," + pseudoSelf; - - for(int port : this.portsOther) { - this.client.sendMessageUDP(message, port, InetAddress.getLocalHost()); + if (!pseudoSelf.equals("")) { + + String message = prefixe + "," + idSelf + "," + pseudoSelf; + // A modifier pour créer un objet de type Message + // + // + + 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(); // } diff --git a/POO/src/communication/UDPClient.java b/POO/src/communication/UDPClient.java index 6a275f8..85c6c5c 100644 --- a/POO/src/communication/UDPClient.java +++ b/POO/src/communication/UDPClient.java @@ -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); +// } + } diff --git a/POO/src/communication/UDPServer.java b/POO/src/communication/UDPServer.java index 68621fd..f91b693 100644 --- a/POO/src/communication/UDPServer.java +++ b/POO/src/communication/UDPServer.java @@ -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(); - } else if (msg.contains("add,")) { - System.out.println("add"); + + if (msg.equals("first_connection")) { + //System.out.println("first co"); + ArrayList portClient = new ArrayList(); + portClient.add(inPacket.getPort()+1); + this.commUDP.sendMessageAdd(portClient); + + } else if (msg.contains("add,")) { + //System.out.println("add"); ArrayList datas = this.getDatas(inPacket); Communication.addUser(datas); - } else if (msg.contains("modify,")) { + + } else if (msg.contains("modify,")) { ArrayList datas = this.getDatas(inPacket); Communication.changePseudoUser(datas); + } else if (msg.contains("del,")) { ArrayList datas = this.getDatas(inPacket); Communication.removeUser(datas); @@ -54,9 +59,14 @@ public class UDPServer extends Thread { } protected ArrayList getDatas(DatagramPacket inPacket) { + //Message + // + String msg = new String(inPacket.getData(), 0, inPacket.getLength()); String tmp[] = msg.split(","); + + ArrayList datas = new ArrayList(Arrays.asList(tmp)); datas.remove(0); datas.add(inPacket.getAddress().toString()); diff --git a/POO/src/main/ControleurStandard.java b/POO/src/main/ControleurStandard.java index caee7b0..206daca 100644 --- a/POO/src/main/ControleurStandard.java +++ b/POO/src/main/ControleurStandard.java @@ -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(); + } } } diff --git a/POO/src/main/Utilisateur.java b/POO/src/main/Utilisateur.java index 70b550f..bd44e5e 100644 --- a/POO/src/main/Utilisateur.java +++ b/POO/src/main/Utilisateur.java @@ -15,7 +15,7 @@ public class Utilisateur implements Serializable{ private static Utilisateur self; - public Utilisateur(String id, String pseudo,String host) throws UnknownHostException { + public Utilisateur(String id, String pseudo, String host) throws UnknownHostException { this.id = id; this.pseudo = pseudo; this.ip = InetAddress.getLocalHost(); diff --git a/POO/src/main/VueStandard.java b/POO/src/main/VueStandard.java index 69c87ce..589cc7d 100644 --- a/POO/src/main/VueStandard.java +++ b/POO/src/main/VueStandard.java @@ -32,6 +32,8 @@ public class VueStandard extends Vue { private JList activeUsersList; private JTextField pseudoSelf; private JButton modifierPseudo; + private JButton seConnecter; + private JButton seDeconnecter; private ControleurStandard c; public static DefaultListModel userList = new DefaultListModel(); @@ -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(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(); }