Histoirque ok + fichiers de config server + inscription/connexion
This commit is contained in:
부모
5222751b1f
커밋
9af289bc55
112개의 변경된 파일과 1154개의 추가작업 그리고 487개의 파일을 삭제
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -1,213 +0,0 @@
|
|||
package controller;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
import java.net.SocketException;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import model.*;
|
||||
import network.*;
|
||||
|
||||
public class Agent {
|
||||
private Contact user;
|
||||
private UDPInput BIn;
|
||||
private UDPOutput BOut;
|
||||
//private TCPChat TCPSend;
|
||||
private TCPServer server;
|
||||
private ListeContacts list;
|
||||
private InetAddress localAddress;
|
||||
private InetAddress broadcast;
|
||||
private ArrayList<TCPChat> listTCP;
|
||||
private DataBase db;
|
||||
|
||||
|
||||
|
||||
public Agent(InetAddress address, int portIn, int portOut) throws IOException {
|
||||
this.user = new Contact("", address, portIn);
|
||||
this.BIn = new UDPInput(user.getAddress(), portIn);
|
||||
this.BOut = new UDPOutput(user, portOut);
|
||||
this.server = new TCPServer(user.getPort(), user.getAddress());
|
||||
//this.TCPSend = new TCPChat(user);
|
||||
this.list = ListeContacts.getInstance();
|
||||
this.localAddress=Tools.getAdress()[0];
|
||||
this.broadcast= Tools.getAdress()[1];
|
||||
this.db = DataBase.getInstance();
|
||||
this.listTCP = new ArrayList<TCPChat>();
|
||||
BIn.start();
|
||||
}
|
||||
|
||||
|
||||
|
||||
//Envoi sur 5001 et reçoit sur 5000
|
||||
public void testSendBroadcast() throws IOException, InterruptedException {
|
||||
MessagePseudo msg = new MessagePseudo(this.user.getAddress(), Tools.getLocalIp(), user.getPort(), 14000, 1, "Michel");
|
||||
BOut.send(msg, user.getAddress(), user.getPort());
|
||||
Thread.sleep(10);
|
||||
MessagePseudo msgRecu = BIn.getMessage();
|
||||
handleMessage(msgRecu);
|
||||
MessagePseudo msg2 = new MessagePseudo(this.user.getAddress(), Tools.getLocalIp(), user.getPort(), 14000, 1, "Jacques");
|
||||
BOut.send(msg2, user.getAddress(), user.getPort());
|
||||
Thread.sleep(10);
|
||||
MessagePseudo msgRecu2 = BIn.getMessage();
|
||||
handleMessage(msgRecu2);
|
||||
MessagePseudo msg3 = new MessagePseudo(this.user.getAddress(), Tools.getLocalIp(), user.getPort(), 14000, 1, "Bob");
|
||||
BOut.send(msg3, user.getAddress(), user.getPort());
|
||||
Thread.sleep(10);
|
||||
MessagePseudo msgRecu3 = BIn.getMessage();
|
||||
handleMessage(msgRecu3);
|
||||
choisirPseudo("Bob");
|
||||
choisirPseudo("Patrick");
|
||||
}
|
||||
|
||||
|
||||
/*public void testSendTCP() throws IOException, InterruptedException {
|
||||
|
||||
TCPSend.sendMsg("COUCOU");
|
||||
Thread.sleep(1000);
|
||||
TCPSend.sendMsg("Ca va ?");
|
||||
Thread.sleep(1000);
|
||||
TCPSend.sendMsg("Non je suis KO");
|
||||
Thread.sleep(1000);
|
||||
TCPSend.sendMsg("Moi aussi :'(");
|
||||
Thread.sleep(1000);
|
||||
|
||||
}*/
|
||||
|
||||
/*type 0 : premier message de broadcast
|
||||
* type, 1 : deuxieme message de broadcast avec pseudo
|
||||
* type 2 : Message de déconnexion
|
||||
*/
|
||||
|
||||
public void handleMessage(Message msg) throws IOException {
|
||||
switch(msg.getTypeMessage()) {
|
||||
case 0 :
|
||||
MessagePseudo msgPresentation = new MessagePseudo(this.localAddress, msg.getAddressSrc(), this.user.getPort(), msg.getPortSrc(), 1, this.user.getPseudo());
|
||||
BOut.send(msgPresentation, msg.getAddressSrc(), msg.getPortSrc());
|
||||
|
||||
case 1 :
|
||||
MessagePseudo msgPseudo = (MessagePseudo) msg;
|
||||
if(!list.pseudoExist(msgPseudo.getPseudo())) {
|
||||
Contact newUser = new Contact(msgPseudo.getPseudo(), msgPseudo.getAddressSrc(), msgPseudo.getPortSrc());
|
||||
list.addContact(newUser);
|
||||
}
|
||||
/*case 2 :
|
||||
MessageDeconnexion messageDeconnexion = (MessageDeconnexion) msg;
|
||||
if(list.pseudoExist(messageDeconnexion.getPseudo())){
|
||||
Contact contact = list.findContact(messageDeconnexion.getPseudo());
|
||||
list.deleteContact(contact);
|
||||
}*/
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public boolean connect(String pseudo) throws IOException, InterruptedException {
|
||||
MessagePseudo msg_connexion = new MessagePseudo(this.localAddress, this.broadcast, this.user.getPort(), 25000, 0, "");
|
||||
BOut.send(msg_connexion, this.broadcast, 25000);
|
||||
Thread.sleep(5000);
|
||||
ArrayList<MessagePseudo> msg_recus = BIn.getListMessage();
|
||||
for(int i =0; i<msg_recus.size(); i++) {
|
||||
handleMessage(msg_recus.get(i));
|
||||
}
|
||||
boolean pseudoOK = choisirPseudo(pseudo);
|
||||
list.addContact(user);
|
||||
if(pseudoOK){
|
||||
int id = db.addUser(pseudo);
|
||||
this.user.setId(id);
|
||||
System.out.println("Connexion réussie avec le pseudo : "+pseudo);
|
||||
}else {
|
||||
System.out.println("Echec de la connexion, veuillez choisir un autre pseudo");
|
||||
}
|
||||
return pseudoOK;
|
||||
}
|
||||
|
||||
public boolean choisirPseudo(String pseudo) {
|
||||
if(!list.pseudoExist(pseudo)){
|
||||
user.setPseudo(pseudo);
|
||||
System.out.println("Pseudo set : "+pseudo);
|
||||
return true;
|
||||
}else {
|
||||
System.out.println("Pseudo déjà utilisé");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public void deconnexion() throws IOException {
|
||||
MessageDeconnexion msgDeconnexion = new MessageDeconnexion(this.localAddress, this.broadcast, this.user.getPort(), 25000, 3, user.getPseudo());
|
||||
BOut.send(msgDeconnexion, this.broadcast, this.user.getPort());
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
public boolean changerPseudo(String pseudo) {
|
||||
boolean changeOK = choisirPseudo(pseudo);
|
||||
if(changeOK){
|
||||
System.out.println("Pseudo changé à : "+pseudo);
|
||||
db.updatePseudo(this.user.getId(), pseudo);
|
||||
return true;
|
||||
}else {
|
||||
System.out.println("Veuillez choisir un autre pseudo");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean chatExists(int destID) {
|
||||
boolean chatExists = false;
|
||||
for(TCPChat tcp : listTCP) {
|
||||
if(tcp.getDestID()==destID) {
|
||||
chatExists = true;
|
||||
}
|
||||
}
|
||||
return chatExists;
|
||||
}
|
||||
|
||||
public void createChat (int destId, String destPseudo) throws IOException {
|
||||
boolean chatExists = chatExists(destId);
|
||||
if(!chatExists) {
|
||||
for(Contact user : list.getListe()) {
|
||||
if(user.getPseudo().equals(destPseudo)) {
|
||||
TCPChat connexionTCP = new TCPChat(user, destId);
|
||||
listTCP.add(connexionTCP);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public Contact getUser() {
|
||||
return user;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void setUser(Contact user) {
|
||||
this.user = user;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public ListeContacts getList() {
|
||||
return list;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void setList(ListeContacts list) {
|
||||
this.list = list;
|
||||
}
|
||||
|
||||
|
||||
public DataBase getDb() {
|
||||
return db;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void setDb(DataBase db) {
|
||||
this.db = db;
|
||||
}
|
||||
|
||||
|
||||
public ArrayList<TCPChat> getListeTCPChat(){
|
||||
return listTCP;
|
||||
}
|
||||
}
|
|
@ -1,15 +0,0 @@
|
|||
package controller;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import network.Tools;
|
||||
|
||||
public class Test {
|
||||
public static void main(String args[]) throws IOException, InterruptedException {
|
||||
Agent agent1 = new Agent(Tools.getAdress()[0], 22001, 22000);
|
||||
int id = agent1.getDb().getIdFromPseudo(agent1.getUser().getPseudo());
|
||||
System.out.println("ID : "+id);
|
||||
|
||||
//Tools.printInterfaces();
|
||||
}
|
||||
}
|
|
@ -1,54 +0,0 @@
|
|||
package gui;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.swing.DefaultListModel;
|
||||
import javax.swing.JComboBox;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JList;
|
||||
import javax.swing.JTabbedPane;
|
||||
|
||||
import model.Contact;
|
||||
import model.ListeContacts;
|
||||
|
||||
public class FenetreAccueil extends JFrame{
|
||||
private static final long serialVersionUID = 1L;
|
||||
private JList<String> contacts;
|
||||
private JTabbedPane tab;
|
||||
private JComboBox<String> status;
|
||||
public static DefaultListModel<ListeContacts> listeUtilisateurs = new DefaultListModel<ListeContacts>();
|
||||
private Contact user;
|
||||
|
||||
|
||||
public FenetreAccueil(Contact c) {
|
||||
this.user = c;
|
||||
init();
|
||||
}
|
||||
|
||||
|
||||
public void init(){
|
||||
setTitle("Application Chat");
|
||||
setSize(300, 550);
|
||||
|
||||
try {
|
||||
= new ListeContacts();
|
||||
if(listeContacts.length()){
|
||||
contacts = new JList<String>(listeContacts);
|
||||
//discussion = new JList<String>(liste2);
|
||||
//nbRoom=liste2.length;
|
||||
}else{
|
||||
contacts = new JList<String>();
|
||||
//discussion = new JList<String>();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
|
||||
}
|
||||
|
||||
tab = new JTabbedPane(JTabbedPane.BOTTOM);
|
||||
status = new JComboBox<String>(new String[]{
|
||||
"connecté",
|
||||
"déconnecté"
|
||||
});
|
||||
}
|
||||
|
||||
}
|
|
@ -1,87 +0,0 @@
|
|||
package gui;
|
||||
|
||||
import java.awt.EventQueue;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
|
||||
import javax.swing.*;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class FenetreLogin extends JFrame{
|
||||
|
||||
private JPanel contentPane;
|
||||
/**
|
||||
* Launch the application.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
EventQueue.invokeLater(new Runnable() {
|
||||
public void run() {
|
||||
try {
|
||||
FenetreLogin window = new FenetreLogin();
|
||||
window.setVisible(true);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the application.
|
||||
*/
|
||||
public FenetreLogin() {
|
||||
initialize();
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the contents of the frame.
|
||||
*/
|
||||
private void initialize() {
|
||||
contentPane = new JPanel(new GridBagLayout());
|
||||
setContentPane(contentPane);
|
||||
setTitle("Login Frame");
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
setSize(597, 412);
|
||||
JButton connect = new JButton("Connect");
|
||||
connect.addActionListener(new ActionListener(){
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
FenetreChat windowChat = new FenetreChat();
|
||||
windowChat.setVisible(true);
|
||||
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
|
||||
|
||||
}
|
||||
});
|
||||
//Listen to events from the Convert button.
|
||||
// convertTemp.addActionListener(this);
|
||||
|
||||
//Add the widgets to the container.
|
||||
/*converterPanel.add(tempCelsius);
|
||||
converterPanel.add(celsiusLabel);
|
||||
converterPanel.add(convertTemp);
|
||||
converterPanel.add(fahrenheitLabel);
|
||||
*/
|
||||
|
||||
GridBagConstraints gbcConnect = new GridBagConstraints();
|
||||
gbcConnect.insets = new Insets(0, 0, 5, 0);
|
||||
gbcConnect.gridx = 4;
|
||||
gbcConnect.gridy = 0;
|
||||
|
||||
JTextField pseudo = new JTextField(10);
|
||||
|
||||
GridBagConstraints gbcPseudo = new GridBagConstraints();
|
||||
gbcPseudo.insets = new Insets(0, 0, 5, 5);
|
||||
|
||||
gbcPseudo.gridx = 0;
|
||||
gbcPseudo.gridy = 0;
|
||||
gbcPseudo.ipadx = 75;
|
||||
gbcPseudo.ipady = 75;
|
||||
gbcPseudo.gridwidth = 4;
|
||||
|
||||
contentPane.add(pseudo, gbcPseudo);
|
||||
contentPane.add(connect, gbcConnect);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,31 +0,0 @@
|
|||
package gui;
|
||||
|
||||
import java.awt.Component;
|
||||
|
||||
public class Onglet extends javax.swing.JTabbedPane {
|
||||
private static final long serialVersionUID = 1365462L;
|
||||
|
||||
Onglet me;
|
||||
|
||||
public Onglet() {
|
||||
super();
|
||||
me = this;
|
||||
}
|
||||
|
||||
public void addTab(String title, Component component,int endroit) {
|
||||
super.addTab(title, component); //on ajoute une Tab à JTabbedPane
|
||||
super.setTabComponentAt(endroit, new CloseTabPanel(title)); //on applique le closeTabPanel a l'element "endroit"
|
||||
}
|
||||
|
||||
//fonction qui permet d'afficher le bouton close
|
||||
public void afficheIconAt(int endroit){
|
||||
((CloseTabPanel)moi.getTabComponentAt(endroit)).afficheIcon(true);
|
||||
}
|
||||
//fonction qui permet d'enlever le bouton close
|
||||
public void cacheIconAt(int endroit){
|
||||
((CloseTabPanel)moi.getTabComponentAt(endroit)).afficheIcon(false);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -1,25 +0,0 @@
|
|||
package gui;
|
||||
|
||||
import javax.swing.JTextArea;
|
||||
|
||||
public class Room extends JTextArea{
|
||||
private String nom;
|
||||
|
||||
public Room(){
|
||||
super(25, 20);
|
||||
this.nom ="room";
|
||||
}
|
||||
public Room(String nom){
|
||||
super(25, 20);
|
||||
this.nom= nom;
|
||||
}
|
||||
|
||||
public String getNom() {
|
||||
return nom;
|
||||
}
|
||||
public void setNom(String nom) {
|
||||
this.nom = nom;
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -11,5 +11,10 @@
|
|||
<attribute name="module" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="lib" path="C:/Users/momof/OneDrive/Documents/INSA/4A/poo/java-json.jar">
|
||||
<attributes>
|
||||
<attribute name="module" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
BIN
chatGit/bin/config/ConfLoad$ConfLoadCb.class
Normal file
BIN
chatGit/bin/config/ConfLoad$ConfLoadCb.class
Normal file
Binary file not shown.
BIN
chatGit/bin/config/ConfLoad.class
Normal file
BIN
chatGit/bin/config/ConfLoad.class
Normal file
Binary file not shown.
BIN
chatGit/bin/config/Configuration$Conf.class
Normal file
BIN
chatGit/bin/config/Configuration$Conf.class
Normal file
Binary file not shown.
BIN
chatGit/bin/config/Configuration$SConf.class
Normal file
BIN
chatGit/bin/config/Configuration$SConf.class
Normal file
Binary file not shown.
BIN
chatGit/bin/config/Configuration.class
Normal file
BIN
chatGit/bin/config/Configuration.class
Normal file
Binary file not shown.
BIN
chatGit/bin/config/package-info.class
Normal file
BIN
chatGit/bin/config/package-info.class
Normal file
Binary file not shown.
BIN
chatGit/bin/controller/Agent.class
Normal file
BIN
chatGit/bin/controller/Agent.class
Normal file
Binary file not shown.
BIN
chatGit/bin/controller/DataBase.class
Normal file
BIN
chatGit/bin/controller/DataBase.class
Normal file
Binary file not shown.
BIN
chatGit/bin/controller/Test.class
Normal file
BIN
chatGit/bin/controller/Test.class
Normal file
Binary file not shown.
Binary file not shown.
BIN
chatGit/bin/gui/FenetreChat$2.class
Normal file
BIN
chatGit/bin/gui/FenetreChat$2.class
Normal file
Binary file not shown.
BIN
chatGit/bin/gui/FenetreChat.class
Normal file
BIN
chatGit/bin/gui/FenetreChat.class
Normal file
Binary file not shown.
BIN
chatGit/bin/gui/FenetreConnexion$1.class
Normal file
BIN
chatGit/bin/gui/FenetreConnexion$1.class
Normal file
Binary file not shown.
BIN
chatGit/bin/gui/FenetreConnexion.class
Normal file
BIN
chatGit/bin/gui/FenetreConnexion.class
Normal file
Binary file not shown.
BIN
chatGit/bin/gui/FenetreInscription$1.class
Normal file
BIN
chatGit/bin/gui/FenetreInscription$1.class
Normal file
Binary file not shown.
BIN
chatGit/bin/gui/FenetreInscription.class
Normal file
BIN
chatGit/bin/gui/FenetreInscription.class
Normal file
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
chatGit/bin/gui/FenetreMenu$3$1.class
Normal file
BIN
chatGit/bin/gui/FenetreMenu$3$1.class
Normal file
Binary file not shown.
BIN
chatGit/bin/gui/FenetreMenu$3.class
Normal file
BIN
chatGit/bin/gui/FenetreMenu$3.class
Normal file
Binary file not shown.
BIN
chatGit/bin/gui/FenetreMenu$4$1.class
Normal file
BIN
chatGit/bin/gui/FenetreMenu$4$1.class
Normal file
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Before Width: | Height: | 크기: 5.8 KiB After Width: | Height: | 크기: 5.8 KiB |
Before Width: | Height: | 크기: 5.8 KiB After Width: | Height: | 크기: 5.8 KiB |
Binary file not shown.
BIN
chatGit/bin/model/MessageAffichage.class
Normal file
BIN
chatGit/bin/model/MessageAffichage.class
Normal file
Binary file not shown.
BIN
chatGit/bin/module-info.class
Normal file
BIN
chatGit/bin/module-info.class
Normal file
Binary file not shown.
BIN
chatGit/bin/network/TCPChat.class
Normal file
BIN
chatGit/bin/network/TCPChat.class
Normal file
Binary file not shown.
BIN
chatGit/bin/network/TCPServer.class
Normal file
BIN
chatGit/bin/network/TCPServer.class
Normal file
Binary file not shown.
BIN
chatGit/bin/network/Tools.class
Normal file
BIN
chatGit/bin/network/Tools.class
Normal file
Binary file not shown.
BIN
chatGit/bin/network/UDPInput.class
Normal file
BIN
chatGit/bin/network/UDPInput.class
Normal file
Binary file not shown.
BIN
chatGit/bin/server/Server.class
Normal file
BIN
chatGit/bin/server/Server.class
Normal file
Binary file not shown.
BIN
chatGit/bin/server/package-info.class
Normal file
BIN
chatGit/bin/server/package-info.class
Normal file
Binary file not shown.
BIN
chatGit/bin/test/App$1.class
Normal file
BIN
chatGit/bin/test/App$1.class
Normal file
Binary file not shown.
BIN
chatGit/bin/test/App.class
Normal file
BIN
chatGit/bin/test/App.class
Normal file
Binary file not shown.
BIN
chatGit/bin/test/Launcher.class
Normal file
BIN
chatGit/bin/test/Launcher.class
Normal file
Binary file not shown.
BIN
chatGit/bin/test/package-info.class
Normal file
BIN
chatGit/bin/test/package-info.class
Normal file
Binary file not shown.
12
chatGit/config.json
Normal file
12
chatGit/config.json
Normal file
|
@ -0,0 +1,12 @@
|
|||
{
|
||||
"serveur":{
|
||||
"actif": 1,
|
||||
"url": "test",
|
||||
"port": 30000
|
||||
},
|
||||
"config":{
|
||||
"interface": "wlan3",
|
||||
"portSrc": 26001,
|
||||
"portDest":26000
|
||||
}
|
||||
}
|
39
chatGit/src/config/ConfLoad.java
Normal file
39
chatGit/src/config/ConfLoad.java
Normal file
|
@ -0,0 +1,39 @@
|
|||
package config;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import org.json.JSONTokener;
|
||||
|
||||
public class ConfLoad {
|
||||
|
||||
public static final String PATH ="config.json";
|
||||
|
||||
public static Configuration load() {
|
||||
Configuration conf;
|
||||
try {
|
||||
InputStream is = new FileInputStream(PATH);
|
||||
JSONTokener tokener = new JSONTokener(is);
|
||||
JSONObject obj = new JSONObject(tokener);
|
||||
conf = new Configuration(obj);
|
||||
return conf;
|
||||
} catch (FileNotFoundException e) {
|
||||
System.out.println("No config file found");
|
||||
conf = new Configuration();
|
||||
return conf;
|
||||
} catch (JSONException e) {
|
||||
System.out.println("Could not read config file. Please make sure it is a valid JSON.");
|
||||
conf = new Configuration();
|
||||
return conf;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public interface ConfLoadCb {
|
||||
void onLoaded(Configuration config);
|
||||
}
|
||||
|
||||
}
|
170
chatGit/src/config/Configuration.java
Normal file
170
chatGit/src/config/Configuration.java
Normal file
|
@ -0,0 +1,170 @@
|
|||
package config;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
public class Configuration {
|
||||
|
||||
|
||||
|
||||
private SConf sConf;
|
||||
private Conf conf;
|
||||
|
||||
|
||||
public Configuration() {
|
||||
this.sConf = new SConf();
|
||||
this.conf = new Conf();
|
||||
}
|
||||
|
||||
public Configuration(JSONObject obj) throws JSONException {
|
||||
this();
|
||||
if(obj.has("serveur")) {
|
||||
this.sConf = new SConf(obj.getJSONObject("serveur"));
|
||||
if(obj.has("config")) {
|
||||
this.conf = new Conf(obj.getJSONObject("config"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class SConf {
|
||||
private JSONObject obj;
|
||||
|
||||
private boolean ok;
|
||||
private String url;
|
||||
private int portServer;
|
||||
|
||||
private final String OK = "actif";
|
||||
private final String URL = "url";
|
||||
private final String PORT = "port";
|
||||
|
||||
|
||||
public SConf() {
|
||||
this.ok = false;
|
||||
this.url= "";
|
||||
}
|
||||
|
||||
public SConf (JSONObject obj) {
|
||||
this();
|
||||
this.obj = obj;
|
||||
checkServerOk();
|
||||
if(this.ok) {
|
||||
checkServerUrl();
|
||||
if(this.url.isBlank()) {
|
||||
this.ok = false;
|
||||
}else {
|
||||
checkServerPort();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void checkServerOk() {
|
||||
try {
|
||||
this.ok = obj.getInt(OK) == 1;
|
||||
}catch(JSONException e) {
|
||||
this.ok = false;
|
||||
}
|
||||
}
|
||||
|
||||
private void checkServerUrl() {
|
||||
try {
|
||||
this.url = obj.getString(URL);
|
||||
}catch(JSONException e) {
|
||||
this.url = "";
|
||||
}
|
||||
}
|
||||
|
||||
private void checkServerPort() {
|
||||
try {
|
||||
this.portServer = obj.getInt(PORT);
|
||||
}catch(JSONException e) {
|
||||
this.portServer = 30000; //port 30000 set par defaut
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isOK() {
|
||||
return this.ok;
|
||||
}
|
||||
|
||||
public String getUrl() {
|
||||
return this.url;
|
||||
}
|
||||
|
||||
public int getPortServer() {
|
||||
return this.portServer;
|
||||
}
|
||||
}
|
||||
|
||||
public static class Conf{
|
||||
|
||||
private JSONObject obj;
|
||||
|
||||
private String interfaceRzo;
|
||||
private int srcPort;
|
||||
private int destPort;
|
||||
|
||||
private final String INTERFACE = "interface";
|
||||
private final String SRC_PORT = "portSrc";
|
||||
private final String DEST_PORT = "portDest";
|
||||
|
||||
public Conf() {
|
||||
this.interfaceRzo = "";
|
||||
this.srcPort = 25000;
|
||||
this.destPort = 25001;
|
||||
}
|
||||
|
||||
public Conf(JSONObject obj) {
|
||||
this();
|
||||
this.obj = obj;
|
||||
checkInterfaceRzo();
|
||||
checkSrcPort();
|
||||
checkDestPort();
|
||||
|
||||
}
|
||||
|
||||
private void checkInterfaceRzo() {
|
||||
try {
|
||||
this.interfaceRzo = obj.getString(INTERFACE);
|
||||
}catch(JSONException e) {
|
||||
this.interfaceRzo = "";
|
||||
}
|
||||
}
|
||||
|
||||
private void checkSrcPort() {
|
||||
try {
|
||||
this.srcPort = obj.getInt(SRC_PORT);
|
||||
}catch(JSONException e) {
|
||||
this.srcPort = 25000; //port 25000 set par defaut pour port src
|
||||
}
|
||||
}
|
||||
|
||||
private void checkDestPort() {
|
||||
try {
|
||||
this.destPort = obj.getInt(DEST_PORT);
|
||||
}catch(JSONException e) {
|
||||
this.destPort = 25001; //port 25001 set par defaut pour port dest
|
||||
}
|
||||
}
|
||||
|
||||
public String getInterface() {
|
||||
return this.interfaceRzo;
|
||||
}
|
||||
|
||||
public int getPortSrc() {
|
||||
return this.srcPort;
|
||||
}
|
||||
|
||||
public int getDestPort() {
|
||||
return this.destPort;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public Conf getConf() {
|
||||
return this.conf;
|
||||
}
|
||||
|
||||
|
||||
public SConf getsConf() {
|
||||
return this.sConf;
|
||||
}
|
||||
}
|
1
chatGit/src/config/package-info.java
Normal file
1
chatGit/src/config/package-info.java
Normal file
|
@ -0,0 +1 @@
|
|||
package config;
|
357
chatGit/src/controller/Agent.java
Normal file
357
chatGit/src/controller/Agent.java
Normal file
|
@ -0,0 +1,357 @@
|
|||
package controller;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.beans.PropertyChangeEvent;
|
||||
import java.beans.PropertyChangeListener;
|
||||
import java.beans.PropertyChangeSupport;
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
import java.net.SocketException;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import javax.swing.text.BadLocationException;
|
||||
import javax.swing.text.SimpleAttributeSet;
|
||||
import javax.swing.text.StyleConstants;
|
||||
import javax.swing.text.StyledDocument;
|
||||
|
||||
import config.Configuration;
|
||||
import gui.FenetreChat;
|
||||
import model.*;
|
||||
import network.*;
|
||||
import server.Server;
|
||||
import test.App;
|
||||
|
||||
public class Agent implements PropertyChangeListener{
|
||||
private Contact user;
|
||||
private UDPInput BIn;
|
||||
private UDPOutput BOut;
|
||||
//private TCPChat TCPSend;
|
||||
private TCPServer server;
|
||||
private ListeContacts list;
|
||||
private InetAddress localAddress;
|
||||
private InetAddress broadcast;
|
||||
private ArrayList<TCPChat> listTCPOk;
|
||||
private ArrayList<TCPChat> listTCPDeg;
|
||||
private DataBase db;
|
||||
|
||||
private Server serverPresence;
|
||||
|
||||
private PropertyChangeSupport support;
|
||||
|
||||
|
||||
|
||||
public Agent(InetAddress address, int portIn, int portOut) throws IOException {
|
||||
this.user = new Contact("", address, portIn);
|
||||
this.BIn = new UDPInput(user.getAddress(), portIn);
|
||||
this.BOut = new UDPOutput(user, portOut);
|
||||
this.server = new TCPServer(user.getPort(), user.getAddress());
|
||||
this.server.addPropertyChangeListener(this);
|
||||
//this.TCPSend = new TCPChat(user);
|
||||
this.list = ListeContacts.getInstance();
|
||||
this.localAddress=Tools.getAdress()[0];
|
||||
this.broadcast= Tools.getAdress()[1];
|
||||
this.db = DataBase.getInstance();
|
||||
this.listTCPOk = new ArrayList<TCPChat>();
|
||||
this.listTCPDeg = new ArrayList<TCPChat>();
|
||||
this.support = new PropertyChangeSupport(this);
|
||||
this.BIn.addPropertyChangeListener(this);
|
||||
BIn.start();
|
||||
}
|
||||
|
||||
//user ayant déjà fait une connexion
|
||||
public Agent(InetAddress address, int portIn, int portOut, String login) throws IOException {
|
||||
this.db = DataBase.getInstance();
|
||||
this.user = new Contact(db.getPseudoFromLogin(login), address, portIn);
|
||||
this.BIn = new UDPInput(user.getAddress(), portIn);
|
||||
this.BOut = new UDPOutput(user, portOut);
|
||||
this.server = new TCPServer(user.getPort(), user.getAddress());
|
||||
this.server.addPropertyChangeListener(this);
|
||||
//this.TCPSend = new TCPChat(user);
|
||||
this.list = ListeContacts.getInstance();
|
||||
this.localAddress=Tools.getAdress()[0];
|
||||
this.broadcast= Tools.getAdress()[1];
|
||||
this.listTCPOk = new ArrayList<TCPChat>();
|
||||
this.listTCPDeg = new ArrayList<TCPChat>();
|
||||
this.support = new PropertyChangeSupport(this);
|
||||
this.BIn.addPropertyChangeListener(this);
|
||||
BIn.start();
|
||||
}
|
||||
|
||||
|
||||
public void propertyChange(PropertyChangeEvent evt) {
|
||||
switch (evt.getPropertyName()) {
|
||||
case "Msg UDP Recu" :
|
||||
//System.out.println("envoi message");
|
||||
Message msg = BIn.getMessage();
|
||||
try {
|
||||
handleMessage(msg);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
break;
|
||||
case "session chat demarree" :
|
||||
//System.out.println("dans session demarree");
|
||||
TCPChat tcpChat = this.server.getConnexionTCPChat();
|
||||
tcpChat.addPropertyChangeListener(this);
|
||||
this.listTCPOk.add(tcpChat);
|
||||
break;
|
||||
|
||||
case "Msg TCP Recu" :
|
||||
SimpleAttributeSet left = new SimpleAttributeSet();
|
||||
StyleConstants.setAlignment(left, StyleConstants.ALIGN_LEFT);
|
||||
StyleConstants.setForeground(left, Color.RED);
|
||||
//System.out.println("dans property change");
|
||||
TCPChat tcpChatCo = this.listTCPOk.get(0);
|
||||
MessageChat msgRecu = tcpChatCo.getMessageRecu();
|
||||
//FenetreChat.getTa().append("kevin"+ " : "+msgRecu.getMessage()+"\n");
|
||||
StyledDocument doc = FenetreChat.getDoc();
|
||||
try {
|
||||
doc.insertString(doc.getLength(), "\n"+msgRecu.getMessage(), left );
|
||||
} catch (BadLocationException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
doc.setParagraphAttributes(doc.getLength(), 1, left, false);
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
//Envoi sur 5001 et reçoit sur 5000
|
||||
public void testSendBroadcast() throws IOException, InterruptedException {
|
||||
MessagePseudo msg = new MessagePseudo(this.user.getAddress(), Tools.getLocalIp(), user.getPort(), 14000, 1, "Michel");
|
||||
BOut.send(msg, user.getAddress(), user.getPort());
|
||||
Thread.sleep(10);
|
||||
MessagePseudo msgRecu = BIn.getMessage();
|
||||
handleMessage(msgRecu);
|
||||
MessagePseudo msg2 = new MessagePseudo(this.user.getAddress(), Tools.getLocalIp(), user.getPort(), 14000, 1, "Jacques");
|
||||
BOut.send(msg2, user.getAddress(), user.getPort());
|
||||
Thread.sleep(10);
|
||||
MessagePseudo msgRecu2 = BIn.getMessage();
|
||||
handleMessage(msgRecu2);
|
||||
MessagePseudo msg3 = new MessagePseudo(this.user.getAddress(), Tools.getLocalIp(), user.getPort(), 14000, 1, "Bob");
|
||||
BOut.send(msg3, user.getAddress(), user.getPort());
|
||||
Thread.sleep(10);
|
||||
MessagePseudo msgRecu3 = BIn.getMessage();
|
||||
handleMessage(msgRecu3);
|
||||
choisirPseudo("Bob");
|
||||
choisirPseudo("Patrick");
|
||||
}
|
||||
|
||||
|
||||
/*public void testSendTCP() throws IOException, InterruptedException {
|
||||
|
||||
TCPSend.sendMsg("COUCOU");
|
||||
Thread.sleep(1000);
|
||||
TCPSend.sendMsg("Ca va ?");
|
||||
Thread.sleep(1000);
|
||||
TCPSend.sendMsg("Non je suis KO");
|
||||
Thread.sleep(1000);
|
||||
TCPSend.sendMsg("Moi aussi :'(");
|
||||
Thread.sleep(1000);
|
||||
|
||||
}*/
|
||||
|
||||
/*type 0 : premier message de broadcast
|
||||
* type, 1 : deuxieme message de broadcast avec pseudo
|
||||
* type 2 : Message de déconnexion
|
||||
*/
|
||||
|
||||
public void handleMessage(Message msg) throws IOException {
|
||||
switch(msg.getTypeMessage()) {
|
||||
case 0 :
|
||||
//System.out.println("Envoi message présentation");
|
||||
MessagePseudo msgPresentation = new MessagePseudo(this.localAddress, msg.getAddressSrc(), this.user.getPort(), msg.getPortSrc(), 1, this.user.getPseudo());
|
||||
BOut.send(msgPresentation, msg.getAddressSrc(), msg.getPortSrc());
|
||||
break;
|
||||
case 1 :
|
||||
MessagePseudo msgPseudo = (MessagePseudo) msg;
|
||||
if(!list.pseudoExist(msgPseudo.getPseudo())) {
|
||||
Contact newUser = new Contact(msgPseudo.getPseudo(), msgPseudo.getAddressSrc(), msgPseudo.getPortSrc());
|
||||
list.addContact(newUser);
|
||||
}
|
||||
break;
|
||||
/*case 2 :
|
||||
MessageDeconnexion messageDeconnexion = (MessageDeconnexion) msg;
|
||||
if(list.pseudoExist(messageDeconnexion.getPseudo())){
|
||||
Contact contact = list.findContact(messageDeconnexion.getPseudo());
|
||||
list.deleteContact(contact);
|
||||
}*/
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public boolean connectInscription(String pseudo, String login) throws IOException, InterruptedException {
|
||||
MessagePseudo msg_connexion = new MessagePseudo(this.localAddress, this.broadcast, this.user.getPort(), App.portDest, 0, "");
|
||||
BOut.send(msg_connexion, this.broadcast, App.portDest);
|
||||
Thread.sleep(5000);
|
||||
ArrayList<MessagePseudo> msg_recus = BIn.getListMessage();
|
||||
for(int i =0; i<msg_recus.size(); i++) {
|
||||
handleMessage(msg_recus.get(i));
|
||||
}
|
||||
boolean pseudoOK = choisirPseudo(pseudo);
|
||||
list.addContact(user);
|
||||
if(pseudoOK){
|
||||
int id = db.addUser(pseudo, login);
|
||||
this.user.setId(id);
|
||||
MessagePseudo msgPresentation = new MessagePseudo(this.localAddress, this.broadcast, this.user.getPort(), App.portDest, 1, this.user.getPseudo());
|
||||
BOut.send(msgPresentation, this.broadcast, App.portDest);
|
||||
this.user.setStatut("En ligne");
|
||||
//System.out.println("Connexion réussie avec le pseudo : "+pseudo);
|
||||
}else {
|
||||
System.out.println("Echec de la connexion, veuillez choisir un autre pseudo");
|
||||
}
|
||||
return pseudoOK;
|
||||
}
|
||||
|
||||
public boolean connectConnexion(String pseudo, String login) throws IOException, InterruptedException {
|
||||
MessagePseudo msg_connexion = new MessagePseudo(this.localAddress, this.broadcast, this.user.getPort(), App.portDest, 0, "");
|
||||
BOut.send(msg_connexion, this.broadcast, App.portDest);
|
||||
Thread.sleep(5000);
|
||||
ArrayList<MessagePseudo> msg_recus = BIn.getListMessage();
|
||||
for(int i =0; i<msg_recus.size(); i++) {
|
||||
handleMessage(msg_recus.get(i));
|
||||
}
|
||||
boolean pseudoOK = choisirPseudo(pseudo);
|
||||
list.addContact(user);
|
||||
if(pseudoOK){
|
||||
int id = db.getIdFromLogin(login);
|
||||
this.user.setId(id);
|
||||
MessagePseudo msgPresentation = new MessagePseudo(this.localAddress, this.broadcast, this.user.getPort(), App.portDest, 1, this.user.getPseudo());
|
||||
BOut.send(msgPresentation, this.broadcast, App.portDest);
|
||||
this.user.setStatut("En ligne");
|
||||
//System.out.println("Connexion réussie avec le pseudo : "+pseudo);
|
||||
}else {
|
||||
System.out.println("Echec de la connexion, veuillez choisir un autre pseudo");
|
||||
}
|
||||
return pseudoOK;
|
||||
}
|
||||
|
||||
public boolean choisirPseudo(String pseudo) {
|
||||
if(!list.pseudoExist(pseudo)){
|
||||
user.setPseudo(pseudo);
|
||||
//System.out.println("Pseudo set : "+pseudo);
|
||||
return true;
|
||||
}else {
|
||||
System.out.println("Pseudo déjà utilisé");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public void deconnexion() throws IOException {
|
||||
MessageDeconnexion msgDeconnexion = new MessageDeconnexion(this.localAddress, this.broadcast, this.user.getPort(), 25000, 3, user.getPseudo());
|
||||
BOut.send(msgDeconnexion, this.broadcast, this.user.getPort());
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
public boolean changerPseudo(String pseudo) {
|
||||
boolean changeOK = choisirPseudo(pseudo);
|
||||
if(changeOK){
|
||||
//System.out.println("Pseudo changé à : "+pseudo);
|
||||
db.updatePseudo(this.user.getId(), pseudo);
|
||||
return true;
|
||||
}else {
|
||||
//System.out.println("Veuillez choisir un autre pseudo");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean chatExists(int destID) {
|
||||
boolean chatExists = false;
|
||||
for(TCPChat tcp : listTCPDeg) {
|
||||
if(tcp.getDestID()==destID) {
|
||||
chatExists = true;
|
||||
}
|
||||
}
|
||||
return chatExists;
|
||||
}
|
||||
|
||||
public void createChat (int destId, String destPseudo) throws IOException {
|
||||
boolean chatExists = chatExists(destId);
|
||||
if(!chatExists) {
|
||||
for(Contact user : list.getListe()) {
|
||||
if(user.getPseudo().equals(destPseudo)) {
|
||||
TCPChat connexionTCP = new TCPChat(user, destId);
|
||||
listTCPDeg.add(connexionTCP);
|
||||
connexionTCP.addPropertyChangeListener(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void initServer(Configuration conf) {
|
||||
final Configuration.SConf sConf = conf.getsConf();
|
||||
if(sConf.isOK()) {
|
||||
try {
|
||||
final String url = sConf.getUrl();
|
||||
final int port = sConf.getPortServer();
|
||||
this.serverPresence = new Server(url, port);
|
||||
subscribeToServer();
|
||||
}catch(Exception e) {
|
||||
serverPresence = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void subscribeToServer() {
|
||||
if(this.serverPresence != null ) {
|
||||
serverPresence.suscribe();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void addPropertyChangeListener(PropertyChangeListener pcl, String propertyName){
|
||||
this.support.addPropertyChangeListener(propertyName, pcl);
|
||||
}
|
||||
|
||||
public void deletePropertyChangeListener(PropertyChangeListener pcl, String propertyName){
|
||||
this.support.removePropertyChangeListener(propertyName,pcl);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public Contact getUser() {
|
||||
return user;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void setUser(Contact user) {
|
||||
this.user = user;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public ListeContacts getList() {
|
||||
return list;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void setList(ListeContacts list) {
|
||||
this.list = list;
|
||||
}
|
||||
|
||||
|
||||
public DataBase getDb() {
|
||||
return db;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void setDb(DataBase db) {
|
||||
this.db = db;
|
||||
}
|
||||
|
||||
|
||||
public ArrayList<TCPChat> getListeTCPChat(){
|
||||
return listTCPDeg;
|
||||
}
|
||||
|
||||
public TCPChat getTCPChat() {
|
||||
return this.listTCPDeg.remove(0);
|
||||
}
|
||||
}
|
|
@ -1,6 +1,11 @@
|
|||
package controller;
|
||||
|
||||
import java.sql.*;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import model.MessageAffichage;
|
||||
//import model.MessageAffichage;
|
||||
import model.MessageChat;
|
||||
|
||||
public class DataBase {
|
||||
|
||||
|
@ -38,12 +43,14 @@ public class DataBase {
|
|||
}
|
||||
|
||||
//retourne l'id associé au user ajouté
|
||||
public int addUser(String pseudo) {
|
||||
String query = "INSERT INTO users (`pseudo`) VALUES (?)";
|
||||
public int addUser(String pseudo, String login) {
|
||||
String query = "INSERT INTO users (pseudo, statut, login) VALUES (?, ?, ?)";
|
||||
PreparedStatement pStat = null;
|
||||
try {
|
||||
pStat = connexion.prepareStatement(query);
|
||||
pStat.setString(1, pseudo);
|
||||
pStat.setString(2, "En ligne");
|
||||
pStat.setString(3, login);
|
||||
pStat.executeUpdate();
|
||||
}catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
|
@ -82,7 +89,7 @@ public class DataBase {
|
|||
|
||||
public int getIdFromPseudo(String pseudo) {
|
||||
int id = -1;
|
||||
String query = "SELECT * FROM users WHERE pseudo='" + pseudo + "'";
|
||||
String query = "SELECT * FROM users WHERE pseudo='" + pseudo + "' AND statut = 'En ligne'";
|
||||
PreparedStatement pStat = null;
|
||||
try {
|
||||
pStat = connexion.prepareStatement(query);
|
||||
|
@ -97,6 +104,58 @@ public class DataBase {
|
|||
return id;
|
||||
}
|
||||
|
||||
public boolean loginExiste(String login) {
|
||||
String loginAux = null;
|
||||
String query = "SELECT * FROM users WHERE login =?";
|
||||
PreparedStatement pStat = null;
|
||||
try {
|
||||
pStat = connexion.prepareStatement(query);
|
||||
pStat.setString(1, login);
|
||||
ResultSet rs = pStat.executeQuery();
|
||||
if (rs.first()) {
|
||||
loginAux = rs.getString("login");
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
//System.out.println("loginAux = "+loginAux);
|
||||
return !(loginAux==null);
|
||||
}
|
||||
|
||||
public String getPseudoFromLogin(String login) {
|
||||
String pseudo = null;
|
||||
String query = "SELECT * FROM users WHERE login=?";
|
||||
PreparedStatement pStat = null;
|
||||
try {
|
||||
pStat = connexion.prepareStatement(query);
|
||||
pStat.setString(1, login);
|
||||
ResultSet resPseudo = pStat.executeQuery();
|
||||
if(resPseudo.first()) {
|
||||
pseudo = resPseudo.getString("pseudo");
|
||||
}
|
||||
}catch(SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return pseudo;
|
||||
}
|
||||
|
||||
public int getIdFromLogin(String login) {
|
||||
int id = -1;
|
||||
String query = "SELECT * FROM users WHERE login=?";
|
||||
PreparedStatement pStat = null;
|
||||
try {
|
||||
pStat = connexion.prepareStatement(query);
|
||||
pStat.setString(1, login);
|
||||
ResultSet resPseudo = pStat.executeQuery();
|
||||
if(resPseudo.first()) {
|
||||
id = resPseudo.getInt("id");
|
||||
}
|
||||
}catch(SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return id;
|
||||
}
|
||||
|
||||
public void deleteFromId(int id) {
|
||||
String query = "DELETE FROM `users` WHERE `id` = ?";
|
||||
PreparedStatement pStat = null;
|
||||
|
@ -196,4 +255,33 @@ public class DataBase {
|
|||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public ArrayList<MessageAffichage> getHistoric(int id1, int id2) throws SQLException{
|
||||
ArrayList<MessageAffichage> result = new ArrayList<MessageAffichage>();
|
||||
int idFirst;
|
||||
int idSecond;
|
||||
if (id1 > id2) {
|
||||
idSecond = id1;
|
||||
idFirst = id2;
|
||||
} else {
|
||||
idSecond = id2;
|
||||
idFirst = id1;
|
||||
}
|
||||
String nameTable = idFirst+"_"+idSecond;
|
||||
String preparedQuery = "SELECT * FROM " + nameTable;
|
||||
PreparedStatement prSt = null;
|
||||
try {
|
||||
prSt = connexion.prepareStatement(preparedQuery);
|
||||
ResultSet rs = prSt.executeQuery();
|
||||
while (rs.next()) {
|
||||
MessageAffichage msg = new MessageAffichage(rs.getString(4), rs.getInt(2));
|
||||
result.add(msg);
|
||||
}
|
||||
rs.close();
|
||||
prSt.close();
|
||||
}catch (SQLException throwables) {
|
||||
throwables.printStackTrace();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
12
chatGit/src/controller/Test.java
Normal file
12
chatGit/src/controller/Test.java
Normal file
|
@ -0,0 +1,12 @@
|
|||
package controller;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import network.Tools;
|
||||
|
||||
public class Test {
|
||||
public static void main(String args[]) throws IOException, InterruptedException {
|
||||
boolean res = DataBase.getInstance().loginExiste("kikou");
|
||||
System.out.println(res);
|
||||
}
|
||||
}
|
|
@ -4,18 +4,28 @@ import java.awt.event.ActionEvent;
|
|||
import java.awt.event.ActionListener;
|
||||
import java.awt.event.WindowAdapter;
|
||||
import java.awt.event.WindowEvent;
|
||||
import java.beans.PropertyChangeEvent;
|
||||
import java.beans.PropertyChangeListener;
|
||||
import java.io.IOException;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.swing.*;
|
||||
import javax.swing.text.BadLocationException;
|
||||
import javax.swing.text.SimpleAttributeSet;
|
||||
import javax.swing.text.StyleConstants;
|
||||
import javax.swing.text.StyledDocument;
|
||||
|
||||
import controller.Agent;
|
||||
import model.Contact;
|
||||
import model.MessageAffichage;
|
||||
import model.MessageChat;
|
||||
import network.TCPChat;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class FenetreChat extends JFrame{
|
||||
public class FenetreChat extends JFrame {
|
||||
|
||||
JFrame frame;
|
||||
Agent agent;
|
||||
|
@ -24,11 +34,13 @@ public class FenetreChat extends JFrame{
|
|||
|
||||
WindowAdapter wa;
|
||||
JPanel panel;
|
||||
JTextArea ta;
|
||||
static JTextArea ta;
|
||||
|
||||
static StyledDocument doc;
|
||||
|
||||
|
||||
public FenetreChat(Agent agent, String userString) {
|
||||
this.frame = new JFrame("Fenetre Chat");
|
||||
this.frame = new JFrame("Fenetre Chat avec "+ userString);
|
||||
this.agent = agent;
|
||||
this.user = agent.getUser();
|
||||
this.dest = agent.getList().findContact(userString);
|
||||
|
@ -47,6 +59,8 @@ public class FenetreChat extends JFrame{
|
|||
}};
|
||||
|
||||
addWidgets();
|
||||
afficherHistorique();
|
||||
|
||||
frame.addWindowListener(wa);
|
||||
frame.setVisible(true);
|
||||
|
||||
|
@ -54,6 +68,20 @@ public class FenetreChat extends JFrame{
|
|||
|
||||
|
||||
private void addWidgets() {
|
||||
|
||||
JTextPane textPane = new JTextPane();
|
||||
textPane.setEditable(false);
|
||||
|
||||
this.doc = textPane.getStyledDocument();
|
||||
|
||||
SimpleAttributeSet right = new SimpleAttributeSet();
|
||||
StyleConstants.setAlignment(right, StyleConstants.ALIGN_RIGHT);
|
||||
StyleConstants.setForeground(right, Color.BLUE);
|
||||
|
||||
SimpleAttributeSet left = new SimpleAttributeSet();
|
||||
StyleConstants.setAlignment(left, StyleConstants.ALIGN_LEFT);
|
||||
StyleConstants.setForeground(left, Color.RED);
|
||||
|
||||
GridBagLayout gridBagLayout = new GridBagLayout();
|
||||
gridBagLayout.columnWidths = new int[]{394, 0};
|
||||
gridBagLayout.rowHeights = new int[]{16, 220, 74, 0};
|
||||
|
@ -72,11 +100,7 @@ public class FenetreChat extends JFrame{
|
|||
JScrollPane sp = new JScrollPane(text);
|
||||
|
||||
//zone affichage texte
|
||||
this.ta = new JTextArea(20, 20);
|
||||
this.ta.setLineWrap(true);
|
||||
this.ta.setText("");
|
||||
JScrollPane sp2 = new JScrollPane(ta);
|
||||
this.ta.setEditable(false);
|
||||
JScrollPane sp2 = new JScrollPane(textPane);
|
||||
GridBagConstraints gbc = new GridBagConstraints();
|
||||
gbc.fill = GridBagConstraints.BOTH;
|
||||
gbc.insets = new Insets(0, 0, 5, 0);
|
||||
|
@ -90,12 +114,12 @@ public class FenetreChat extends JFrame{
|
|||
envoyer.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
String message = text.getText();
|
||||
System.out.println("Message :"+ message);
|
||||
System.out.println(dest);
|
||||
//System.out.println("Message :"+ message);
|
||||
//System.out.println(dest);
|
||||
int idUser = agent.getDb().getIdFromPseudo(user.getPseudo());
|
||||
System.out.println("IDuser = "+idUser);
|
||||
//System.out.println("IDuser = "+idUser);
|
||||
int idDest = agent.getDb().getIdFromPseudo(dest.getPseudo());
|
||||
System.out.println("IDdest = "+idDest);
|
||||
//System.out.println("IDdest = "+idDest);
|
||||
try {
|
||||
TCPChat tcpChat = null;
|
||||
for(TCPChat tcp : agent.getListeTCPChat()) {
|
||||
|
@ -105,16 +129,18 @@ public class FenetreChat extends JFrame{
|
|||
}
|
||||
//text.append(message);
|
||||
text.setText("");
|
||||
ta.append(agent.getUser().getPseudo()+ " : "+message+"\n");
|
||||
//ta.append(agent.getUser().getPseudo()+ " : "+message+"\n");
|
||||
//ta.setText("");
|
||||
tcpChat.sendMsg(message);
|
||||
doc.insertString(doc.getLength(), "\n"+message, right );
|
||||
doc.setParagraphAttributes(doc.getLength(), 1, right, false);
|
||||
if(agent.getDb().tableChatExists(idUser, idDest)) {
|
||||
agent.getDb().addMessage(idUser, idDest, message);
|
||||
}else {
|
||||
agent.getDb().newChatTable(idUser, idDest);
|
||||
agent.getDb().addMessage(idUser, idDest, message);
|
||||
}
|
||||
} catch (IOException e1) {
|
||||
} catch (IOException | BadLocationException e1) {
|
||||
System.out.println("Envoi du message impossible");
|
||||
e1.printStackTrace();
|
||||
}
|
||||
|
@ -132,5 +158,66 @@ public class FenetreChat extends JFrame{
|
|||
gbc_panel.gridy = 2;
|
||||
frame.getContentPane().add(panel, gbc_panel);
|
||||
}
|
||||
|
||||
|
||||
public static JTextArea getTa() {
|
||||
return ta;
|
||||
}
|
||||
|
||||
|
||||
public void setTa(JTextArea ta) {
|
||||
this.ta = ta;
|
||||
}
|
||||
|
||||
|
||||
public static StyledDocument getDoc() {
|
||||
return doc;
|
||||
}
|
||||
|
||||
|
||||
public static void setDoc(StyledDocument doc) {
|
||||
FenetreChat.doc = doc;
|
||||
}
|
||||
|
||||
public void afficherHistorique() {
|
||||
SimpleAttributeSet right = new SimpleAttributeSet();
|
||||
StyleConstants.setAlignment(right, StyleConstants.ALIGN_RIGHT);
|
||||
StyleConstants.setForeground(right, Color.BLUE);
|
||||
|
||||
SimpleAttributeSet left = new SimpleAttributeSet();
|
||||
StyleConstants.setAlignment(left, StyleConstants.ALIGN_LEFT);
|
||||
StyleConstants.setForeground(left, Color.RED);
|
||||
int idUser = agent.getDb().getIdFromPseudo(user.getPseudo());
|
||||
//System.out.println("IDuser = "+idUser);
|
||||
int idDest = agent.getDb().getIdFromPseudo(dest.getPseudo());
|
||||
if(agent.getDb().tableChatExists(idUser, idDest)) {
|
||||
ArrayList<MessageAffichage> historic = null;
|
||||
try {
|
||||
historic = agent.getDb().getHistoric(idUser, idDest);
|
||||
} catch (SQLException e2) {
|
||||
e2.printStackTrace();
|
||||
}
|
||||
for(MessageAffichage msg : historic) {
|
||||
if(msg.getId() == idUser) {
|
||||
try {
|
||||
doc.insertString(doc.getLength(), "\n"+msg.getMessage(), right );
|
||||
} catch (BadLocationException e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
doc.setParagraphAttributes(doc.getLength(), 1, right, false);
|
||||
}else {
|
||||
try {
|
||||
doc.insertString(doc.getLength(), "\n"+msg.getMessage(), left );
|
||||
} catch (BadLocationException e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
doc.setParagraphAttributes(doc.getLength(), 1, left, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -21,8 +21,9 @@ import java.io.IOException;
|
|||
|
||||
import controller.*;
|
||||
import network.Tools;
|
||||
import test.App;
|
||||
|
||||
public class FenetreAcc implements ActionListener {
|
||||
public class FenetreConnexion implements ActionListener {
|
||||
JFrame frame;
|
||||
JPanel panel;
|
||||
JTextField pseudofield;
|
||||
|
@ -33,10 +34,9 @@ public class FenetreAcc implements ActionListener {
|
|||
* Constructeur d'une fenetre d'affichage pour la connexion d'un utilisateur.
|
||||
* Cette fenetre sera munie d'un bouton de connexion et d'une zone de saisie de pseudo.
|
||||
*/
|
||||
public FenetreAcc() {
|
||||
// TODO Auto-generated constructor stub
|
||||
public FenetreConnexion() {
|
||||
//creer une instance JFrame
|
||||
frame = new JFrame("Fenetre accueil");
|
||||
frame = new JFrame("Fenetre connexion");
|
||||
//sortir quand l’utilisateur ferme le frame
|
||||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
// fixer les dimensions de la fenetre
|
||||
|
@ -60,7 +60,7 @@ public class FenetreAcc implements ActionListener {
|
|||
// Créer Zone d'insertion de texte pour le pseudo
|
||||
this.pseudofield = new JTextField(2);
|
||||
// creation d'un label qui contiendra un txt au centre
|
||||
this.Text = new JLabel("Type your user name", SwingConstants.CENTER);
|
||||
this.Text = new JLabel("Type your login", SwingConstants.CENTER);
|
||||
//Ajout d'un bouton Connexion
|
||||
this.Connexion = new JButton("Connexion");
|
||||
//On associe au bouton Connexion des actions a realiser
|
||||
|
@ -75,34 +75,39 @@ public class FenetreAcc implements ActionListener {
|
|||
|
||||
public void actionPerformed(ActionEvent event) {
|
||||
// on recupere le texte entree dans la zone de saisie
|
||||
String pseudo = pseudofield.getText();
|
||||
// On crée un objet de type ChatApp
|
||||
Agent agent;
|
||||
boolean connexion;
|
||||
|
||||
try {
|
||||
agent = new Agent(Tools.getAdress()[0], 25000, 25001);
|
||||
// on tente une connexion avec ce pseudo
|
||||
connexion = agent.connect(pseudo);
|
||||
// Dans les deux cas de figures (reussite ou echec) on affiche un pop-up pour expliquer la situation
|
||||
if(connexion) {
|
||||
// La connexion a reussi
|
||||
JOptionPane.showMessageDialog(frame, "Bonjour " + pseudo) ;
|
||||
frame.dispose();
|
||||
// on lance une nouvelle fenetre de type View_Menu
|
||||
FenetreMenu fenetreCourante= new FenetreMenu(agent);
|
||||
String login = pseudofield.getText();
|
||||
if(!DataBase.getInstance().loginExiste(login)) {
|
||||
JOptionPane.showMessageDialog(frame, "This login doesn't exist") ;
|
||||
}else {
|
||||
// On crée un objet de type ChatApp
|
||||
Agent agent;
|
||||
boolean connexion;
|
||||
|
||||
try {
|
||||
agent = new Agent(Tools.getAdress()[0], App.portSrc, App.portDest, login);
|
||||
String pseudo = agent.getDb().getPseudoFromLogin(login);
|
||||
// on tente une connexion avec ce pseudo
|
||||
connexion = agent.connectConnexion(pseudo, login);
|
||||
// Dans les deux cas de figures (reussite ou echec) on affiche un pop-up pour expliquer la situation
|
||||
if(connexion) {
|
||||
// La connexion a reussi
|
||||
JOptionPane.showMessageDialog(frame, "Bonjour " + pseudo) ;
|
||||
frame.dispose();
|
||||
// on lance une nouvelle fenetre de type View_Menu
|
||||
FenetreMenu fenetreCourante= new FenetreMenu(agent);
|
||||
}
|
||||
else {
|
||||
// La connexion a echoue, il est possible de rentrer un nouveau pseudo
|
||||
JOptionPane.showMessageDialog(frame, "Echec de Connexion , ce pseudo est deja pris !");
|
||||
}
|
||||
} catch (IOException e) {
|
||||
System.out.println("Création thread impossible");
|
||||
e.printStackTrace();
|
||||
} catch (InterruptedException e) {
|
||||
System.out.println("connect impossible");
|
||||
e.printStackTrace();
|
||||
}
|
||||
else {
|
||||
// La connexion a echoue, il est possible de rentrer un nouveau pseudo
|
||||
JOptionPane.showMessageDialog(frame, "Echec de Connexion , ce pseudo est deja pris !");
|
||||
}
|
||||
} catch (IOException e) {
|
||||
System.out.println("Création thread impossible");
|
||||
e.printStackTrace();
|
||||
} catch (InterruptedException e) {
|
||||
System.out.println("connect impossible");
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@ -110,7 +115,7 @@ public class FenetreAcc implements ActionListener {
|
|||
// Etre certain d'avoir une joli fenetre
|
||||
JFrame.setDefaultLookAndFeelDecorated(true);
|
||||
// On crée une fentre d'acceuil
|
||||
FenetreAcc fenetre = new FenetreAcc();
|
||||
FenetreConnexion fenetre = new FenetreConnexion();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
150
chatGit/src/gui/FenetreInscription.java
Normal file
150
chatGit/src/gui/FenetreInscription.java
Normal file
|
@ -0,0 +1,150 @@
|
|||
package gui;
|
||||
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.GridLayout;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
|
||||
import javax.swing.BorderFactory;
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.JTextField;
|
||||
import javax.swing.SwingConstants;
|
||||
import javax.swing.JOptionPane;
|
||||
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.io.IOException;
|
||||
|
||||
import controller.*;
|
||||
import network.Tools;
|
||||
import test.App;
|
||||
|
||||
public class FenetreInscription implements ActionListener {
|
||||
JFrame frame;
|
||||
JPanel panel;
|
||||
JTextField loginfield;
|
||||
JTextField pseudofield;
|
||||
JLabel Text;
|
||||
JButton inscription;
|
||||
JButton connexion;
|
||||
|
||||
/*
|
||||
* Constructeur d'une fenetre d'affichage pour la connexion d'un utilisateur.
|
||||
* Cette fenetre sera munie d'un bouton de connexion et d'une zone de saisie de pseudo.
|
||||
*/
|
||||
public FenetreInscription() {
|
||||
//creer une instance JFrame
|
||||
frame = new JFrame("Fenetre inscription");
|
||||
//sortir quand l’utilisateur ferme le frame
|
||||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
// fixer les dimensions de la fenetre
|
||||
frame.setSize(new Dimension(40, 100));
|
||||
//Creer le JPanel
|
||||
panel = new JPanel(new GridLayout(5,0));
|
||||
//Ajouter les elements
|
||||
this.addWidgets();
|
||||
//Set the default button.
|
||||
//frame.getRootPane().setDefaultButton(inscription);
|
||||
//frame.getRootPane().setDefaultButton(connexion);
|
||||
//frame.setLayout(new BorderLayout());
|
||||
//On ajoute le bouton au content pane de la JFrame
|
||||
//frame.getContentPane().add(inscription, BorderLayout.CENTER);
|
||||
//frame.getContentPane().add(connexion, BorderLayout.SOUTH);
|
||||
//Ajouter le panel a la window
|
||||
frame.getContentPane().add(panel, BorderLayout.CENTER);
|
||||
//L'utilisateur ne pourra pas aggrandir la fenetre
|
||||
this.frame.setResizable(false);
|
||||
//Afficher la fenetre
|
||||
frame.pack();
|
||||
frame.setVisible(true);
|
||||
}
|
||||
|
||||
private void addWidgets() {
|
||||
// Créer Zone d'insertion de texte pour le pseudo
|
||||
this.loginfield = new JTextField(2);
|
||||
this.pseudofield = new JTextField(2);
|
||||
// creation d'un label qui contiendra un txt au centre
|
||||
this.Text = new JLabel("Type your login and your pseudo", SwingConstants.CENTER);
|
||||
//Ajout d'un bouton Connexion
|
||||
this.inscription = new JButton("Sign Up");
|
||||
this.connexion = new JButton("Sign In");
|
||||
//On associe au bouton Connexion des actions a realiser
|
||||
this.inscription.addActionListener(this);
|
||||
this.connexion.addActionListener(this);
|
||||
// On ajouter les differents elements au panel
|
||||
panel.add(loginfield);
|
||||
panel.add(pseudofield);
|
||||
panel.add(Text);
|
||||
panel.add(inscription);
|
||||
panel.add(connexion);
|
||||
//ajouter un effet de bord transparent au composant Jlabel
|
||||
Text.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
|
||||
}
|
||||
|
||||
public void actionPerformed(ActionEvent event) {
|
||||
Object source = event.getSource();
|
||||
if(source == inscription) {
|
||||
// on recupere le texte entree dans la zone de saisie
|
||||
String login = loginfield.getText();
|
||||
String pseudo = pseudofield.getText();
|
||||
if(login.isBlank()||pseudo.isBlank()) {
|
||||
JOptionPane.showMessageDialog(frame, "Please do not enter a blank pseudo or login") ;
|
||||
}else if(DataBase.getInstance().loginExiste(login)){
|
||||
JOptionPane.showMessageDialog(frame, "This login is already used");
|
||||
}else {
|
||||
// On crée un objet de type ChatApp
|
||||
Agent agent;
|
||||
boolean connexion;
|
||||
|
||||
try {
|
||||
agent = new Agent(Tools.getAdress()[0], App.portSrc, App.portDest);
|
||||
// on tente une connexion avec ce pseudo
|
||||
connexion = agent.connectInscription(pseudo, login);
|
||||
// Dans les deux cas de figures (reussite ou echec) on affiche un pop-up pour expliquer la situation
|
||||
if(connexion) {
|
||||
// La connexion a reussi
|
||||
JOptionPane.showMessageDialog(frame, "Bonjour " + pseudo) ;
|
||||
frame.dispose();
|
||||
// on lance une nouvelle fenetre de type View_Menu
|
||||
FenetreMenu fenetreCourante= new FenetreMenu(agent);
|
||||
}
|
||||
else {
|
||||
// La connexion a echoue, il est possible de rentrer un nouveau pseudo
|
||||
JOptionPane.showMessageDialog(frame, "Echec de Connexion , ce pseudo est deja pris !");
|
||||
}
|
||||
} catch (IOException e) {
|
||||
System.out.println("Création thread impossible");
|
||||
e.printStackTrace();
|
||||
} catch (InterruptedException e) {
|
||||
System.out.println("connect impossible");
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}else if(source == connexion){
|
||||
frame.dispose();
|
||||
FenetreConnexion fenetreCo = new FenetreConnexion();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void createAndShowGUI() {
|
||||
// Etre certain d'avoir une joli fenetre
|
||||
JFrame.setDefaultLookAndFeelDecorated(true);
|
||||
// On crée une fentre d'acceuil
|
||||
FenetreInscription fenetre = new FenetreInscription();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
//Schedule a job for the event-dispatching thread:
|
||||
//creating and showing this application's GUI.
|
||||
javax.swing.SwingUtilities.invokeLater(new Runnable() {
|
||||
public void run() {
|
||||
createAndShowGUI();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
|
@ -10,6 +10,7 @@ import java.awt.event.ActionEvent;
|
|||
import java.awt.event.ActionListener;
|
||||
import java.awt.event.WindowAdapter;
|
||||
import java.awt.event.WindowEvent;
|
||||
import java.beans.PropertyChangeListener;
|
||||
import java.io.IOException;
|
||||
import java.util.Vector;
|
||||
|
||||
|
@ -96,6 +97,10 @@ public class FenetreMenu {
|
|||
JMenu contenu = new JMenu("Actions");
|
||||
contenu.setForeground(Color.BLUE);
|
||||
contenu.setHorizontalAlignment(SwingConstants.CENTER);
|
||||
|
||||
JButton refresh = new JButton("Refresh");
|
||||
refresh.setForeground(Color.GRAY);
|
||||
refresh.setHorizontalAlignment(SwingConstants.CENTER);
|
||||
|
||||
//JMenuItem usersActifs = new JMenuItem("Active users");
|
||||
JMenuItem modifierPseudo = new JMenuItem("Modify your pseudo");
|
||||
|
@ -107,6 +112,7 @@ public class FenetreMenu {
|
|||
|
||||
|
||||
menu.add(contenu);
|
||||
menu.add(refresh);
|
||||
frame.setJMenuBar(menu);
|
||||
|
||||
/*liste déroulante des utilsateurs actifs,
|
||||
|
@ -136,9 +142,40 @@ public class FenetreMenu {
|
|||
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
/*actions pour refresh*/
|
||||
refresh.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
String users = agent.getList().actifUsers();
|
||||
for(String pseudo : users.split("\n")) {
|
||||
if (!v.contains(pseudo)) {
|
||||
v.add(pseudo);
|
||||
}
|
||||
}
|
||||
JComboBox cb = new JComboBox(v);
|
||||
cb.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent event) {
|
||||
Object selected = cb.getSelectedItem();
|
||||
int idDest = agent.getDb().getIdFromPseudo(selected.toString());
|
||||
try {
|
||||
agent.createChat(idDest, selected.toString());
|
||||
} catch (IOException e) {
|
||||
System.out.println("Creation tcp chat impossible");
|
||||
e.printStackTrace();
|
||||
}
|
||||
new FenetreChat(agent, selected.toString());
|
||||
}
|
||||
});
|
||||
}});
|
||||
|
||||
frame.getContentPane().add(refresh, BorderLayout.NORTH);
|
||||
panel.add(BorderLayout.CENTER, jlabel);
|
||||
panel.add(BorderLayout.SOUTH , texte );
|
||||
panel.add(BorderLayout.WEST, cb);
|
||||
|
||||
|
||||
|
||||
|
||||
/*actions pour modifier le pseudo*/
|
||||
modifierPseudo.addActionListener(new ActionListener() {
|
BIN
chatGit/src/gui/panda.png
Normal file
BIN
chatGit/src/gui/panda.png
Normal file
Binary file not shown.
After Width: | Height: | 크기: 5.8 KiB |
BIN
chatGit/src/images/panda.png
Normal file
BIN
chatGit/src/images/panda.png
Normal file
Binary file not shown.
After Width: | Height: | 크기: 5.8 KiB |
|
@ -16,7 +16,7 @@ public class Contact {
|
|||
public Contact(String pseudo, InetAddress address, int port) {
|
||||
this.pseudo = pseudo;
|
||||
this.address = address;
|
||||
this.status = "En ligne";
|
||||
this.status = "";
|
||||
this.port = port;
|
||||
}
|
||||
|
29
chatGit/src/model/MessageAffichage.java
Normal file
29
chatGit/src/model/MessageAffichage.java
Normal file
|
@ -0,0 +1,29 @@
|
|||
package model;
|
||||
|
||||
public class MessageAffichage {
|
||||
|
||||
private String message;
|
||||
private int idSrc;
|
||||
|
||||
public MessageAffichage(String message, int idSrc) {
|
||||
this.message = message;
|
||||
this.idSrc = idSrc;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return idSrc;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.idSrc = id;
|
||||
}
|
||||
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show more
불러오는 중…
Reference in a new issue