Première version envoie de messages

This commit is contained in:
Morgane Foussats 2020-12-27 16:58:39 +01:00
parent 7ae4d2ea04
commit 43fa39f42b
24 changed files with 243 additions and 90 deletions

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.

View file

@ -12,7 +12,7 @@ public class Agent {
private Contact user;
private UDPInput BIn;
private UDPOutput BOut;
private TCPChat TCPSend;
//private TCPChat TCPSend;
private TCPServer server;
private ListeContacts list;
private InetAddress localAddress;
@ -60,7 +60,7 @@ public class Agent {
}
public void testSendTCP() throws IOException, InterruptedException {
/*public void testSendTCP() throws IOException, InterruptedException {
TCPSend.sendMsg("COUCOU");
Thread.sleep(1000);
@ -71,7 +71,7 @@ public class Agent {
TCPSend.sendMsg("Moi aussi :'(");
Thread.sleep(1000);
}
}*/
/*type 0 : premier message de broadcast
* type, 1 : deuxieme message de broadcast avec pseudo
@ -194,4 +194,20 @@ public class Agent {
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;
}
}

View file

@ -80,6 +80,23 @@ public class DataBase {
return pseudo;
}
public int getIdFromPseudo(String pseudo) {
int id = -1;
String query = "SELECT * FROM users WHERE pseudo='" + pseudo + "'";
PreparedStatement pStat = null;
try {
pStat = connexion.prepareStatement(query);
//pStat.setString(1, pseudo);
ResultSet resId = pStat.executeQuery();
if(resId.next()) {
id = resId.getInt(1);
}
}catch(SQLException e) {
e.printStackTrace();
}
return id;
}
public void deleteFromId(int id) {
String query = "DELETE FROM `users` WHERE `id` = ?";
PreparedStatement pStat = null;
@ -106,24 +123,77 @@ public class DataBase {
}
}
public void newChatTable (int id1, int id2) {
int idSrc = id1;
int idDest = id2;
if(id1>id2) {
idSrc = id2;
idDest = id1;
}
String nameTable = idSrc+"_"+idDest;
//String query = "CREATE TABLE `"+nameTable+"`(\n"+"`id` int "=? WHERE id=?";
/*PreparedStatement pStat = null;
try {
pStat = connexion.prepareStatement(query);
pStat.setString(1, pseudo);
pStat.setInt(2, id);
pStat.executeUpdate();
}catch(SQLException e) {
e.printStackTrace();
}*/
}
//chat table : nom : id1id2
//colonnes :
//id (l'id unique du message)
//id1 et id2 les id des deux users qui se parlent
//message : le message envoyé
//date : la date d'envoi du msg
public void newChatTable (int id1, int id2) {
int idSrc = id1;
int idDest = id2;
if(id1>id2) {
idSrc = id2;
idDest = id1;
}
String nameTable = idSrc+""+idDest;
String query = "CREATE TABLE " +nameTable+" (\n" +
"id INT PRIMARY KEY NOT NULL AUTO_INCREMENT, \n" +
"idSrc INT NOT NULL,\n" +
"idDest INT NOT NULL,\n" +
"message MEDIUMTEXT NOT NULL,\n" +
"date DATE NOT NULL\n" +
");";
PreparedStatement pStat = null;
try {
pStat = connexion.prepareStatement(query);
pStat.executeUpdate();
}catch(SQLException e) {
e.printStackTrace();
}
}
public boolean tableChatExists(int id1, int id2) {
int idSrc = id1;
int idDest = id2;
boolean res = true;
if(id1>id2) {
idSrc = id2;
idDest = id1;
}
String nameTable = idSrc+"_"+idDest;
String query = "SELECT * FROM" + nameTable;
PreparedStatement pStat = null;
try {
pStat = connexion.prepareStatement(query);
pStat.executeQuery();
}catch(SQLException e) {
res = false;
e.printStackTrace();
}
return res;
}
//il faut mettre dans l'ordre idSrc pour id1 et id Dest pour id2 dans les params
public void addMessage(int id1, int id2, String message) {
int idSrc = id1;
int idDest = id2;
if(id1>id2) {
idSrc = id2;
idDest = id1;
}
String nameTable = idSrc+"_"+idDest;
String query = "INSERT INTO " + nameTable + "(idSrc, idDest, message, date) VALUES (?, ?, ?, CURRENT_TIME)";
PreparedStatement pStat = null;
try {
pStat = connexion.prepareStatement(query);
pStat.setInt(1, id1);
pStat.setInt(2, id2);
pStat.setString(3, message);
pStat.executeUpdate();
}catch(SQLException e) {
e.printStackTrace();
}
}
}

View file

@ -6,8 +6,10 @@ import network.Tools;
public class Test {
public static void main(String args[]) throws IOException, InterruptedException {
Agent agent1 = new Agent(Tools.getAdress()[1], 22001, 22000);
agent1.testSendBroadcast();
Agent agent1 = new Agent(Tools.getAdress()[0], 22001, 22000);
int id = agent1.getDb().getIdFromPseudo(agent1.getUser().getPseudo());
System.out.println("ID : "+id);
//Tools.printInterfaces();
}
}

View file

@ -81,7 +81,7 @@ public class FenetreAcc implements ActionListener {
boolean connexion;
try {
agent = new Agent(Tools.getLocalIp(), 25000, 25001);
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

View file

@ -1,78 +1,132 @@
package gui;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.util.List;
import javax.swing.*;
import controller.Agent;
import model.Contact;
import network.TCPChat;
import java.awt.*;
public class FenetreChat extends JFrame{
private String title = "Fenetre Chat";
private JPanel contentPane;
private JTextArea txtOutput = new JTextArea();
private JTextField txtMessage = new JTextField();
private JButton btnSend = new JButton("Envoyer");
/**
* Launch the application.
*/
/*public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame frame;
Agent agent;
Contact dest; //destinataire du message
Contact user; //utilisateur
WindowAdapter wa;
JPanel panel;
JTextArea ta;
public FenetreChat(Agent agent, String userString) {
this.frame = new JFrame("Fenetre Chat");
this.agent = agent;
this.user = agent.getUser();
this.dest = agent.getList().findContact(userString);
this.frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
this.frame.setSize(new Dimension(400, 400));
this.frame.setResizable(true);
this.wa = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
int quitter = FenetreMenu.fenetreFermeture();
if(quitter == 0) {
frame.dispose();
}
}};
addWidgets();
frame.addWindowListener(wa);
frame.setVisible(true);
}
private void addWidgets() {
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[]{394, 0};
gridBagLayout.rowHeights = new int[]{16, 220, 74, 0};
gridBagLayout.columnWeights = new double[]{0.0, Double.MIN_VALUE};
gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, Double.MIN_VALUE};
this.frame.getContentPane().setLayout(gridBagLayout);
this.panel = new JPanel();
//zone pour rentrer les message à envoyer
JTextArea text = new JTextArea(10, 7);
text.setForeground(Color.BLACK);
text.setFont(new Font("Century Schoolbook", Font.PLAIN, 11));
text.setText("Enter your message");
text.setLineWrap(true);
JScrollPane sp = new JScrollPane(text);
//bouton envoyer
JButton envoyer = new JButton("Send");
frame.getRootPane().setDefaultButton(envoyer);
envoyer.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String message = text.getText();
System.out.println("Message :"+ message);
System.out.println(dest);
int idUser = agent.getDb().getIdFromPseudo(user.getPseudo());
System.out.println("IDuser = "+idUser);
int idDest = agent.getDb().getIdFromPseudo(dest.getPseudo());
System.out.println("IDdest = "+idDest);
try {
FenetreChat window = new FenetreChat();
window.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
TCPChat tcpChat = null;
for(TCPChat tcp : agent.getListeTCPChat()) {
if (tcp.getDestID() == idDest) {
tcpChat = tcp;
}
}
tcpChat.sendMsg(message);
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) {
System.out.println("Envoi du message impossible");
e1.printStackTrace();
}
}
});
}*/
/**
* Create the application.
*/
public FenetreChat(Agent agent, String s) {
this.setTitle(title);
initialize2();
}
/**
* Initialize the contents of the frame.
*/
private void initialize2() {
this.setAlwaysOnTop(true);
contentPane = (JPanel)this.getContentPane();
JScrollPane sclPane = new JScrollPane(txtOutput);
contentPane.add(sclPane, BorderLayout.CENTER);
JPanel southPanel = new JPanel(new BorderLayout());
southPanel.add(this.txtMessage, BorderLayout.CENTER);
southPanel.add(this.btnSend, BorderLayout.EAST);
contentPane.add(southPanel, BorderLayout.SOUTH);
this.txtOutput.setBackground(new Color(220,220,220));
this.txtOutput.setEditable(false);
this.setSize(500,400);
this.setVisible(true);
this.txtMessage.requestFocus();
/*setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 300);
text.append(message);
text.setText("");
}});
//zone affichage texte
this.ta = new JTextArea(10, 10);
this.ta.setLineWrap(true);
this.ta.setText("");
JScrollPane sp2 = new JScrollPane(ta);
this.ta.setEditable(false);
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(0, 0, 5, 0);
gbc.gridx = 0;
gbc.gridy = 1;
frame.getContentPane().add(sp2, gbc);
JPanel panel = new JPanel();
JLabel label = new JLabel("Enter text");
JTextField tf = new JTextField(10);
JButton send = new JButton("Send");
panel.add(label);
panel.add(tf);
panel.add(send);
contentPane.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
JTextArea ta = new JTextArea();
contentPane.add(ta);
contentPane.add(panel);*/
panel.add(BorderLayout.CENTER, sp);
panel.add(BorderLayout.SOUTH,envoyer);
panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
GridBagConstraints gbc_panel = new GridBagConstraints();
gbc_panel.anchor = GridBagConstraints.NORTH;
gbc_panel.fill = GridBagConstraints.HORIZONTAL;
gbc_panel.gridx = 0;
gbc_panel.gridy = 2;
frame.getContentPane().add(panel, gbc_panel);
}
}

View file

@ -50,7 +50,7 @@ public class FenetreMenu {
wa = new WindowAdapter(){
public void windowClosing(WindowEvent e){
int reponse = showConfirmDialog();
int reponse = fenetreFermeture();
if (reponse==0){
try {
// on deconnecte l'app avant de quitter
@ -78,7 +78,7 @@ public class FenetreMenu {
frame.setVisible(true);
}
static int showConfirmDialog() {
static int fenetreFermeture() {
return JOptionPane.showConfirmDialog(
null,
"Are you sure you wanna quit?",
@ -125,6 +125,13 @@ public class FenetreMenu {
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());
}

View file

@ -59,4 +59,8 @@ public class Contact {
public void setId(int id) {
this.id = id;
}
public String toString() {
return "Pseudo : " + pseudo + " status : " + status + " \n";
}
}

View file

@ -69,7 +69,7 @@ public class ListeContacts {
Contact contact = null;
for(Contact c : listeContact) {
if(c.getPseudo().equals(pseudo)){
c = contact;
contact = c;
break;
}
}