package messages v1 terminé + quelques modifs de communication pour utiliser avec des Message

This commit is contained in:
m-gues 2020-12-02 15:16:17 +01:00
parent f0c08d59c0
commit 69392477dd
10 changed files with 517 additions and 12 deletions

View file

@ -0,0 +1,83 @@
package communication;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import main.Utilisateur;
/*import main.VueStandard;*/
public class Communication extends Thread{
protected static ArrayList<Utilisateur> users = new ArrayList<Utilisateur>();
protected static boolean containsUserFromID(String id) {
for(Utilisateur u : Communication.users) {
if(u.getId().equals(id) ) {
return true;
}
}
return false;
}
public static boolean containsUserFromPseudo(String pseudo) {
for(Utilisateur u : Communication.users) {
if(u.getPseudo().equals(pseudo) ) {
return true;
}
}
return false;
}
protected static int getIndexFromID(String id) {
for(int i=0; i < Communication.users.size() ; i++) {
if(Communication.users.get(i).getId().equals(id) ) {
return i;
}
}
return -1;
}
//TODO
//Combiner add et change
protected static synchronized void addUser(List<String> datas) throws UnknownHostException {
String idClient = datas.get(0);
String pseudoClient = datas.get(1);
String clientAddress = datas.get(2);
if (!Communication.containsUserFromID(idClient)) {
Communication.users.add(new Utilisateur(idClient, pseudoClient, clientAddress));
/*VueStandard.userList.addElement(pseudoClient);*/
}
}
protected static synchronized void changePseudoUser(List<String> datas) {
String idClient = datas.get(0);
String pseudoClient = datas.get(1);
int index = Communication.getIndexFromID(idClient);
System.out.println(index);
if(index != -1) {
Communication.users.get(index).setPseudo(pseudoClient);
/*VueStandard.userList.set(index, pseudoClient);*/
}
}
protected static synchronized void removeUser(List<String> datas) {
String idClient = datas.get(0);
int index = Communication.getIndexFromID(idClient);
System.out.println(index);
if( index != -1) {
Communication.users.remove(index);
/*VueStandard.userList.remove(index);*/
}
}
public static void removeAll(){
int oSize = Communication.users.size();
for(int i=0; i<oSize;i++) {
Communication.users.remove(0);
}
}
}

View file

@ -0,0 +1,113 @@
package communication;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
import main.Utilisateur;
public class CommunicationUDP extends Communication {
// public enum Mode {PREMIERE_CONNEXION, CHANGEMENT_PSEUDO, DECONNEXION};
private UDPClient client;
private int portServer;
private ArrayList<Integer> portOthers;
public CommunicationUDP(int portClient, int portServer, int[] portsOther) throws IOException {
this.portServer = portServer;
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) {
tmp.add(port);
}
tmp.remove(Integer.valueOf(portServer));
return tmp;
}
public void sendMessageConnecte() throws UnknownHostException, IOException {
for(int port : this.portOthers) {
this.client.sendMessageUDP_local("first_connection", port, InetAddress.getLocalHost());
}
}
// 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<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 : 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();
// }
}

View file

@ -0,0 +1,41 @@
package communication;
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;
import messages.*;
public class UDPClient {
private DatagramSocket sockUDP;
private InetAddress broadcast;
public UDPClient(int port) throws SocketException, UnknownHostException {
this.sockUDP = new DatagramSocket(port);
InetAddress localHost = InetAddress.getLocalHost();
NetworkInterface networkInterface = NetworkInterface.getByInetAddress(localHost);
this.broadcast = networkInterface.getInterfaceAddresses().get(0).getBroadcast();
}
//Send a message casted as string to the specified port on localhost
protected void sendMessageUDP_local(Message message, int port, InetAddress clientAddress) throws IOException {
String messageString=message.toString();
DatagramPacket outpacket = new DatagramPacket(messageString.getBytes(), messageString.length(), clientAddress, port);
this.sockUDP.send(outpacket);
}
// protected void sendMessageUDP_broadcast(String message, int port) throws IOException{
// String messageString=message.toString();
// DatagramPacket outpacket = new DatagramPacket(messageString.getBytes(), messageString.length(), this.broadcast, port);
// this.sockUDP.send(outpacket);
// }
}

View file

@ -0,0 +1,77 @@
package communication;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Arrays;
public class UDPServer extends Thread {
private DatagramSocket sockUDP;
private CommunicationUDP commUDP;
private byte[] buffer;
public UDPServer(int port, CommunicationUDP commUDP) throws SocketException {
this.commUDP = commUDP;
this.sockUDP = new DatagramSocket(port);
this.buffer = new byte[256];
}
@Override
public void run() {
while (true) {
try {
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");
ArrayList<Integer> portClient = new ArrayList<Integer>();
portClient.add(inPacket.getPort()+1);
this.commUDP.sendMessageAdd(portClient);
} else if (msg.contains("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);
}
} catch (IOException e) {
System.out.println("receive exception");
}
}
}
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());
return datas;
}
}

View file

@ -0,0 +1,52 @@
package main;
import java.io.Serializable;
import java.net.*;
public class Utilisateur implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private String id;
private String pseudo;
private InetAddress ip;
private static Utilisateur self;
public Utilisateur(String id, String pseudo, String host) throws UnknownHostException {
this.id = id;
this.pseudo = pseudo;
this.ip = InetAddress.getLocalHost();
System.out.println(InetAddress.getLocalHost());
}
public String getId() {
return id;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo(String pseudo) {
this.pseudo = pseudo;
}
public InetAddress getIp() {
return ip;
}
public static void setSelf(String id, String pseudo,String host) throws UnknownHostException {
if(Utilisateur.self == null) {
Utilisateur.self = new Utilisateur(id, pseudo, host);
}
}
public static Utilisateur getSelf() {
return Utilisateur.self;
}
}

View file

@ -0,0 +1,8 @@
package messages;
public class MauvaisTypeMessageException extends Exception {
private static final long serialVersionUID = 1L;
}

View file

@ -1,15 +1,69 @@
package messages;
import java.io.Serializable;
import java.lang.instrument.Instrumentation;
import java.util.Arrays;
public class Message implements Serializable {
import messages.Message.TypeMessage;
protected enum TypeMessage {JE_SUIS_ACTIF, JE_SUIS_INACTIF, INFO_PSEUDO, TEXTE, IMAGE, FICHIER}
public abstract class Message implements Serializable {
public enum TypeMessage {JE_SUIS_CONNECTE, JE_SUIS_DECONNECTE, INFO_PSEUDO, TEXTE, IMAGE, FICHIER, MESSAGE_NUL}
protected TypeMessage type;
protected static final long serialVersionUID = 1L;
protected Exception
private static final long serialVersionUID = 1L;
private static Instrumentation inst;
public TypeMessage getTypeMessage() {
return this.type;
}
protected abstract String attributsToString();
public String toString() {
return this.type+"###"+this.attributsToString();
}
public static Message stringToMessage(String messageString) {
try {
String[] parts = messageString.split("###");
switch (parts[0]) {
case "JE_SUIS_CONNECTE" :
return new MessageSysteme(TypeMessage.JE_SUIS_CONNECTE);
case "JE_SUIS_DECONNECTE" :
return new MessageSysteme(TypeMessage.JE_SUIS_DECONNECTE);
case "INFO_PSEUDO" :
return new MessageSysteme(TypeMessage.INFO_PSEUDO, parts[1]);
case "TEXTE" :
return new MessageTexte(TypeMessage.TEXTE, parts[1]);
case "IMAGE" :
return new MessageFichier(TypeMessage.IMAGE, parts[1], parts[2]);
case "FICHIER" :
return new MessageFichier(TypeMessage.FICHIER, parts[1], parts[2]);
}
} catch (MauvaisTypeMessageException e) {}
return null;
}
//tests ici
public static void main(String[] args) throws MauvaisTypeMessageException {
Message m1 = new MessageSysteme(TypeMessage.JE_SUIS_CONNECTE);
Message m2 = new MessageSysteme(TypeMessage.JE_SUIS_DECONNECTE);
Message m3 = new MessageSysteme(TypeMessage.INFO_PSEUDO, "pseudo156434518");
Message m4 = new MessageTexte(TypeMessage.TEXTE, "blablabla");
Message m5 = new MessageFichier(TypeMessage.FICHIER, "truc", ".pdf");
System.out.println(Message.stringToMessage(m1.toString()));
System.out.println(Message.stringToMessage(m2.toString()));
System.out.println(Message.stringToMessage(m3.toString()));
System.out.println(Message.stringToMessage(m4.toString()));
System.out.println(Message.stringToMessage(m5.toString()));
}
}

View file

@ -0,0 +1,33 @@
package messages;
import messages.Message.TypeMessage;
public class MessageFichier extends Message {
private static final long serialVersionUID = 1L;
private String contenu;
private String extension;
public MessageFichier(TypeMessage type, String contenu, String extension) throws MauvaisTypeMessageException{
if ((type==TypeMessage.IMAGE)||(type==TypeMessage.FICHIER)) {
this.type=type;
this.contenu=contenu;
this.extension=extension;
}
else throw new MauvaisTypeMessageException();
}
public String getContenu() {
return this.contenu;
}
public String getExtension() {
return this.extension;
}
@Override
protected String attributsToString() {
return this.contenu+"###"+this.extension;
}
}

View file

@ -1,15 +1,32 @@
package messages;
import messages.Message.TypeMessage;
public class MessageActivite extends Message {
public class MessageSysteme extends Message {
private static final long serialVersionUID = 1L;
private String pseudo;
public MessageSysteme(TypeMessage t){
if (t!=TypeMessage.) {
this.type=t;
public MessageSysteme(TypeMessage type) throws MauvaisTypeMessageException{
if ((type==TypeMessage.JE_SUIS_CONNECTE)||(type==TypeMessage.JE_SUIS_DECONNECTE)||(type==TypeMessage.MESSAGE_NUL)) {
this.type=type;
this.pseudo="";
}
else throw new MauvaisTypeMessageException();
}
public MessageSysteme(TypeMessage type, String pseudo) throws MauvaisTypeMessageException {
if (type==TypeMessage.INFO_PSEUDO) {
this.type=type;
this.pseudo=pseudo;
}
else throw new MauvaisTypeMessageException();
}
public String getPseudo() {
return this.pseudo;
}
@Override
protected String attributsToString() {
return this.pseudo;
}
}

View file

@ -0,0 +1,27 @@
package messages;
import messages.Message.TypeMessage;
public class MessageTexte extends Message {
private static final long serialVersionUID = 1L;
private String contenu;
public MessageTexte(TypeMessage type, String contenu) throws MauvaisTypeMessageException{
if (type==TypeMessage.TEXTE) {
this.type=type;
this.contenu=contenu;
}
else throw new MauvaisTypeMessageException();
}
public String getContenu() {
return this.contenu;
}
@Override
protected String attributsToString() {
return this.contenu;
}
}