list actifs user deplacée sur server

This commit is contained in:
Elise Barnabe 2021-01-15 14:00:52 +01:00
父節點 9af289bc55
當前提交 db688b27a1
共有 62 個文件被更改,包括 524 次插入177 次删除

查看文件

@ -11,7 +11,7 @@
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="C:/Users/momof/OneDrive/Documents/INSA/4A/poo/java-json.jar">
<classpathentry kind="lib" path="C:/Users/elise/OneDrive/Bureau/ELISE/INSA/4A/POO COO/java-json.jar">
<attributes>
<attribute name="module" value="true"/>
</attributes>

查看文件

@ -1,9 +1,9 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate
org.eclipse.jdt.core.compiler.codegen.targetPlatform=13
org.eclipse.jdt.core.compiler.codegen.targetPlatform=11
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=13
org.eclipse.jdt.core.compiler.compliance=11
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
@ -12,4 +12,4 @@ org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
org.eclipse.jdt.core.compiler.release=enabled
org.eclipse.jdt.core.compiler.source=13
org.eclipse.jdt.core.compiler.source=11

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.

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.

二進制
chatGit/bin/server/Request.class Normal file

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,12 +1,12 @@
{
"serveur":{
"actif": 1,
"url": "test",
"port": 30000
"url": "http://localhost:8080/test/toto",
"port": 8080
},
"config":{
"interface": "wlan3",
"portSrc": 26001,
"portDest":26000
"interface": "wlan2",
"portSrc": 26000,
"portDest":26001
}
}

二進制
chatGit/panda.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  大小: 5.8 KiB

查看文件

@ -16,9 +16,10 @@ import javax.swing.text.StyledDocument;
import config.Configuration;
import gui.FenetreChat;
import gui.FenetreMenu;
import model.*;
import network.*;
import server.Server;
import server.Request;
import test.App;
public class Agent implements PropertyChangeListener{
@ -27,15 +28,13 @@ public class Agent implements PropertyChangeListener{
private UDPOutput BOut;
//private TCPChat TCPSend;
private TCPServer server;
private ListeContacts list;
//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;
@ -47,7 +46,7 @@ public class Agent implements PropertyChangeListener{
this.server = new TCPServer(user.getPort(), user.getAddress());
this.server.addPropertyChangeListener(this);
//this.TCPSend = new TCPChat(user);
this.list = ListeContacts.getInstance();
//this.list = ListeContacts.getInstance();
this.localAddress=Tools.getAdress()[0];
this.broadcast= Tools.getAdress()[1];
this.db = DataBase.getInstance();
@ -67,7 +66,7 @@ public class Agent implements PropertyChangeListener{
this.server = new TCPServer(user.getPort(), user.getAddress());
this.server.addPropertyChangeListener(this);
//this.TCPSend = new TCPChat(user);
this.list = ListeContacts.getInstance();
//this.list = ListeContacts.getInstance();
this.localAddress=Tools.getAdress()[0];
this.broadcast= Tools.getAdress()[1];
this.listTCPOk = new ArrayList<TCPChat>();
@ -104,98 +103,91 @@ public class Agent implements PropertyChangeListener{
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();
if(FenetreChat.isOuvert()) {
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;
}
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
* type 3 : message pseudo changé
* type 4 : Message affichage
* type 5 : Message chat
*/
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());
MessagePseudo msgPresentation = new MessagePseudo(this.localAddress, msg.getAddressSrc(), this.user.getPort(), msg.getPortSrc(), 1, this.user.getPseudo(), this.user.getId());
BOut.send(msgPresentation, msg.getAddressSrc(), msg.getPortSrc());
break;
case 1 :
MessagePseudo msgPseudo = (MessagePseudo) msg;
if(!list.pseudoExist(msgPseudo.getPseudo())) {
boolean pseudoExist = Request.sendPseudo(msgPseudo.getPseudo(), "pseudoOK");
if(pseudoExist) {
Contact newUser = new Contact(msgPseudo.getPseudo(), msgPseudo.getAddressSrc(), msgPseudo.getPortSrc());
list.addContact(newUser);
Request.sendUser(msgPseudo.getPseudo(), msgPseudo.getAddressSrc().toString(), Integer.toString(msgPseudo.getPortSrc()), "addUser");
//list.addContact(newUser);
if(FenetreMenu.ouvert) {
FenetreMenu.getCb().addItem(newUser.getPseudo());
}
}
break;
/*case 2 :
MessageDeconnexion messageDeconnexion = (MessageDeconnexion) msg;
if(list.pseudoExist(messageDeconnexion.getPseudo())){
Contact contact = list.findContact(messageDeconnexion.getPseudo());
list.deleteContact(contact);
}*/
case 2 :
MessagePseudo messageDeconnexion = (MessagePseudo) msg;
boolean pseudoExiste = Request.sendPseudo(messageDeconnexion.getPseudo(), "pseudoOK");
if(!pseudoExiste){
Request.sendDeconnexion(Integer.toString(messageDeconnexion.getId()),messageDeconnexion.getPseudo(), "deconnexion");
FenetreMenu.getCb().removeItem(messageDeconnexion.getPseudo());
}
case 3 :
MessagePseudo messageNewPseudo = (MessagePseudo) msg;
//Contact contact = list.findContact(messageNewPseudo.getPseudo());
//int id = this.db.getIdFromPseudo(contact.getPseudo());
for(int i = 0 ; i<FenetreMenu.getCb().getItemCount() ; i++){//Combo étant ton JComboBox
String pseudo = (String) FenetreMenu.getCb().getItemAt(i);
if(this.db.getIdFromPseudo(pseudo)==messageNewPseudo.getId()) {
FenetreMenu.getCb().removeItem(pseudo);
FenetreMenu.getCb().addItem(messageNewPseudo.getPseudo());
}
}
FenetreMenu.getCb().removeItem(messageNewPseudo.getPseudo());
}
}
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, "");
MessagePseudo msg_connexion = new MessagePseudo(this.localAddress, this.broadcast, this.user.getPort(), App.portDest, 0, "", this.user.getId());
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);
//list.addContact(user);
if(pseudoOK){
Request.sendUser(pseudo, this.localAddress.toString(), Integer.toString(App.portSrc), "addUser");
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());
MessagePseudo msgPresentation = new MessagePseudo(this.localAddress, this.broadcast, this.user.getPort(), App.portDest, 1, this.user.getPseudo(), this.user.getId());
BOut.send(msgPresentation, this.broadcast, App.portDest);
this.user.setStatut("En ligne");
//System.out.println("Connexion réussie avec le pseudo : "+pseudo);
@ -206,7 +198,7 @@ public class Agent implements PropertyChangeListener{
}
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, "");
MessagePseudo msg_connexion = new MessagePseudo(this.localAddress, this.broadcast, this.user.getPort(), App.portDest, 0, "", this.user.getId());
BOut.send(msg_connexion, this.broadcast, App.portDest);
Thread.sleep(5000);
ArrayList<MessagePseudo> msg_recus = BIn.getListMessage();
@ -214,12 +206,14 @@ public class Agent implements PropertyChangeListener{
handleMessage(msg_recus.get(i));
}
boolean pseudoOK = choisirPseudo(pseudo);
list.addContact(user);
//list.addContact(user);
if(pseudoOK){
Request.sendUser(pseudo, this.localAddress.toString(), Integer.toString(App.portSrc), "addUser");
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());
MessagePseudo msgPresentation = new MessagePseudo(this.localAddress, this.broadcast, this.user.getPort(), App.portDest, 1, this.user.getPseudo(), this.user.getId());
BOut.send(msgPresentation, this.broadcast, App.portDest);
db.updateStatus(id, "En ligne");
this.user.setStatut("En ligne");
//System.out.println("Connexion réussie avec le pseudo : "+pseudo);
}else {
@ -228,8 +222,10 @@ public class Agent implements PropertyChangeListener{
return pseudoOK;
}
public boolean choisirPseudo(String pseudo) {
if(!list.pseudoExist(pseudo)){
public boolean choisirPseudo(String pseudo) throws IOException {
boolean pseudoExist = Request.sendPseudo(pseudo, "pseudoOK");
if(pseudoExist){
user.setPseudo(pseudo);
//System.out.println("Pseudo set : "+pseudo);
return true;
@ -240,16 +236,23 @@ public class Agent implements PropertyChangeListener{
}
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());
Request.sendDeconnexion(Integer.toString(this.user.getId()),this.user.getPseudo(), "deconnexion");
MessagePseudo msgDeconnexion = new MessagePseudo(this.localAddress, this.broadcast, App.portSrc, App.portDest, 2, this.user.getPseudo(), this.user.getId());
BOut.send(msgDeconnexion, this.broadcast, App.portSrc);
//list.deleteContact(user);
System.exit(1);
}
public boolean changerPseudo(String pseudo) {
public boolean changerPseudo(String pseudo) throws IOException {
String ancienPseudo = user.getPseudo();
boolean changeOK = choisirPseudo(pseudo);
if(changeOK){
//System.out.println("Pseudo changé à : "+pseudo);
db.updatePseudo(this.user.getId(), pseudo);
//list.modifierListe(ancienPseudo, pseudo);
Request.sendPseudoChange(ancienPseudo, pseudo, "pseudoChanged");
MessagePseudo msgNewPseudo = new MessagePseudo(this.localAddress, this.broadcast, App.portSrc, App.portDest, 3, pseudo, this.user.getId());
BOut.send(msgNewPseudo, this.broadcast, App.portSrc);
return true;
}else {
//System.out.println("Veuillez choisir un autre pseudo");
@ -270,37 +273,22 @@ public class Agent implements PropertyChangeListener{
public void createChat (int destId, String destPseudo) throws IOException {
boolean chatExists = chatExists(destId);
if(!chatExists) {
for(Contact user : list.getListe()) {
boolean isNotOnList = Request.sendPseudo(destPseudo, "pseudoOK");
if(!isNotOnList) {
Contact dest = Request.getUser(destPseudo, "getUser");
TCPChat connexionTCP = new TCPChat(user, destId);
listTCPDeg.add(connexionTCP);
connexionTCP.addPropertyChangeListener(this);
}
/*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){
@ -325,7 +313,7 @@ public class Agent implements PropertyChangeListener{
public ListeContacts getList() {
/*public ListeContacts getList() {
return list;
}
@ -333,7 +321,7 @@ public class Agent implements PropertyChangeListener{
public void setList(ListeContacts list) {
this.list = list;
}
}*/
public DataBase getDb() {
@ -354,4 +342,5 @@ public class Agent implements PropertyChangeListener{
public TCPChat getTCPChat() {
return this.listTCPDeg.remove(0);
}
}

查看文件

@ -201,7 +201,7 @@ public class DataBase {
"idSrc INT NOT NULL,\n" +
"idDest INT NOT NULL,\n" +
"message MEDIUMTEXT NOT NULL,\n" +
"date DATE NOT NULL\n" +
"date_heure DATETIME NOT NULL\n" +
");";
PreparedStatement pStat = null;
try {
@ -243,7 +243,7 @@ public class DataBase {
idDest = id1;
}
String nameTable = idSrc+"_"+idDest;
String query = "INSERT INTO " + nameTable + "(idSrc, idDest, message, date) VALUES (?, ?, ?, CURRENT_TIME)";
String query = "INSERT INTO " + nameTable + "(idSrc, idDest, message, date_heure) VALUES (?, ?, ?, CURRENT_TIME)";
PreparedStatement pStat = null;
try {
pStat = connexion.prepareStatement(query);
@ -274,7 +274,7 @@ public class DataBase {
prSt = connexion.prepareStatement(preparedQuery);
ResultSet rs = prSt.executeQuery();
while (rs.next()) {
MessageAffichage msg = new MessageAffichage(rs.getString(4), rs.getInt(2));
MessageAffichage msg = new MessageAffichage(rs.getString(4), rs.getInt(2), rs.getInt(3));
result.add(msg);
}
rs.close();
@ -284,4 +284,41 @@ public class DataBase {
}
return result;
}
public Date getDateFromMessage(int id1, int id2, String message) {
Date date = null;
int idSrc = id1;
int idDest = id2;
if(id1>id2) {
idSrc = id2;
idDest = id1;
}
String nameTable = idSrc+"_"+idDest;
String query = "SELECT * FROM "+nameTable+" WHERE message=?";
PreparedStatement pStat = null;
try {
pStat = connexion.prepareStatement(query);
pStat.setString(1, message);
ResultSet resPseudo = pStat.executeQuery();
if(resPseudo.first()) {
date = resPseudo.getDate("date_heure");
}
}catch(SQLException e) {
e.printStackTrace();
}
return date;
}
public void updateStatus(int id, String status) {
String query = "UPDATE `users` SET `statut`=? WHERE id=?";
PreparedStatement pStat = null;
try {
pStat = connexion.prepareStatement(query);
pStat.setString(1, status);
pStat.setInt(2, id);
pStat.executeUpdate();
}catch(SQLException e) {
e.printStackTrace();
}
}
}

查看文件

@ -1,12 +1,16 @@
package controller;
import java.io.IOException;
import java.net.InetAddress;
import java.net.URL;
import config.ConfLoad;
import network.Tools;
import test.App;
public class Test {
public static void main(String args[]) throws IOException, InterruptedException {
boolean res = DataBase.getInstance().loginExiste("kikou");
System.out.println(res);
Agent agent = new Agent(Tools.getAdress()[0], App.portSrc, App.portDest);
//agent.initServer(ConfLoad.load(), "elise");
}
}

查看文件

@ -8,7 +8,10 @@ import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.IOException;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.swing.*;
@ -20,8 +23,8 @@ import javax.swing.text.StyledDocument;
import controller.Agent;
import model.Contact;
import model.MessageAffichage;
import model.MessageChat;
import network.TCPChat;
import server.Request;
import java.awt.*;
@ -37,14 +40,16 @@ public class FenetreChat extends JFrame {
static JTextArea ta;
static StyledDocument doc;
private static boolean ouvert=false;
public FenetreChat(Agent agent, String userString) {
public FenetreChat(Agent agent, String userString) throws IOException {
FenetreChat.ouvert=true;
this.frame = new JFrame("Fenetre Chat avec "+ userString);
this.agent = agent;
this.user = agent.getUser();
this.dest = agent.getList().findContact(userString);
//this.dest = agent.getList().findContact(userString);
this.dest = Request.getUser(userString, "getUser");
this.frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
this.frame.setSize(new Dimension(400, 400));
@ -112,6 +117,7 @@ public class FenetreChat extends JFrame {
JButton envoyer = new JButton("Send");
frame.getRootPane().setDefaultButton(envoyer);
envoyer.addActionListener(new ActionListener() {
private Date date;
public void actionPerformed(ActionEvent e) {
String message = text.getText();
//System.out.println("Message :"+ message);
@ -127,12 +133,13 @@ public class FenetreChat extends JFrame {
tcpChat = tcp;
}
}
//text.append(message);
text.setText("");
//ta.append(agent.getUser().getPseudo()+ " : "+message+"\n");
//ta.setText("");
this.setDate(new Date());
DateFormat format = new SimpleDateFormat(" (yyyy/MM/dd HH:mm:ss)");
String dateString = format.format(this.date);
tcpChat.sendMsg(message);
doc.insertString(doc.getLength(), "\n"+message, right );
doc.insertString(doc.getLength(), "\n"+message+dateString, right );
doc.setParagraphAttributes(doc.getLength(), 1, right, false);
if(agent.getDb().tableChatExists(idUser, idDest)) {
agent.getDb().addMessage(idUser, idDest, message);
@ -144,8 +151,12 @@ public class FenetreChat extends JFrame {
System.out.println("Envoi du message impossible");
e1.printStackTrace();
}
}
public void setDate(Date d) {
this.date=d;
}
}});
});
panel.add(BorderLayout.CENTER, sp);
panel.add(BorderLayout.SOUTH,envoyer);
@ -217,7 +228,9 @@ public class FenetreChat extends JFrame {
}
}
public static boolean isOuvert() {
return ouvert;
}
}

查看文件

@ -21,6 +21,7 @@ import java.io.IOException;
import controller.*;
import network.Tools;
import server.Request;
import test.App;
public class FenetreConnexion implements ActionListener {
@ -29,6 +30,7 @@ public class FenetreConnexion implements ActionListener {
JTextField pseudofield;
JLabel Text;
JButton Connexion;
JButton Cancel;
/*
* Constructeur d'une fenetre d'affichage pour la connexion d'un utilisateur.
@ -42,7 +44,7 @@ public class FenetreConnexion implements ActionListener {
// fixer les dimensions de la fenetre
frame.setSize(new Dimension(120, 40));
//Creer le JPanel
panel = new JPanel(new GridLayout(3,1));
panel = new JPanel(new GridLayout(4,1));
//Ajouter les elements
this.addWidgets();
//Set the default button.
@ -63,51 +65,71 @@ public class FenetreConnexion implements ActionListener {
this.Text = new JLabel("Type your login", SwingConstants.CENTER);
//Ajout d'un bouton Connexion
this.Connexion = new JButton("Connexion");
this.Cancel = new JButton("Cancel");
this.Connexion.addActionListener(this);
this.Cancel.addActionListener(this);
//On associe au bouton Connexion des actions a realiser
this.Connexion.addActionListener(this);
// On ajouter les differents elements au panel
panel.add(pseudofield);
panel.add(Text);
panel.add(Connexion);
panel.add(Cancel);
//ajouter un effet de bord transparent au composant Jlabel
Text.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
}
public void actionPerformed(ActionEvent event) {
// on recupere le texte entree dans la zone de saisie
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();
}
}
Object source = event.getSource();
if(source == Connexion){
// on recupere le texte entree dans la zone de saisie
String login = pseudofield.getText();
boolean loginExiste = false;
try {
loginExiste = Request.sendLogin(login, "loginExiste");
} catch (IOException e1) {
System.out.println("Pb envoi requete au serveur");
e1.printStackTrace();
}
if(!loginExiste) {
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 if(source == Cancel) {
frame.dispose();
new FenetreInscription();
}
}

查看文件

@ -44,7 +44,7 @@ public class FenetreInscription implements ActionListener {
// fixer les dimensions de la fenetre
frame.setSize(new Dimension(40, 100));
//Creer le JPanel
panel = new JPanel(new GridLayout(5,0));
panel = new JPanel(new GridLayout(7,1));
//Ajouter les elements
this.addWidgets();
//Set the default button.
@ -68,7 +68,9 @@ public class FenetreInscription implements ActionListener {
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);
this.Text = new JLabel("Forbidden characters: space, '?','&', '/', '=' ", SwingConstants.CENTER);
JLabel text = new JLabel("Login: ", SwingConstants.CENTER);
JLabel text2 = new JLabel("Pseudo: ", SwingConstants.CENTER);
//Ajout d'un bouton Connexion
this.inscription = new JButton("Sign Up");
this.connexion = new JButton("Sign In");
@ -76,7 +78,9 @@ public class FenetreInscription implements ActionListener {
this.inscription.addActionListener(this);
this.connexion.addActionListener(this);
// On ajouter les differents elements au panel
panel.add(text);
panel.add(loginfield);
panel.add(text2);
panel.add(pseudofield);
panel.add(Text);
panel.add(inscription);

查看文件

@ -5,6 +5,7 @@ import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.HeadlessException;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
@ -31,6 +32,7 @@ import javax.swing.SwingConstants;
import controller.Agent;
import model.Contact;
import server.Request;
public class FenetreMenu {
JFrame frame;
@ -40,9 +42,13 @@ public class FenetreMenu {
JLabel jlabel;
JLabel texte;
JLabel liste;
static JComboBox cb;
WindowAdapter wa ;
public FenetreMenu(Agent agent) {
public static boolean ouvert = false;
public FenetreMenu(Agent agent) throws IOException {
FenetreMenu.ouvert=true;
this.agent = agent;
frame = new JFrame("Fenêtre menu");
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
@ -87,8 +93,8 @@ public class FenetreMenu {
JOptionPane.YES_NO_OPTION);
}
private void addWidgets() {
jlabel = new JLabel(new ImageIcon("/Users/momof/eclipse-workspace/chat/src/images/panda.png"), JLabel.CENTER);
private void addWidgets() throws IOException {
jlabel = new JLabel(new ImageIcon("panda.png"), JLabel.CENTER);
texte = new JLabel("Bienvenue sur ce magnifique chat " + agent.getUser().getPseudo());
texte.setFont(new Font("Century Schoolbook", Font.PLAIN, 40));
texte.setHorizontalAlignment(SwingConstants.CENTER);
@ -121,13 +127,14 @@ public class FenetreMenu {
ta.insert("Active users\n",0);
JScrollPane scrollPane = new JScrollPane(ta);
String users = agent.getList().actifUsers();
String users = Request.actifs("actifs");
//String users = agent.getList().actifUsers();
Vector<String> v = new Vector<String>();
for(String pseudo : users.split("\n")) {
v.add(pseudo);
}
JComboBox cb = new JComboBox(v);
cb = new JComboBox(v);
cb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
Object selected = cb.getSelectedItem();
@ -138,14 +145,19 @@ public class FenetreMenu {
System.out.println("Creation tcp chat impossible");
e.printStackTrace();
}
new FenetreChat(agent, selected.toString());
try {
new FenetreChat(agent, selected.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
/*actions pour refresh*/
refresh.addActionListener(new ActionListener() {
/* refresh.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String users = agent.getList().actifUsers();
for(String pseudo : users.split("\n")) {
@ -167,7 +179,7 @@ public class FenetreMenu {
new FenetreChat(agent, selected.toString());
}
});
}});
}});*/
frame.getContentPane().add(refresh, BorderLayout.NORTH);
panel.add(BorderLayout.CENTER, jlabel);
@ -186,20 +198,30 @@ public class FenetreMenu {
cancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.dispose();
new FenetreMenu(agent);
try {
new FenetreMenu(agent);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}});
JButton ok = new JButton("Continue");
frame.getRootPane().setDefaultButton(ok);
ok.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String newPseudo = field.getText();
if(agent.changerPseudo(newPseudo)) {
JOptionPane.showMessageDialog(frame, "Your new pseudo is "+newPseudo);
frame.dispose();
new FenetreMenu(agent);
}else {
JOptionPane.showMessageDialog(frame, "Pseudo already, use choose another one");
};
try {
if(agent.changerPseudo(newPseudo)) {
JOptionPane.showMessageDialog(frame, "Your new pseudo is "+newPseudo);
frame.dispose();
new FenetreMenu(agent);
}else {
JOptionPane.showMessageDialog(frame, "Pseudo already, use choose another one");
}
} catch (HeadlessException | IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
};
}
});
JPanel panel1 = new JPanel(new GridLayout(4,1));
@ -229,6 +251,10 @@ public class FenetreMenu {
}
public static JComboBox getCb() {
return cb;
}
}

查看文件

@ -57,6 +57,16 @@ public class ListeContacts {
}
}
public void modifierListe(String ancien, String nouveau) {
for(Contact c : this.listeContact) {
if(c.getPseudo().equals(ancien)) {
this.listeContact.remove(c);
c.setPseudo(nouveau);
this.addContact(c);
}
}
}
public int length() {
int n = 0;
while(!listeContact.isEmpty()) {

查看文件

@ -1,17 +1,40 @@
package model;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import controller.DataBase;
public class MessageAffichage {
private String message;
private int idSrc;
private int idDest;
private Date date;
public MessageAffichage(String message, int idSrc) {
public MessageAffichage(String message, int idSrc, int idDest) {
this.idDest=idDest;
this.message = message;
this.idSrc = idSrc;
this.setDate(DataBase.getInstance().getDateFromMessage(this.idSrc, this.idDest, this.message));
}
public String dateToString() {
DateFormat format = new SimpleDateFormat("(yyyy/MM/dd HH:mm:ss)");
return format.format(this.getDate());
}
public Date getDate() {
return date;
}
public void setDate(Date d) {
this.date = d;
}
public String getMessage() {
return message;
return message+dateToString();
}
public void setMessage(String message) {

查看文件

@ -1,15 +1,33 @@
package model;
import java.net.InetAddress;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MessageChat extends Message{
private String message;
private Date date;
public MessageChat(InetAddress addressSrc, InetAddress addressDest, int portSrc, int portDest, int typeMessage, String message) {
super(addressSrc, addressDest, portSrc, portDest, typeMessage);
this.setDate(new Date());
this.message = message;
}
public String dateToString() {
DateFormat format = new SimpleDateFormat("(yyyy/MM/dd HH:mm:ss)");
return format.format(this.getDate());
}
public Date getDate() {
return date;
}
public void setDate(Date d) {
this.date = d;
}
public String getMessage() {
return message;

查看文件

@ -5,10 +5,12 @@ import java.net.InetAddress;
public class MessagePseudo extends Message{
private String pseudo;
private int id;
public MessagePseudo(InetAddress addressSrc, InetAddress addressDest, int portSrc, int portDest, int typeMessage,String pseudo) {
public MessagePseudo(InetAddress addressSrc, InetAddress addressDest, int portSrc, int portDest, int typeMessage,String pseudo, int id) {
super(addressSrc, addressDest, portSrc, portDest, typeMessage);
this.pseudo = pseudo;
this.id = id;
}
public String getPseudo() {
@ -19,5 +21,13 @@ public class MessagePseudo extends Message{
this.pseudo = pseudo;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id=id;
}
}

查看文件

@ -6,6 +6,7 @@ import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.util.ArrayList;
@ -38,7 +39,12 @@ public class TCPChat extends Thread{
this.start();
}
public TCPChat(InetAddress adr, int port) throws IOException {
this.socket = new Socket(adr, port);
listeMessagesRecu = new ArrayList<MessageChat>();
this.support = new PropertyChangeSupport(this);
this.start();
}
public void addPropertyChangeListener(PropertyChangeListener pcl) {
this.support.addPropertyChangeListener("Msg TCP Recu", pcl);
@ -108,5 +114,7 @@ public class TCPChat extends Thread{
this.destID = destID;
}
public boolean isConnected() {
return this.socket != null && socket.isConnected() && !socket.isClosed();
}
}

查看文件

@ -0,0 +1,183 @@
package server;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URL;
import config.ConfLoad;
import config.Configuration;
import model.Contact;
public class Request {
private static Configuration conf = ConfLoad.load();
private static String url = conf.getsConf().getUrl();
public static boolean sendLogin(String login, String type) throws IOException {
boolean res;
URL obj = new URL(url+"?login="+login+"&type="+type);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
int responseCode = con.getResponseCode();
String sRes = con.getHeaderField("result");
System.out.println("GET Response Code :: " + responseCode);
if (responseCode == HttpURLConnection.HTTP_OK) {
System.out.println("GET a réussi");
} else {
System.out.println("GET n'a pas fonctionné");
}
if(sRes.equals("true")) {
res = true;
}else {
res = false;
}
return res;
}
public static boolean sendPseudo(String pseudo, String type) throws IOException {
boolean res;
URL obj = new URL(url+"?pseudo="+pseudo+"&type="+type);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
int responseCode = con.getResponseCode();
String sRes = con.getHeaderField("result");
System.out.println("GET Response Code :: " + responseCode);
if (responseCode == HttpURLConnection.HTTP_OK) {
System.out.println("GET a réussi");
} else {
System.out.println("GET n'a pas fonctionné");
}
if(sRes.equals("true")) {
res = true;
}else {
res = false;
}
return res;
}
public static void sendPseudoChange(String pseudoAncien, String pseudo, String type) throws IOException {
URL obj = new URL(url+"?pseudoVieux="+pseudoAncien+"&pseudo="+pseudo+"&type="+type);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
int responseCode = con.getResponseCode();
String sRes = con.getHeaderField("result");
System.out.println("GET Response Code :: " + responseCode);
if (responseCode == HttpURLConnection.HTTP_OK) {
System.out.println("GET a réussi");
} else {
System.out.println("GET n'a pas fonctionné");
}
}
public static void sendUser(String pseudo, String adr, String port, String type) throws IOException {
URL obj = new URL(url+"?pseudo="+pseudo+"&adr="+adr+"&port="+port+"&type="+type);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
int responseCode = con.getResponseCode();
String sRes = con.getHeaderField("result");
System.out.println("GET Response Code :: " + responseCode);
if (responseCode == HttpURLConnection.HTTP_OK) {
System.out.println("GET a réussi");
} else {
System.out.println("GET n'a pas fonctionné");
}
}
public static void sendDeconnexion(String id, String pseudo, String type) throws IOException {
URL obj = new URL(url+"?id="+id+"&pseudo="+pseudo+"&type="+type);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
int responseCode = con.getResponseCode();
String sRes = con.getHeaderField("result");
System.out.println("GET Response Code :: " + responseCode);
if (responseCode == HttpURLConnection.HTTP_OK) {
System.out.println("GET a réussi");
} else {
System.out.println("GET n'a pas fonctionné");
}
}
public static Contact getUser(String pseudo, String type) throws IOException {
Contact user;
URL obj = new URL(url+"?pseudo="+pseudo+"&type="+type);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
int responseCode = con.getResponseCode();
String sAdr = con.getHeaderField("address");
String sPort = con.getHeaderField("port");
System.out.println("GET Response Code :: " + responseCode);
if (responseCode == HttpURLConnection.HTTP_OK) {
System.out.println("GET a réussi");
} else {
System.out.println("GET n'a pas fonctionné");
}
String adr2= sAdr.substring(1);
InetAddress address = InetAddress.getByName(adr2);
int port = Integer.parseInt(sPort);
user = new Contact(pseudo, address, port);
return user;
}
public static String actifs(String type) throws IOException {
String actifs;
URL obj = new URL(url+"?type="+type);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
int responseCode = con.getResponseCode();
actifs = con.getHeaderField("actifs");
System.out.println("GET Response Code :: " + responseCode);
if (responseCode == HttpURLConnection.HTTP_OK) {
System.out.println("GET a réussi");
} else {
System.out.println("GET n'a pas fonctionné");
}
return actifs;
}
}