remaniament squelette servler

This commit is contained in:
m-gues 2020-12-22 23:54:41 +01:00
parent 2545058ee3
commit abf8c14bb2
20 changed files with 866 additions and 438 deletions

View file

@ -1,4 +1,4 @@
package main;
package connexion;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
@ -7,6 +7,9 @@ import java.net.UnknownHostException;
import communication.*;
import main.Observer;
import main.Utilisateur;
import standard.VueStandard;
public class ControleurConnexion implements ActionListener, Observer{
@ -17,6 +20,7 @@ public class ControleurConnexion implements ActionListener, Observer{
private CommunicationUDP comUDP;
private String id;
private String pseudo;
private int portTCP;
public ControleurConnexion(VueConnexion vue, int numtest) {
this.vue = vue;
@ -27,15 +31,19 @@ public class ControleurConnexion implements ActionListener, Observer{
switch(numtest) {
case 0 :
this.comUDP = new CommunicationUDP(2208, 2209, new int[] {2309, 2409});
this.portTCP = 7010;
break;
case 1 :
this.comUDP = new CommunicationUDP(2308, 2309, new int[] {2209, 2409});
this.portTCP = 7020;
break;
case 2 :
this.comUDP = new CommunicationUDP(2408, 2409, new int[] {2209, 2309});
this.portTCP = 7030;
break;
default :
this.comUDP = new CommunicationUDP(2408, 2409, new int[] {2209, 2309});
this.portTCP = 7040;
}
} catch (IOException e) {
e.printStackTrace();
@ -81,15 +89,16 @@ public class ControleurConnexion implements ActionListener, Observer{
else vue.setTexteLabelInput("Identifiant invalide, veuillez réessayer");
}
else {
pseudo=vue.getValeurTextField();
this.pseudo=vue.getValeurTextField();
//Recherche dans la liste locale des utilisateurs connectes, report sur inputOK
inputOK = !comUDP.containsUserFromPseudo(pseudo);
if (inputOK) {
inputOK = !this.comUDP.containsUserFromPseudo(this.pseudo);
if(this.pseudo.equals("")) {
this.vue.setTexteLabelInput("Votre pseudonyme doit contenir au moins 1 caratère");
}else if (inputOK) {
//Reglage de l'utilisateur
try {
Utilisateur.setSelf(id, pseudo, "localhost");
Utilisateur.setSelf(this.id, this.pseudo, "localhost", this.portTCP);
} catch (UnknownHostException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
@ -97,7 +106,7 @@ public class ControleurConnexion implements ActionListener, Observer{
//Broadcast du pseudo
try {
comUDP.sendMessageInfoPseudo();
this.comUDP.sendMessageInfoPseudo();
} catch (UnknownHostException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
@ -106,20 +115,21 @@ public class ControleurConnexion implements ActionListener, Observer{
e1.printStackTrace();
}
try {
vue.close();
new VueStandard("Standard", comUDP);
this.vue.close();
new VueStandard("Standard", comUDP, this.portTCP);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
else vue.setTexteLabelInput("Ce nom est déjà utilisé, veuillez en choisir un autre");
else this.vue.setTexteLabelInput("Ce nom est déjà utilisé, veuillez en choisir un autre");
}
}
@Override
//Rien a faire : pas d'affichage de la liste des utilisateurs connectés durant la phase de connexion
public void update(Object o, Object arg) {
// TODO Auto-generated method stub
}
}

View file

@ -1,10 +1,12 @@
package main;
package connexion;
//Importe les librairies
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import main.Vue;
public class VueConnexion extends Vue {
//Elements vue

View file

@ -1,177 +0,0 @@
package main;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import communication.CommunicationUDP;
public class ControleurStandard implements ActionListener, ListSelectionListener, WindowListener, Observer {
private enum EtatModif {
TERMINE, EN_COURS
}
private EtatModif etatModif;
private VueStandard vue;
private CommunicationUDP commUDP;
private String lastPseudo;
public ControleurStandard(VueStandard vue, CommunicationUDP commUDP) throws IOException {
this.vue = vue;
this.commUDP = commUDP;
this.commUDP.setObserver(this);
this.commUDP.sendMessageConnecte();
this.commUDP.sendMessageInfoPseudo();
this.etatModif = EtatModif.TERMINE;
}
//---------- LISTSELECTION LISTENER OPERATIONS ----------//
@Override
public void valueChanged(ListSelectionEvent e) {
if (!e.getValueIsAdjusting()) {
JList<String> list = vue.getActiveUsersList();
System.out.println(list.getSelectedValue());
}
}
//---------- ACTION LISTENER OPERATIONS ----------//
@Override
public void actionPerformed(ActionEvent e) {
if ((JButton) e.getSource() == this.vue.getButtonModifierPseudo()) {
JButton modifierPseudo = (JButton) e.getSource();
if (this.etatModif == EtatModif.TERMINE) {
this.lastPseudo = Utilisateur.getSelf().getPseudo();
modifierPseudo.setText("OK");
this.etatModif = EtatModif.EN_COURS;
} else {
if (!this.commUDP.containsUserFromPseudo(this.vue.getDisplayedPseudo())) {
Utilisateur.getSelf().setPseudo(this.vue.getDisplayedPseudo());
try {
this.commUDP.sendMessageInfoPseudo();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} else {
this.vue.setDisplayedPseudo(this.lastPseudo);
}
modifierPseudo.setText("Modifier");
this.etatModif = EtatModif.TERMINE;
}
this.vue.toggleEditPseudo();
}
if((JButton) e.getSource() == this.vue.getButtonDeconnexion() ) {
try {
this.commUDP.sendMessageDelete();
this.commUDP.removeAll();
VueStandard.userList.removeAllElements();
Utilisateur.getSelf().setPseudo("");
vue.dispose();
new VueConnexion(5);
/*this.vue.toggleEnableButtonConnexion();
this.vue.toggleEnableButtonDeconnexion();*/
} catch (IOException e1) {
e1.printStackTrace();
}
}
if((JButton) e.getSource() == this.vue.getButtonConnexion() ) {
try {
Utilisateur.getSelf().setPseudo(this.vue.getDisplayedPseudo());
this.commUDP.sendMessageConnecte();
this.commUDP.sendMessageInfoPseudo();
this.vue.toggleEnableButtonConnexion();
this.vue.toggleEnableButtonDeconnexion();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
//---------- WINDOW LISTENER OPERATIONS ----------//
@Override
public void windowClosing(WindowEvent e) {
try {
this.commUDP.sendMessageDelete();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
@Override
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
}
//---------- OBSERVER OPERATIONS ----------//
@Override
public void update(Object o, Object arg) {
ArrayList<Utilisateur> userList = (ArrayList<Utilisateur>) arg;
ArrayList<String> listPseudo = new ArrayList<String>();
vue.resetListUsers();
for (Utilisateur user : userList) {
listPseudo.add(user.getPseudo());
}
vue.addListUsers(listPseudo);
}
}

View file

@ -1,13 +1,10 @@
package main;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JPanel;
import connexion.VueConnexion;
public class Main extends JPanel{

View file

@ -1,13 +0,0 @@
package main;
import java.io.Serializable;
public class Message implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private String contenu;
}

View file

@ -1,5 +1,6 @@
package main;
public interface Observer {
public void update(Object o, Object arg);
}

View file

@ -12,14 +12,17 @@ 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;
/*System.out.println(InetAddress.getLocalHost())*/;
this.port = port;
}
@ -39,13 +42,21 @@ 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);
}
}
public static Utilisateur getSelf() {
return Utilisateur.self;
}
public static void resetSelf() {
Utilisateur.self = null;
}
}

View file

@ -6,6 +6,7 @@ public class Vue extends JFrame{
public Vue(String title) {
super(title);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void reduireAgent() {}

View file

@ -1,202 +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;
import communication.CommunicationUDP;
public class VueStandard extends Vue {
/**
*
*/
private static final long serialVersionUID = 1L;
private JList<String> activeUsersList;
private JTextField pseudoSelf;
private JButton modifierPseudo;
private JButton seConnecter;
private JButton seDeconnecter;
private ControleurStandard c;
public static DefaultListModel<String> userList = new DefaultListModel<String>();
public VueStandard(String title, CommunicationUDP comUDP) 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, comUDP);
//--------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<String>(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<String> 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());
}
//Update de la liste des utilisateurs//
protected void resetListUsers() {
VueStandard.userList.removeAllElements();
}
protected void addListUsers (ArrayList<String> listPseudo) {
VueStandard.userList.addAll(listPseudo);
}
}

View file

@ -1,13 +1,17 @@
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;
@ -30,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]);
@ -49,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");

View file

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

View file

@ -0,0 +1,110 @@
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) {
if(arg instanceof MessageTexte) {
MessageTexte messageIn = (MessageTexte) arg;
System.out.println(messageIn.getContenu());
this.vue.appendMessage(messageIn.getContenu());
}else {
}
}
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
}
}

View file

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

View file

@ -0,0 +1,275 @@
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.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import communication.CommunicationUDP;
import communication.TCPServer;
import connexion.VueConnexion;
import main.Observer;
import main.Utilisateur;
import standard.VueStandard;
import session.VueSession;
public class ControleurStandard implements ActionListener, ListSelectionListener, WindowListener, Observer {
private enum EtatModif {
TERMINE, EN_COURS
}
private EtatModif etatModif;
private VueStandard vue;
private CommunicationUDP commUDP;
private String lastPseudo;
private TCPServer tcpServ;
public ControleurStandard(VueStandard vue, CommunicationUDP commUDP, int portServerTCP) throws IOException {
this.vue = vue;
this.tcpServ = new TCPServer(portServerTCP);
this.tcpServ.addObserver(this);
this.tcpServ.start();
this.commUDP = commUDP;
this.commUDP.setObserver(this);
this.commUDP.sendMessageConnecte();
this.commUDP.sendMessageInfoPseudo();
this.etatModif = EtatModif.TERMINE;
}
//---------- LISTSELECTION LISTENER OPERATIONS ----------//
@Override
public void valueChanged(ListSelectionEvent e) {
//Cas un élément de la liste est sélectionné
if (this.vue.getActiveUsersList().isFocusOwner() && !e.getValueIsAdjusting()) {
JList<String> list = this.vue.getActiveUsersList();
String pseudo = list.getSelectedValue();
int choix = this.vue.displayJOptionCreation(pseudo);
System.out.println("choix : "+choix);
if(choix == 0) {
int tcpServerPort = this.commUDP.getPortFromPseudo(pseudo);
System.out.println("port = "+tcpServerPort);
try {
//Send this user's pseudonyme through a TCP-client socket
//to the TCP-server socket of the selected user
Socket socketComm = new Socket(InetAddress.getLocalHost(), tcpServerPort);
this.sendMessage(socketComm, Utilisateur.getSelf().getPseudo());
//Wait for the answer, either "accepted or refused"
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);
}
}
//---------- ACTION LISTENER OPERATIONS ----------//
@Override
public void actionPerformed(ActionEvent e) {
//Cas Modifier Pseudo
if ((JButton) e.getSource() == this.vue.getButtonModifierPseudo()) {
JButton modifierPseudo = (JButton) e.getSource();
if (this.etatModif == EtatModif.TERMINE) {
this.lastPseudo = Utilisateur.getSelf().getPseudo();
modifierPseudo.setText("OK");
this.etatModif = EtatModif.EN_COURS;
} else {
if (!this.commUDP.containsUserFromPseudo(this.vue.getDisplayedPseudo())) {
Utilisateur.getSelf().setPseudo(this.vue.getDisplayedPseudo());
try {
this.commUDP.sendMessageInfoPseudo();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} else {
this.vue.setDisplayedPseudo(this.lastPseudo);
}
modifierPseudo.setText("Modifier");
this.etatModif = EtatModif.TERMINE;
}
this.vue.toggleEditPseudo();
}
//Cas deconnexion
else if((JButton) e.getSource() == this.vue.getButtonDeconnexion() ) {
try {
this.commUDP.sendMessageDelete();
this.commUDP.removeAll();
this.vue.removeAllUsers();
Utilisateur.resetSelf();
vue.dispose();
new VueConnexion(5);
// this.vue.toggleEnableButtonConnexion();
// this.vue.toggleEnableButtonDeconnexion();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
//---------- WINDOW LISTENER OPERATIONS ----------//
@Override
public void windowClosing(WindowEvent e) {
try {
this.commUDP.sendMessageDelete();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
@Override
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
}
@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));
}else {
this.sendMessage(sockAccept, "refused");
sockAccept.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
if(o == this.commUDP) {
ArrayList<Utilisateur> users = (ArrayList<Utilisateur>) arg;
ArrayList<String> pseudos = new ArrayList<String>();
for (Utilisateur u : users) {
pseudos.add(u.getPseudo());
}
this.vue.setActiveUsersList(pseudos);
}
}
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;
}
}

View file

@ -0,0 +1,275 @@
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 communication.CommunicationUDP;
import main.Utilisateur;
import main.Vue;
import session.VueSession;
public class VueStandard extends Vue {
/**
*
*/
private static final long serialVersionUID = 1L;
private JList<String> activeUsersList;
private JTextField pseudoSelf;
private JTabbedPane zoneSessions;
private JButton modifierPseudo;
private JButton seDeconnecter;
private ControleurStandard c;
private ArrayList<JButton> closeButtonSessions;
private ArrayList<String> pseudoSessions;
private DefaultListModel<String> userList = new DefaultListModel<String>();
public VueStandard(String title, CommunicationUDP commUDP, int portServerTCP) throws IOException {
super(title);
this.closeButtonSessions = new ArrayList<JButton>();
this.c = new ControleurStandard(this, commUDP, 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));
//--------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<String>(this.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.seDeconnecter = new JButton("Se Déconnecter");
this.seDeconnecter.addActionListener(this.c);
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();
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 = 4;
gridBagConstraint2.weightx = 0.66;
gridBagConstraint2.weighty = 1;
getContentPane().add(this.zoneSessions,gridBagConstraint2);
this.pack();
this.setVisible(true);
this.addWindowListener(c);
}
protected JList<String> getActiveUsersList(){
return this.activeUsersList;
}
protected void setActiveUsersList(ArrayList<String> users) {
this.removeAllUsers();
this.userList.addAll(users);
}
protected void removeAllUsers() {
this.userList.removeAllElements();
}
protected JButton getButtonModifierPseudo() {
return this.modifierPseudo;
}
protected JButton getButtonDeconnexion() {
return this.seDeconnecter;
}
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 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) {
// 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 = closeButtonSessions.contains(button);
if (containsKey) {
zoneSessions.remove(closeButtonSessions.indexOf(button) );
closeButtonSessions.remove(button);
}
}
});
tabTitle.add(new JLabel(pseudo));
tabTitle.add(closeTab);
this.zoneSessions.addTab(pseudo, session);
this.zoneSessions.setTabComponentAt(this.zoneSessions.getTabCount()-1, tabTitle);
this.closeButtonSessions.add(closeTab);
session.requestFocus();
}
}

View file

@ -1,5 +1,6 @@
package main;
public interface Observer {
public void update(Object o, Object arg);
}

View file

@ -18,10 +18,11 @@ import communication.CommunicationUDP;
public class ServletPresence extends HttpServlet implements Observer {
private static final long serialVersionUID = 1L;
//suscribe(), publish(), notify()
//suscribe(), publish(), snotify(), puis voir doPost
//Voir avant tout le fonctionnement des requetes HTTP, et comment elles donnent des infos
//rajouter une classe pour la communication HTTP <= tenir Kevin au courant
private CommunicationUDP comUDP;
private ArrayList<Utilisateur> remoteUsers;
public ServletPresence() {
//A changer en passant aux IP
@ -31,7 +32,7 @@ public class ServletPresence extends HttpServlet implements Observer {
// TODO Auto-generated catch block
e.printStackTrace();
}
remoteUsers = new ArrayList<Utilisateur>();
comUDP.setObserver(this);
}
//Permet a un utilisateur externe de s'ajouter/s'enlever à la liste des utilisateurs externes : au tout début de l'application
@ -41,22 +42,15 @@ public class ServletPresence extends HttpServlet implements Observer {
private void unsubscribe() {
}
//Permet de dire si on est connecté/déconnecté
//Permet de dire si on a changé de pseudo
private void publish() {
}
//Informe de la modification de la liste tous les utilisateurs internes et externes
//Informe de la modification de la liste tous les utilisateurs internes et externes => par reponse HTTP
private void snotify() {
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/

View file

@ -12,13 +12,17 @@ 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;
}
@ -38,13 +42,21 @@ 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);
}
}
public static Utilisateur getSelf() {
return Utilisateur.self;
}
public static void resetSelf() {
Utilisateur.self = null;
}
}

View file

@ -1,13 +1,17 @@
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;
@ -30,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]);
@ -49,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");

View file

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