changements communication
This commit is contained in:
parent
54999b3184
commit
b5a1bbae96
5 changed files with 38 additions and 16 deletions
|
@ -50,13 +50,22 @@ public class Communication extends Thread{
|
||||||
Communication.users.get(index).setPseudo(pseudoClient);
|
Communication.users.get(index).setPseudo(pseudoClient);
|
||||||
/*VueStandard.userList.set(index, 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) {
|
protected static synchronized void removeUser(String idClient, String pseudoClient,InetAddress ipClient) {
|
||||||
int index = Communication.getIndexFromID(idClient);
|
int index = Communication.getIndexFromIP(ipClient);
|
||||||
if( index != -1) {
|
if( index != -1) {
|
||||||
Communication.users.remove(index);
|
Communication.users.remove(index);
|
||||||
/*VueStandard.userList.remove(index);*/
|
//VueStandard.userList.remove(index);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -51,24 +51,34 @@ public class CommunicationUDP extends Communication {
|
||||||
// data of this agent
|
// data of this agent
|
||||||
//Typically used to notify of a name change
|
//Typically used to notify of a name change
|
||||||
public void sendMessageInfoPseudo() throws UnknownHostException, IOException {
|
public void sendMessageInfoPseudo() throws UnknownHostException, IOException {
|
||||||
|
|
||||||
Utilisateur self = Utilisateur.getSelf();
|
Utilisateur self = Utilisateur.getSelf();
|
||||||
for(int port : this.portOthers) {
|
|
||||||
try {
|
String pseudoSelf =self.getPseudo();
|
||||||
String pseudoSelf =self.getPseudo();
|
String idSelf = self.getId();
|
||||||
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());
|
this.client.sendMessageUDP_local(msout, port, InetAddress.getLocalHost());
|
||||||
} catch (Exception e) {e.printStackTrace();}
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//Same, but on only one port
|
//Same, but on only one port
|
||||||
//Typically used to give your current name and id to a newly arrived host
|
//Typically used to give your current name and id to a newly arrived host
|
||||||
public void sendMessageInfoPseudo(int portOther) throws UnknownHostException, IOException {
|
public void sendMessageInfoPseudo(int portOther) throws UnknownHostException, IOException {
|
||||||
|
|
||||||
Utilisateur self = Utilisateur.getSelf();
|
Utilisateur self = Utilisateur.getSelf();
|
||||||
try {
|
try {
|
||||||
this.client.sendMessageUDP_local(new MessageSysteme(Message.TypeMessage.INFO_PSEUDO, self.getPseudo(), self.getId()), portOther, InetAddress.getLocalHost());
|
Message msout = new MessageSysteme(Message.TypeMessage.INFO_PSEUDO, self.getPseudo(), self.getId());
|
||||||
} catch (MauvaisTypeMessageException e) {/*Si ça marche pas essayer là*/}
|
this.client.sendMessageUDP_local(msout, portOther, InetAddress.getLocalHost());
|
||||||
|
} catch (MauvaisTypeMessageException e) {e.printStackTrace();}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -28,6 +28,7 @@ public class UDPServer extends Thread {
|
||||||
while (true) {
|
while (true) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
DatagramPacket inPacket = new DatagramPacket(buffer, buffer.length);
|
DatagramPacket inPacket = new DatagramPacket(buffer, buffer.length);
|
||||||
this.sockUDP.receive(inPacket);
|
this.sockUDP.receive(inPacket);
|
||||||
String msgString = new String(inPacket.getData(), 0, inPacket.getLength());
|
String msgString = new String(inPacket.getData(), 0, inPacket.getLength());
|
||||||
|
@ -36,23 +37,25 @@ public class UDPServer extends Thread {
|
||||||
switch(msg.getTypeMessage()) {
|
switch(msg.getTypeMessage()) {
|
||||||
case JE_SUIS_CONNECTE :
|
case JE_SUIS_CONNECTE :
|
||||||
//System.out.println("first co");
|
//System.out.println("first co");
|
||||||
int portServer = inPacket.getPort();
|
int portClient = inPacket.getPort();
|
||||||
int portClient = portServer+1;
|
int portServer = portClient+1;
|
||||||
this.commUDP.sendMessageInfoPseudo(portServer);
|
this.commUDP.sendMessageInfoPseudo(portServer);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case INFO_PSEUDO :
|
case INFO_PSEUDO :
|
||||||
|
|
||||||
if (Communication.containsUserFromID(((MessageSysteme) msg).getId())) {
|
if (Communication.containsUserFromID(((MessageSysteme) msg).getId())) {
|
||||||
Communication.changePseudoUser(((MessageSysteme) msg).getId(), ((MessageSysteme) msg).getPseudo(), inPacket.getAddress());
|
Communication.changePseudoUser(((MessageSysteme) msg).getId(), ((MessageSysteme) msg).getPseudo(), inPacket.getAddress());
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
||||||
Communication.addUser(((MessageSysteme) msg).getId(), ((MessageSysteme) msg).getPseudo(), inPacket.getAddress());
|
Communication.addUser(((MessageSysteme) msg).getId(), ((MessageSysteme) msg).getPseudo(), inPacket.getAddress());
|
||||||
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());
|
Communication.removeUser( ((MessageSysteme) msg).getId() , ((MessageSysteme) msg).getPseudo(), inPacket.getAddress() );
|
||||||
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
|
||||||
|
|
|
@ -23,7 +23,7 @@ public class ControleurConnexion implements ActionListener {
|
||||||
this.id="";
|
this.id="";
|
||||||
//Pour les tests, changer pour un truc plus général quand on change CommunicationUDP
|
//Pour les tests, changer pour un truc plus général quand on change CommunicationUDP
|
||||||
try {
|
try {
|
||||||
this.comUDP = new CommunicationUDP(2209, 2208, new int[] {2905});
|
this.comUDP = new CommunicationUDP(2208, 2209, new int[] {2906});
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,7 +16,7 @@ public class Main2tests {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
Communication comUDP = new CommunicationUDP(2906, 2905, new int[] {});
|
Communication comUDP = new CommunicationUDP(2905, 2906, new int[] {2209});
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
// TODO Auto-generated catch block
|
// TODO Auto-generated catch block
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
|
Loading…
Reference in a new issue