Browse Source

Added Servlet Connection

EyeXion 7 months ago
parent
commit
881394e030

+ 2
- 0
build.gradle View File

@@ -33,6 +33,8 @@ dependencies {
33 33
     runtimeOnly "org.openjfx:javafx-graphics:$javafx.version:win"
34 34
     runtimeOnly "org.openjfx:javafx-graphics:$javafx.version:linux"
35 35
     runtimeOnly "org.openjfx:javafx-graphics:$javafx.version:mac"
36
+    implementation 'com.google.code.gson:gson:2.8.6'
37
+
36 38
 }
37 39
 
38 40
 mainClassName = 'app.insa.clav.Launcher'

+ 57
- 23
src/main/java/app/insa/clav/Core/Model.java View File

@@ -4,10 +4,7 @@ import app.insa.clav.Messages.Message;
4 4
 import app.insa.clav.Messages.MessageChatTxt;
5 5
 import app.insa.clav.Messages.MessageInit;
6 6
 import app.insa.clav.Messages.MessagePseudo;
7
-import app.insa.clav.Reseau.TCPChatConnection;
8
-import app.insa.clav.Reseau.TCPListener;
9
-import app.insa.clav.Reseau.UDPInput;
10
-import app.insa.clav.Reseau.UDPOutput;
7
+import app.insa.clav.Reseau.*;
11 8
 import javafx.application.Application;
12 9
 import javafx.application.Platform;
13 10
 import jdk.jshell.execution.Util;
@@ -16,11 +13,9 @@ import java.beans.PropertyChangeEvent;
16 13
 import java.beans.PropertyChangeListener;
17 14
 import java.beans.PropertyChangeSupport;
18 15
 import java.io.IOException;
19
-import java.util.ArrayList;
16
+import java.nio.charset.StandardCharsets;
17
+import java.util.*;
20 18
 import java.net.*;
21
-import java.util.Collections;
22
-import java.util.Timer;
23
-import java.util.TimerTask;
24 19
 //Toutes les interactions avec l'utilisateur (pour tester)
25 20
 
26 21
 /**
@@ -32,6 +27,9 @@ public class Model implements PropertyChangeListener{
32 27
      * Liste des utilisateurs connectés.
33 28
      */
34 29
     private ArrayList<Utilisateurs> userList;
30
+
31
+    private ServletConnection servCon;
32
+
35 33
     /**
36 34
      * Timer qui permet de planifier des éxécutions dans le temps
37 35
      */
@@ -105,7 +103,7 @@ ID 2 -> Listening on 6002, sending on 5002, tcpServer on 7002
105 103
 */
106 104
     private Model(int inputPort, int outputPort, int tcpListenerPort, Application app){
107 105
         try {
108
-            this.user = new Utilisateurs("NA", InetAddress.getLocalHost(), 0, inputPort);
106
+            this.user = new Utilisateurs("NA", InetAddress.getLocalHost(), 0, inputPort, false);
109 107
             this.UDPOut = new UDPOutput(InetAddress.getLocalHost(), outputPort);
110 108
             this.UDPIn = new UDPInput(user.getInetAddress(),inputPort);
111 109
             this.tcpListener = new TCPListener(this.user.getInetAddress(),tcpListenerPort,user.getId());
@@ -120,6 +118,7 @@ ID 2 -> Listening on 6002, sending on 5002, tcpServer on 7002
120 118
         this.listTCPConnection = new ArrayList<TCPChatConnection>();
121 119
         this.dbAccess = DataBaseAccess.getInstance();
122 120
         this.app = app;
121
+        this.servCon = ServletConnection.getInstance();
123 122
     }
124 123
 
125 124
     /**
@@ -248,16 +247,47 @@ ID 2 -> Listening on 6002, sending on 5002, tcpServer on 7002
248 247
      * Méthode appelée par le controleur quand la vue envoie un signal d'appuis bouton changer Pseudo
249 248
      * @param pseudo
250 249
      *              Pseudo rentré par l'utilisateur
251
-     * @return
252 250
      */
253
-    public boolean choosePseudo(String pseudo, boolean isConfirmationNeeded){
251
+    public void choosePseudo(String pseudo, boolean isConfirmationNeeded){
254 252
         this.ancienPseudo = this.user.getPseudo();
255 253
         this.user.setPseudo(pseudo);
256
-        this.UDPIn.setFilterValue(2,true);
257
-        this.UDPIn.setFilterValue(3,true);
258
-        this.sendPseudoBroadcast();
259
-        this.tim.schedule(new TimerTaskResponseWait(isConfirmationNeeded),1000);
260
-        return true;
254
+        this.UDPIn.setFilterValue(2, true);
255
+        this.UDPIn.setFilterValue(3, true);
256
+        if (!this.user.isOutdoor()) {
257
+            this.sendPseudoBroadcast();
258
+            this.tim.schedule(new TimerTaskResponseWait(isConfirmationNeeded), 2000);
259
+            ArrayList<Utilisateurs> outdoorUsers = servCon.getRemoteActiveUsers();
260
+            for (Utilisateurs newUser : outdoorUsers) {
261
+                if (newUser.getPseudo().equals(this.user.getPseudo())) {
262
+                    this.isPseudoOk = false;
263
+                    this.user.setPseudo(this.ancienPseudo);
264
+                    this.ancienPseudo = "";
265
+                    this.support.firePropertyChange("pseudoRefused", this.user.getPseudo(), this.ancienPseudo);
266
+                }
267
+                if (!this.userList.contains(newUser)) {
268
+                    this.userList.add(newUser);
269
+                    Collections.sort(this.userList);
270
+                    this.support.firePropertyChange("newUserConnected", -1, -2);
271
+                }
272
+            }
273
+        }
274
+        else{
275
+            this.tim.schedule(new TimerTaskResponseWait(isConfirmationNeeded), 2000);
276
+            ArrayList<Utilisateurs> users = servCon.getAllActiveUsers();
277
+            for (Utilisateurs newUser : users) {
278
+                if (newUser.getPseudo().equals(this.user.getPseudo())) {
279
+                    this.isPseudoOk = false;
280
+                    this.user.setPseudo(this.ancienPseudo);
281
+                    this.ancienPseudo = "";
282
+                    this.support.firePropertyChange("pseudoRefused", this.user.getPseudo(), this.ancienPseudo);
283
+                }
284
+                if (!this.userList.contains(newUser)) {
285
+                    this.userList.add(newUser);
286
+                    Collections.sort(this.userList);
287
+                    this.support.firePropertyChange("newUserConnected", -1, -2);
288
+                }
289
+            }
290
+        }
261 291
     }
262 292
 
263 293
     /** Sets the id
@@ -331,7 +361,7 @@ ID 2 -> Listening on 6002, sending on 5002, tcpServer on 7002
331 361
                 break;
332 362
             case 2 :
333 363
                 MessagePseudo msgP2 = (MessagePseudo) msg;
334
-                Utilisateurs newUser2 = new Utilisateurs(msgP2.pseudo,msgP2.srcIP,msgP2.id,msgP2.srcResponsePort);
364
+                Utilisateurs newUser2 = new Utilisateurs(msgP2.pseudo,msgP2.srcIP,msgP2.id,msgP2.srcResponsePort,false);
335 365
                 if (!this.userList.contains(newUser2)) {
336 366
                     this.userList.add(newUser2);
337 367
                     Collections.sort(this.userList);
@@ -341,7 +371,7 @@ ID 2 -> Listening on 6002, sending on 5002, tcpServer on 7002
341 371
             case 3 :
342 372
                 System.out.println("Received message type 3");
343 373
                 MessagePseudo msgP3 = (MessagePseudo) msg;
344
-                Utilisateurs newUser3 = new Utilisateurs(msgP3.pseudo,msgP3.srcIP,msgP3.id,msgP3.srcResponsePort);
374
+                Utilisateurs newUser3 = new Utilisateurs(msgP3.pseudo,msgP3.srcIP,msgP3.id,msgP3.srcResponsePort,false);
345 375
                 if (!this.userList.contains(newUser3)) {
346 376
                     this.userList.add(newUser3);
347 377
                     Collections.sort(this.userList);
@@ -355,7 +385,7 @@ ID 2 -> Listening on 6002, sending on 5002, tcpServer on 7002
355 385
                 break;
356 386
             case 4:
357 387
                 MessagePseudo msgP4 = (MessagePseudo) msg;
358
-                Utilisateurs newUser4 = new Utilisateurs(msgP4.pseudo,msgP4.srcIP,msgP4.id,msgP4.srcResponsePort);
388
+                Utilisateurs newUser4 = new Utilisateurs(msgP4.pseudo,msgP4.srcIP,msgP4.id,msgP4.srcResponsePort,false);
359 389
                 this.userList.remove(newUser4);
360 390
                 this.userList.add(newUser4);
361 391
                 Collections.sort(this.userList);
@@ -363,7 +393,7 @@ ID 2 -> Listening on 6002, sending on 5002, tcpServer on 7002
363 393
                 break;
364 394
             case 7 :
365 395
                 MessagePseudo msgP7 = (MessagePseudo) msg;
366
-                Utilisateurs User7 = new Utilisateurs(msgP7.pseudo,msgP7.srcIP,msgP7.id,msgP7.srcResponsePort);
396
+                Utilisateurs User7 = new Utilisateurs(msgP7.pseudo,msgP7.srcIP,msgP7.id,msgP7.srcResponsePort,false);
367 397
                 this.userList.remove(User7);
368 398
                 this.support.firePropertyChange("newUserConnected",true,false);
369 399
             default :
@@ -486,8 +516,6 @@ ID 2 -> Listening on 6002, sending on 5002, tcpServer on 7002
486 516
         }
487 517
         this.dbAccess.deleteHistory(remoteId,user.getId());
488 518
     }
489
-
490
-
491 519
     /**
492 520
      * Classe interne au model pour au bout d'une seconde d'envoi de demande pseudo type 1,
493 521
      * on desactive les filtes et on met à jour la vue.
@@ -514,7 +542,13 @@ ID 2 -> Listening on 6002, sending on 5002, tcpServer on 7002
514 542
                 support.firePropertyChange("pseudoValide",ancienPseudo,user.getPseudo());
515 543
                 UDPIn.setFilterValue(1,true);
516 544
                 if (isConfirmationNeeded){
517
-                    sendPseudoValideBroadcast();
545
+                    if (!user.isOutdoor()) {
546
+                        sendPseudoValideBroadcast();
547
+                        servCon.submitConnectionIndoor(user);
548
+                    }
549
+                    else{
550
+                        servCon.submitConnectionOutdoor(user);
551
+                    }
518 552
                 }
519 553
             }
520 554
             else{

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

@@ -13,6 +13,7 @@ public class Utilisateurs implements Comparable{
13 13
     private int id;
14 14
     private int port;
15 15
     private String login;
16
+    private boolean isOutdoor;
16 17
 
17 18
     public Date getLatestUpdate() {
18 19
         return latestUpdate;
@@ -24,11 +25,12 @@ public class Utilisateurs implements Comparable{
24 25
 
25 26
     private Date latestUpdate;
26 27
 
27
-    public Utilisateurs(String pseudo, InetAddress inetAddress, int id, int port) {
28
+    public Utilisateurs(String pseudo, InetAddress inetAddress, int id, int port, boolean isOutdoor) {
28 29
         this.pseudo = pseudo;
29 30
         this.inetAddress = inetAddress;
30 31
         this.id = id;
31 32
         this.port = port;
33
+        this.isOutdoor = isOutdoor;
32 34
     }
33 35
 
34 36
     public void setPseudo(String pseudo) {
@@ -63,6 +65,16 @@ public class Utilisateurs implements Comparable{
63 65
         return port;
64 66
     }
65 67
 
68
+
69
+    public boolean isOutdoor() {
70
+        return isOutdoor;
71
+    }
72
+
73
+    public void setOutdoor(boolean outdoor) {
74
+        isOutdoor = outdoor;
75
+    }
76
+
77
+
66 78
     @Override
67 79
     public String toString(){
68 80
         return Integer.toString(this.id) + "|" + this.pseudo + "|" + this.inetAddress.toString() + "|" + Integer.toString(this.port);

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

@@ -0,0 +1,208 @@
1
+package app.insa.clav.Reseau;
2
+import app.insa.clav.Core.Utilisateurs;
3
+import app.insa.clav.Messages.MessagePseudo;
4
+
5
+import java.io.BufferedReader;
6
+import java.io.IOException;
7
+import java.io.InputStreamReader;
8
+import java.io.OutputStream;
9
+import java.lang.reflect.Type;
10
+import java.net.HttpURLConnection;
11
+import java.net.MalformedURLException;
12
+import java.net.URL;
13
+import java.nio.charset.StandardCharsets;
14
+import java.util.ArrayList;
15
+import java.util.Arrays;
16
+import java.util.List;
17
+
18
+import com.google.gson.Gson;
19
+import com.google.gson.GsonBuilder;
20
+import com.google.gson.reflect.TypeToken;
21
+
22
+public class ServletConnection {
23
+
24
+    private static ServletConnection instance = null;
25
+
26
+    public final GsonBuilder builder;
27
+    public final Gson gson;
28
+    public String baseURL;
29
+
30
+    public ServletConnection(){
31
+        builder = new GsonBuilder();
32
+        gson = builder.create();;
33
+        //baseURL = "https://srv-gei-tomcat.insa-toulouse.fr/ServletJiggly/";
34
+        baseURL = "http://localhost:8080/Gradle___com_example___ServeurClavardage_1_0_SNAPSHOT_war/";
35
+    }
36
+
37
+    public static ServletConnection getInstance(){
38
+        synchronized(ServletConnection.class){
39
+            if (instance == null) {
40
+                instance = new ServletConnection();
41
+            }
42
+        }
43
+        return instance;
44
+    }
45
+
46
+    public ArrayList<Utilisateurs> getRemoteActiveUsers(){
47
+        URL url = null;
48
+        HttpURLConnection con = null;
49
+        ArrayList<Utilisateurs> resList = null;
50
+        try {
51
+            url = new URL(baseURL + "getOutdoorUsers");
52
+        } catch (MalformedURLException e) {
53
+            e.printStackTrace();
54
+        }
55
+        try {
56
+            con = (HttpURLConnection) url.openConnection();
57
+            con.setRequestMethod("GET");
58
+            int status = con.getResponseCode();
59
+            System.out.println("Error code HTTP request = " + status);
60
+            BufferedReader in = new BufferedReader(
61
+                    new InputStreamReader(con.getInputStream()));
62
+            String inputLine;
63
+            StringBuffer content = new StringBuffer();
64
+            while ((inputLine = in.readLine()) != null) {
65
+                content.append(inputLine);
66
+            }
67
+            in.close();
68
+            System.out.println("Response : " + content);
69
+            Type listType = new TypeToken<ArrayList<Utilisateurs>>(){}.getType();
70
+            resList = gson.fromJson(content.toString(),listType);
71
+            //resList = new Utilisateurs[Integer.parseInt(con.getHeaderField("sizeArray"))];
72
+        } catch (IOException e) {
73
+            e.printStackTrace();
74
+        }
75
+        return resList;
76
+    }
77
+
78
+    public void submitConnectionIndoor(Utilisateurs user){
79
+        URL url = null;
80
+        HttpURLConnection con = null;
81
+        try {
82
+            url = new URL(baseURL + "submitConnectionIndoor");
83
+        } catch (MalformedURLException e) {
84
+            e.printStackTrace();
85
+        }
86
+
87
+        try {
88
+            con = (HttpURLConnection) url.openConnection();
89
+            con.setRequestMethod("POST");
90
+            con.setRequestProperty("Content-Type", "application/json; utf-8");
91
+            con.setDoOutput(true);
92
+            try(OutputStream os = con.getOutputStream()) {
93
+                byte[] input = gson.toJson(user).getBytes(StandardCharsets.UTF_8);
94
+                os.write(input, 0, input.length);
95
+            }
96
+            int status = con.getResponseCode();
97
+            System.out.println("Error code HTTP request submit indoor = " + status);
98
+        } catch (IOException e) {
99
+            e.printStackTrace();
100
+        }
101
+    }
102
+
103
+    public void submitConnectionOutdoor(Utilisateurs user) {
104
+        URL url = null;
105
+        HttpURLConnection con = null;
106
+        try {
107
+            url = new URL(baseURL + "submitConnectionOutdoor");
108
+        } catch (MalformedURLException e) {
109
+            e.printStackTrace();
110
+        }
111
+
112
+        try {
113
+            con = (HttpURLConnection) url.openConnection();
114
+            con.setRequestMethod("POST");
115
+            con.setRequestProperty("Content-Type", "application/json; utf-8");
116
+            con.setDoOutput(true);
117
+            try(OutputStream os = con.getOutputStream()) {
118
+                byte[] input = gson.toJson(user).getBytes(StandardCharsets.UTF_8);
119
+                os.write(input, 0, input.length);
120
+            }
121
+            int status = con.getResponseCode();
122
+            System.out.println("Error code HTTP request submit outdoor = " + status);
123
+        } catch (IOException e) {
124
+            e.printStackTrace();
125
+        }
126
+    }
127
+
128
+    public ArrayList<Utilisateurs> getAllActiveUsers() {
129
+        URL url = null;
130
+        HttpURLConnection con = null;
131
+        ArrayList<Utilisateurs> resList = null;
132
+        try {
133
+            url = new URL(baseURL + "getAllUsers");
134
+        } catch (MalformedURLException e) {
135
+            e.printStackTrace();
136
+        }
137
+        try {
138
+            con = (HttpURLConnection) url.openConnection();
139
+            con.setRequestMethod("GET");
140
+            int status = con.getResponseCode();
141
+            System.out.println("Error code HTTP request = " + status);
142
+            BufferedReader in = new BufferedReader(
143
+                    new InputStreamReader(con.getInputStream()));
144
+            String inputLine;
145
+            StringBuffer content = new StringBuffer();
146
+            while ((inputLine = in.readLine()) != null) {
147
+                content.append(inputLine);
148
+            }
149
+            in.close();
150
+            System.out.println("Response : " + content);
151
+            Type listType = new TypeToken<ArrayList<Utilisateurs>>(){}.getType();
152
+            resList = gson.fromJson(content.toString(),listType);
153
+        } catch (IOException e) {
154
+            e.printStackTrace();
155
+        }
156
+        return resList;
157
+    }
158
+
159
+    public void submitDeconnectionOutdoor(Utilisateurs user){
160
+        URL url = null;
161
+        HttpURLConnection con = null;
162
+        try {
163
+            url = new URL(baseURL + "submitDeconnectionOutdoor");
164
+        } catch (MalformedURLException e) {
165
+            e.printStackTrace();
166
+        }
167
+
168
+        try {
169
+            con = (HttpURLConnection) url.openConnection();
170
+            con.setRequestMethod("POST");
171
+            con.setRequestProperty("Content-Type", "application/json; utf-8");
172
+            con.setDoOutput(true);
173
+            try(OutputStream os = con.getOutputStream()) {
174
+                byte[] input = gson.toJson(user).getBytes(StandardCharsets.UTF_8);
175
+                os.write(input, 0, input.length);
176
+            }
177
+            int status = con.getResponseCode();
178
+            System.out.println("Error code HTTP request submit deco outdoor = " + status);
179
+        } catch (IOException e) {
180
+            e.printStackTrace();
181
+        }
182
+    }
183
+
184
+    public void submitDeconnectionIndoor(Utilisateurs user){
185
+        URL url = null;
186
+        HttpURLConnection con = null;
187
+        try {
188
+            url = new URL(baseURL + "submitDeconnectionIndoor");
189
+        } catch (MalformedURLException e) {
190
+            e.printStackTrace();
191
+        }
192
+
193
+        try {
194
+            con = (HttpURLConnection) url.openConnection();
195
+            con.setRequestMethod("POST");
196
+            con.setRequestProperty("Content-Type", "application/json; utf-8");
197
+            con.setDoOutput(true);
198
+            try(OutputStream os = con.getOutputStream()) {
199
+                byte[] input = gson.toJson(user).getBytes(StandardCharsets.UTF_8);
200
+                os.write(input, 0, input.length);
201
+            }
202
+            int status = con.getResponseCode();
203
+            System.out.println("Error code HTTP request submit deco indoor = " + status);
204
+        } catch (IOException e) {
205
+            e.printStackTrace();
206
+        }
207
+    }
208
+}

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

@@ -4,6 +4,7 @@ import app.insa.clav.Core.DataBaseAccess;
4 4
 import app.insa.clav.Core.Model;
5 5
 import app.insa.clav.Core.Utilisateurs;
6 6
 import com.jfoenix.controls.JFXButton;
7
+import com.jfoenix.controls.JFXToggleButton;
7 8
 import javafx.application.Platform;
8 9
 import javafx.collections.FXCollections;
9 10
 import javafx.event.ActionEvent;
@@ -117,6 +118,13 @@ public class ConnectionScreenController implements Initializable, PropertyChange
117 118
      */
118 119
     private boolean isSubmittingNewPseudoIn;
119 120
 
121
+
122
+    /**
123
+     * True if the user is outdoor
124
+     */
125
+    @FXML
126
+    private JFXToggleButton isOutdoorUserButton;
127
+
120 128
     private DataBaseAccess dbAccess;
121 129
 
122 130
     private String loginUp;
@@ -193,6 +201,11 @@ public class ConnectionScreenController implements Initializable, PropertyChange
193 201
     }
194 202
 
195 203
 
204
+    @FXML
205
+    void outdoorUserAction(ActionEvent event) {
206
+        this.model.user.setOutdoor(this.isOutdoorUserButton.isSelected());
207
+    }
208
+
196 209
     /**
197 210
      * Handler of notifications sent by te model
198 211
      * @param evt
@@ -204,7 +217,7 @@ public class ConnectionScreenController implements Initializable, PropertyChange
204 217
                 this.model.deletePropertyChangeListener(this,"pseudoValide");
205 218
                 this.model.deletePropertyChangeListener(this,"pseudoRefused");
206 219
                 System.out.println("Pseudo valide");
207
-                if (isSubmittingUp){
220
+                if (isSubmittingUp && !this.model.user.isOutdoor()){
208 221
                     this.pseudoValideUp();
209 222
                 }
210 223
                 if (this.isSubmittingNewPseudoIn){

+ 2
- 0
src/main/resources/fxml/connectionScreen.fxml View File

@@ -1,6 +1,7 @@
1 1
 <?xml version="1.0" encoding="UTF-8"?>
2 2
 
3 3
 <?import com.jfoenix.controls.JFXButton?>
4
+<?import com.jfoenix.controls.JFXToggleButton?>
4 5
 <?import javafx.scene.control.Label?>
5 6
 <?import javafx.scene.control.ProgressIndicator?>
6 7
 <?import javafx.scene.control.TextField?>
@@ -50,5 +51,6 @@
50 51
             <Font name="Ubuntu" size="14.0" />
51 52
          </font>
52 53
       </Label>
54
+      <JFXToggleButton fx:id="isOutdoorUserButton" layoutX="-9.0" onAction="#outdoorUserAction" prefHeight="24.0" prefWidth="194.0" text="Outdoor User" />
53 55
    </children>
54 56
 </AnchorPane>

Loading…
Cancel
Save