changements communication

This commit is contained in:
Cavailles Kevin 2020-12-05 19:06:21 +01:00
parent 54999b3184
commit b5a1bbae96
5 changed files with 38 additions and 16 deletions

View file

@ -51,12 +51,21 @@ public class Communication extends Thread{
/*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);
}
}

View file

@ -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);
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();}
}

View file

@ -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

View file

@ -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();
}

View file

@ -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();