From b5a1bbae962f80da1a400dfb4050d3a0fb2bc4a1 Mon Sep 17 00:00:00 2001 From: Cavailles Kevin Date: Sat, 5 Dec 2020 19:06:21 +0100 Subject: [PATCH] changements communication --- POO/src/communication/Communication.java | 15 +++++++++--- POO/src/communication/CommunicationUDP.java | 26 ++++++++++++++------- POO/src/communication/UDPServer.java | 9 ++++--- POO/src/main/ControleurConnexion.java | 2 +- POO/src/main/Main2tests.java | 2 +- 5 files changed, 38 insertions(+), 16 deletions(-) diff --git a/POO/src/communication/Communication.java b/POO/src/communication/Communication.java index 2d4d82f..786401c 100644 --- a/POO/src/communication/Communication.java +++ b/POO/src/communication/Communication.java @@ -50,13 +50,22 @@ public class Communication extends Thread{ Communication.users.get(index).setPseudo(pseudoClient); /*VueStandard.userList.set(index, pseudoClient);*/ } + + protected static int getIndexFromIP(InetAddress ip) { + for(int i=0; i < Communication.users.size() ; i++) { + if(Communication.users.get(i).getIp().equals(ip)) { + return i; + } + } + return -1; + } - protected static synchronized void removeUser(String idClient, String pseudoClient, InetAddress ipClient) { - int index = Communication.getIndexFromID(idClient); + protected static synchronized void removeUser(String idClient, String pseudoClient,InetAddress ipClient) { + int index = Communication.getIndexFromIP(ipClient); if( index != -1) { Communication.users.remove(index); - /*VueStandard.userList.remove(index);*/ + //VueStandard.userList.remove(index); } } diff --git a/POO/src/communication/CommunicationUDP.java b/POO/src/communication/CommunicationUDP.java index 4736e63..bcb88b6 100644 --- a/POO/src/communication/CommunicationUDP.java +++ b/POO/src/communication/CommunicationUDP.java @@ -51,24 +51,34 @@ public class CommunicationUDP extends Communication { // data of this agent //Typically used to notify of a name change public void sendMessageInfoPseudo() throws UnknownHostException, IOException { + Utilisateur self = Utilisateur.getSelf(); - for(int port : this.portOthers) { - try { - String pseudoSelf =self.getPseudo(); - String idSelf = self.getId(); - Message msout = new MessageSysteme(Message.TypeMessage.INFO_PSEUDO, pseudoSelf, idSelf); + + 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();} + } + } 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 { - this.client.sendMessageUDP_local(new MessageSysteme(Message.TypeMessage.INFO_PSEUDO, self.getPseudo(), self.getId()), portOther, InetAddress.getLocalHost()); - } catch (MauvaisTypeMessageException e) {/*Si ça marche pas essayer là*/} + 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();} } diff --git a/POO/src/communication/UDPServer.java b/POO/src/communication/UDPServer.java index 936051a..5f2c734 100644 --- a/POO/src/communication/UDPServer.java +++ b/POO/src/communication/UDPServer.java @@ -28,6 +28,7 @@ public class UDPServer extends Thread { while (true) { try { + DatagramPacket inPacket = new DatagramPacket(buffer, buffer.length); this.sockUDP.receive(inPacket); String msgString = new String(inPacket.getData(), 0, inPacket.getLength()); @@ -36,23 +37,25 @@ public class UDPServer extends Thread { switch(msg.getTypeMessage()) { case JE_SUIS_CONNECTE : //System.out.println("first co"); - int portServer = inPacket.getPort(); - int portClient = portServer+1; + int portClient = inPacket.getPort(); + int portServer = portClient+1; this.commUDP.sendMessageInfoPseudo(portServer); break; case INFO_PSEUDO : + if (Communication.containsUserFromID(((MessageSysteme) msg).getId())) { Communication.changePseudoUser(((MessageSysteme) msg).getId(), ((MessageSysteme) msg).getPseudo(), inPacket.getAddress()); } else { + Communication.addUser(((MessageSysteme) msg).getId(), ((MessageSysteme) msg).getPseudo(), inPacket.getAddress()); System.out.println(((MessageSysteme) msg).getId()+", "+((MessageSysteme) msg).getPseudo()); } break; case JE_SUIS_DECONNECTE : - Communication.removeUser(((MessageSysteme) msg).getId(), ((MessageSysteme) msg).getPseudo(), inPacket.getAddress()); + Communication.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 diff --git a/POO/src/main/ControleurConnexion.java b/POO/src/main/ControleurConnexion.java index 38deccd..af65f9d 100644 --- a/POO/src/main/ControleurConnexion.java +++ b/POO/src/main/ControleurConnexion.java @@ -23,7 +23,7 @@ public class ControleurConnexion implements ActionListener { this.id=""; //Pour les tests, changer pour un truc plus général quand on change CommunicationUDP try { - this.comUDP = new CommunicationUDP(2209, 2208, new int[] {2905}); + this.comUDP = new CommunicationUDP(2208, 2209, new int[] {2906}); } catch (IOException e) { e.printStackTrace(); } diff --git a/POO/src/main/Main2tests.java b/POO/src/main/Main2tests.java index a771490..ec71046 100644 --- a/POO/src/main/Main2tests.java +++ b/POO/src/main/Main2tests.java @@ -16,7 +16,7 @@ public class Main2tests { e.printStackTrace(); } try { - Communication comUDP = new CommunicationUDP(2906, 2905, new int[] {}); + Communication comUDP = new CommunicationUDP(2905, 2906, new int[] {2209}); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace();