Browse Source

Added disconection and handler to close chat window

EyeXion 3 years ago
parent
commit
5ffe752e33

BIN
build/libs/Clavardage-1.0-all.jar View File


+ 53
- 0
src/main/java/app/insa/clav/Core/DataBaseAccess.java View File

@@ -127,6 +127,31 @@ public class DataBaseAccess {
127 127
         return id;
128 128
     }
129 129
 
130
+    public boolean isTableCreated(int id1, int id2){
131
+        int idGrand;
132
+        int idPetit;
133
+        boolean res = true;
134
+        if (id1 > id2) {
135
+            idGrand = id1;
136
+            idPetit = id2;
137
+        } else {
138
+            idGrand = id2;
139
+            idPetit = id1;
140
+        }
141
+        String nomTable = "Chat" + idPetit + "_" + idGrand;
142
+        String preparedQuery = "SELECT * FROM " + nomTable;
143
+        PreparedStatement prSt = null;
144
+        try {
145
+            prSt = con.prepareStatement(preparedQuery);
146
+            ResultSet rs = prSt.executeQuery();
147
+        } catch (SQLException throwables) {
148
+            res = false;
149
+            throwables.printStackTrace();
150
+        }
151
+        System.out.println(res);
152
+        return res;
153
+    }
154
+
130 155
     public ArrayList<MessageHistoryList> getMessageHistory(int id1, int id2) {
131 156
         ArrayList<MessageHistoryList> history = new ArrayList<MessageHistoryList>();
132 157
         int idPetit;
@@ -179,5 +204,33 @@ public class DataBaseAccess {
179 204
             throwables.printStackTrace();
180 205
         }
181 206
     }
207
+
208
+    public void createChatTable(int id1, int id2){
209
+        int idPetit;
210
+        int idGrand;
211
+        if (id1 > id2) {
212
+            idGrand = id1;
213
+            idPetit = id2;
214
+        } else {
215
+            idGrand = id2;
216
+            idPetit = id1;
217
+        }
218
+        String nomTable = "Chat" + idPetit + "_" + idGrand;
219
+        String preparedQuery = "CREATE TABLE `" +  nomTable +"` (\n" + "`id` int NOT NULL,\n" + "  `sourceId` int NOT NULL,\n" +  "  `date` timestamp NOT NULL,\n" +  "  `payload` mediumtext NOT NULL\n" +  ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;";
220
+        PreparedStatement prSt = null;
221
+        System.out.println(preparedQuery);
222
+        try {
223
+            prSt = con.prepareStatement(preparedQuery);
224
+            prSt.executeUpdate();
225
+            preparedQuery = "\nALTER TABLE `"+ nomTable + "`\n" + "  ADD PRIMARY KEY (`id`);";
226
+            prSt = con.prepareStatement(preparedQuery);
227
+            prSt.executeUpdate();
228
+            preparedQuery = "\nALTER TABLE `" + nomTable + "`\n" + "  MODIFY `id` int NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=7;";
229
+            prSt = con.prepareStatement(preparedQuery);
230
+            prSt.executeUpdate();
231
+        } catch (SQLException throwables) {
232
+            throwables.printStackTrace();
233
+        }
234
+    }
182 235
 }
183 236
 

+ 47
- 5
src/main/java/app/insa/clav/Core/Model.java View File

@@ -8,6 +8,8 @@ import app.insa.clav.Reseau.TCPChatConnection;
8 8
 import app.insa.clav.Reseau.TCPListener;
9 9
 import app.insa.clav.Reseau.UDPInput;
10 10
 import app.insa.clav.Reseau.UDPOutput;
11
+import javafx.application.Application;
12
+import javafx.application.Platform;
11 13
 import jdk.jshell.execution.Util;
12 14
 
13 15
 import java.beans.PropertyChangeEvent;
@@ -75,6 +77,8 @@ public class Model implements PropertyChangeListener{
75 77
 
76 78
     private DataBaseAccess dbAccess;
77 79
 
80
+    private Application app;
81
+
78 82
 
79 83
     /**
80 84
      * Constructeur
@@ -90,7 +94,7 @@ ID 1 -> Listening on 6000, sending on 5000, tcpServer on 7000
90 94
 ID 2 -> Listening on 6001, sending on 5001, tcpServer on 7001
91 95
 ID 2 -> Listening on 6002, sending on 5002, tcpServer on 7002
92 96
 */
93
-    private Model(int inputPort, int outputPort, int tcpListenerPort){
97
+    private Model(int inputPort, int outputPort, int tcpListenerPort, Application app){
94 98
         try {
95 99
             this.user = new Utilisateurs("NA", InetAddress.getLocalHost(), 0, inputPort);
96 100
             this.UDPOut = new UDPOutput(InetAddress.getLocalHost(), outputPort);
@@ -106,6 +110,7 @@ ID 2 -> Listening on 6002, sending on 5002, tcpServer on 7002
106 110
         this.userList = new ArrayList<Utilisateurs>();
107 111
         this.listTCPConnection = new ArrayList<TCPChatConnection>();
108 112
         this.dbAccess = DataBaseAccess.getInstance();
113
+        this.app = app;
109 114
     }
110 115
 
111 116
     /**
@@ -118,10 +123,10 @@ ID 2 -> Listening on 6002, sending on 5002, tcpServer on 7002
118 123
      * @return
119 124
      *         instance of Model
120 125
      */
121
-    public static Model getInstance(int inputPort, int outputPort, int tcpListenerPort){
126
+    public static Model getInstance(int inputPort, int outputPort, int tcpListenerPort, Application app){
122 127
         synchronized(Model.class){
123 128
             if (instance == null) {
124
-                instance = new Model(inputPort, outputPort,tcpListenerPort);
129
+                instance = new Model(inputPort, outputPort,tcpListenerPort, app);
125 130
             }
126 131
         }
127 132
         return instance;
@@ -245,8 +250,7 @@ ID 2 -> Listening on 6002, sending on 5002, tcpServer on 7002
245 250
     }
246 251
 
247 252
 
248
-    public void createChatFromLocalRequest(String remotePseudo){
249
-        int remoteId = this.getUserFromPseudo(remotePseudo).getId();
253
+    public void createChatFromLocalRequest(int remoteId, String remotePseudo){
250 254
         boolean isChatAlreadyCreated = false;
251 255
         for (TCPChatConnection tcpCo : listTCPConnection){
252 256
             if (tcpCo.remoteUserId == remoteId){
@@ -382,6 +386,44 @@ ID 2 -> Listening on 6002, sending on 5002, tcpServer on 7002
382 386
         return res;
383 387
     }
384 388
 
389
+    public void sendDeconnectionMessage() {
390
+        try {
391
+            if (user.getId() == 1 || user.getId() == 2) {
392
+                MessagePseudo msg = new MessagePseudo(7, this.user.getInetAddress(), this.user.getPort(), InetAddress.getLocalHost(), 6002, this.user.getPseudo(),this.user.getId());
393
+                UDPOut.sendMsg(msg);
394
+            }
395
+            if (user.getId() == 2 || user.getId() == 3) {
396
+                MessagePseudo msg = new MessagePseudo(7, this.user.getInetAddress(), this.user.getPort(),  InetAddress.getLocalHost(), 6000, this.user.getPseudo(),this.user.getId());
397
+                UDPOut.sendMsg(msg);
398
+            }
399
+            if (user.getId() == 1 || user.getId() == 3) {
400
+                MessagePseudo msg = new MessagePseudo(7, this.user.getInetAddress(), this.user.getPort(),  InetAddress.getLocalHost(), 6001,this.user.getPseudo(),this.user.getId());
401
+                UDPOut.sendMsg(msg);
402
+            }
403
+        }
404
+        catch (UnknownHostException e){
405
+            System.out.println(("exception Trouver host dans sendPseudoBroadcast"));
406
+            e.printStackTrace();
407
+        }
408
+        try {
409
+            this.app.stop();
410
+            Platform.exit();
411
+            System.exit(0);
412
+        } catch (Exception e) {
413
+            e.printStackTrace();
414
+        }
415
+    }
416
+
417
+    public void notifyCloseChat(TCPChatConnection tcpCo) {
418
+        this.listTCPConnection.remove(tcpCo);
419
+        Socket link = tcpCo.getSocket();
420
+        tcpCo.interrupt();
421
+        try {
422
+            link.close();
423
+        } catch (IOException e) {
424
+            e.printStackTrace();
425
+        }
426
+    }
385 427
 
386 428
 
387 429
     /**

+ 1
- 1
src/main/java/app/insa/clav/Main.java View File

@@ -18,7 +18,7 @@ public class Main extends Application{
18 18
         int outPutPort = Integer.parseInt(args[1]);
19 19
         int tcpListenerPort = Integer.parseInt(args[2]);
20 20
 
21
-        Model model = Model.getInstance(inputPort, outPutPort,tcpListenerPort);
21
+        Model model = Model.getInstance(inputPort, outPutPort,tcpListenerPort, this);
22 22
 
23 23
         FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/fxml/splashScreen.fxml"));
24 24
         Parent root =fxmlLoader.load();

+ 2
- 0
src/main/java/app/insa/clav/Messages/Message.java View File

@@ -12,6 +12,8 @@ import java.net.InetAddress;
12 12
     4 ---> Messages.MessagePseudo si après type 1 aucun type 2 pour un delai, envoi confirmation pseudo
13 13
     5 ---> Message Init envoyé quand on se connecte en TCP à un user distant
14 14
     6 ---> MessageChatTxt pour le chat
15
+    7 ---> Message.MessagePseudo de deconnexion
16
+    8 ---> Message fermeture fenetre chat
15 17
  */
16 18
 
17 19
 /**

+ 32
- 3
src/main/java/app/insa/clav/Reseau/TCPChatConnection.java View File

@@ -1,4 +1,5 @@
1 1
 package app.insa.clav.Reseau;
2
+import app.insa.clav.Core.DataBaseAccess;
2 3
 import app.insa.clav.Messages.Message;
3 4
 import app.insa.clav.Messages.MessageChatTxt;
4 5
 import app.insa.clav.Messages.MessageInit;
@@ -30,6 +31,7 @@ public class TCPChatConnection extends Thread{
30 31
     PropertyChangeSupport support;
31 32
 
32 33
     public int remoteUserId;
34
+    public boolean exit = false;
33 35
 
34 36
 
35 37
     /**
@@ -71,6 +73,8 @@ public class TCPChatConnection extends Thread{
71 73
 
72 74
     public void addPropertyChangeListener(PropertyChangeListener pcl){
73 75
         this.support.addPropertyChangeListener("messageTextReceivedTCP",pcl);
76
+        this.support.addPropertyChangeListener("connectionChatClosed",pcl);
77
+
74 78
     }
75 79
 
76 80
 
@@ -85,7 +89,7 @@ public class TCPChatConnection extends Thread{
85 89
 
86 90
     @Override
87 91
     public void run() {
88
-        while (true){
92
+        while (!exit){
89 93
             Message msgReceived = null;
90 94
             try {
91 95
                 msgReceived = (Message) this.objectInStream.readObject();
@@ -94,8 +98,20 @@ public class TCPChatConnection extends Thread{
94 98
             } catch (ClassNotFoundException e) {
95 99
                 e.printStackTrace();
96 100
             }
97
-            this.msgReceivedBuffer.add(msgReceived);
98
-            this.support.firePropertyChange("messageTextReceivedTCP",true,false);
101
+            if (msgReceived.typeMessage == 8){
102
+                this.support.firePropertyChange("connectionChatClosed",true,false);
103
+                try {
104
+                    synchronized (this){
105
+                        this.wait();
106
+                    }
107
+                } catch (InterruptedException e) {
108
+                    exit = true;
109
+                }
110
+            }
111
+            else {
112
+                this.msgReceivedBuffer.add(msgReceived);
113
+                this.support.firePropertyChange("messageTextReceivedTCP", true, false);
114
+            }
99 115
         }
100 116
     }
101 117
 
@@ -107,4 +123,17 @@ public class TCPChatConnection extends Thread{
107 123
             e.printStackTrace();
108 124
         }
109 125
     }
126
+
127
+    public void sendCloseChat() {
128
+        Message msg = new Message(8,this.link.getLocalAddress(),this.link.getLocalPort(),this.link.getInetAddress(),this.link.getPort());
129
+        try {
130
+            this.objectOutStream.writeObject(msg);
131
+        } catch (IOException e) {
132
+            e.printStackTrace();
133
+        }
134
+    }
135
+
136
+    public Socket getSocket() {
137
+        return link;
138
+    }
110 139
 }

+ 22
- 0
src/main/java/app/insa/clav/UIControllers/ChatWindowController.java View File

@@ -11,11 +11,14 @@ import javafx.application.Platform;
11 11
 import javafx.collections.FXCollections;
12 12
 import javafx.collections.ObservableList;
13 13
 import javafx.event.ActionEvent;
14
+import javafx.event.EventHandler;
14 15
 import javafx.fxml.FXML;
15 16
 import javafx.fxml.Initializable;
16 17
 import javafx.scene.control.ListView;
17 18
 import javafx.scene.control.TextField;
18 19
 import javafx.scene.layout.AnchorPane;
20
+import javafx.stage.Stage;
21
+import javafx.stage.WindowEvent;
19 22
 
20 23
 import java.beans.PropertyChangeEvent;
21 24
 import java.beans.PropertyChangeListener;
@@ -85,6 +88,13 @@ public class ChatWindowController implements Initializable, PropertyChangeListen
85 88
                 MessageChatTxt msg = (MessageChatTxt) tcpCo.getMessageReceived();
86 89
                 String payload = msg.payload;
87 90
                 Platform.runLater(() -> this.listMessages.add(payload));
91
+                break;
92
+            case "connectionChatClosed":
93
+                tcpCo.sendCloseChat();
94
+                model.notifyCloseChat(tcpCo);
95
+                Stage mainStage = (Stage) rootAnchor.getScene().getWindow();
96
+                Platform.runLater(mainStage::close);
97
+                break;
88 98
         }
89 99
     }
90 100
 
@@ -95,4 +105,16 @@ public class ChatWindowController implements Initializable, PropertyChangeListen
95 105
         this.tcpCo.sendMessageTxt(payload);
96 106
         this.dbAccess.addMessage(this.localUserId,this.remoteUser.getId(),payload);
97 107
     }
108
+
109
+    public void setHandler() {
110
+        Stage mainStage = (Stage) rootAnchor.getScene().getWindow();
111
+        mainStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
112
+            @Override
113
+            public void handle(WindowEvent t) {
114
+                tcpCo.sendCloseChat();
115
+                model.notifyCloseChat(tcpCo);
116
+                Platform.runLater(mainStage::close);
117
+            }
118
+        });
119
+    }
98 120
 }

+ 7
- 0
src/main/java/app/insa/clav/UIControllers/MainDrawerController.java View File

@@ -33,6 +33,8 @@ public class MainDrawerController implements PropertyChangeListener,Initializabl
33 33
      */
34 34
     private PseudoStage pseudoWindow;
35 35
 
36
+    @FXML
37
+    private JFXButton disconnectButton;
36 38
 
37 39
     /**
38 40
      * constructor. Gets the Model
@@ -82,4 +84,9 @@ public class MainDrawerController implements PropertyChangeListener,Initializabl
82 84
                 break;
83 85
         }
84 86
     }
87
+
88
+    @FXML
89
+    void disconnectButtonHandler(ActionEvent event) {
90
+        model.sendDeconnectionMessage();
91
+    }
85 92
 }

+ 12
- 1
src/main/java/app/insa/clav/UIControllers/MainWindowController.java View File

@@ -1,5 +1,6 @@
1 1
 package app.insa.clav.UIControllers;
2 2
 
3
+import app.insa.clav.Core.DataBaseAccess;
3 4
 import app.insa.clav.Core.Model;
4 5
 import app.insa.clav.Core.Utilisateurs;
5 6
 import com.jfoenix.controls.JFXDrawer;
@@ -11,6 +12,7 @@ import javafx.application.Platform;
11 12
 import javafx.collections.FXCollections;
12 13
 import javafx.collections.ObservableList;
13 14
 import javafx.event.ActionEvent;
15
+import javafx.event.EventHandler;
14 16
 import javafx.fxml.FXML;
15 17
 import javafx.fxml.FXMLLoader;
16 18
 import javafx.fxml.Initializable;
@@ -20,8 +22,11 @@ import javafx.scene.input.MouseEvent;
20 22
 import javafx.scene.layout.AnchorPane;
21 23
 import javafx.scene.layout.StackPane;
22 24
 import javafx.scene.layout.VBox;
25
+import javafx.stage.Stage;
26
+import javafx.stage.WindowEvent;
23 27
 import javafx.util.Duration;
24 28
 
29
+import javax.xml.crypto.Data;
25 30
 import java.beans.PropertyChangeEvent;
26 31
 import java.beans.PropertyChangeListener;
27 32
 import java.io.IOException;
@@ -165,6 +170,12 @@ public class MainWindowController implements PropertyChangeListener, Initializab
165 170
 
166 171
     @FXML
167 172
     void openChat(ActionEvent event) {
168
-        model.createChatFromLocalRequest(userListView.getFocusModel().getFocusedItem());
173
+        String remotePseudo = userListView.getFocusModel().getFocusedItem();
174
+        int remoteId = model.getUserFromPseudo(remotePseudo).getId();
175
+        DataBaseAccess dbAccess = DataBaseAccess.getInstance();
176
+        if (!dbAccess.isTableCreated(model.user.getId(),remoteId)){
177
+            dbAccess.createChatTable(remoteId, model.user.getId());
178
+        }
179
+        model.createChatFromLocalRequest(remoteId, remotePseudo);
169 180
     }
170 181
 }

+ 9
- 0
src/main/java/app/insa/clav/UIControllers/SplashScreenController.java View File

@@ -1,7 +1,9 @@
1 1
 package app.insa.clav.UIControllers;
2 2
 
3
+import app.insa.clav.Core.Model;
3 4
 import javafx.animation.FadeTransition;
4 5
 import javafx.animation.RotateTransition;
6
+import javafx.event.EventHandler;
5 7
 import javafx.fxml.FXML;
6 8
 import javafx.fxml.FXMLLoader;
7 9
 import javafx.fxml.Initializable;
@@ -10,6 +12,7 @@ import javafx.scene.Scene;
10 12
 import javafx.scene.layout.StackPane;
11 13
 import javafx.scene.shape.Circle;
12 14
 import javafx.stage.Stage;
15
+import javafx.stage.WindowEvent;
13 16
 import javafx.util.Duration;
14 17
 
15 18
 import java.io.IOException;
@@ -91,6 +94,12 @@ public class SplashScreenController implements Initializable {
91 94
         fadeIn.setOnFinished((e) -> {
92 95
             fadeOut.play();
93 96
             this.mainStage = (Stage) rootSplash.getScene().getWindow();
97
+            mainStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
98
+                @Override
99
+                public void handle(WindowEvent t) {
100
+                    Model.getInstance().sendDeconnectionMessage();
101
+                }
102
+            });
94 103
             this.fxmlLoader = new FXMLLoader(getClass().getResource("/fxml/connectionScreen.fxml"));
95 104
             try {
96 105
                 this.rootMainScreen = fxmlLoader.load();

+ 1
- 0
src/main/java/app/insa/clav/UISubStages/ChatStage.java View File

@@ -25,6 +25,7 @@ public class ChatStage extends Stage {
25 25
             this.setMaxHeight(450);
26 26
             this.setMaxWidth(550);
27 27
             this.show();
28
+            ctrl.setHandler();
28 29
         } catch (IOException e) {
29 30
             e.printStackTrace();
30 31
         }

+ 23
- 2
src/main/resources/fxml/mainDrawerContent.fxml View File

@@ -10,7 +10,7 @@
10 10
 <?import javafx.scene.text.Font?>
11 11
 <?import org.kordamp.ikonli.javafx.FontIcon?>
12 12
 
13
-<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="150.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="app.insa.clav.UIControllers.MainDrawerController">
13
+<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="150.0" style="-fx-background-color: ea9c61;" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="app.insa.clav.UIControllers.MainDrawerController">
14 14
    <children>
15 15
       <ImageView fitHeight="171.0" fitWidth="150.0" pickOnBounds="true">
16 16
          <image>
@@ -26,7 +26,7 @@
26 26
                         <Insets left="20.0" top="20.0" />
27 27
                      </HBox.margin>
28 28
                      <font>
29
-                        <Font size="15.0" />
29
+                        <Font name="Ubuntu" size="15.0" />
30 30
                      </font>
31 31
                   </Label>
32 32
                   <FontIcon iconColor="#f52424" iconLiteral="fa-slack" iconSize="30" wrappingWidth="31.0">
@@ -38,5 +38,26 @@
38 38
             </HBox>
39 39
          </graphic>
40 40
       </JFXButton>
41
+      <JFXButton fx:id="disconnectButton" buttonType="RAISED" onAction="#disconnectButtonHandler" prefHeight="58.0" prefWidth="150.0" ripplerFill="#2cf2e1" style="-fx-background-color: ef9ff4;">
42
+         <graphic>
43
+            <HBox prefHeight="59.0" prefWidth="130.0">
44
+               <children>
45
+                  <Label prefHeight="22.0" prefWidth="106.0" text="Disconnect">
46
+                     <HBox.margin>
47
+                        <Insets top="20.0" />
48
+                     </HBox.margin>
49
+                     <font>
50
+                        <Font name="Ubuntu" size="15.0" />
51
+                     </font>
52
+                  </Label>
53
+                  <FontIcon iconColor="#d1b71b" iconLiteral="fa-close" iconSize="30" wrappingWidth="31.0">
54
+                     <HBox.margin>
55
+                        <Insets top="15.0" />
56
+                     </HBox.margin>
57
+                  </FontIcon>
58
+               </children>
59
+            </HBox>
60
+         </graphic>
61
+      </JFXButton>
41 62
    </children>
42 63
 </VBox>

Loading…
Cancel
Save