session fonctionnelle, messages simples
This commit is contained in:
parent
26bdd96bfd
commit
1e3a1b4f37
20 changed files with 510 additions and 461 deletions
|
@ -1,82 +0,0 @@
|
|||
package communication;
|
||||
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import main.Utilisateur;
|
||||
import main.VueSession;
|
||||
|
||||
public class Communication extends Thread{
|
||||
protected static ArrayList<Utilisateur> users = new ArrayList<Utilisateur>();
|
||||
|
||||
protected static boolean containsUserFromID(String id) {
|
||||
for(Utilisateur u : Communication.users) {
|
||||
if(u.getId().equals(id) ) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean containsUserFromPseudo(String pseudo) {
|
||||
for(Utilisateur u : Communication.users) {
|
||||
if(u.getPseudo().equals(pseudo) ) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
protected static int getIndexFromID(String id) {
|
||||
for(int i=0; i < Communication.users.size() ; i++) {
|
||||
if(Communication.users.get(i).getId().equals(id) ) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
//TODO
|
||||
//Combiner add et change
|
||||
protected static synchronized void addUser(List<String> datas) throws UnknownHostException {
|
||||
|
||||
String idClient = datas.get(0);
|
||||
String pseudoClient = datas.get(1);
|
||||
String clientAddress = datas.get(2);
|
||||
|
||||
if (!Communication.containsUserFromID(idClient)) {
|
||||
Communication.users.add(new Utilisateur(idClient, pseudoClient, clientAddress));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
protected static synchronized void changePseudoUser(List<String> datas) {
|
||||
String idClient = datas.get(0);
|
||||
String pseudoClient = datas.get(1);
|
||||
int index = Communication.getIndexFromID(idClient);
|
||||
// System.out.println(index);
|
||||
if(index != -1) {
|
||||
Communication.users.get(index).setPseudo(pseudoClient);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
protected static synchronized void removeUser(List<String> datas) {
|
||||
String idClient = datas.get(0);
|
||||
int index = Communication.getIndexFromID(idClient);
|
||||
//System.out.println(index);
|
||||
if( index != -1) {
|
||||
Communication.users.remove(index);
|
||||
}
|
||||
}
|
||||
|
||||
public static void removeAll(){
|
||||
int oSize = Communication.users.size();
|
||||
for(int i=0; i<oSize;i++) {
|
||||
Communication.users.remove(0);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,113 +0,0 @@
|
|||
package communication;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
||||
import main.Utilisateur;
|
||||
|
||||
public class CommunicationUDP extends Communication {
|
||||
|
||||
// public enum Mode {PREMIERE_CONNEXION, CHANGEMENT_PSEUDO, DECONNEXION};
|
||||
|
||||
private UDPClient client;
|
||||
private int portServer;
|
||||
private ArrayList<Integer> portOthers;
|
||||
|
||||
public CommunicationUDP(int portClient, int portServer, int[] portsOther) throws IOException {
|
||||
this.portServer = portServer;
|
||||
this.portOthers = this.getArrayListFromArray(portsOther);
|
||||
new UDPServer(portServer, this).start();
|
||||
this.client = new UDPClient(portClient);
|
||||
}
|
||||
|
||||
private ArrayList<Integer> getArrayListFromArray(int ports[]) {
|
||||
ArrayList<Integer> tmp = new ArrayList<Integer>();
|
||||
for (int port : ports) {
|
||||
tmp.add(port);
|
||||
}
|
||||
tmp.remove(Integer.valueOf(portServer));
|
||||
|
||||
return tmp;
|
||||
}
|
||||
|
||||
|
||||
public void sendMessageConnecte() throws UnknownHostException, IOException {
|
||||
for(int port : this.portOthers) {
|
||||
this.client.sendMessageUDP_local("first_connection", port, InetAddress.getLocalHost());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Send the message "add,id,pseudo" to localhost on all the ports in
|
||||
// "portOthers"
|
||||
// This allows the receivers' agent (portOthers) to create an entry with the
|
||||
// data of this agent
|
||||
public void sendMessageAdd() throws UnknownHostException, IOException {
|
||||
this.sendIDPseudo_local("add");
|
||||
}
|
||||
|
||||
public void sendMessageAdd(ArrayList<Integer> portServers) throws UnknownHostException, IOException {
|
||||
this.sendIDPseudo_local("add", portServers);
|
||||
}
|
||||
|
||||
// Send the message "modify,id,pseudo" to localhost on all the ports in
|
||||
// "portOthers"
|
||||
// This allows the receivers' agent (portOthers) to update the entry
|
||||
// corresponding to this agent
|
||||
public void sendMessageModify() throws UnknownHostException, IOException {
|
||||
this.sendIDPseudo_local("modify");
|
||||
}
|
||||
|
||||
// Send the message "del,id,pseudo" to localhost on all the ports in
|
||||
// "portOthers"
|
||||
// This allows the receivers' agent (portOthers) to delete the entry
|
||||
// corresponding to this agent
|
||||
public void sendMessageDelete() throws UnknownHostException, IOException {
|
||||
this.sendIDPseudo_local("del");
|
||||
}
|
||||
|
||||
// Private function to create the message "[prefix],id,pseudo"
|
||||
// and send it to localhost on all the ports in "portOthers"
|
||||
private void sendIDPseudo_local(String prefixe, ArrayList<Integer> portServers) throws UnknownHostException, IOException {
|
||||
Utilisateur self = Utilisateur.getSelf();
|
||||
String idSelf = self.getId();
|
||||
String pseudoSelf = self.getPseudo();
|
||||
|
||||
if (!pseudoSelf.equals("")) {
|
||||
|
||||
String message = prefixe + "," + idSelf + "," + pseudoSelf;
|
||||
// A modifier pour créer un objet de type Message
|
||||
//
|
||||
//
|
||||
|
||||
for (int port : portServers) {
|
||||
this.client.sendMessageUDP_local(message, port, InetAddress.getLocalHost());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void sendIDPseudo_local(String prefixe) throws UnknownHostException, IOException {
|
||||
this.sendIDPseudo_local(prefixe, this.portOthers);
|
||||
}
|
||||
|
||||
// private void sendIDPseudo_broadcast(String prefixe) throws UnknownHostException, IOException {
|
||||
// Utilisateur self = Utilisateur.getSelf();
|
||||
// String idSelf = self.getId();
|
||||
// String pseudoSelf = self.getPseudo();
|
||||
//
|
||||
// String message = prefixe+","+idSelf + "," + pseudoSelf;
|
||||
//
|
||||
//
|
||||
// this.client.sendMessageUDP_broadcast(message, this.portServer);
|
||||
//
|
||||
// }
|
||||
|
||||
// public synchronized void createSenderUDP(int port, Mode mode) throws SocketException {
|
||||
// new SenderUDP(mode, port).start();
|
||||
// }
|
||||
|
||||
}
|
61
POO/src/communication/TCPClient.java
Normal file
61
POO/src/communication/TCPClient.java
Normal file
|
@ -0,0 +1,61 @@
|
|||
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(this.sockTCP.getOutputStream()) ;
|
||||
ObjectInputStream input = new ObjectInputStream(this.sockTCP.getInputStream());
|
||||
this.inputThread = new TCPInputThread(input);
|
||||
}
|
||||
|
||||
public TCPClient(InetAddress addr, int port) throws IOException {
|
||||
this(new Socket(addr, port)) ;
|
||||
|
||||
}
|
||||
|
||||
public void connexionAccepted() throws IOException {
|
||||
|
||||
System.out.println("avant vue");
|
||||
|
||||
new VueSession("Application", this);
|
||||
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
34
POO/src/communication/TCPHandlerConnection.java
Normal file
34
POO/src/communication/TCPHandlerConnection.java
Normal file
|
@ -0,0 +1,34 @@
|
|||
package communication;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
import java.net.Socket;
|
||||
|
||||
public class TCPHandlerConnection extends Thread {
|
||||
|
||||
private Socket sockAccept;
|
||||
|
||||
public TCPHandlerConnection(Socket sockAccept) {
|
||||
this.sockAccept = sockAccept;
|
||||
}
|
||||
|
||||
public TCPHandlerConnection(InetAddress addr, int port) throws IOException {
|
||||
this(new Socket(addr, port));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
TCPClient tcpC;
|
||||
try {
|
||||
tcpC = new TCPClient(sockAccept);
|
||||
tcpC.connexionAccepted();
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
59
POO/src/communication/TCPInputThread.java
Normal file
59
POO/src/communication/TCPInputThread.java
Normal file
|
@ -0,0 +1,59 @@
|
|||
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() {
|
||||
this.running = false;
|
||||
}
|
||||
|
||||
private void flushBuffer() {
|
||||
Arrays.fill(this.buffer, '\u0000');
|
||||
}
|
||||
|
||||
protected void setObserver(Observer o) {
|
||||
this.obs = o;
|
||||
}
|
||||
|
||||
}
|
32
POO/src/communication/TCPServer.java
Normal file
32
POO/src/communication/TCPServer.java
Normal file
|
@ -0,0 +1,32 @@
|
|||
package communication;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
|
||||
public class TCPServer extends Thread {
|
||||
|
||||
private ServerSocket sockListenTCP;
|
||||
|
||||
public TCPServer(int port) throws UnknownHostException, IOException {
|
||||
this.sockListenTCP = new ServerSocket(port, 5, InetAddress.getLocalHost());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
Socket sockAccept;
|
||||
while(true) {
|
||||
try {
|
||||
sockAccept = this.sockListenTCP.accept();
|
||||
new TCPHandlerConnection(sockAccept).run();
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,41 +0,0 @@
|
|||
package communication;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.InetAddress;
|
||||
import java.net.NetworkInterface;
|
||||
import java.net.SocketException;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
public class UDPClient {
|
||||
|
||||
private DatagramSocket sockUDP;
|
||||
private InetAddress broadcast;
|
||||
|
||||
public UDPClient(int port) throws SocketException, UnknownHostException {
|
||||
this.sockUDP = new DatagramSocket(port);
|
||||
|
||||
InetAddress localHost = InetAddress.getLocalHost();
|
||||
NetworkInterface networkInterface = NetworkInterface.getByInetAddress(localHost);
|
||||
this.broadcast = networkInterface.getInterfaceAddresses().get(0).getBroadcast();
|
||||
}
|
||||
|
||||
|
||||
//Send a string message to the specified port on localhost
|
||||
protected void sendMessageUDP_local(String message, int port, InetAddress clientAddress) throws IOException {
|
||||
|
||||
//A modifier, faire passer un type Message en paramètre
|
||||
//puis écrire les instructions pour envoyer un Message à traver la socket
|
||||
|
||||
DatagramPacket outpacket = new DatagramPacket(message.getBytes(), message.length(), clientAddress, port);
|
||||
this.sockUDP.send(outpacket);
|
||||
|
||||
}
|
||||
|
||||
// protected void sendMessageUDP_broadcast(String message, int port) throws IOException{
|
||||
// DatagramPacket outpacket = new DatagramPacket(message.getBytes(), message.length(), this.broadcast, port);
|
||||
// this.sockUDP.send(outpacket);
|
||||
// }
|
||||
|
||||
}
|
|
@ -1,77 +0,0 @@
|
|||
package communication;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.SocketException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
||||
|
||||
|
||||
public class UDPServer extends Thread {
|
||||
|
||||
private DatagramSocket sockUDP;
|
||||
private CommunicationUDP commUDP;
|
||||
private byte[] buffer;
|
||||
|
||||
public UDPServer(int port, CommunicationUDP commUDP) throws SocketException {
|
||||
this.commUDP = commUDP;
|
||||
this.sockUDP = new DatagramSocket(port);
|
||||
this.buffer = new byte[256];
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
while (true) {
|
||||
|
||||
try {
|
||||
DatagramPacket inPacket = new DatagramPacket(buffer, buffer.length);
|
||||
this.sockUDP.receive(inPacket);
|
||||
String msg = new String(inPacket.getData(), 0, inPacket.getLength());
|
||||
|
||||
if (msg.equals("first_connection")) {
|
||||
//System.out.println("first co");
|
||||
ArrayList<Integer> portClient = new ArrayList<Integer>();
|
||||
portClient.add(inPacket.getPort()+1);
|
||||
this.commUDP.sendMessageAdd(portClient);
|
||||
|
||||
} else if (msg.contains("add,")) {
|
||||
//System.out.println("add");
|
||||
ArrayList<String> datas = this.getDatas(inPacket);
|
||||
Communication.addUser(datas);
|
||||
|
||||
} else if (msg.contains("modify,")) {
|
||||
ArrayList<String> datas = this.getDatas(inPacket);
|
||||
Communication.changePseudoUser(datas);
|
||||
|
||||
} else if (msg.contains("del,")) {
|
||||
ArrayList<String> datas = this.getDatas(inPacket);
|
||||
Communication.removeUser(datas);
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
System.out.println("receive exception");
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
protected ArrayList<String> getDatas(DatagramPacket inPacket) {
|
||||
//Message
|
||||
//
|
||||
|
||||
String msg = new String(inPacket.getData(), 0, inPacket.getLength());
|
||||
String tmp[] = msg.split(",");
|
||||
|
||||
|
||||
|
||||
ArrayList<String> datas = new ArrayList<String>(Arrays.asList(tmp));
|
||||
datas.remove(0);
|
||||
datas.add(inPacket.getAddress().toString());
|
||||
|
||||
return datas;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,36 +1,81 @@
|
|||
package main;
|
||||
|
||||
import java.awt.event.ActionEvent;
|
||||
|
||||
import java.awt.event.ActionListener;
|
||||
import java.awt.event.WindowEvent;
|
||||
import java.awt.event.WindowListener;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.event.KeyListener;
|
||||
import java.io.IOException;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JList;
|
||||
import javax.swing.event.ListSelectionEvent;
|
||||
import javax.swing.event.ListSelectionListener;
|
||||
|
||||
import communication.Communication;
|
||||
import communication.CommunicationUDP;
|
||||
import java.util.concurrent.*;
|
||||
|
||||
public class ControleurSession implements ActionListener{
|
||||
import communication.TCPClient;
|
||||
import messages.MauvaisTypeMessageException;
|
||||
import messages.Message;
|
||||
import messages.MessageTexte;
|
||||
|
||||
public class ControleurSession implements ActionListener, Observer {
|
||||
|
||||
private VueSession vue;
|
||||
private TCPClient tcpClient;
|
||||
|
||||
public ControleurSession(VueSession vue) throws IOException {
|
||||
protected ControleurSession(VueSession vue, TCPClient tcpClient) throws IOException {
|
||||
this.vue = vue;
|
||||
this.tcpClient = tcpClient;
|
||||
this.tcpClient.setObserverInputThread(this);
|
||||
this.tcpClient.startInputThread();
|
||||
}
|
||||
|
||||
|
||||
|
||||
//---------- ACTION LISTENER OPERATIONS ----------//
|
||||
// ---------- 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)+">";
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,6 +1,10 @@
|
|||
package main;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
|
||||
import communication.TCPClient;
|
||||
import communication.TCPServer;
|
||||
|
||||
|
||||
|
||||
|
@ -45,7 +49,10 @@ public class Main {
|
|||
private static void createApp(int i) {
|
||||
try {
|
||||
Utilisateur.setSelf(Main.ids[i], Main.pseudo[i], "localhost");
|
||||
new VueSession("Application");
|
||||
System.out.println("Avant tcpcli");
|
||||
TCPClient tcpCli = new TCPClient(InetAddress.getLocalHost(), 7001);
|
||||
tcpCli.connexionAccepted();
|
||||
//new VueSession("Application");
|
||||
} catch (IOException e) {
|
||||
System.out.println(e.toString());
|
||||
}
|
||||
|
|
18
POO/src/main/MainServTCP.java
Normal file
18
POO/src/main/MainServTCP.java
Normal file
|
@ -0,0 +1,18 @@
|
|||
package main;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import communication.TCPServer;
|
||||
|
||||
public class MainServTCP {
|
||||
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
Utilisateur.setSelf("id1", "toto", "localhost");
|
||||
new TCPServer(7001).run();
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
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);
|
||||
}
|
|
@ -15,11 +15,11 @@ public class Utilisateur implements Serializable{
|
|||
|
||||
private static Utilisateur self;
|
||||
|
||||
public Utilisateur(String id, String pseudo, String host) throws UnknownHostException {
|
||||
public Utilisateur(String id, String pseudo, InetAddress ip) throws UnknownHostException {
|
||||
this.id = id;
|
||||
this.pseudo = pseudo;
|
||||
this.ip = InetAddress.getLocalHost();
|
||||
//System.out.println(InetAddress.getLocalHost());
|
||||
this.ip = ip;
|
||||
System.out.println(InetAddress.getLocalHost());
|
||||
}
|
||||
|
||||
|
||||
|
@ -41,9 +41,8 @@ public class Utilisateur implements Serializable{
|
|||
|
||||
public static void setSelf(String id, String pseudo,String host) throws UnknownHostException {
|
||||
if(Utilisateur.self == null) {
|
||||
Utilisateur.self = new Utilisateur(id, pseudo, host);
|
||||
Utilisateur.self = new Utilisateur(id, pseudo, InetAddress.getByName(host));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static Utilisateur getSelf() {
|
||||
|
|
|
@ -20,8 +20,12 @@ import javax.swing.JScrollPane;
|
|||
import javax.swing.JTabbedPane;
|
||||
import javax.swing.JTextArea;
|
||||
import javax.swing.JTextField;
|
||||
import javax.swing.JTextPane;
|
||||
import javax.swing.ListSelectionModel;
|
||||
import javax.swing.ScrollPaneConstants;
|
||||
import javax.swing.border.EmptyBorder;
|
||||
|
||||
import communication.TCPClient;
|
||||
|
||||
public class VueSession extends Vue {
|
||||
|
||||
|
@ -32,72 +36,51 @@ public class VueSession extends Vue {
|
|||
|
||||
|
||||
private JButton envoyerMessage;
|
||||
private JTextArea chatWindow;
|
||||
private JTextField chatInput;
|
||||
private ControleurSession c;
|
||||
|
||||
public VueSession(String title) throws IOException {
|
||||
public VueSession(String title, TCPClient tcpClient) throws IOException {
|
||||
|
||||
super(title);
|
||||
|
||||
JPanel main = new JPanel(new BorderLayout());
|
||||
main.setBackground(Color.green);
|
||||
|
||||
|
||||
JTextArea chatWindow = new JTextArea();
|
||||
|
||||
|
||||
JScrollPane chatScroll = new JScrollPane();
|
||||
chatScroll.setPreferredSize(new Dimension(575, 600));
|
||||
chatScroll.setBackground(Color.blue);
|
||||
|
||||
JTextField chatInput = new JTextField("Entrez votre message");
|
||||
chatInput.setPreferredSize(new Dimension(575, 150));
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
this.c = new ControleurSession(this);
|
||||
|
||||
|
||||
|
||||
|
||||
GridBagConstraints gridBagConstraint = new GridBagConstraints();
|
||||
|
||||
gridBagConstraint.fill = GridBagConstraints.BOTH;
|
||||
gridBagConstraint.gridx = 0;
|
||||
gridBagConstraint.gridy = 0;
|
||||
gridBagConstraint.gridwidth = 1;
|
||||
gridBagConstraint.gridheight = 5;
|
||||
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.c = new ControleurSession(this, tcpClient);
|
||||
|
||||
this.setBounds(100, 100, 600, 600);
|
||||
JPanel main = new JPanel();
|
||||
main.setBorder(new EmptyBorder(5, 5, 5, 5));
|
||||
this.add(main);
|
||||
main.setLayout(new BorderLayout(0, 0));
|
||||
|
||||
this.setSize(900,900);
|
||||
JPanel bottom = new JPanel();
|
||||
main.add(bottom, BorderLayout.SOUTH);
|
||||
bottom.setLayout(new BorderLayout(0, 0));
|
||||
|
||||
this.chatInput = new JTextField();
|
||||
|
||||
//textField.setPreferredSize(new Dimension(300, 50));
|
||||
bottom.add(this.chatInput);
|
||||
this.chatInput.setColumns(10);
|
||||
|
||||
this.envoyerMessage = new JButton("Envoyer");
|
||||
this.envoyerMessage.addActionListener(this.c);
|
||||
|
||||
bottom.add(this.envoyerMessage, BorderLayout.EAST);
|
||||
|
||||
this.chatWindow = new JTextArea();
|
||||
this.chatWindow.setEditable(false);
|
||||
|
||||
ScrollPane chatScroll = new ScrollPane();
|
||||
|
||||
chatScroll.add(this.chatWindow);
|
||||
|
||||
main.add(chatScroll, BorderLayout.CENTER);
|
||||
|
||||
this.getRootPane().setDefaultButton(this.envoyerMessage);
|
||||
|
||||
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
|
||||
|
||||
this.setSize(600,600);
|
||||
this.setVisible(true);
|
||||
|
||||
}
|
||||
|
@ -107,4 +90,16 @@ public class VueSession extends Vue {
|
|||
return this.envoyerMessage;
|
||||
}
|
||||
|
||||
protected JTextField getZoneSaisie() {
|
||||
return this.chatInput;
|
||||
}
|
||||
|
||||
protected void resetZoneSaisie() {
|
||||
this.chatInput.setText("");
|
||||
}
|
||||
|
||||
protected void appendMessage(String message) {
|
||||
this.chatWindow.append(message);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,70 +0,0 @@
|
|||
package main;
|
||||
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.EventQueue;
|
||||
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.border.EmptyBorder;
|
||||
import java.awt.FlowLayout;
|
||||
import javax.swing.JTextField;
|
||||
import javax.swing.JButton;
|
||||
import java.awt.ScrollPane;
|
||||
import javax.swing.JTextPane;
|
||||
import javax.swing.JTabbedPane;
|
||||
|
||||
public class VueTest extends JFrame {
|
||||
|
||||
private JPanel contentPane;
|
||||
private JTextField txtEntrezUnMessage;
|
||||
|
||||
/**
|
||||
* Launch the application.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
EventQueue.invokeLater(new Runnable() {
|
||||
public void run() {
|
||||
try {
|
||||
VueTest frame = new VueTest();
|
||||
frame.setVisible(true);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the frame.
|
||||
*/
|
||||
public VueTest() {
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
setBounds(100, 100, 600, 600);
|
||||
contentPane = new JPanel();
|
||||
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
|
||||
setContentPane(contentPane);
|
||||
contentPane.setLayout(new BorderLayout(0, 0));
|
||||
|
||||
JPanel panel = new JPanel();
|
||||
contentPane.add(panel, BorderLayout.SOUTH);
|
||||
panel.setLayout(new BorderLayout(0, 0));
|
||||
|
||||
txtEntrezUnMessage = new JTextField();
|
||||
txtEntrezUnMessage.setText("Entrez un message");
|
||||
//textField.setPreferredSize(new Dimension(300, 50));
|
||||
panel.add(txtEntrezUnMessage);
|
||||
txtEntrezUnMessage.setColumns(10);
|
||||
|
||||
JButton btnNewButton = new JButton("Envoyer");
|
||||
panel.add(btnNewButton, BorderLayout.EAST);
|
||||
|
||||
JTextPane textPane = new JTextPane();
|
||||
|
||||
ScrollPane scrollPane = new ScrollPane();
|
||||
contentPane.add(scrollPane, BorderLayout.CENTER);
|
||||
|
||||
scrollPane.add(textPane);
|
||||
}
|
||||
|
||||
}
|
8
POO/src/messages/MauvaisTypeMessageException.java
Normal file
8
POO/src/messages/MauvaisTypeMessageException.java
Normal file
|
@ -0,0 +1,8 @@
|
|||
package messages;
|
||||
|
||||
public class MauvaisTypeMessageException extends Exception {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
|
||||
}
|
69
POO/src/messages/Message.java
Normal file
69
POO/src/messages/Message.java
Normal file
|
@ -0,0 +1,69 @@
|
|||
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;
|
||||
}
|
||||
|
||||
protected abstract String attributsToString();
|
||||
|
||||
public String toString() {
|
||||
return this.type+"###"+this.attributsToString();
|
||||
}
|
||||
|
||||
public static Message stringToMessage(String messageString) {
|
||||
try {
|
||||
String[] parts = messageString.split("###");
|
||||
switch (parts[0]) {
|
||||
case "JE_SUIS_CONNECTE" :
|
||||
return new MessageSysteme(TypeMessage.JE_SUIS_CONNECTE);
|
||||
|
||||
case "JE_SUIS_DECONNECTE" :
|
||||
return new MessageSysteme(TypeMessage.JE_SUIS_DECONNECTE);
|
||||
|
||||
case "INFO_PSEUDO" :
|
||||
return new MessageSysteme(TypeMessage.INFO_PSEUDO, parts[1], parts[2]);
|
||||
|
||||
case "TEXTE" :
|
||||
return new MessageTexte(TypeMessage.TEXTE, parts[1]);
|
||||
|
||||
case "IMAGE" :
|
||||
return new MessageFichier(TypeMessage.IMAGE, parts[1], parts[2]);
|
||||
|
||||
case "FICHIER" :
|
||||
return new MessageFichier(TypeMessage.FICHIER, parts[1], parts[2]);
|
||||
}
|
||||
} catch (MauvaisTypeMessageException e) {}
|
||||
return null;
|
||||
}
|
||||
|
||||
//tests ici
|
||||
public static void main(String[] args) throws MauvaisTypeMessageException {
|
||||
Message m1 = new MessageSysteme(TypeMessage.JE_SUIS_CONNECTE);
|
||||
Message m2 = new MessageSysteme(TypeMessage.JE_SUIS_DECONNECTE);
|
||||
Message m3 = new MessageSysteme(TypeMessage.INFO_PSEUDO, "pseudo156434518", "id236");
|
||||
Message m4 = new MessageTexte(TypeMessage.TEXTE, "blablabla");
|
||||
Message m5 = new MessageFichier(TypeMessage.FICHIER, "truc", ".pdf");
|
||||
|
||||
|
||||
System.out.println(Message.stringToMessage(m1.toString()));
|
||||
System.out.println(Message.stringToMessage(m2.toString()));
|
||||
System.out.println(Message.stringToMessage(m3.toString()));
|
||||
System.out.println(Message.stringToMessage(m4.toString()));
|
||||
System.out.println(Message.stringToMessage(m5.toString()));
|
||||
|
||||
}
|
||||
|
||||
}
|
33
POO/src/messages/MessageFichier.java
Normal file
33
POO/src/messages/MessageFichier.java
Normal file
|
@ -0,0 +1,33 @@
|
|||
package messages;
|
||||
|
||||
import messages.Message.TypeMessage;
|
||||
|
||||
public class MessageFichier extends Message {
|
||||
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
private String contenu;
|
||||
private String extension;
|
||||
|
||||
public MessageFichier(TypeMessage type, String contenu, String extension) throws MauvaisTypeMessageException{
|
||||
if ((type==TypeMessage.IMAGE)||(type==TypeMessage.FICHIER)) {
|
||||
this.type=type;
|
||||
this.contenu=contenu;
|
||||
this.extension=extension;
|
||||
}
|
||||
else throw new MauvaisTypeMessageException();
|
||||
}
|
||||
|
||||
public String getContenu() {
|
||||
return this.contenu;
|
||||
}
|
||||
|
||||
public String getExtension() {
|
||||
return this.extension;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String attributsToString() {
|
||||
return this.contenu+"###"+this.extension;
|
||||
}
|
||||
}
|
39
POO/src/messages/MessageSysteme.java
Normal file
39
POO/src/messages/MessageSysteme.java
Normal file
|
@ -0,0 +1,39 @@
|
|||
package messages;
|
||||
|
||||
public class MessageSysteme extends Message {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
private String pseudo;
|
||||
private String id;
|
||||
|
||||
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="";
|
||||
}
|
||||
else throw new MauvaisTypeMessageException();
|
||||
}
|
||||
|
||||
public MessageSysteme(TypeMessage type, String pseudo, String id) throws MauvaisTypeMessageException {
|
||||
if (type==TypeMessage.INFO_PSEUDO) {
|
||||
this.type=type;
|
||||
this.pseudo=pseudo;
|
||||
this.id=id;
|
||||
}
|
||||
else throw new MauvaisTypeMessageException();
|
||||
}
|
||||
|
||||
public String getPseudo() {
|
||||
return this.pseudo;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String attributsToString() {
|
||||
return this.pseudo+"###"+this.id;
|
||||
}
|
||||
}
|
27
POO/src/messages/MessageTexte.java
Normal file
27
POO/src/messages/MessageTexte.java
Normal file
|
@ -0,0 +1,27 @@
|
|||
package messages;
|
||||
|
||||
import messages.Message.TypeMessage;
|
||||
|
||||
public class MessageTexte extends Message {
|
||||
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
private String contenu;
|
||||
|
||||
public MessageTexte(TypeMessage type, String contenu) throws MauvaisTypeMessageException{
|
||||
if (type==TypeMessage.TEXTE) {
|
||||
this.type=type;
|
||||
this.contenu=contenu;
|
||||
}
|
||||
else throw new MauvaisTypeMessageException();
|
||||
}
|
||||
|
||||
public String getContenu() {
|
||||
return this.contenu;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String attributsToString() {
|
||||
return this.contenu;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue