From b0206bc0dde1e3c54bc392be1466ccf03face801 Mon Sep 17 00:00:00 2001 From: m-gues Date: Fri, 18 Dec 2020 12:25:31 +0100 Subject: [PATCH] squelette servlet --- .../src/communication/CommunicationUDP.java | 194 ++++++++++++++++++ POO_Server/src/communication/UDPClient.java | 41 ++++ POO_Server/src/communication/UDPServer.java | 71 +++++++ POO_Server/src/main/Main.java | 5 - POO_Server/src/main/ServletPresence.java | 33 ++- POO_Server/src/main/Utilisateur.java | 50 +++++ POO_Server/src/messages/Message.java | 4 - 7 files changed, 382 insertions(+), 16 deletions(-) create mode 100644 POO_Server/src/communication/CommunicationUDP.java create mode 100644 POO_Server/src/communication/UDPClient.java create mode 100644 POO_Server/src/communication/UDPServer.java delete mode 100644 POO_Server/src/main/Main.java create mode 100644 POO_Server/src/main/Utilisateur.java diff --git a/POO_Server/src/communication/CommunicationUDP.java b/POO_Server/src/communication/CommunicationUDP.java new file mode 100644 index 0000000..702ddc1 --- /dev/null +++ b/POO_Server/src/communication/CommunicationUDP.java @@ -0,0 +1,194 @@ +package communication; + +import java.io.IOException; +import java.net.InetAddress; +import java.net.UnknownHostException; +import java.util.ArrayList; +import java.util.Arrays; + +import main.Observer; +import main.Utilisateur; +import main.VueStandard; +import messages.*; + + +public class CommunicationUDP extends Thread { + + // public enum Mode {PREMIERE_CONNEXION, CHANGEMENT_PSEUDO, DECONNEXION}; + + private UDPClient client; + private int portServer; + private ArrayList portOthers; + private ArrayList users = new ArrayList(); + private Observer observer; + + 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); + } + + public void setObserver (Observer obs) { + this.observer=obs; + } + + public ArrayList getListUsers(){ + return users; + } + + protected boolean containsUserFromID(String id) { + for(Utilisateur u : users) { + if(u.getId().equals(id) ) { + return true; + } + } + return false; + } + + public boolean containsUserFromPseudo(String pseudo) { + for(Utilisateur u : users) { + if(u.getPseudo().equals(pseudo) ) { + return true; + } + } + + return false; + } + + //Marche pas + private int getIndexFromID(String id) { + int index = -1; + for(int i=0; i < users.size() ; i++) { + if(users.get(i).getId().contentEquals(id) ) { + index=i; + } + } + return index; + } + + private int getIndexFromIP(InetAddress ip) { + for(int i=0; i < users.size() ; i++) { + if(users.get(i).getIp().equals(ip)) { + return i; + } + } + return -1; + } + + + protected synchronized void addUser(String idClient, String pseudoClient, InetAddress ipClient) throws IOException { + users.add(new Utilisateur(idClient, pseudoClient, ipClient)); + observer.update(this, users); + } + + protected synchronized void changePseudoUser(String idClient, String pseudoClient, InetAddress ipClient) { + int index = getIndexFromID(idClient); + users.get(index).setPseudo(pseudoClient); + observer.update(this, users); + } + + + protected synchronized void removeUser(String idClient, String pseudoClient,InetAddress ipClient) { + int index = getIndexFromIP(ipClient); + //System.out.println("index : "+index); + if( index != -1) { + users.remove(index); + } + observer.update(this, users); + } + + public void removeAll(){ + int oSize = users.size(); + for(int i=0; i getArrayListFromArray(int ports[]) { + ArrayList tmp = new ArrayList(); + 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) { + try { + this.client.sendMessageUDP_local(new MessageSysteme(Message.TypeMessage.JE_SUIS_CONNECTE), port, InetAddress.getLocalHost()); + } catch (MauvaisTypeMessageException e) {/*Si ça marche pas essayer là*/} + } + } + + + // Send the message "add,id,pseudo" to localhost on all the ports in + // "portOthers" + // This allows the receivers' agent (portOthers) to create or modify an entry with the + // data of this agent + //Typically used to notify of a name change + public void sendMessageInfoPseudo() throws UnknownHostException, IOException { + + Utilisateur self = Utilisateur.getSelf(); + + 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(); + } + + + } + + //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 { + 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();} + } + + + // 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 { + for(int port : this.portOthers) { + try { + this.client.sendMessageUDP_local(new MessageSysteme(Message.TypeMessage.JE_SUIS_DECONNECTE), port, InetAddress.getLocalHost()); + } catch (MauvaisTypeMessageException e) {} + } + } + + //Pas encore adapte message +// 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_Server/src/communication/UDPClient.java b/POO_Server/src/communication/UDPClient.java new file mode 100644 index 0000000..4aff7c0 --- /dev/null +++ b/POO_Server/src/communication/UDPClient.java @@ -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); +// } + +} diff --git a/POO_Server/src/communication/UDPServer.java b/POO_Server/src/communication/UDPServer.java new file mode 100644 index 0000000..f6ee4a5 --- /dev/null +++ b/POO_Server/src/communication/UDPServer.java @@ -0,0 +1,71 @@ +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; + +import messages.*; + + + +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 msgString = new String(inPacket.getData(), 0, inPacket.getLength()); + Message msg = Message.stringToMessage(msgString); + + switch(msg.getTypeMessage()) { + case JE_SUIS_CONNECTE : + //System.out.println("first co"); + int portClient = inPacket.getPort(); + int portServer = portClient+1; + + this.commUDP.sendMessageInfoPseudo(portServer); + break; + + case INFO_PSEUDO : + + if (commUDP.containsUserFromID(((MessageSysteme) msg).getId())) { + commUDP.changePseudoUser(((MessageSysteme) msg).getId(), ((MessageSysteme) msg).getPseudo(), inPacket.getAddress()); + } + else { + + commUDP.addUser(((MessageSysteme) msg).getId(), ((MessageSysteme) msg).getPseudo(), inPacket.getAddress()); + //System.out.println(((MessageSysteme) msg).getId()+", "+((MessageSysteme) msg).getPseudo()); + } + break; + + case JE_SUIS_DECONNECTE : + commUDP.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 + } + + } catch (IOException e) { + System.out.println("receive exception"); + } + + } + } +} diff --git a/POO_Server/src/main/Main.java b/POO_Server/src/main/Main.java deleted file mode 100644 index 06cc0f6..0000000 --- a/POO_Server/src/main/Main.java +++ /dev/null @@ -1,5 +0,0 @@ -package main; - -public class Main { - -} diff --git a/POO_Server/src/main/ServletPresence.java b/POO_Server/src/main/ServletPresence.java index ce78ade..7a75bb2 100644 --- a/POO_Server/src/main/ServletPresence.java +++ b/POO_Server/src/main/ServletPresence.java @@ -1,7 +1,8 @@ package main; -//il faut lier le serveur tomcat, voir avec K ou le prof import java.io.IOException; +import java.util.ArrayList; + import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; @@ -15,19 +16,37 @@ import javax.servlet.http.HttpServletResponse; public class ServletPresence extends HttpServlet { private static final long serialVersionUID = 1L; - /** - * Default constructor. - */ + //suscribe(), publish(), notify() + + private ArrayList localUsers; + private ArrayList remoteUsers; + public ServletPresence() { - // TODO Auto-generated constructor stub + localUsers = new ArrayList(); + remoteUsers = new ArrayList(); } + //Permet a un utilisateur externe de s'ajouter/s'enlever à la liste des utilisateurs externes : au tout début de l'application + private void suscribe() { + } + + private void unsubscribe() { + } + + //Permet de dire si on est connecté/déconnecté + private void publish() { + } + + //Informe de la modification de la liste tous les utilisateurs iinternes et externes + private void snotify() { + + } + /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - // TODO Auto-generated method stub - response.getWriter().append("Served at: ").append(request.getContextPath()); + } /** diff --git a/POO_Server/src/main/Utilisateur.java b/POO_Server/src/main/Utilisateur.java new file mode 100644 index 0000000..78b7c79 --- /dev/null +++ b/POO_Server/src/main/Utilisateur.java @@ -0,0 +1,50 @@ +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, InetAddress ip) throws UnknownHostException { + this.id = id; + this.pseudo = pseudo; + this.ip = ip; + } + + + 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, InetAddress.getByName(host)); + } + } + + public static Utilisateur getSelf() { + return Utilisateur.self; + } +} diff --git a/POO_Server/src/messages/Message.java b/POO_Server/src/messages/Message.java index 4afae87..dc1cb72 100644 --- a/POO_Server/src/messages/Message.java +++ b/POO_Server/src/messages/Message.java @@ -1,17 +1,13 @@ package messages; import java.io.Serializable; -import java.lang.instrument.Instrumentation; -import java.util.Arrays; -import messages.Message.TypeMessage; 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; private static final long serialVersionUID = 1L; - private static Instrumentation inst; public TypeMessage getTypeMessage() { return this.type;