From bed2d47efa872f3aefa35bf0d6d926eb7c1900e2 Mon Sep 17 00:00:00 2001 From: Cavailles Kevin Date: Wed, 9 Dec 2020 10:43:48 +0100 Subject: [PATCH 1/2] initial commit standard+session --- POO/src/communication/Communication.java | 37 ++- POO/src/communication/CommunicationUDP.java | 5 +- POO/src/communication/TCPClient.java | 53 ++++ POO/src/communication/TCPInputThread.java | 66 ++++ POO/src/communication/TCPServer.java | 40 +++ POO/src/communication/UDPClient.java | 2 +- POO/src/communication/UDPServer.java | 6 +- POO/src/main/Main.java | 9 +- POO/src/main/Observer.java | 6 + POO/src/main/Utilisateur.java | 12 +- POO/src/main/Vue.java | 1 + POO/src/main/VueStandard.java | 189 ------------ POO/src/messages/Message.java | 4 +- POO/src/messages/MessageSysteme.java | 11 +- POO/src/session/ControleurSession.java | 106 +++++++ POO/src/session/VueSession.java | 109 +++++++ .../ControleurStandard.java | 127 +++++++- POO/src/standard/VueStandard.java | 291 ++++++++++++++++++ 18 files changed, 856 insertions(+), 218 deletions(-) create mode 100644 POO/src/communication/TCPClient.java create mode 100644 POO/src/communication/TCPInputThread.java create mode 100644 POO/src/communication/TCPServer.java create mode 100644 POO/src/main/Observer.java delete mode 100644 POO/src/main/VueStandard.java create mode 100644 POO/src/session/ControleurSession.java create mode 100644 POO/src/session/VueSession.java rename POO/src/{main => standard}/ControleurStandard.java (52%) create mode 100644 POO/src/standard/VueStandard.java diff --git a/POO/src/communication/Communication.java b/POO/src/communication/Communication.java index 55fb9a4..9ae8748 100644 --- a/POO/src/communication/Communication.java +++ b/POO/src/communication/Communication.java @@ -6,7 +6,7 @@ import java.util.ArrayList; import java.util.List; import main.Utilisateur; -import main.VueStandard; +import standard.VueStandard; public class Communication extends Thread{ protected static ArrayList users = new ArrayList(); @@ -31,6 +31,22 @@ public class Communication extends Thread{ return false; } + public static InetAddress getIPFromPseudo(String pseudo) { + int index = Communication.getIndexFromPseudo(pseudo); + if (index != -1) { + return Communication.users.get(index).getIp(); + } + return null; + } + + public static int getPortFromPseudo(String pseudo) { + int index = Communication.getIndexFromPseudo(pseudo); + if (index != -1) { + return Communication.users.get(index).getPort(); + } + return -1; + } + protected static int getIndexFromID(String id) { for(int i=0; i < Communication.users.size() ; i++) { if(Communication.users.get(i).getId().equals(id) ) { @@ -49,20 +65,30 @@ public class Communication extends Thread{ return -1; } + protected static int getIndexFromPseudo(String pseudo) { + for(int i=0; i < Communication.users.size() ; i++) { + if(Communication.users.get(i).getPseudo().equals(pseudo)) { + return i; + } + } + return -1; + } - protected static synchronized void addUser(String idClient, String pseudoClient, InetAddress ipClient) throws UnknownHostException { - Communication.users.add(new Utilisateur(idClient, pseudoClient, ipClient)); + + protected static synchronized void addUser(String idClient, String pseudoClient, InetAddress ipClient, int portTCP) throws UnknownHostException { + System.out.println("port de " +pseudoClient+" : "+ portTCP); + Communication.users.add(new Utilisateur(idClient, pseudoClient, ipClient, portTCP)); VueStandard.userList.addElement(pseudoClient); } - protected static synchronized void changePseudoUser(String idClient, String pseudoClient, InetAddress ipClient) { + protected static synchronized void changePseudoUser(String idClient, String pseudoClient, InetAddress ipClient, int port) { int index = Communication.getIndexFromID(idClient); Communication.users.get(index).setPseudo(pseudoClient); VueStandard.userList.set(index, pseudoClient); } - protected static synchronized void removeUser(String idClient, String pseudoClient,InetAddress ipClient) { + protected static synchronized void removeUser(String idClient, String pseudoClient,InetAddress ipClient, int port) { int index = Communication.getIndexFromIP(ipClient); if( index != -1) { Communication.users.remove(index); @@ -76,4 +102,5 @@ public class Communication extends Thread{ Communication.users.remove(0); } } + } diff --git a/POO/src/communication/CommunicationUDP.java b/POO/src/communication/CommunicationUDP.java index bcb88b6..d487845 100644 --- a/POO/src/communication/CommunicationUDP.java +++ b/POO/src/communication/CommunicationUDP.java @@ -56,10 +56,11 @@ public class CommunicationUDP extends Communication { String pseudoSelf =self.getPseudo(); String idSelf = self.getId(); + int portSelf = self.getPort(); Message msout = null; try { - msout = new MessageSysteme(Message.TypeMessage.INFO_PSEUDO, pseudoSelf, idSelf); + msout = new MessageSysteme(Message.TypeMessage.INFO_PSEUDO, pseudoSelf, idSelf, portSelf); for(int port : this.portOthers) { this.client.sendMessageUDP_local(msout, port, InetAddress.getLocalHost()); } @@ -76,7 +77,7 @@ public class CommunicationUDP extends Communication { Utilisateur self = Utilisateur.getSelf(); try { - Message msout = new MessageSysteme(Message.TypeMessage.INFO_PSEUDO, self.getPseudo(), self.getId()); + Message msout = new MessageSysteme(Message.TypeMessage.INFO_PSEUDO, self.getPseudo(), self.getId(), self.getPort()); this.client.sendMessageUDP_local(msout, portOther, InetAddress.getLocalHost()); } catch (MauvaisTypeMessageException e) {e.printStackTrace();} } diff --git a/POO/src/communication/TCPClient.java b/POO/src/communication/TCPClient.java new file mode 100644 index 0000000..4037260 --- /dev/null +++ b/POO/src/communication/TCPClient.java @@ -0,0 +1,53 @@ +package communication; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.io.OutputStream; +import java.io.PrintWriter; +import java.net.InetAddress; +import java.net.Socket; + +import main.Observer; +//import main.VueSession; +import messages.MessageTexte; +import messages.MauvaisTypeMessageException; +import messages.Message; +import messages.Message.TypeMessage; + +public class TCPClient { + + private Socket sockTCP; + private ObjectOutputStream output; + private TCPInputThread inputThread; + + public TCPClient(Socket sockTCP) throws IOException { + this.sockTCP = sockTCP; + + this.output = new ObjectOutputStream(sockTCP.getOutputStream()) ; + ObjectInputStream input = new ObjectInputStream(sockTCP.getInputStream()); + this.inputThread = new TCPInputThread(input); + } + + public TCPClient(InetAddress addr, int port) throws IOException { + this(new Socket(addr, port)) ; + + } + + public void startInputThread() { + this.inputThread.start(); + } + + public void sendMessage(String contenu) throws IOException, MauvaisTypeMessageException { + System.out.println("dans write"); + MessageTexte message = new MessageTexte(TypeMessage.TEXTE, contenu); + this.output.writeObject(message); + } + + public void setObserverInputThread(Observer o) { + this.inputThread.setObserver(o); + } +} diff --git a/POO/src/communication/TCPInputThread.java b/POO/src/communication/TCPInputThread.java new file mode 100644 index 0000000..b038f47 --- /dev/null +++ b/POO/src/communication/TCPInputThread.java @@ -0,0 +1,66 @@ +package communication; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.util.Arrays; + +import main.Observer; +import messages.Message; + +public class TCPInputThread extends Thread { + + private ObjectInputStream input; + private boolean running; + private char[] buffer; + private Observer obs; + + public TCPInputThread(ObjectInputStream input) { + this.input = input; + this.running = true; + this.buffer = new char[200]; + } + + @Override + public void run() { + + while (this.running) { + try { + + + System.out.println("dans read"); + Object o = this.input.readObject(); + this.obs.update(this, o); + //this.flushBuffer(); + + + + } catch (IOException | ClassNotFoundException e) { + this.interrupt(); + //e.printStackTrace(); + } + + } + } + + @Override + public void interrupt() { + + try { + this.running = false; + this.input.close(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + private void flushBuffer() { + Arrays.fill(this.buffer, '\u0000'); + } + + protected void setObserver(Observer o) { + this.obs = o; + } + +} diff --git a/POO/src/communication/TCPServer.java b/POO/src/communication/TCPServer.java new file mode 100644 index 0000000..71ddf0f --- /dev/null +++ b/POO/src/communication/TCPServer.java @@ -0,0 +1,40 @@ +package communication; + +import java.io.IOException; +import java.net.InetAddress; +import java.net.ServerSocket; +import java.net.Socket; +import java.net.UnknownHostException; + +import main.Observer; + + +public class TCPServer extends Thread { + + private ServerSocket sockListenTCP; + private Observer obs; + + public TCPServer(int port) throws UnknownHostException, IOException { + this.sockListenTCP = new ServerSocket(port, 5, InetAddress.getLocalHost()); + } + + @Override + public void run() { + System.out.println("TCP running"); + Socket sockAccept; + while(true) { + try { + sockAccept = this.sockListenTCP.accept(); + this.obs.update(this, sockAccept); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } + } + + public void addObserver(Observer obs) { + this.obs = obs; + } +} diff --git a/POO/src/communication/UDPClient.java b/POO/src/communication/UDPClient.java index 4aff7c0..e1aed10 100644 --- a/POO/src/communication/UDPClient.java +++ b/POO/src/communication/UDPClient.java @@ -26,7 +26,7 @@ public class UDPClient { //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(); + String messageString= message.toString(); DatagramPacket outpacket = new DatagramPacket(messageString.getBytes(), messageString.length(), clientAddress, port); this.sockUDP.send(outpacket); diff --git a/POO/src/communication/UDPServer.java b/POO/src/communication/UDPServer.java index cfe4cb8..3614cbf 100644 --- a/POO/src/communication/UDPServer.java +++ b/POO/src/communication/UDPServer.java @@ -46,17 +46,17 @@ public class UDPServer extends Thread { case INFO_PSEUDO : 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(),((MessageSysteme) msg).getPort()); } else { - Communication.addUser(((MessageSysteme) msg).getId(), ((MessageSysteme) msg).getPseudo(), inPacket.getAddress()); + Communication.addUser(((MessageSysteme) msg).getId(), ((MessageSysteme) msg).getPseudo(), inPacket.getAddress(), ((MessageSysteme) msg).getPort() ); 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(), inPacket.getPort()); 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/Main.java b/POO/src/main/Main.java index 8a1f5e0..8df7e96 100644 --- a/POO/src/main/Main.java +++ b/POO/src/main/Main.java @@ -2,13 +2,16 @@ package main; import java.io.IOException; +import standard.VueStandard; + public class Main { - private static int portServers[] = {1526,1501,1551,1561}; + private static int portServersUDP[] = {1526,1501,1551,1561}; private static String ids[] = {"Raijila", "titi33", "Semtexx", "Salam"}; private static String pseudo[] = {"Raijila", "Mirasio", "Semtexx", "Xaegon"}; + private static int portServersTCP[] = {1625,1600,1650,1660}; public static void main(String[] args) { @@ -44,8 +47,8 @@ public class Main { private static void createApp(int i) { try { - Utilisateur.setSelf(Main.ids[i], Main.pseudo[i], "localhost"); - new VueStandard("Application", Main.portServers[i]-1, Main.portServers[i], Main.portServers); + Utilisateur.setSelf(Main.ids[i], Main.pseudo[i], "localhost", Main.portServersTCP[i]); + new VueStandard("Application", Main.portServersUDP[i]-1, Main.portServersUDP[i], Main.portServersUDP, Main.portServersTCP[i]); } catch (IOException e) { System.out.println(e.toString()); } diff --git a/POO/src/main/Observer.java b/POO/src/main/Observer.java new file mode 100644 index 0000000..c95af4d --- /dev/null +++ b/POO/src/main/Observer.java @@ -0,0 +1,6 @@ +package main; + +public interface Observer { + + public void update(Object o, Object arg); +} diff --git a/POO/src/main/Utilisateur.java b/POO/src/main/Utilisateur.java index caaf122..1afd32f 100644 --- a/POO/src/main/Utilisateur.java +++ b/POO/src/main/Utilisateur.java @@ -12,13 +12,15 @@ public class Utilisateur implements Serializable{ private String id; private String pseudo; private InetAddress ip; + private int port; private static Utilisateur self; - public Utilisateur(String id, String pseudo, InetAddress ip) throws UnknownHostException { + public Utilisateur(String id, String pseudo, InetAddress ip, int port) throws UnknownHostException { this.id = id; this.pseudo = pseudo; this.ip = ip; + this.port = port; System.out.println(InetAddress.getLocalHost()); } @@ -39,9 +41,13 @@ public class Utilisateur implements Serializable{ return ip; } - public static void setSelf(String id, String pseudo,String host) throws UnknownHostException { + public int getPort() { + return port; + } + + public static void setSelf(String id, String pseudo, String host, int port) throws UnknownHostException { if(Utilisateur.self == null) { - Utilisateur.self = new Utilisateur(id, pseudo, InetAddress.getByName(host)); + Utilisateur.self = new Utilisateur(id, pseudo, InetAddress.getByName(host), port); } } diff --git a/POO/src/main/Vue.java b/POO/src/main/Vue.java index 9e7832d..5f85d5b 100644 --- a/POO/src/main/Vue.java +++ b/POO/src/main/Vue.java @@ -6,6 +6,7 @@ public class Vue extends JFrame{ public Vue(String title) { super(title); + this.setDefaultCloseOperation(EXIT_ON_CLOSE); } public void reduireAgent() {} diff --git a/POO/src/main/VueStandard.java b/POO/src/main/VueStandard.java deleted file mode 100644 index 7e6b06c..0000000 --- a/POO/src/main/VueStandard.java +++ /dev/null @@ -1,189 +0,0 @@ -package main; - -import java.awt.*; -import java.awt.event.WindowAdapter; -import java.awt.event.WindowEvent; -import java.io.IOException; -import java.net.SocketException; -import java.net.UnknownHostException; -import java.util.ArrayList; -import java.util.List; -import java.util.Vector; - -import javax.swing.BorderFactory; -import javax.swing.DefaultListModel; -import javax.swing.JButton; -import javax.swing.JLabel; -import javax.swing.JList; -import javax.swing.JPanel; -import javax.swing.JScrollPane; -import javax.swing.JTabbedPane; -import javax.swing.JTextField; -import javax.swing.ListSelectionModel; -import javax.swing.ScrollPaneConstants; - -public class VueStandard extends Vue { - - /** - * - */ - private static final long serialVersionUID = 1L; - - private JList activeUsersList; - private JTextField pseudoSelf; - private JButton modifierPseudo; - private JButton seConnecter; - private JButton seDeconnecter; - private ControleurStandard c; - public static DefaultListModel userList = new DefaultListModel(); - - - public VueStandard(String title, int port, int clientPort, int[] portsOther) throws IOException { - super(title); - - JPanel main = new JPanel(new GridBagLayout()); - main.setBackground(Color.green); - - JPanel left = new JPanel(new BorderLayout()); - left.setBackground(Color.red); - left.setPreferredSize(new Dimension(200, 200)); - - JPanel chat = new JPanel(); - chat.setBackground(Color.blue); - chat.setPreferredSize(new Dimension(575, 600)); - - JPanel bottom = new JPanel(new GridLayout(1, 2)); - bottom.setBackground(Color.yellow); - bottom.setPreferredSize(new Dimension(575, 150)); - - - - this.c = new ControleurStandard(this, port, clientPort, portsOther); - - //--------Panel haut pseudo--------// - JPanel self = new JPanel(new FlowLayout()); - - this.pseudoSelf = new JTextField(Utilisateur.getSelf().getPseudo()); - this.pseudoSelf.setPreferredSize(new Dimension(100, 20)); - this.pseudoSelf.setEditable(false); - - this.modifierPseudo = new JButton("Modifier"); - this.modifierPseudo.addActionListener(this.c); - - self.add(new JLabel("Moi : ")); - self.add(this.pseudoSelf); - self.add(this.modifierPseudo); - - //--------Panel milieu liste utilisateurs--------// - this.activeUsersList = new JList(VueStandard.userList); - this.activeUsersList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); - this.activeUsersList.setLayoutOrientation(JList.VERTICAL); - this.activeUsersList.addListSelectionListener(this.c); - - JScrollPane listScroller = new JScrollPane(this.activeUsersList); - listScroller.setPreferredSize(new Dimension(50,50)); - listScroller.setAlignmentX(LEFT_ALIGNMENT); - listScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); - listScroller.setBorder(BorderFactory.createCompoundBorder( - BorderFactory.createTitledBorder("Utilisateurs Actifs"), - BorderFactory.createEmptyBorder(5,2,2,2))); - - - //--------Panel bas deconnexion--------// - JPanel deconnexion = new JPanel(new GridLayout(1, 2)); - - this.seConnecter = new JButton("Se Connecter"); - this.seConnecter.setEnabled(false); - this.seConnecter.addActionListener(this.c); - - this.seDeconnecter = new JButton("Se Déconnecter"); - this.seDeconnecter.addActionListener(this.c); - - deconnexion.add(this.seConnecter); - deconnexion.add(this.seDeconnecter); - - //--------Ajout à la vue--------// - left.add(self, BorderLayout.PAGE_START); - left.add(listScroller, BorderLayout.CENTER); - left.add(deconnexion, BorderLayout.PAGE_END); - - - - GridBagConstraints gridBagConstraint = new GridBagConstraints(); - - gridBagConstraint.fill = GridBagConstraints.BOTH; - gridBagConstraint.gridx = 0; - gridBagConstraint.gridy = 0; - gridBagConstraint.gridwidth = 1; - gridBagConstraint.gridheight = 4; - gridBagConstraint.weightx = 0.33; - gridBagConstraint.weighty = 1; - - main.add(left,gridBagConstraint); - - gridBagConstraint.fill = GridBagConstraints.BOTH; - gridBagConstraint.gridx = 1; - gridBagConstraint.gridy = 0; - gridBagConstraint.gridwidth = 2; - gridBagConstraint.gridheight = 3; - gridBagConstraint.weightx = 0.66; - gridBagConstraint.weighty = 0.66; - - main.add(chat,gridBagConstraint); - - gridBagConstraint.fill = GridBagConstraints.BOTH; - gridBagConstraint.gridx = 1; - gridBagConstraint.gridy = 3; - gridBagConstraint.gridwidth = 2; - gridBagConstraint.gridheight = 1; - gridBagConstraint.weightx = 0.66; - gridBagConstraint.weighty = 0.33; - - - main.add(bottom,gridBagConstraint); - - this.add(main); - - this.setSize(900,900); - this.setVisible(true); - - this.addWindowListener(c); - } - - public JList getActiveUsersList(){ - return this.activeUsersList; - } - - - protected JButton getButtonModifierPseudo() { - return this.modifierPseudo; - } - - protected JButton getButtonDeconnexion() { - return this.seDeconnecter; - } - - protected JButton getButtonConnexion() { - return this.seConnecter; - } - - protected String getDisplayedPseudo() { - return this.pseudoSelf.getText(); - } - - protected void setDisplayedPseudo(String pseudo) { - this.pseudoSelf.setText(pseudo); - } - - protected void toggleEditPseudo() { - this.pseudoSelf.setEditable(!this.pseudoSelf.isEditable()); - } - - protected void toggleEnableButtonDeconnexion() { - this.seDeconnecter.setEnabled(!this.seDeconnecter.isEnabled()); - } - - protected void toggleEnableButtonConnexion() { - this.seConnecter.setEnabled(!this.seConnecter.isEnabled()); - } -} diff --git a/POO/src/messages/Message.java b/POO/src/messages/Message.java index 4afae87..6036b00 100644 --- a/POO/src/messages/Message.java +++ b/POO/src/messages/Message.java @@ -34,7 +34,7 @@ public abstract class Message implements Serializable { return new MessageSysteme(TypeMessage.JE_SUIS_DECONNECTE); case "INFO_PSEUDO" : - return new MessageSysteme(TypeMessage.INFO_PSEUDO, parts[1], parts[2]); + return new MessageSysteme(TypeMessage.INFO_PSEUDO, parts[1], parts[2], Integer.parseInt(parts[3]) ); case "TEXTE" : return new MessageTexte(TypeMessage.TEXTE, parts[1]); @@ -53,7 +53,7 @@ public abstract class Message implements Serializable { 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", "id236"); + Message m3 = new MessageSysteme(TypeMessage.INFO_PSEUDO, "pseudo156434518", "id236", 1500); Message m4 = new MessageTexte(TypeMessage.TEXTE, "blablabla"); Message m5 = new MessageFichier(TypeMessage.FICHIER, "truc", ".pdf"); diff --git a/POO/src/messages/MessageSysteme.java b/POO/src/messages/MessageSysteme.java index dcda974..0ff111d 100644 --- a/POO/src/messages/MessageSysteme.java +++ b/POO/src/messages/MessageSysteme.java @@ -5,21 +5,24 @@ public class MessageSysteme extends Message { private static final long serialVersionUID = 1L; private String pseudo; private String id; + private int port; 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=""; this.id=""; + this.port = -1; } else throw new MauvaisTypeMessageException(); } - public MessageSysteme(TypeMessage type, String pseudo, String id) throws MauvaisTypeMessageException { + public MessageSysteme(TypeMessage type, String pseudo, String id, int port) throws MauvaisTypeMessageException { if (type==TypeMessage.INFO_PSEUDO) { this.type=type; this.pseudo=pseudo; this.id=id; + this.port = port; } else throw new MauvaisTypeMessageException(); } @@ -32,8 +35,12 @@ public class MessageSysteme extends Message { return this.id; } + public int getPort() { + return this.port; + } + @Override protected String attributsToString() { - return this.pseudo+"###"+this.id; + return this.pseudo+"###"+this.id+"###"+this.port; } } diff --git a/POO/src/session/ControleurSession.java b/POO/src/session/ControleurSession.java new file mode 100644 index 0000000..b73b4a0 --- /dev/null +++ b/POO/src/session/ControleurSession.java @@ -0,0 +1,106 @@ +package session; + +import java.awt.event.ActionEvent; + +import java.awt.event.ActionListener; +import java.awt.event.KeyEvent; +import java.awt.event.KeyListener; +import java.io.IOException; +import java.net.Socket; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + +import javax.swing.JButton; +import javax.swing.JList; + +import java.util.concurrent.*; + +import communication.TCPClient; +import main.Observer; +import main.Utilisateur; +import messages.MauvaisTypeMessageException; +import messages.Message; +import messages.MessageTexte; + +public class ControleurSession implements ActionListener, Observer, KeyListener { + + private VueSession vue; + private TCPClient tcpClient; + + protected ControleurSession(VueSession vue, Socket socketComm) throws IOException { + this.vue = vue; + this.tcpClient = new TCPClient(socketComm); + this.tcpClient.setObserverInputThread(this); + this.tcpClient.startInputThread(); + } + + // ---------- ACTION LISTENER OPERATIONS ----------// + @Override + public void actionPerformed(ActionEvent e) { + + //Quand le bouton envoyer est presse + if ((JButton) e.getSource() == this.vue.getButtonEnvoyer()) { + String messageOut = this.vue.getZoneSaisie().getText(); + System.out.println(messageOut); + + //Si le texte field n'est pas vide + if (!messageOut.equals("")) { + + //On recupere la date et on prepare les messages a afficher/envoyer + String date = this.getDateAndTime(); + String messageToDisplay = date+" Moi : "+ messageOut; + messageOut = date +" "+ Utilisateur.getSelf().getPseudo() + " : " + messageOut+"\n"; + + try { + this.tcpClient.sendMessage(messageOut); + } catch (MauvaisTypeMessageException | IOException e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } + + this.vue.appendMessage(messageToDisplay + "\n"); + this.vue.resetZoneSaisie(); + } + } + + } + + //Methode appelee quand l'inputStream de la socket de communication recoit des donnees + @Override + public void update(Object o, Object arg) { + MessageTexte messageIn = (MessageTexte) arg; + System.out.println(messageIn.getContenu()); + this.vue.appendMessage(messageIn.getContenu()); + } + + + + private String getDateAndTime() { + DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"); + LocalDateTime now = LocalDateTime.now(); + return "<"+dtf.format(now)+">"; + } + + @Override + public void keyTyped(KeyEvent e) { + // TODO Auto-generated method stub + + } + + @Override + public void keyPressed(KeyEvent e) { + if(e.getKeyCode() == KeyEvent.VK_ENTER) { + if(!e.isShiftDown()) { + this.vue.getButtonEnvoyer().doClick(); + } + + } + } + + @Override + public void keyReleased(KeyEvent e) { + // TODO Auto-generated method stub + + } + +} diff --git a/POO/src/session/VueSession.java b/POO/src/session/VueSession.java new file mode 100644 index 0000000..8471522 --- /dev/null +++ b/POO/src/session/VueSession.java @@ -0,0 +1,109 @@ +package session; + +import java.awt.*; +import java.awt.event.KeyEvent; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; +import java.io.IOException; +import java.net.Socket; +import java.net.SocketException; +import java.net.UnknownHostException; +import java.util.ArrayList; +import java.util.List; +import java.util.Vector; + +import javax.swing.BorderFactory; +import javax.swing.DefaultListModel; +import javax.swing.JButton; +import javax.swing.JLabel; +import javax.swing.JList; +import javax.swing.JPanel; +import javax.swing.JScrollPane; +import javax.swing.JTabbedPane; +import javax.swing.JTextArea; +import javax.swing.JTextField; +import javax.swing.JTextPane; +import javax.swing.KeyStroke; +import javax.swing.ListSelectionModel; +import javax.swing.ScrollPaneConstants; +import javax.swing.border.EmptyBorder; + +import communication.TCPClient; + +public class VueSession extends JPanel{ + + /** + * + */ + private static final long serialVersionUID = 1L; + + private JButton envoyerMessage; + private JTextArea chatWindow; + private JTextArea chatInput; + private ControleurSession c; + + public VueSession(Socket socketComm) throws IOException { + + this.c = new ControleurSession(this, socketComm); + + this.setBorder(new EmptyBorder(5, 5, 5, 5)); + this.setLayout(new BorderLayout(0, 0)); + + JPanel bottom = new JPanel(); + bottom.setLayout(new BorderLayout(0, 0)); + + this.chatInput = new JTextArea(); + this.chatInput.setColumns(10); + this.chatInput.setLineWrap(true); + this.chatInput.setWrapStyleWord(true); + this.chatInput.addKeyListener(this.c); + + //remap enter to none to avoid \n after sending message + KeyStroke enter = KeyStroke.getKeyStroke("ENTER"); + this.chatInput.getInputMap().put(enter, "none"); + KeyStroke shiftEnter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, KeyEvent.SHIFT_DOWN_MASK); + this.chatInput.getInputMap().put(shiftEnter, "insert-break"); + + JScrollPane inputScroll = new JScrollPane(this.chatInput); + inputScroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); + + //inputScroll.add(this.chatInput, BorderLayout.CENTER); + + this.envoyerMessage = new JButton("Envoyer"); + this.envoyerMessage.addActionListener(this.c); + + bottom.add(inputScroll); + bottom.add(this.envoyerMessage, BorderLayout.EAST); + + this.chatWindow = new JTextArea(); + this.chatWindow.setEditable(false); + this.chatWindow.setLineWrap(true); + this.chatWindow.setWrapStyleWord(true); + + JScrollPane chatScroll = new JScrollPane(this.chatWindow); + + this.add(chatScroll, BorderLayout.CENTER); + this.add(bottom, BorderLayout.SOUTH); + + this.setPreferredSize(new Dimension(500, 500)); + // this.setVisible(true); + + } + + protected JButton getButtonEnvoyer() { + return this.envoyerMessage; + } + + protected JTextArea getZoneSaisie() { + return this.chatInput; + } + + protected void resetZoneSaisie() { + this.chatInput.setText(""); + } + + protected void appendMessage(String message) { + this.chatWindow.append(message); + } + +} diff --git a/POO/src/main/ControleurStandard.java b/POO/src/standard/ControleurStandard.java similarity index 52% rename from POO/src/main/ControleurStandard.java rename to POO/src/standard/ControleurStandard.java index 54e8587..3c825c2 100644 --- a/POO/src/main/ControleurStandard.java +++ b/POO/src/standard/ControleurStandard.java @@ -1,10 +1,17 @@ -package main; +package standard; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; +import java.io.BufferedReader; import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.io.PrintWriter; +import java.net.InetAddress; +import java.net.Socket; + import javax.swing.JButton; import javax.swing.JList; import javax.swing.event.ListSelectionEvent; @@ -12,8 +19,12 @@ import javax.swing.event.ListSelectionListener; import communication.Communication; import communication.CommunicationUDP; +import communication.TCPServer; +import main.Observer; +import main.Utilisateur; +import session.VueSession; -public class ControleurStandard implements ActionListener, ListSelectionListener, WindowListener { +public class ControleurStandard implements ActionListener, ListSelectionListener, WindowListener, Observer { private enum EtatModif { TERMINE, EN_COURS @@ -23,22 +34,68 @@ public class ControleurStandard implements ActionListener, ListSelectionListener private VueStandard vue; private CommunicationUDP commUDP; private String lastPseudo; - private int clientPort; + private TCPServer tcpServ; - public ControleurStandard(VueStandard vue, int portClient, int portServer, int[] portsOther) throws IOException { + public ControleurStandard(VueStandard vue, int portClientUDP, int portServerUDP, int[] portsOther, int portServerTCP) throws IOException { this.vue = vue; - this.commUDP = new CommunicationUDP(portClient,portServer, portsOther); + + this.tcpServ = new TCPServer(portServerTCP); + this.tcpServ.addObserver(this); + this.tcpServ.start(); + + this.commUDP = new CommunicationUDP(portClientUDP,portServerUDP, portsOther); this.commUDP.sendMessageConnecte(); this.commUDP.sendMessageInfoPseudo(); + this.etatModif = EtatModif.TERMINE; } //---------- LISTSELECTION LISTENER OPERATIONS ----------// @Override public void valueChanged(ListSelectionEvent e) { - if (!e.getValueIsAdjusting()) { - JList list = vue.getActiveUsersList(); - System.out.println(list.getSelectedValue()); + + //Cas où un élément de la liste est sélectionné + if (this.vue.getActiveUsersList().isFocusOwner() && !e.getValueIsAdjusting()) { + + JList list = this.vue.getActiveUsersList(); + String pseudo = list.getSelectedValue(); + + int choix = this.vue.displayJOptionCreation(pseudo); + System.out.println("choix : "+choix); + if(choix == 0) { + int port = Communication.getPortFromPseudo(pseudo); + try { + + Socket socketComm = new Socket(InetAddress.getLocalHost(), port); + + this.sendMessage(socketComm, Utilisateur.getSelf().getPseudo()); + + String reponse = this.readMessage(socketComm); + + + System.out.println("reponse : " + reponse); + + if(reponse.contains("accepted")) { + + this.vue.displayJOptionResponse("acceptée"); + this.vue.addSession(pseudo, new VueSession(socketComm)); + + }else{ + this.vue.displayJOptionResponse("refusée"); + socketComm.close(); + System.out.println("refused"); + } + + } catch (IOException e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } + + } + + this.vue.getButtonModifierPseudo().requestFocus(); + System.out.println("pseudo de la personne a atteindre : " + pseudo); + } } @@ -46,6 +103,8 @@ public class ControleurStandard implements ActionListener, ListSelectionListener //---------- ACTION LISTENER OPERATIONS ----------// @Override public void actionPerformed(ActionEvent e) { + + //Cas Modifier Pseudo if ((JButton) e.getSource() == this.vue.getButtonModifierPseudo()) { JButton modifierPseudo = (JButton) e.getSource(); @@ -77,6 +136,9 @@ public class ControleurStandard implements ActionListener, ListSelectionListener this.vue.toggleEditPseudo(); } + + + //Cas deconnexion if((JButton) e.getSource() == this.vue.getButtonDeconnexion() ) { try { this.commUDP.sendMessageDelete(); @@ -95,6 +157,7 @@ public class ControleurStandard implements ActionListener, ListSelectionListener } + //Cas connexion if((JButton) e.getSource() == this.vue.getButtonConnexion() ) { try { Utilisateur.getSelf().setPseudo(this.vue.getDisplayedPseudo()); @@ -161,4 +224,52 @@ public class ControleurStandard implements ActionListener, ListSelectionListener } + @Override + public void update(Object o, Object arg) { + if(o == this.tcpServ) { + + Socket sockAccept = (Socket) arg; + + try { + + String pseudo = this.readMessage(sockAccept); + + int reponse = this.vue.displayJOptionDemande(pseudo); + + System.out.println("reponse : " + reponse); + + if(reponse == 0) { + this.sendMessage(sockAccept, "accepted"); + this.vue.addSession(pseudo, new VueSession(sockAccept)); + //new TCPHandlerConnection(sockAccept).start(); + + }else { + this.sendMessage(sockAccept, "refused"); + sockAccept.close(); + } + + + } catch (IOException e) { + e.printStackTrace(); + } + } + + } + + + private void sendMessage(Socket sock, String message) throws IOException { + PrintWriter output= new PrintWriter(sock.getOutputStream(), true); + output.println(message); + } + + private String readMessage(Socket sock) throws IOException { + + BufferedReader input = new BufferedReader(new InputStreamReader( sock.getInputStream() )); + char buffer[] = new char[25]; + input.read(buffer); + + String reponse = new String(buffer).split("\n")[0]; + return reponse; + } + } diff --git a/POO/src/standard/VueStandard.java b/POO/src/standard/VueStandard.java new file mode 100644 index 0000000..6289fba --- /dev/null +++ b/POO/src/standard/VueStandard.java @@ -0,0 +1,291 @@ +package standard; + +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.KeyEvent; +import java.awt.event.KeyListener; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; + +import javax.swing.BorderFactory; +import javax.swing.DefaultListModel; +import javax.swing.JButton; +import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.JList; +import javax.swing.JOptionPane; +import javax.swing.JPanel; +import javax.swing.JScrollPane; +import javax.swing.JTabbedPane; +import javax.swing.JTextField; +import javax.swing.ListSelectionModel; +import javax.swing.ScrollPaneConstants; +import javax.swing.plaf.basic.BasicButtonUI; + +import main.Utilisateur; +import main.Vue; +import session.VueSession; + +public class VueStandard extends Vue { + + /** + * + */ + private static final long serialVersionUID = 1L; + + private JList activeUsersList; + private JTextField pseudoSelf; + private JTabbedPane zoneSessions; + private JButton modifierPseudo; + private JButton seConnecter; + private JButton seDeconnecter; + private ControleurStandard c; + private HashMap sessions; + public static DefaultListModel userList = new DefaultListModel(); + + + public VueStandard(String title, int portClientUDP, int portServerUDP, int[] portsOther, int portServerTCP) throws IOException { + super(title); + + this.sessions = new HashMap(); + this.c = new ControleurStandard(this, portClientUDP, portServerUDP, portsOther, portServerTCP); + + + getContentPane().setLayout(new GridBagLayout()); + + + JPanel left = new JPanel(new BorderLayout()); + left.setBackground(Color.red); + //left.setPreferredSize(new Dimension(200, 200)); + + this.zoneSessions = new JTabbedPane(); + this.zoneSessions.setTabPlacement(JTabbedPane.BOTTOM); + + //JPanel defaultTab = new JPanel(new GridLayout(1,1)); + + //JLabel noSession = new JLabel("Aucune session en cours"); + //noSession.setHorizontalAlignment(JLabel.CENTER); + //defaultTab.add(noSession); + + //this.zoneSessions.addTab("1", defaultTab); + + this.zoneSessions.setBackground(Color.green); + this.zoneSessions.setPreferredSize(new Dimension(600, 600)); + + JPanel bottom = new JPanel(new GridLayout(1, 2)); + bottom.setBackground(Color.yellow); + bottom.setPreferredSize(new Dimension(600, 100)); + + + + //--------Panel haut pseudo--------// + JPanel self = new JPanel(new FlowLayout()); + + this.pseudoSelf = new JTextField(Utilisateur.getSelf().getPseudo()); + this.pseudoSelf.setPreferredSize(new Dimension(100, 20)); + this.pseudoSelf.setEditable(false); + this.pseudoSelf.setFocusable(false); + + this.modifierPseudo = new JButton("Modifier"); + this.modifierPseudo.addActionListener(this.c); + + self.add(new JLabel("Moi : ")); + self.add(this.pseudoSelf); + self.add(this.modifierPseudo); + this.pseudoSelf.addKeyListener(new KeyListener() { + + @Override + public void keyTyped(KeyEvent e) { + } + + @Override + public void keyReleased(KeyEvent e) { + } + + @Override + public void keyPressed(KeyEvent e) { + if(e.getKeyCode() == KeyEvent.VK_ENTER) { + modifierPseudo.doClick(); + } + } + }); + + //--------Panel milieu liste utilisateurs--------// + this.activeUsersList = new JList(VueStandard.userList); + this.activeUsersList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); + this.activeUsersList.setLayoutOrientation(JList.VERTICAL); + this.activeUsersList.addListSelectionListener(this.c); + + System.out.println("listener ajouté"); + + JScrollPane listScroller = new JScrollPane(this.activeUsersList); + listScroller.setPreferredSize(new Dimension(50,50)); + listScroller.setAlignmentX(LEFT_ALIGNMENT); + listScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); + listScroller.setBorder(BorderFactory.createCompoundBorder( + BorderFactory.createTitledBorder("Utilisateurs Actifs"), + BorderFactory.createEmptyBorder(5,2,2,2))); + + + //--------Panel bas deconnexion--------// + JPanel deconnexion = new JPanel(new GridLayout(1, 2)); + + this.seConnecter = new JButton("Se Connecter"); + this.seConnecter.setEnabled(false); + this.seConnecter.addActionListener(this.c); + + this.seDeconnecter = new JButton("Se Déconnecter"); + this.seDeconnecter.addActionListener(this.c); + + deconnexion.add(this.seConnecter); + deconnexion.add(this.seDeconnecter); + + //--------Ajout à la vue--------// + left.add(self, BorderLayout.PAGE_START); + left.add(listScroller, BorderLayout.CENTER); + left.add(deconnexion, BorderLayout.PAGE_END); + + + + GridBagConstraints gridBagConstraint1 = new GridBagConstraints(); + GridBagConstraints gridBagConstraint2 = new GridBagConstraints(); + GridBagConstraints gridBagConstraint3 = new GridBagConstraints(); + + gridBagConstraint1.fill = GridBagConstraints.BOTH; + gridBagConstraint1.gridx = 0; + gridBagConstraint1.gridy = 0; + gridBagConstraint1.gridwidth = 1; + gridBagConstraint1.gridheight = 4; + gridBagConstraint1.weightx = 0.33; + gridBagConstraint1.weighty = 1; + + getContentPane().add(left,gridBagConstraint1); + + gridBagConstraint2.fill = GridBagConstraints.BOTH; + gridBagConstraint2.gridx = 1; + gridBagConstraint2.gridy = 0; + gridBagConstraint2.gridwidth = 2; + gridBagConstraint2.gridheight = 3; + gridBagConstraint2.weightx = 0.66; + gridBagConstraint2.weighty = 0.66; + + getContentPane().add(this.zoneSessions,gridBagConstraint2); + + gridBagConstraint3.fill = GridBagConstraints.BOTH; + gridBagConstraint3.gridx = 1; + gridBagConstraint3.gridy = 3; + gridBagConstraint3.gridwidth = 2; + gridBagConstraint3.gridheight = 1; + gridBagConstraint3.weightx = 0.66; + gridBagConstraint3.weighty = 0.33; + + + getContentPane().add(bottom,gridBagConstraint3); + + + this.pack(); + this.setVisible(true); + + this.addWindowListener(c); + } + + public JList getActiveUsersList(){ + return this.activeUsersList; + } + + + protected JButton getButtonModifierPseudo() { + return this.modifierPseudo; + } + + protected JButton getButtonDeconnexion() { + return this.seDeconnecter; + } + + protected JButton getButtonConnexion() { + return this.seConnecter; + } + + protected String getDisplayedPseudo() { + return this.pseudoSelf.getText(); + } + + protected void setDisplayedPseudo(String pseudo) { + this.pseudoSelf.setText(pseudo); + } + + protected void toggleEditPseudo() { + this.pseudoSelf.setEditable(!this.pseudoSelf.isEditable()); + this.pseudoSelf.setFocusable(!this.pseudoSelf.isFocusable()); + } + + protected void toggleEnableButtonDeconnexion() { + this.seDeconnecter.setEnabled(!this.seDeconnecter.isEnabled()); + } + + protected void toggleEnableButtonConnexion() { + this.seConnecter.setEnabled(!this.seConnecter.isEnabled()); + } + + protected int displayJOptionCreation(String pseudo) { + return JOptionPane.showConfirmDialog(this, + "Voulez vous créer une session avec "+pseudo+" ?", + "Confirmation session", + JOptionPane.YES_NO_OPTION); + } + + protected int displayJOptionDemande(String pseudo) { + return JOptionPane.showConfirmDialog(this, + pseudo+" souhaite creer une session avec vous.", + "Accepter demande", + JOptionPane.YES_NO_OPTION); + } + + protected void displayJOptionResponse(String reponse) { + JOptionPane.showMessageDialog(this, "Demande de session "+reponse); + } + + protected void addSession(String pseudo, VueSession session) { + int nbTab = this.zoneSessions.getTabCount(); +// if(nbTab == 1) { +// this.zoneSessions.removeTabAt(0); +// } + + JPanel tabTitle = new JPanel(); + + JButton closeTab = new JButton("X"); + closeTab.setToolTipText("close this tab"); + //Make the button looks the same for all Laf's + closeTab.setUI(new BasicButtonUI()); + //Make it transparent + closeTab.setContentAreaFilled(false); + closeTab.addActionListener(new ActionListener() { + + @Override + public void actionPerformed(ActionEvent e) { + JButton button = (JButton) e.getSource(); + boolean containsKey = sessions.containsKey(button); + if (containsKey) { + zoneSessions.remove(zoneSessions.indexOfTabComponent(button.getParent() ) ); + sessions.remove(button); + } + } + }); + + + tabTitle.add(new JLabel(pseudo)); + tabTitle.add(closeTab); + + this.zoneSessions.addTab(pseudo, session); + this.zoneSessions.setTabComponentAt(this.zoneSessions.getTabCount()-1, tabTitle); + + this.sessions.put(closeTab, session); + session.requestFocus(); + + } + + +} From 08a801f0778340fb7d078d63096f630bbb181269 Mon Sep 17 00:00:00 2001 From: Cavailles Kevin Date: Mon, 21 Dec 2020 17:01:13 +0100 Subject: [PATCH 2/2] communication static -> private --- POO/src/communication/CommunicationUDP.java | 89 ++++++++++++++++++++- POO/src/communication/UDPServer.java | 8 +- POO/src/main/Message.java | 13 --- POO/src/standard/ControleurStandard.java | 22 +++-- POO/src/standard/VueStandard.java | 16 +++- 5 files changed, 120 insertions(+), 28 deletions(-) delete mode 100644 POO/src/main/Message.java diff --git a/POO/src/communication/CommunicationUDP.java b/POO/src/communication/CommunicationUDP.java index d487845..3ea40a4 100644 --- a/POO/src/communication/CommunicationUDP.java +++ b/POO/src/communication/CommunicationUDP.java @@ -6,17 +6,21 @@ import java.net.UnknownHostException; import java.util.ArrayList; import java.util.Arrays; +import main.Observer; import main.Utilisateur; +import standard.VueStandard; import messages.*; -public class CommunicationUDP extends Communication { +public class CommunicationUDP extends Thread { // public enum Mode {PREMIERE_CONNEXION, CHANGEMENT_PSEUDO, DECONNEXION}; private UDPClient client; private int portServer; private ArrayList portOthers; + private static ArrayList users = new ArrayList(); + private Observer observer; public CommunicationUDP(int portClient, int portServer, int[] portsOther) throws IOException { this.portServer = portServer; @@ -24,7 +28,8 @@ public class CommunicationUDP extends Communication { new UDPServer(portServer, this).start(); this.client = new UDPClient(portClient); } - + + private ArrayList getArrayListFromArray(int ports[]) { ArrayList tmp = new ArrayList(); for (int port : ports) { @@ -34,6 +39,86 @@ public class CommunicationUDP extends Communication { return tmp; } + + public void setObserver (Observer obs) { + this.observer=obs; + } + + protected static boolean containsUserFromID(String id) { + for(Utilisateur u : users) { + if(u.getId().equals(id) ) { + return true; + } + } + return false; + } + + public static boolean containsUserFromPseudo(String pseudo) { + for(Utilisateur u : users) { + if(u.getPseudo().equals(pseudo) ) { + return true; + } + } + + return false; + } + + public static int getPortFromPseudo(String pseudo) { + for(int i=0; i < users.size() ; i++) { + + if(users.get(i).getPseudo().equals(pseudo) ) { + return users.get(i).getPort(); + } + } + return -1; + } + + private static int getIndexFromID(String id) { + for(int i=0; i < users.size() ; i++) { + if(users.get(i).getId().equals(id) ) { + return i; + } + } + return -1; + } + + private static 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, int port) throws IOException { + users.add(new Utilisateur(idClient, pseudoClient, ipClient, port)); + observer.update(this, users); + + } + + protected synchronized void changePseudoUser(String idClient, String pseudoClient, InetAddress ipClient, int port) { + int index = getIndexFromID(idClient); + users.get(index).setPseudo(pseudoClient); + observer.update(this, users); + } + + + protected synchronized void removeUser(String idClient, String pseudoClient,InetAddress ipClient, int port) { + int index = getIndexFromIP(ipClient); + if( index != -1) { + users.remove(index); + } + observer.update(this, users); + } + + public void removeAll(){ + int oSize = users.size(); + for(int i=0; i users = (ArrayList) arg; + ArrayList pseudos = new ArrayList(); + for (Utilisateur u : users) { + pseudos.add(u.getPseudo()); + } + this.vue.setActiveUsersList(pseudos); + } + } diff --git a/POO/src/standard/VueStandard.java b/POO/src/standard/VueStandard.java index 6289fba..54de864 100644 --- a/POO/src/standard/VueStandard.java +++ b/POO/src/standard/VueStandard.java @@ -44,7 +44,7 @@ public class VueStandard extends Vue { private JButton seDeconnecter; private ControleurStandard c; private HashMap sessions; - public static DefaultListModel userList = new DefaultListModel(); + private DefaultListModel userList = new DefaultListModel(); public VueStandard(String title, int portClientUDP, int portServerUDP, int[] portsOther, int portServerTCP) throws IOException { @@ -114,7 +114,7 @@ public class VueStandard extends Vue { }); //--------Panel milieu liste utilisateurs--------// - this.activeUsersList = new JList(VueStandard.userList); + this.activeUsersList = new JList(this.userList); this.activeUsersList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); this.activeUsersList.setLayoutOrientation(JList.VERTICAL); this.activeUsersList.addListSelectionListener(this.c); @@ -192,10 +192,20 @@ public class VueStandard extends Vue { this.addWindowListener(c); } - public JList getActiveUsersList(){ + protected JList getActiveUsersList(){ return this.activeUsersList; } + protected void setActiveUsersList(ArrayList users) { + this.removeAllUsers(); + this.userList.addAll(users); + } + + + protected void removeAllUsers() { + this.userList.removeAllElements(); + } + protected JButton getButtonModifierPseudo() { return this.modifierPseudo;