Browse Source

Version Ok Serveur présence et gestion des crash OK

Faure Paul 7 months ago
parent
commit
49c388523c

+ 1
- 0
.idea/gradle.xml View File

@@ -6,6 +6,7 @@
6 6
       <GradleProjectSettings>
7 7
         <option name="delegatedBuild" value="true" />
8 8
         <option name="testRunner" value="GRADLE" />
9
+        <option name="disableWrapperSourceDistributionNotification" value="true" />
9 10
         <option name="distributionType" value="DEFAULT_WRAPPED" />
10 11
         <option name="externalProjectPath" value="$PROJECT_DIR$" />
11 12
         <option name="gradleJvm" value="11" />

+ 0
- 8
.idea/modules.xml View File

@@ -1,8 +0,0 @@
1
-<?xml version="1.0" encoding="UTF-8"?>
2
-<project version="4">
3
-  <component name="ProjectModuleManager">
4
-    <modules>
5
-      <module fileurl="file://$PROJECT_DIR$/.idea/Clavardage.iml" filepath="$PROJECT_DIR$/.idea/Clavardage.iml" group="Clavardage" />
6
-    </modules>
7
-  </component>
8
-</project>

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


+ 187
- 33
src/main/java/app/insa/clav/Core/Model.java View File

@@ -24,7 +24,7 @@ public class Model implements PropertyChangeListener{
24 24
     /**
25 25
      * Liste des utilisateurs connectés.
26 26
      */
27
-    private ArrayList<Utilisateurs> userList;
27
+    private List<Utilisateurs> userList;
28 28
 
29 29
     private ServletConnection servCon;
30 30
 
@@ -32,6 +32,7 @@ public class Model implements PropertyChangeListener{
32 32
      * Timer qui permet de planifier des éxécutions dans le temps
33 33
      */
34 34
     private Timer tim;
35
+    private Timer tim2s;
35 36
 
36 37
     /**
37 38
      * Notre interface UDP pour envoyer des messages
@@ -93,13 +94,14 @@ public class Model implements PropertyChangeListener{
93 94
             this.tcpListener = new TCPListener(user.getId());
94 95
             this.user.setTcpListeningPort(this.tcpListener.getPort());
95 96
             this.tim= new Timer();
97
+            this.tim2s= new Timer();
96 98
             this.support = new PropertyChangeSupport(this);
97 99
         }
98 100
         catch (IOException e){
99 101
             System.out.println("IOException dans la creation du modele");
100 102
             e.printStackTrace();
101 103
         }
102
-        this.userList = new ArrayList<Utilisateurs>();
104
+        this.userList = Collections.synchronizedList(new ArrayList<>());
103 105
         this.listTCPConnection = new ArrayList<TCPChatConnection>();
104 106
         this.dbAccess = DataBaseAccess.getInstance(addrBdd, userBdd, mdpBdd, nomBdd);
105 107
         this.app = app;
@@ -269,10 +271,12 @@ public class Model implements PropertyChangeListener{
269 271
                 this.ancienPseudo = "";
270 272
                 this.support.firePropertyChange("pseudoRefused", this.user.getPseudo(), this.ancienPseudo);
271 273
             }
272
-            if (!this.userList.contains(newUser)) {
273
-                this.userList.add(newUser);
274
-                Collections.sort(this.userList);
275
-                this.support.firePropertyChange("newUserConnected", -1, -2);
274
+            synchronized (userList) {
275
+                if (!this.userList.contains(newUser)) {
276
+                    this.userList.add(newUser);
277
+                    Collections.sort(this.userList);
278
+                    this.support.firePropertyChange("newUserConnected", -1, -2);
279
+                }
276 280
             }
277 281
         }
278 282
     }
@@ -294,10 +298,12 @@ public class Model implements PropertyChangeListener{
294 298
                 this.ancienPseudo = "";
295 299
                 this.support.firePropertyChange("pseudoRefused", this.user.getPseudo(), this.ancienPseudo);
296 300
             }
297
-            if (!this.userList.contains(newUser)) {
298
-                this.userList.add(newUser);
299
-                Collections.sort(this.userList);
300
-                this.support.firePropertyChange("newUserConnected", -1, -2);
301
+            synchronized (userList) {
302
+                if (!this.userList.contains(newUser)) {
303
+                    this.userList.add(newUser);
304
+                    Collections.sort(this.userList);
305
+                    this.support.firePropertyChange("newUserConnected", -1, -2);
306
+                }
301 307
             }
302 308
         }
303 309
     }
@@ -323,11 +329,13 @@ public class Model implements PropertyChangeListener{
323 329
             }
324 330
         }
325 331
         if (!isChatAlreadyCreated) {
326
-            for (Utilisateurs u : userList) {
327
-                if (u.getPseudo().equals(remotePseudo)) {
328
-                    MessageInit msgInit = new MessageInit(7, user.getInetAddress(), u.getInetAddress(), u.getTcpListeningPort(), user.getId());
329
-                    TCPChatConnection tcpCo = new TCPChatConnection(msgInit,u.getInetAddress(), u.getTcpListeningPort(), u.getId());
330
-                    listTCPConnection.add(tcpCo);
332
+            synchronized (userList) {
333
+                for (Utilisateurs u : userList) {
334
+                    if (u.getPseudo().equals(remotePseudo)) {
335
+                        MessageInit msgInit = new MessageInit(7, user.getInetAddress(), u.getInetAddress(), u.getTcpListeningPort(), user.getId());
336
+                        TCPChatConnection tcpCo = new TCPChatConnection(msgInit, u.getInetAddress(), u.getTcpListeningPort(), u.getId());
337
+                        listTCPConnection.add(tcpCo);
338
+                    }
331 339
                 }
332 340
             }
333 341
         }
@@ -368,10 +376,12 @@ public class Model implements PropertyChangeListener{
368 376
                 //System.out.println("Message de type 2 reçu : " + msgP2.toString());
369 377
                 Utilisateurs newUser2 = new Utilisateurs(msgP2.pseudo,msgP2.srcIP,msgP2.id, msgP2.portEcouteTCP);
370 378
                 //System.out.println("Utilisateur créé : " + newUser2.toString());
371
-                if (!this.userList.contains(newUser2)) {
372
-                    this.userList.add(newUser2);
373
-                    Collections.sort(this.userList);
374
-                    this.support.firePropertyChange("newUserConnected",-1,-2);
379
+                synchronized (userList) {
380
+                    if (!this.userList.contains(newUser2)) {
381
+                        this.userList.add(newUser2);
382
+                        Collections.sort(this.userList);
383
+                        this.support.firePropertyChange("newUserConnected", -1, -2);
384
+                    }
375 385
                 }
376 386
                 break;
377 387
             case 3 :
@@ -388,17 +398,22 @@ public class Model implements PropertyChangeListener{
388 398
                 //System.out.println("Message de type 4 reçu : " + msgP4.toString());
389 399
                 if (msgP4.id != this.user.getId()) {
390 400
                     Utilisateurs newUser4 = new Utilisateurs(msgP4.pseudo,msgP4.srcIP,msgP4.id, msgP4.portEcouteTCP);
401
+                    newUser4.setOutdoor(false);
391 402
                     //System.out.println("Utilisateur créé : " + newUser4.toString());
392
-                    this.userList.remove(newUser4);
393
-                    this.userList.add(newUser4);
394
-                    Collections.sort(this.userList);
403
+                    synchronized (userList) {
404
+                        this.userList.remove(newUser4);
405
+                        this.userList.add(newUser4);
406
+                        Collections.sort(this.userList);
407
+                    }
395 408
                     this.support.firePropertyChange("newUserConnected",-1,newUser4.getId());
396 409
                 }
397 410
                 break;
398 411
             case 7 :
399 412
                 MessagePseudo msgP7 = (MessagePseudo) msg;
400 413
                 Utilisateurs User7 = new Utilisateurs(msgP7.pseudo,msgP7.srcIP,msgP7.id, msgP7.portEcouteTCP);
401
-                this.userList.remove(User7);
414
+                synchronized (userList) {
415
+                    this.userList.remove(User7);
416
+                }
402 417
                 this.support.firePropertyChange("newUserConnected",-1,-2);
403 418
                 break;
404 419
             default :
@@ -431,8 +446,10 @@ public class Model implements PropertyChangeListener{
431 446
     /**
432 447
      * @return the list of users
433 448
      */
434
-    public ArrayList<Utilisateurs> getUserList(){
435
-        return userList;
449
+    public List<Utilisateurs> getUserList(){
450
+        synchronized (userList) {
451
+            return userList;
452
+        }
436 453
     }
437 454
 
438 455
     /** returns connected the user identified by the id
@@ -441,10 +458,12 @@ public class Model implements PropertyChangeListener{
441 458
      */
442 459
     public Utilisateurs getUserFromId(int id){
443 460
         Utilisateurs res = null;
444
-        for (Utilisateurs u : userList){
445
-            if (u.getId() == id){
446
-                res = u;
447
-                break;
461
+        synchronized (userList) {
462
+            for (Utilisateurs u : userList) {
463
+                if (u.getId() == id) {
464
+                    res = u;
465
+                    break;
466
+                }
448 467
             }
449 468
         }
450 469
         return res;
@@ -457,10 +476,12 @@ public class Model implements PropertyChangeListener{
457 476
      */
458 477
     public Utilisateurs getUserFromPseudo(String pseudo){
459 478
         Utilisateurs res = null;
460
-        for (Utilisateurs u : userList){
461
-            if (u.getPseudo().equals(pseudo)){
462
-                res = u;
463
-                break;
479
+        synchronized (userList) {
480
+            for (Utilisateurs u : userList) {
481
+                if (u.getPseudo().equals(pseudo)) {
482
+                    res = u;
483
+                    break;
484
+                }
464 485
             }
465 486
         }
466 487
         return res;
@@ -476,6 +497,7 @@ public class Model implements PropertyChangeListener{
476 497
             servCon.submitDeconnectionIndoor(this.user);
477 498
         }
478 499
         else{
500
+            //System.out.println("Send deco Outdoor sent");
479 501
             servCon.submitDeconnectionOutdoor(this.user);
480 502
         }
481 503
         try {
@@ -518,6 +540,13 @@ public class Model implements PropertyChangeListener{
518 540
         }
519 541
         this.dbAccess.deleteHistory(remoteId,user.getId());
520 542
     }
543
+
544
+    public void startTim2s() {
545
+        tim2s.scheduleAtFixedRate(new TimerTaskCheckUsers(), 2000, 2000);
546
+    }
547
+
548
+
549
+
521 550
     /**
522 551
      * Classe interne au model pour au bout d'une seconde d'envoi de demande pseudo type 1,
523 552
      * on desactive les filtes et on met à jour la vue.
@@ -562,4 +591,129 @@ public class Model implements PropertyChangeListener{
562 591
             }
563 592
         }
564 593
     }
594
+
595
+    /**
596
+     * Classe interne au model check si tout les utilisateurs sont toujours présents
597
+     */
598
+    class TimerTaskCheckUsers extends TimerTask {
599
+        public void run() {
600
+            //System.out.println(user);
601
+            synchronized (userList) {
602
+                Date currentDate = new Date();
603
+                if (user.isOutdoor()) {
604
+                    System.out.println("Timer task 2s Outdoor");
605
+                    servCon.submitConnectionOutdoor(user);
606
+                    ArrayList<Utilisateurs> servUserList = servCon.getAllActiveUsers();
607
+                    ArrayList<Utilisateurs> userListToRemove = new ArrayList<>();
608
+                    ArrayList<Utilisateurs> userListToAdd = new ArrayList<>();
609
+                    ArrayList<Utilisateurs> userListWatched = new ArrayList<>();
610
+                    for (Iterator<Utilisateurs> iter = userList.iterator(); iter.hasNext(); ) {
611
+                        Utilisateurs userloop = iter.next();
612
+                        Utilisateurs userfound = null;
613
+                        for (Utilisateurs userExtloop : servUserList) {
614
+                            if (userExtloop.getId() == userloop.getId()) {
615
+                                userfound = userExtloop;
616
+                                break;
617
+                            }
618
+                        }
619
+                        if (userfound == null) {
620
+                            userListToRemove.add(userloop);
621
+                        } else {
622
+                            userListWatched.add(userfound);
623
+                            if (userfound.userToOld(currentDate)) {
624
+                                userListToRemove.add(userloop);
625
+                                if (userfound.isOutdoor()) {
626
+                                    servCon.submitDeconnectionOutdoor(userfound);
627
+                                } else {
628
+                                    servCon.submitDeconnectionIndoor(userfound);
629
+                                }
630
+                            } else {
631
+                                userListToRemove.add(userloop);
632
+                                userListToAdd.add(userfound);
633
+                            }
634
+                        }
635
+                    }
636
+                    userList.removeAll(userListToRemove);
637
+                    servUserList.removeAll(userListWatched);
638
+                    servUserList.remove(user);
639
+                    userListToAdd.addAll(servUserList);
640
+                    userList.addAll(userListToAdd);
641
+                } else {
642
+                    // On previent qu'on existe
643
+                    System.out.println("Timer task 2s Indoor");
644
+                    System.out.println("UserList " + userList.toString());
645
+                    sendPseudoValideBroadcast();
646
+                    servCon.submitConnectionIndoor(user);
647
+
648
+                    ArrayList<Utilisateurs> utilisateursExternes = servCon.getRemoteActiveUsers();
649
+                    ArrayList<Utilisateurs> userListToRemove = new ArrayList<>();
650
+                    ArrayList<Utilisateurs> userListToAdd = new ArrayList<>();
651
+                    ArrayList<Utilisateurs> userListExternWatched = new ArrayList<>();
652
+                    for (Iterator<Utilisateurs> iter = userList.iterator(); iter.hasNext(); ) {
653
+                        Utilisateurs userloop = iter.next();
654
+                        if (userloop.isOutdoor()) {
655
+                            Utilisateurs userfound = null;
656
+                            for (Utilisateurs userExtloop : utilisateursExternes) {
657
+                                if (userExtloop.getId() == userloop.getId()) {
658
+                                    userfound = userExtloop;
659
+                                    break;
660
+                                }
661
+                            }
662
+                            if (userfound == null) {
663
+                                userListToRemove.add(userloop);
664
+                            } else {
665
+                                userListExternWatched.add(userfound);
666
+                                if (userfound.userToOld(currentDate)) {
667
+                                    userListToRemove.add(userloop);
668
+                                    servCon.submitDeconnectionOutdoor(userfound);
669
+                                } else {
670
+                                    userListToRemove.add(userloop);
671
+                                    userListToAdd.add(userfound);
672
+                                }
673
+                            }
674
+                        } else {
675
+                            if (userloop.userToOld(currentDate)) {
676
+                                userListToRemove.add(userloop);
677
+                                servCon.submitDeconnectionIndoor(userloop);
678
+                            }
679
+                        }
680
+                    }
681
+                    userList.removeAll(userListToRemove);
682
+                    utilisateursExternes.removeAll(userListExternWatched);
683
+                    userListToAdd.addAll(utilisateursExternes);
684
+                    userList.addAll(userListToAdd);
685
+                }
686
+            }
687
+
688
+            Collections.sort(userList);
689
+            support.firePropertyChange("newUserConnected",-1,-2);
690
+        }
691
+    }
692
+
693
+    /** if (user.isOutdoor()) {
694
+     userList = servCon.getAllActiveUsers();
695
+     } else {
696
+     ArrayList<Utilisateurs> userListToRemove = new ArrayList<>();
697
+     for (Iterator<Utilisateurs> iter = userList.iterator(); iter.hasNext(); ) {
698
+     Utilisateurs userloop = iter.next();
699
+     if (userloop.isOutdoor()) {
700
+     //System.out.println("OUAIP CA DECNNE");
701
+     userListToRemove.add(userloop);
702
+     }
703
+     }
704
+     //System.out.println(userListToRemove);
705
+     userList.removeAll(userListToRemove);
706
+     userList.addAll(servCon.getRemoteActiveUsers());
707
+     }
708
+     support.firePropertyChange("newUserConnected",-1,-2);
709
+     *
710
+     *
711
+     *
712
+     *
713
+     *
714
+     *
715
+     *
716
+     *
717
+*/
565 718
 }
719
+

+ 18
- 1
src/main/java/app/insa/clav/Core/Utilisateurs.java View File

@@ -1,6 +1,7 @@
1 1
 package app.insa.clav.Core;
2 2
 
3 3
 import java.net.*;
4
+import java.util.Date;
4 5
 
5 6
 
6 7
 //Classe qui permet d'identifier un utilisateur
@@ -11,7 +12,7 @@ public class Utilisateurs implements Comparable{
11 12
     private int id;
12 13
     private String login;
13 14
     private boolean isOutdoor;
14
-
15
+    private Date lastUpdate;
15 16
     private int tcpListeningPort;
16 17
 
17 18
 
@@ -21,6 +22,8 @@ public class Utilisateurs implements Comparable{
21 22
         this.id = id;
22 23
         this.tcpListeningPort = tcpListeningPort;
23 24
         this.isOutdoor = true;
25
+        this.lastUpdate = new Date();
26
+        System.out.println("Date du jour " + this.lastUpdate.toString());
24 27
     }
25 28
 
26 29
     public void setPseudo(String pseudo) {
@@ -71,6 +74,18 @@ public class Utilisateurs implements Comparable{
71 74
         return tcpListeningPort;
72 75
     }
73 76
 
77
+    public void update() {
78
+        this.lastUpdate = new Date();
79
+    }
80
+
81
+    public boolean userToOld(Date date) {
82
+        System.out.println("Local Date : " + this.lastUpdate.toString());
83
+        System.out.println("Local time : " + this.lastUpdate.getTime());
84
+        System.out.println("Remote Date : " + date.toString());
85
+        System.out.println("Remote Time : " + date.getTime());
86
+        return (date.getTime() - this.lastUpdate.getTime() > 4000);
87
+    }
88
+
74 89
     @Override
75 90
     public String toString() {
76 91
         return "Utilisateurs{" +
@@ -78,6 +93,8 @@ public class Utilisateurs implements Comparable{
78 93
                 ", inetAddress=" + inetAddress +
79 94
                 ", id=" + id +
80 95
                 ", login='" + login + '\'' +
96
+                ", isOutdoor=" + isOutdoor +
97
+                ", lastUpdate=" + lastUpdate +
81 98
                 ", tcpListeningPort=" + tcpListeningPort +
82 99
                 '}';
83 100
     }

+ 1
- 0
src/main/java/app/insa/clav/Reseau/ServletConnection.java View File

@@ -85,6 +85,7 @@ public class ServletConnection {
85 85
     }
86 86
 
87 87
     public void submitConnectionIndoor(Utilisateurs user){
88
+        System.out.println("On envoi l'user au serveur avec " + user.toString());
88 89
         URL url = null;
89 90
         HttpURLConnection con = null;
90 91
         try {

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

@@ -279,6 +279,9 @@ public class ChatWindowController implements Initializable, PropertyChangeListen
279 279
      */
280 280
     private void getHistory(){
281 281
         this.dbAccess = DataBaseAccess.getInstance();
282
+        if (remoteUser == null) {
283
+            System.out.println("\n\n\nATTTENNNNTIIONNN\n \n\n");
284
+        }
282 285
         ArrayList<MessageDisplay> history = this.dbAccess.getMessageHistory(this.localUserId,remoteUser.getId());
283 286
         this.listMessages = FXCollections.observableList(history);
284 287
         this.messageList.setItems(this.listMessages);

+ 1
- 0
src/main/java/app/insa/clav/UIControllers/ConnectionScreenController.java View File

@@ -250,6 +250,7 @@ public class ConnectionScreenController implements Initializable, PropertyChange
250 250
                 }
251 251
                 this.isSubmittingUp = false;
252 252
                 this.isSubmittingIn = false;
253
+                this.model.startTim2s();
253 254
                 Platform.runLater(new Runnable() {
254 255
                     @Override
255 256
                     public void run() {

+ 4
- 2
src/main/java/app/insa/clav/UIControllers/MainWindowController.java View File

@@ -176,9 +176,11 @@ public class MainWindowController implements PropertyChangeListener, Initializab
176 176
     @FXML
177 177
     void openChat(ActionEvent event) {
178 178
         String remotePseudo = userListView.getFocusModel().getFocusedItem();
179
-        int remoteId = model.getUserFromPseudo(remotePseudo).getId();
179
+        System.out.println("Pseudo " + remotePseudo);
180
+        Utilisateurs remoteUser = model.getUserFromPseudo(remotePseudo);
181
+        int remoteId = remoteUser.getId();
180 182
         DataBaseAccess dbAccess = DataBaseAccess.getInstance();
181
-        if (!dbAccess.isTableCreated(model.user.getId(),remoteId)){
183
+        if (!dbAccess.isTableCreated(model.user.getId(), remoteId)) {
182 184
             dbAccess.createChatTable(remoteId, model.user.getId());
183 185
         }
184 186
         model.createChatFromLocalRequest(remoteId, remotePseudo);

Loading…
Cancel
Save