Browse Source

Version Ok tentative proxy

Faure Paul 3 years ago
parent
commit
9121b146b1

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


+ 43
- 16
src/main/java/app/insa/clav/Core/Model.java View File

@@ -1,8 +1,6 @@
1 1
 package app.insa.clav.Core;
2 2
 
3
-import app.insa.clav.Messages.Message;
4
-import app.insa.clav.Messages.MessageInit;
5
-import app.insa.clav.Messages.MessagePseudo;
3
+import app.insa.clav.Messages.*;
6 4
 import app.insa.clav.Reseau.*;
7 5
 import javafx.application.Application;
8 6
 import javafx.application.Platform;
@@ -11,6 +9,9 @@ import java.beans.PropertyChangeEvent;
11 9
 import java.beans.PropertyChangeListener;
12 10
 import java.beans.PropertyChangeSupport;
13 11
 import java.io.IOException;
12
+import java.io.ObjectInputStream;
13
+import java.io.ObjectOutputStream;
14
+import java.io.OutputStream;
14 15
 import java.nio.charset.StandardCharsets;
15 16
 import java.util.*;
16 17
 import java.net.*;
@@ -329,12 +330,25 @@ public class Model implements PropertyChangeListener{
329 330
             }
330 331
         }
331 332
         if (!isChatAlreadyCreated) {
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);
333
+            Utilisateurs remoteUser = this.getUserFromId(remoteId);
334
+            if (remoteUser.isOutdoor() || this.user.isOutdoor()) {
335
+                System.out.println("Creation d'un chat avec un outdoor");
336
+                MessageInit msgInit = new MessageInit(7, user.getInetAddress(), remoteUser.getInetAddress(), remoteUser.getTcpListeningPort(), user.getId());
337
+                this.servCon.SubmitConnectionChat(remoteUser.getId(), msgInit);
338
+                TCPChatConnection tcpCo = new TCPChatConnection(null, remoteId, this.user.getId(), null, null, null, null);
339
+                tcpCo.setOutdoor();
340
+                tcpCo.startTCPCo();
341
+                listTCPConnection.add(tcpCo);
342
+            } else {
343
+                System.out.println("Creattion d'un chat avec un indoor");
344
+                synchronized (userList) {
345
+                    for (Utilisateurs u : userList) {
346
+                        if (u.getPseudo().equals(remotePseudo)) {
347
+                            MessageInit msgInit = new MessageInit(7, user.getInetAddress(), u.getInetAddress(), u.getTcpListeningPort(), user.getId());
348
+                            TCPChatConnection tcpCo = new TCPChatConnection(msgInit, u.getInetAddress(), u.getTcpListeningPort(), u.getId());
349
+                            tcpCo.startTCPCo();
350
+                            listTCPConnection.add(tcpCo);
351
+                        }
338 352
                     }
339 353
                 }
340 354
             }
@@ -428,18 +442,16 @@ public class Model implements PropertyChangeListener{
428 442
      */
429 443
     private void handleType1Message(MessagePseudo msg){
430 444
         MessagePseudo msgResponse;
431
-        //System.out.println("Message de type 1 reçu : " + msg.toString());
432
-        if (msg.id != this.user.getId()) {
433
-            if (this.user.getPseudo().equals(msg.pseudo)){
445
+        System.out.println("Message de type 1 reçu : " + msg.toString() + " local addr IP " + this.user.getInetAddress());
446
+        if (!this.user.getInetAddress().equals(msg.srcIP)) {
447
+            if (this.user.getPseudo().equals(msg.pseudo)) {
434 448
                 msgResponse = new MessagePseudo(3, null, msg.pseudo, 0, 0);
435 449
                 //System.out.println("Pseudo pas OK, on envoi : " + msgResponse.toString());
436
-            } else{
437
-                msgResponse = new MessagePseudo(2, this.user.getInetAddress(),this.user.getPseudo(), this.user.getTcpListeningPort(), this.user.getId());
450
+            } else {
451
+                msgResponse = new MessagePseudo(2, this.user.getInetAddress(), this.user.getPseudo(), this.user.getTcpListeningPort(), this.user.getId());
438 452
                 //System.out.println("Pseudo OK, on envoi : " + msgResponse.toString());
439 453
             }
440 454
             this.UDPOut.sendMsg(msgResponse, msg.srcIP);
441
-        } else {
442
-            //System.out.println("Moi-même -> IGNORED");
443 455
         }
444 456
     }
445 457
 
@@ -546,6 +558,19 @@ public class Model implements PropertyChangeListener{
546 558
     }
547 559
 
548 560
 
561
+    public void recupAndHandleConnexion() {
562
+        ArrayList<MessageInit> messages = this.servCon.GetConnectionChat(this.user.getId());
563
+        if (messages != null) {
564
+            for (Iterator<MessageInit> iter = messages.iterator(); iter.hasNext(); ) {
565
+                MessageInit msg = iter.next();
566
+                TCPChatConnection newCo = new TCPChatConnection(null, msg.id, this.user.getId(), null, null, null, null);
567
+                newCo.setOutdoor();
568
+                newCo.startTCPCo();
569
+                this.listTCPConnection.add(newCo);
570
+            }
571
+        }
572
+    }
573
+
549 574
 
550 575
     /**
551 576
      * Classe interne au model pour au bout d'une seconde d'envoi de demande pseudo type 1,
@@ -687,6 +712,8 @@ public class Model implements PropertyChangeListener{
687 712
 
688 713
             Collections.sort(userList);
689 714
             support.firePropertyChange("newUserConnected",-1,-2);
715
+
716
+            recupAndHandleConnexion();
690 717
         }
691 718
     }
692 719
 

+ 26
- 0
src/main/java/app/insa/clav/Messages/MessageSrvTCP.java View File

@@ -0,0 +1,26 @@
1
+package app.insa.clav.Messages;
2
+
3
+
4
+public class MessageSrvTCP {
5
+    private int userId;
6
+    private int id;
7
+    private Message message;
8
+
9
+    public MessageSrvTCP(int userId, int id, Message message) {
10
+        this.userId = userId;
11
+        this.id = id;
12
+        this.message = message;
13
+    }
14
+
15
+    public int getUserId() {
16
+        return userId;
17
+    }
18
+
19
+    public int getId() {
20
+        return id;
21
+    }
22
+
23
+    public Message getMessage() {
24
+        return message;
25
+    }
26
+}

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

@@ -1,5 +1,7 @@
1 1
 package app.insa.clav.Reseau;
2 2
 import app.insa.clav.Core.Utilisateurs;
3
+import app.insa.clav.Messages.Message;
4
+import app.insa.clav.Messages.MessageInit;
3 5
 import app.insa.clav.Messages.MessagePseudo;
4 6
 
5 7
 import java.io.BufferedReader;
@@ -15,6 +17,7 @@ import java.util.ArrayList;
15 17
 import java.util.Arrays;
16 18
 import java.util.List;
17 19
 
20
+import app.insa.clav.Messages.MessageSrvTCP;
18 21
 import com.google.gson.Gson;
19 22
 import com.google.gson.GsonBuilder;
20 23
 import com.google.gson.reflect.TypeToken;
@@ -215,4 +218,134 @@ public class ServletConnection {
215 218
             e.printStackTrace();
216 219
         }
217 220
     }
221
+
222
+    public void SubmitMessageChat(int userId, int remoteId, Message msg){
223
+        URL url = null;
224
+        HttpURLConnection con = null;
225
+        try {
226
+            url = new URL(baseURL + "SubmitMessageChat");
227
+        } catch (MalformedURLException e) {
228
+            e.printStackTrace();
229
+        }
230
+
231
+        try {
232
+            con = (HttpURLConnection) url.openConnection();
233
+            con.setRequestMethod("POST");
234
+            con.setRequestProperty("Content-Type", "application/json; utf-8");
235
+            con.setDoOutput(true);
236
+            try(OutputStream os = con.getOutputStream()) {
237
+                byte[] input = gson.toJson(new MessageSrvTCP(userId, remoteId, msg)).getBytes(StandardCharsets.UTF_8);
238
+                os.write(input, 0, input.length);
239
+            }
240
+            int status = con.getResponseCode();
241
+            System.out.println("Error code HTTP request submit msg chat = " + status);
242
+        } catch (IOException e) {
243
+            e.printStackTrace();
244
+        }
245
+    }
246
+
247
+    public void SubmitConnectionChat(int userId, MessageInit msg){
248
+        URL url = null;
249
+        HttpURLConnection con = null;
250
+        try {
251
+            url = new URL(baseURL + "SubmitConnectionChat");
252
+        } catch (MalformedURLException e) {
253
+            e.printStackTrace();
254
+        }
255
+
256
+        try {
257
+            con = (HttpURLConnection) url.openConnection();
258
+            con.setRequestMethod("POST");
259
+            con.setRequestProperty("Content-Type", "application/json; utf-8");
260
+            con.setDoOutput(true);
261
+            try(OutputStream os = con.getOutputStream()) {
262
+                byte[] input = gson.toJson(new MessageSrvTCP(userId, 0, msg)).getBytes(StandardCharsets.UTF_8);
263
+                os.write(input, 0, input.length);
264
+            }
265
+            int status = con.getResponseCode();
266
+            System.out.println("Error code HTTP request submit connection  avec = " + status);
267
+        } catch (IOException e) {
268
+            e.printStackTrace();
269
+        }
270
+    }
271
+
272
+    public Message GetMessageChat(int userId, int remoteId){
273
+        //System.out.println("Demande");
274
+        URL url = null;
275
+        HttpURLConnection con = null;
276
+        Message resList = null;
277
+        try {
278
+            url = new URL(baseURL + "GetMessageChat");
279
+        } catch (MalformedURLException e) {
280
+            System.out.println("Erreur recup message avec " + url.toString());
281
+            //e.printStackTrace();
282
+        }
283
+        try {
284
+            con = (HttpURLConnection) url.openConnection();
285
+            con.setRequestMethod("POST");
286
+            con.setRequestProperty("Content-Type", "application/json; utf-8");
287
+            con.setDoOutput(true);
288
+            con.setDoInput(true);
289
+            try(OutputStream os = con.getOutputStream()) {
290
+                byte[] input = gson.toJson(new MessageSrvTCP(userId, remoteId, null)).getBytes(StandardCharsets.UTF_8);
291
+                os.write(input, 0, input.length);
292
+            }
293
+            int status = con.getResponseCode();
294
+            BufferedReader in = new BufferedReader(
295
+                    new InputStreamReader(con.getInputStream()));
296
+            String inputLine;
297
+            StringBuffer content = new StringBuffer();
298
+            while ((inputLine = in.readLine()) != null) {
299
+                content.append(inputLine);
300
+            }
301
+            in.close();
302
+            System.out.println("Response : " + content);
303
+            resList = gson.fromJson(content.toString(),Message.class);
304
+            //resList = new Utilisateurs[Integer.parseInt(con.getHeaderField("sizeArray"))];
305
+        } catch (IOException e) {
306
+            System.out.println("Erreur recup msg chat 2 avec " + url.toString());
307
+            e.printStackTrace();
308
+        }
309
+        return resList;
310
+    }
311
+
312
+    public ArrayList<MessageInit> GetConnectionChat(int userId){
313
+        URL url = null;
314
+        HttpURLConnection con = null;
315
+        ArrayList<MessageInit> resList = null;
316
+        try {
317
+            url = new URL(baseURL + "GetConnectionChat");
318
+        } catch (MalformedURLException e) {
319
+            System.out.println("Erreur recup connecion" + url.toString());
320
+            //e.printStackTrace();
321
+        }
322
+        try {
323
+            con = (HttpURLConnection) url.openConnection();
324
+            con.setRequestMethod("POST");
325
+            con.setRequestProperty("Content-Type", "application/json; utf-8");
326
+            con.setDoOutput(true);
327
+            con.setDoInput(true);
328
+            try(OutputStream os = con.getOutputStream()) {
329
+                byte[] input = gson.toJson(new MessageSrvTCP(userId, 0, null)).getBytes(StandardCharsets.UTF_8);
330
+                os.write(input, 0, input.length);
331
+            }
332
+            int status = con.getResponseCode();
333
+            BufferedReader in = new BufferedReader(
334
+                    new InputStreamReader(con.getInputStream()));
335
+            String inputLine;
336
+            StringBuffer content = new StringBuffer();
337
+            while ((inputLine = in.readLine()) != null) {
338
+                content.append(inputLine);
339
+            }
340
+            in.close();
341
+            System.out.println("Response : " + content);
342
+            Type listType = new TypeToken<ArrayList<MessageInit>>(){}.getType();
343
+            resList = gson.fromJson(content.toString(),listType);
344
+            //resList = new Utilisateurs[Integer.parseInt(con.getHeaderField("sizeArray"))];
345
+        } catch (IOException e) {
346
+            System.out.println("Erreur recup connecion 2 " + url.toString());
347
+            e.printStackTrace();
348
+        }
349
+        return resList;
350
+    }
218 351
 }

+ 170
- 82
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.Utilisateurs;
2 3
 import app.insa.clav.Messages.*;
3 4
 import app.insa.clav.UISubStages.ChatStage;
4 5
 import javafx.application.Platform;
@@ -35,18 +36,22 @@ public class TCPChatConnection extends Thread{
35 36
     private ObjectInputStream objectInStream;
36 37
     private DataInputStream dis;
37 38
     private DataOutputStream dos;
39
+    private boolean isOutdoor;
40
+    private ServletConnection serCon;
38 41
 
39 42
     PropertyChangeSupport support;
40 43
 
41 44
     public int remoteUserId;
42 45
     public int localUserId;
43 46
 
47
+
44 48
     /**
45 49
      * Constructeur utilisé quand l'utilisateur distant inititie la connexion
46 50
      * @param link
47 51
      */
48 52
     public TCPChatConnection(Socket link, int remoteUserId, int localUserId,InputStream is, OutputStream os, ObjectOutputStream objectOutputStream, ObjectInputStream objectInputStream){
49 53
         this.link = link;
54
+        this.isOutdoor = false;
50 55
         this.objectOutStream = objectOutputStream;
51 56
         this.objectInStream = objectInputStream;
52 57
         this.dos = new DataOutputStream(os);
@@ -55,9 +60,9 @@ public class TCPChatConnection extends Thread{
55 60
         this.msgReceivedBufferFiles = new ArrayList<MessageDisplayFile>();
56 61
         this.remoteUserId = remoteUserId;
57 62
         this.localUserId = localUserId;
63
+        this.serCon = ServletConnection.getInstance();
58 64
         this.support = new PropertyChangeSupport(this);
59 65
         Platform.runLater(() -> new ChatStage(this));
60
-        this.start();
61 66
     }
62 67
 
63 68
     /**
@@ -90,15 +95,28 @@ public class TCPChatConnection extends Thread{
90 95
         } catch (IOException e) {
91 96
             e.printStackTrace();
92 97
         }
98
+        this.isOutdoor = false;
99
+        this.serCon = ServletConnection.getInstance();
93 100
         this.msgReceivedBuffer = new ArrayList<Message>();
94 101
         this.msgReceivedBufferFiles = new ArrayList<MessageDisplayFile>();
95 102
         this.remoteUserId = remoteUserId;
96 103
         this.localUserId = msgInit.id;
97 104
         this.support = new PropertyChangeSupport(this);
98 105
         Platform.runLater(() -> new ChatStage(this));
106
+    }
107
+
108
+    public void startTCPCo() {
99 109
         this.start();
100 110
     }
101 111
 
112
+    public void setOutdoor() {
113
+        this.isOutdoor = true;
114
+    }
115
+
116
+    public boolean isOutdoor() {
117
+        return this.isOutdoor;
118
+    }
119
+
102 120
     public void addPropertyChangeListener(PropertyChangeListener pcl){
103 121
         this.support.addPropertyChangeListener("messageTextReceivedTCP",pcl);
104 122
         this.support.addPropertyChangeListener("connectionChatClosed",pcl);
@@ -127,99 +145,164 @@ public class TCPChatConnection extends Thread{
127 145
 
128 146
     @Override
129 147
     public void run() {
130
-        while (true){
131
-            Message msgReceived = null;
132
-            try {
133
-                msgReceived = (Message) this.objectInStream.readObject();
134
-                System.out.println("Message reçu");
135
-            } catch (IOException e) {
136
-                this.support.firePropertyChange("userDisconnected",true,false);
137
-                break;
138
-            } catch (ClassNotFoundException e) {
139
-                e.printStackTrace();
140
-            }
141
-            if (msgReceived.typeMessage == 8){
142
-                this.support.firePropertyChange("connectionChatClosed",true,false);
143
-                break;
144
-            }
145
-            else if (msgReceived.typeMessage == 9){
146
-                int bytes = 0;
147
-                MessageChatFile msgFile = (MessageChatFile) msgReceived;
148
+        if (!this.isOutdoor) {
149
+            while (true) {
150
+                Message msgReceived = null;
148 151
                 try {
149
-                    String path;
150
-                    if (localUserId > remoteUserId){
151
-                        path = "./file_" + remoteUserId + "_" + localUserId + "_" + msgFile.date + "." + msgFile.ext;
152
-                    }
153
-                    else{
154
-                        path = "./file_" + localUserId + "_" + remoteUserId + "_" + msgFile.date + "." + msgFile.ext;
155
-                    }
156
-                    File file = new File(path);
157
-                    FileOutputStream fileOutputStream = new FileOutputStream(file);
158
-                    long size = msgFile.fileSize;
159
-                    byte[] buffer = new byte[4*1024];
160
-                    while (size > 0){
161
-                        bytes = dis.read(buffer, 0, (int)Math.min(buffer.length, size));
162
-                        fileOutputStream.write(buffer,0,bytes);
163
-                        size -= bytes;
164
-                    }
165
-                    System.out.println("Reception fichier terminée");
166
-                    int type = 2;
167
-                    switch (msgFile.ext) {
168
-                        case "png":
169
-                        case "gif":
170
-                        case "jpeg":
171
-                        case "svg":
172
-                        case "jpg":
173
-                            type = 3;
174
-                            break;
175
-                    }
176
-                    this.msgReceivedBufferFiles.add(new MessageDisplayFile(this.remoteUserId,msgFile.date,msgFile.payload,type,file,msgFile.ext, -1));
177
-                    this.support.firePropertyChange("fileReceived",true,false);
178
-                    fileOutputStream.close();
179
-                    file.deleteOnExit();
152
+                    msgReceived = (Message) this.objectInStream.readObject();
153
+                    System.out.println("Message reçu");
180 154
                 } catch (IOException e) {
155
+                    this.support.firePropertyChange("userDisconnected", true, false);
156
+                    break;
157
+                } catch (ClassNotFoundException e) {
181 158
                     e.printStackTrace();
182 159
                 }
160
+                if (msgReceived.typeMessage == 8) {
161
+                    this.support.firePropertyChange("connectionChatClosed", true, false);
162
+                    break;
163
+                } else if (msgReceived.typeMessage == 9) {
164
+                    int bytes = 0;
165
+                    MessageChatFile msgFile = (MessageChatFile) msgReceived;
166
+                    try {
167
+                        String path;
168
+                        if (localUserId > remoteUserId) {
169
+                            path = "./file_" + remoteUserId + "_" + localUserId + "_" + msgFile.date + "." + msgFile.ext;
170
+                        } else {
171
+                            path = "./file_" + localUserId + "_" + remoteUserId + "_" + msgFile.date + "." + msgFile.ext;
172
+                        }
173
+                        File file = new File(path);
174
+                        FileOutputStream fileOutputStream = new FileOutputStream(file);
175
+                        long size = msgFile.fileSize;
176
+                        byte[] buffer = new byte[4 * 1024];
177
+                        while (size > 0) {
178
+                            bytes = dis.read(buffer, 0, (int) Math.min(buffer.length, size));
179
+                            fileOutputStream.write(buffer, 0, bytes);
180
+                            size -= bytes;
181
+                        }
182
+                        System.out.println("Reception fichier terminée");
183
+                        int type = 2;
184
+                        switch (msgFile.ext) {
185
+                            case "png":
186
+                            case "gif":
187
+                            case "jpeg":
188
+                            case "svg":
189
+                            case "jpg":
190
+                                type = 3;
191
+                                break;
192
+                        }
193
+                        this.msgReceivedBufferFiles.add(new MessageDisplayFile(this.remoteUserId, msgFile.date, msgFile.payload, type, file, msgFile.ext, -1));
194
+                        this.support.firePropertyChange("fileReceived", true, false);
195
+                        fileOutputStream.close();
196
+                        file.deleteOnExit();
197
+                    } catch (IOException e) {
198
+                        e.printStackTrace();
199
+                    }
200
+                } else {
201
+                    this.msgReceivedBuffer.add(msgReceived);
202
+                    this.support.firePropertyChange("messageTextReceivedTCP", true, false);
203
+                }
183 204
             }
184
-            else {
185
-                this.msgReceivedBuffer.add(msgReceived);
186
-                this.support.firePropertyChange("messageTextReceivedTCP", true, false);
205
+        } else {
206
+            while (true) {
207
+                try {
208
+                    Thread.sleep(500);
209
+                } catch (InterruptedException e) {
210
+                    e.printStackTrace();
211
+                }
212
+                Message msgReceived = this.serCon.GetMessageChat(this.localUserId, this.remoteUserId);
213
+                if (msgReceived != null) {//this.support.firePropertyChange("userDisconnected", true, false);
214
+
215
+                    if (msgReceived.typeMessage == 8) {
216
+                        this.support.firePropertyChange("connectionChatClosed", true, false);
217
+                        break;
218
+                    } else if (msgReceived.typeMessage == 9) {
219
+                        int bytes = 0;
220
+                        MessageChatFile msgFile = (MessageChatFile) msgReceived;
221
+                        try {
222
+                            String path;
223
+                            if (localUserId > remoteUserId) {
224
+                                path = "./file_" + remoteUserId + "_" + localUserId + "_" + msgFile.date + "." + msgFile.ext;
225
+                            } else {
226
+                                path = "./file_" + localUserId + "_" + remoteUserId + "_" + msgFile.date + "." + msgFile.ext;
227
+                            }
228
+                            File file = new File(path);
229
+                            FileOutputStream fileOutputStream = new FileOutputStream(file);
230
+                            long size = msgFile.fileSize;
231
+                            byte[] buffer = new byte[4 * 1024];
232
+                            while (size > 0) {
233
+                                bytes = dis.read(buffer, 0, (int) Math.min(buffer.length, size));
234
+                                fileOutputStream.write(buffer, 0, bytes);
235
+                                size -= bytes;
236
+                            }
237
+                            System.out.println("Reception fichier terminée");
238
+                            int type = 2;
239
+                            switch (msgFile.ext) {
240
+                                case "png":
241
+                                case "gif":
242
+                                case "jpeg":
243
+                                case "svg":
244
+                                case "jpg":
245
+                                    type = 3;
246
+                                    break;
247
+                            }
248
+                            this.msgReceivedBufferFiles.add(new MessageDisplayFile(this.remoteUserId, msgFile.date, msgFile.payload, type, file, msgFile.ext, -1));
249
+                            this.support.firePropertyChange("fileReceived", true, false);
250
+                            fileOutputStream.close();
251
+                            file.deleteOnExit();
252
+                        } catch (IOException e) {
253
+                            e.printStackTrace();
254
+                        }
255
+                    } else {
256
+                        this.msgReceivedBuffer.add(msgReceived);
257
+                        this.support.firePropertyChange("messageTextReceivedTCP", true, false);
258
+                    }
259
+                }
187 260
             }
188 261
         }
189 262
     }
190 263
 
191 264
     public void sendMessageTxt(MessageDisplay msgDisp){
192
-        if (this.link == null) {
193
-            System.out.println("LINK NULL");
194
-        }
195
-        MessageChatTxt msg = new MessageChatTxt(6,this.link.getLocalAddress(),this.link.getInetAddress(),this.link.getPort(),msgDisp.getPayload(),msgDisp.getDate());
196
-        try {
197
-            this.objectOutStream.writeObject(msg);
198
-        } catch (IOException e) {
199
-            e.printStackTrace();
265
+        if (!isOutdoor) {
266
+            if (this.link == null) {
267
+                System.out.println("LINK NULL");
268
+            }
269
+            MessageChatTxt msg = new MessageChatTxt(6, this.link.getLocalAddress(), this.link.getInetAddress(), this.link.getPort(), msgDisp.getPayload(), msgDisp.getDate());
270
+            try {
271
+                this.objectOutStream.writeObject(msg);
272
+            } catch (IOException e) {
273
+                e.printStackTrace();
274
+            }
275
+        } else {
276
+            Message msg = new MessageChatTxt(6, null, null, 0, msgDisp.getPayload(), msgDisp.getDate());
277
+            this.serCon.SubmitMessageChat(this.remoteUserId, this.localUserId, msg);
200 278
         }
201 279
     }
202 280
 
203 281
 
204 282
     public void sendMessageFile(MessageDisplayFile msgDisp){
205
-        int bytes = 0;
206
-        MessageChatFile msgStartofFile = new MessageChatFile(9,this.link.getLocalAddress(),this.link.getInetAddress(),this.link.getPort(),msgDisp.getPayload(),msgDisp.getDate(),msgDisp.getFile().length(),msgDisp.getExt());
207
-        try {
208
-            this.objectOutStream.writeObject(msgStartofFile);
209
-        } catch (IOException e) {
210
-            e.printStackTrace();
211
-        }
283
+        if (!isOutdoor) {
284
+            int bytes = 0;
285
+            MessageChatFile msgStartofFile = new MessageChatFile(9,this.link.getLocalAddress(),this.link.getInetAddress(),this.link.getPort(),msgDisp.getPayload(),msgDisp.getDate(),msgDisp.getFile().length(),msgDisp.getExt());
286
+            try {
287
+                this.objectOutStream.writeObject(msgStartofFile);
288
+            } catch (IOException e) {
289
+                e.printStackTrace();
290
+            }
212 291
 
213
-        try {
214
-            FileInputStream fis = new FileInputStream(msgDisp.getFile());
215
-            byte[] buffer = new byte[4*1024];
216
-            while ((bytes=fis.read(buffer))!=-1){
217
-                dos.write(buffer,0,bytes);
218
-                dos.flush();
292
+            try {
293
+                FileInputStream fis = new FileInputStream(msgDisp.getFile());
294
+                byte[] buffer = new byte[4*1024];
295
+                while ((bytes=fis.read(buffer))!=-1){
296
+                    dos.write(buffer,0,bytes);
297
+                    dos.flush();
298
+                }
299
+                fis.close();
300
+            } catch (IOException e) {
301
+                e.printStackTrace();
219 302
             }
220
-            fis.close();
221
-        } catch (IOException e) {
222
-            e.printStackTrace();
303
+        } else {
304
+            Message msg = new MessageChatFile(9,null, null, 0,msgDisp.getPayload(),msgDisp.getDate(),msgDisp.getFile().length(),msgDisp.getExt());
305
+            this.serCon.SubmitMessageChat(this.remoteUserId, this.localUserId, msg);
223 306
         }
224 307
     }
225 308
 
@@ -227,11 +310,16 @@ public class TCPChatConnection extends Thread{
227 310
      * When receiving a type 8 message, closed the chat connection
228 311
      */
229 312
     public void sendCloseChat() {
230
-        Message msg = new Message(8, this.link.getLocalAddress());
231
-        try {
232
-            this.objectOutStream.writeObject(msg);
233
-        } catch (IOException e) {
234
-            e.printStackTrace();
313
+        if (!isOutdoor) {
314
+            Message msg = new Message(8, this.link.getLocalAddress());
315
+            try {
316
+                this.objectOutStream.writeObject(msg);
317
+            } catch (IOException e) {
318
+                e.printStackTrace();
319
+            }
320
+        } else {
321
+            Message msg = new Message(8, null);
322
+            this.serCon.SubmitMessageChat(this.remoteUserId, this.localUserId, msg);
235 323
         }
236 324
     }
237 325
 

+ 3
- 1
src/main/java/app/insa/clav/Reseau/TCPListener.java View File

@@ -80,7 +80,9 @@ public class TCPListener extends Thread{
80 80
                 ObjectInputStream objectInStream = new ObjectInputStream(is);
81 81
                 MessageInit msgInit = (MessageInit) objectInStream.readObject();
82 82
                 int remoteUserId = msgInit.id;
83
-                this.bufferTCPConnection.add(new TCPChatConnection(link,remoteUserId,localId,is,os,objectOutStream,objectInStream));
83
+                TCPChatConnection tcpCo = new TCPChatConnection(link,remoteUserId,localId,is,os,objectOutStream,objectInStream);
84
+                tcpCo.startTCPCo();
85
+                this.bufferTCPConnection.add(tcpCo);
84 86
                 this.support.firePropertyChange("chatCreated",true,false);
85 87
             } catch (IOException | ClassNotFoundException e) {
86 88
                 e.printStackTrace();

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

@@ -357,40 +357,40 @@ public class ChatWindowController implements Initializable, PropertyChangeListen
357 357
      * @param actionEvent
358 358
      */
359 359
     public void buttonSendMessageClicked(ActionEvent actionEvent) {
360
-        this.sendButton.setDisable(true);
361
-        String timeStamp = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss").format(new Date());
362
-        String payload = this.messageInput.getText();
363
-        if (!payload.equals("")) {
364
-            MessageDisplay msg = new MessageDisplay(model.user.getId(),timeStamp,payload,1);
365
-            this.listMessages.add(msg);
366
-            this.messageInput.clear();
367
-            this.tcpCo.sendMessageTxt(msg);
368
-            this.dbAccess.addMessage(this.localUserId,this.remoteUser.getId(),msg);
369
-        }
360
+            this.sendButton.setDisable(true);
361
+            String timeStamp = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss").format(new Date());
362
+            String payload = this.messageInput.getText();
363
+            if (!payload.equals("")) {
364
+                MessageDisplay msg = new MessageDisplay(model.user.getId(), timeStamp, payload, 1);
365
+                this.listMessages.add(msg);
366
+                this.messageInput.clear();
367
+                this.tcpCo.sendMessageTxt(msg);
368
+                this.dbAccess.addMessage(this.localUserId, this.remoteUser.getId(), msg);
369
+            }
370 370
 
371
-        //We send the File if the filed is not null
372
-        if (this.filePicked != null){
373
-            String ext = FilenameUtils.getExtension(this.filePicked.getPath());
374
-            int type = 2;
375
-            switch (ext) {
376
-                case "png":
377
-                case "gif":
378
-                case "jpeg":
379
-                case "svg":
380
-                case "jpg":
381
-                    type = 3;
382
-                    break;
371
+            //We send the File if the filed is not null
372
+            if (this.filePicked != null) {
373
+                String ext = FilenameUtils.getExtension(this.filePicked.getPath());
374
+                int type = 2;
375
+                switch (ext) {
376
+                    case "png":
377
+                    case "gif":
378
+                    case "jpeg":
379
+                    case "svg":
380
+                    case "jpg":
381
+                        type = 3;
382
+                        break;
383
+                }
384
+                MessageDisplayFile msgFile = new MessageDisplayFile(model.user.getId(), timeStamp, this.filePicked.getName(), type, this.filePicked, ext, -1);
385
+                this.listMessages.add(msgFile);
386
+                this.tcpCo.sendMessageFile(msgFile);
387
+                this.dbAccess.addMessage(this.localUserId, this.remoteUser.getId(), msgFile);
388
+                this.filePicked = null;
389
+                this.labelFile.setVisible(false);
383 390
             }
384
-            MessageDisplayFile msgFile = new MessageDisplayFile(model.user.getId(),timeStamp,this.filePicked.getName(),type,this.filePicked, ext, -1);
385
-            this.listMessages.add(msgFile);
386
-            this.tcpCo.sendMessageFile(msgFile);
387
-            this.dbAccess.addMessage(this.localUserId,this.remoteUser.getId(),msgFile);
388
-            this.filePicked = null;
389
-            this.labelFile.setVisible(false);
390
-        }
391
-        this.sendButton.setDisable(false);
392
-        int size = messageList.getItems().size();
393
-        messageList.scrollTo(size - 1);
391
+            this.sendButton.setDisable(false);
392
+            int size = messageList.getItems().size();
393
+            messageList.scrollTo(size - 1);
394 394
     }
395 395
 
396 396
     /**

Loading…
Cancel
Save