initial commit standard+session
This commit is contained in:
parent
7a4d831d56
commit
bed2d47efa
18 changed files with 856 additions and 218 deletions
|
@ -6,7 +6,7 @@ import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import main.Utilisateur;
|
import main.Utilisateur;
|
||||||
import main.VueStandard;
|
import standard.VueStandard;
|
||||||
|
|
||||||
public class Communication extends Thread{
|
public class Communication extends Thread{
|
||||||
protected static ArrayList<Utilisateur> users = new ArrayList<Utilisateur>();
|
protected static ArrayList<Utilisateur> users = new ArrayList<Utilisateur>();
|
||||||
|
@ -31,6 +31,22 @@ public class Communication extends Thread{
|
||||||
return false;
|
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) {
|
protected static int getIndexFromID(String id) {
|
||||||
for(int i=0; i < Communication.users.size() ; i++) {
|
for(int i=0; i < Communication.users.size() ; i++) {
|
||||||
if(Communication.users.get(i).getId().equals(id) ) {
|
if(Communication.users.get(i).getId().equals(id) ) {
|
||||||
|
@ -49,20 +65,30 @@ public class Communication extends Thread{
|
||||||
return -1;
|
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);
|
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);
|
int index = Communication.getIndexFromID(idClient);
|
||||||
Communication.users.get(index).setPseudo(pseudoClient);
|
Communication.users.get(index).setPseudo(pseudoClient);
|
||||||
VueStandard.userList.set(index, 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);
|
int index = Communication.getIndexFromIP(ipClient);
|
||||||
if( index != -1) {
|
if( index != -1) {
|
||||||
Communication.users.remove(index);
|
Communication.users.remove(index);
|
||||||
|
@ -76,4 +102,5 @@ public class Communication extends Thread{
|
||||||
Communication.users.remove(0);
|
Communication.users.remove(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -56,10 +56,11 @@ public class CommunicationUDP extends Communication {
|
||||||
|
|
||||||
String pseudoSelf =self.getPseudo();
|
String pseudoSelf =self.getPseudo();
|
||||||
String idSelf = self.getId();
|
String idSelf = self.getId();
|
||||||
|
int portSelf = self.getPort();
|
||||||
|
|
||||||
Message msout = null;
|
Message msout = null;
|
||||||
try {
|
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) {
|
for(int port : this.portOthers) {
|
||||||
this.client.sendMessageUDP_local(msout, port, InetAddress.getLocalHost());
|
this.client.sendMessageUDP_local(msout, port, InetAddress.getLocalHost());
|
||||||
}
|
}
|
||||||
|
@ -76,7 +77,7 @@ public class CommunicationUDP extends Communication {
|
||||||
|
|
||||||
Utilisateur self = Utilisateur.getSelf();
|
Utilisateur self = Utilisateur.getSelf();
|
||||||
try {
|
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());
|
this.client.sendMessageUDP_local(msout, portOther, InetAddress.getLocalHost());
|
||||||
} catch (MauvaisTypeMessageException e) {e.printStackTrace();}
|
} catch (MauvaisTypeMessageException e) {e.printStackTrace();}
|
||||||
}
|
}
|
||||||
|
|
53
POO/src/communication/TCPClient.java
Normal file
53
POO/src/communication/TCPClient.java
Normal file
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
66
POO/src/communication/TCPInputThread.java
Normal file
66
POO/src/communication/TCPInputThread.java
Normal file
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
40
POO/src/communication/TCPServer.java
Normal file
40
POO/src/communication/TCPServer.java
Normal file
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -26,7 +26,7 @@ public class UDPClient {
|
||||||
|
|
||||||
//Send a message casted as string to the specified port on localhost
|
//Send a message casted as string to the specified port on localhost
|
||||||
protected void sendMessageUDP_local(Message message, int port, InetAddress clientAddress) throws IOException {
|
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);
|
DatagramPacket outpacket = new DatagramPacket(messageString.getBytes(), messageString.length(), clientAddress, port);
|
||||||
this.sockUDP.send(outpacket);
|
this.sockUDP.send(outpacket);
|
||||||
|
|
||||||
|
|
|
@ -46,17 +46,17 @@ public class UDPServer extends Thread {
|
||||||
case INFO_PSEUDO :
|
case INFO_PSEUDO :
|
||||||
|
|
||||||
if (Communication.containsUserFromID(((MessageSysteme) msg).getId())) {
|
if (Communication.containsUserFromID(((MessageSysteme) msg).getId())) {
|
||||||
Communication.changePseudoUser(((MessageSysteme) msg).getId(), ((MessageSysteme) msg).getPseudo(), inPacket.getAddress());
|
Communication.changePseudoUser(((MessageSysteme) msg).getId(), ((MessageSysteme) msg).getPseudo(), inPacket.getAddress(),((MessageSysteme) msg).getPort());
|
||||||
}
|
}
|
||||||
else {
|
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());
|
System.out.println(((MessageSysteme) msg).getId()+", "+((MessageSysteme) msg).getPseudo());
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case JE_SUIS_DECONNECTE :
|
case JE_SUIS_DECONNECTE :
|
||||||
Communication.removeUser( ((MessageSysteme) msg).getId() , ((MessageSysteme) msg).getPseudo(), inPacket.getAddress() );
|
Communication.removeUser( ((MessageSysteme) msg).getId() , ((MessageSysteme) msg).getPseudo(), inPacket.getAddress(), inPacket.getPort());
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default : //Others types of messages are ignored because they are supposed to be transmitted by TCP and not UDP
|
default : //Others types of messages are ignored because they are supposed to be transmitted by TCP and not UDP
|
||||||
|
|
|
@ -2,13 +2,16 @@ package main;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import standard.VueStandard;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public class Main {
|
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 ids[] = {"Raijila", "titi33", "Semtexx", "Salam"};
|
||||||
private static String pseudo[] = {"Raijila", "Mirasio", "Semtexx", "Xaegon"};
|
private static String pseudo[] = {"Raijila", "Mirasio", "Semtexx", "Xaegon"};
|
||||||
|
private static int portServersTCP[] = {1625,1600,1650,1660};
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
|
||||||
|
@ -44,8 +47,8 @@ public class Main {
|
||||||
|
|
||||||
private static void createApp(int i) {
|
private static void createApp(int i) {
|
||||||
try {
|
try {
|
||||||
Utilisateur.setSelf(Main.ids[i], Main.pseudo[i], "localhost");
|
Utilisateur.setSelf(Main.ids[i], Main.pseudo[i], "localhost", Main.portServersTCP[i]);
|
||||||
new VueStandard("Application", Main.portServers[i]-1, Main.portServers[i], Main.portServers);
|
new VueStandard("Application", Main.portServersUDP[i]-1, Main.portServersUDP[i], Main.portServersUDP, Main.portServersTCP[i]);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
System.out.println(e.toString());
|
System.out.println(e.toString());
|
||||||
}
|
}
|
||||||
|
|
6
POO/src/main/Observer.java
Normal file
6
POO/src/main/Observer.java
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
package main;
|
||||||
|
|
||||||
|
public interface Observer {
|
||||||
|
|
||||||
|
public void update(Object o, Object arg);
|
||||||
|
}
|
|
@ -12,13 +12,15 @@ public class Utilisateur implements Serializable{
|
||||||
private String id;
|
private String id;
|
||||||
private String pseudo;
|
private String pseudo;
|
||||||
private InetAddress ip;
|
private InetAddress ip;
|
||||||
|
private int port;
|
||||||
|
|
||||||
private static Utilisateur self;
|
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.id = id;
|
||||||
this.pseudo = pseudo;
|
this.pseudo = pseudo;
|
||||||
this.ip = ip;
|
this.ip = ip;
|
||||||
|
this.port = port;
|
||||||
System.out.println(InetAddress.getLocalHost());
|
System.out.println(InetAddress.getLocalHost());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -39,9 +41,13 @@ public class Utilisateur implements Serializable{
|
||||||
return ip;
|
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) {
|
if(Utilisateur.self == null) {
|
||||||
Utilisateur.self = new Utilisateur(id, pseudo, InetAddress.getByName(host));
|
Utilisateur.self = new Utilisateur(id, pseudo, InetAddress.getByName(host), port);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,6 +6,7 @@ public class Vue extends JFrame{
|
||||||
|
|
||||||
public Vue(String title) {
|
public Vue(String title) {
|
||||||
super(title);
|
super(title);
|
||||||
|
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void reduireAgent() {}
|
public void reduireAgent() {}
|
||||||
|
|
|
@ -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<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, 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<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());
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -34,7 +34,7 @@ public abstract class Message implements Serializable {
|
||||||
return new MessageSysteme(TypeMessage.JE_SUIS_DECONNECTE);
|
return new MessageSysteme(TypeMessage.JE_SUIS_DECONNECTE);
|
||||||
|
|
||||||
case "INFO_PSEUDO" :
|
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" :
|
case "TEXTE" :
|
||||||
return new MessageTexte(TypeMessage.TEXTE, parts[1]);
|
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 {
|
public static void main(String[] args) throws MauvaisTypeMessageException {
|
||||||
Message m1 = new MessageSysteme(TypeMessage.JE_SUIS_CONNECTE);
|
Message m1 = new MessageSysteme(TypeMessage.JE_SUIS_CONNECTE);
|
||||||
Message m2 = new MessageSysteme(TypeMessage.JE_SUIS_DECONNECTE);
|
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 m4 = new MessageTexte(TypeMessage.TEXTE, "blablabla");
|
||||||
Message m5 = new MessageFichier(TypeMessage.FICHIER, "truc", ".pdf");
|
Message m5 = new MessageFichier(TypeMessage.FICHIER, "truc", ".pdf");
|
||||||
|
|
||||||
|
|
|
@ -5,21 +5,24 @@ public class MessageSysteme extends Message {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
private String pseudo;
|
private String pseudo;
|
||||||
private String id;
|
private String id;
|
||||||
|
private int port;
|
||||||
|
|
||||||
public MessageSysteme(TypeMessage type) throws MauvaisTypeMessageException{
|
public MessageSysteme(TypeMessage type) throws MauvaisTypeMessageException{
|
||||||
if ((type==TypeMessage.JE_SUIS_CONNECTE)||(type==TypeMessage.JE_SUIS_DECONNECTE)||(type==TypeMessage.MESSAGE_NUL)) {
|
if ((type==TypeMessage.JE_SUIS_CONNECTE)||(type==TypeMessage.JE_SUIS_DECONNECTE)||(type==TypeMessage.MESSAGE_NUL)) {
|
||||||
this.type=type;
|
this.type=type;
|
||||||
this.pseudo="";
|
this.pseudo="";
|
||||||
this.id="";
|
this.id="";
|
||||||
|
this.port = -1;
|
||||||
}
|
}
|
||||||
else throw new MauvaisTypeMessageException();
|
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) {
|
if (type==TypeMessage.INFO_PSEUDO) {
|
||||||
this.type=type;
|
this.type=type;
|
||||||
this.pseudo=pseudo;
|
this.pseudo=pseudo;
|
||||||
this.id=id;
|
this.id=id;
|
||||||
|
this.port = port;
|
||||||
}
|
}
|
||||||
else throw new MauvaisTypeMessageException();
|
else throw new MauvaisTypeMessageException();
|
||||||
}
|
}
|
||||||
|
@ -32,8 +35,12 @@ public class MessageSysteme extends Message {
|
||||||
return this.id;
|
return this.id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getPort() {
|
||||||
|
return this.port;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String attributsToString() {
|
protected String attributsToString() {
|
||||||
return this.pseudo+"###"+this.id;
|
return this.pseudo+"###"+this.id+"###"+this.port;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
106
POO/src/session/ControleurSession.java
Normal file
106
POO/src/session/ControleurSession.java
Normal file
|
@ -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
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
109
POO/src/session/VueSession.java
Normal file
109
POO/src/session/VueSession.java
Normal 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,10 +1,17 @@
|
||||||
package main;
|
package standard;
|
||||||
|
|
||||||
import java.awt.event.ActionEvent;
|
import java.awt.event.ActionEvent;
|
||||||
import java.awt.event.ActionListener;
|
import java.awt.event.ActionListener;
|
||||||
import java.awt.event.WindowEvent;
|
import java.awt.event.WindowEvent;
|
||||||
import java.awt.event.WindowListener;
|
import java.awt.event.WindowListener;
|
||||||
|
import java.io.BufferedReader;
|
||||||
import java.io.IOException;
|
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.JButton;
|
||||||
import javax.swing.JList;
|
import javax.swing.JList;
|
||||||
import javax.swing.event.ListSelectionEvent;
|
import javax.swing.event.ListSelectionEvent;
|
||||||
|
@ -12,8 +19,12 @@ import javax.swing.event.ListSelectionListener;
|
||||||
|
|
||||||
import communication.Communication;
|
import communication.Communication;
|
||||||
import communication.CommunicationUDP;
|
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 {
|
private enum EtatModif {
|
||||||
TERMINE, EN_COURS
|
TERMINE, EN_COURS
|
||||||
|
@ -23,22 +34,68 @@ public class ControleurStandard implements ActionListener, ListSelectionListener
|
||||||
private VueStandard vue;
|
private VueStandard vue;
|
||||||
private CommunicationUDP commUDP;
|
private CommunicationUDP commUDP;
|
||||||
private String lastPseudo;
|
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.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.sendMessageConnecte();
|
||||||
this.commUDP.sendMessageInfoPseudo();
|
this.commUDP.sendMessageInfoPseudo();
|
||||||
|
|
||||||
this.etatModif = EtatModif.TERMINE;
|
this.etatModif = EtatModif.TERMINE;
|
||||||
}
|
}
|
||||||
|
|
||||||
//---------- LISTSELECTION LISTENER OPERATIONS ----------//
|
//---------- LISTSELECTION LISTENER OPERATIONS ----------//
|
||||||
@Override
|
@Override
|
||||||
public void valueChanged(ListSelectionEvent e) {
|
public void valueChanged(ListSelectionEvent e) {
|
||||||
if (!e.getValueIsAdjusting()) {
|
|
||||||
JList<String> list = vue.getActiveUsersList();
|
//Cas où un élément de la liste est sélectionné
|
||||||
System.out.println(list.getSelectedValue());
|
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 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 ----------//
|
//---------- ACTION LISTENER OPERATIONS ----------//
|
||||||
@Override
|
@Override
|
||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
|
||||||
|
//Cas Modifier Pseudo
|
||||||
if ((JButton) e.getSource() == this.vue.getButtonModifierPseudo()) {
|
if ((JButton) e.getSource() == this.vue.getButtonModifierPseudo()) {
|
||||||
JButton modifierPseudo = (JButton) e.getSource();
|
JButton modifierPseudo = (JButton) e.getSource();
|
||||||
|
|
||||||
|
@ -77,6 +136,9 @@ public class ControleurStandard implements ActionListener, ListSelectionListener
|
||||||
this.vue.toggleEditPseudo();
|
this.vue.toggleEditPseudo();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//Cas deconnexion
|
||||||
if((JButton) e.getSource() == this.vue.getButtonDeconnexion() ) {
|
if((JButton) e.getSource() == this.vue.getButtonDeconnexion() ) {
|
||||||
try {
|
try {
|
||||||
this.commUDP.sendMessageDelete();
|
this.commUDP.sendMessageDelete();
|
||||||
|
@ -95,6 +157,7 @@ public class ControleurStandard implements ActionListener, ListSelectionListener
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//Cas connexion
|
||||||
if((JButton) e.getSource() == this.vue.getButtonConnexion() ) {
|
if((JButton) e.getSource() == this.vue.getButtonConnexion() ) {
|
||||||
try {
|
try {
|
||||||
Utilisateur.getSelf().setPseudo(this.vue.getDisplayedPseudo());
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
291
POO/src/standard/VueStandard.java
Normal file
291
POO/src/standard/VueStandard.java
Normal file
|
@ -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<String> activeUsersList;
|
||||||
|
private JTextField pseudoSelf;
|
||||||
|
private JTabbedPane zoneSessions;
|
||||||
|
private JButton modifierPseudo;
|
||||||
|
private JButton seConnecter;
|
||||||
|
private JButton seDeconnecter;
|
||||||
|
private ControleurStandard c;
|
||||||
|
private HashMap<JButton,VueSession> sessions;
|
||||||
|
public static DefaultListModel<String> userList = new DefaultListModel<String>();
|
||||||
|
|
||||||
|
|
||||||
|
public VueStandard(String title, int portClientUDP, int portServerUDP, int[] portsOther, int portServerTCP) throws IOException {
|
||||||
|
super(title);
|
||||||
|
|
||||||
|
this.sessions = new HashMap<JButton,VueSession>();
|
||||||
|
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<String>(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<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());
|
||||||
|
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();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in a new issue