|
@@ -28,17 +28,22 @@ import view.Interface;
|
28
|
28
|
public class Controller {
|
29
|
29
|
|
30
|
30
|
/*** CONSTANTES ***/
|
31
|
|
- final static int portUDPlistening_remoteUsr2 = 31002; // TO REMOVE when we use broadcast
|
32
|
|
- final static int portUDPlistening_remoteUsr3 = 31003; // TO REMOVE when we use broadcast
|
33
|
|
-
|
34
|
|
- public Boolean interfaceRunning;
|
|
31
|
+ int NB_SECOND_WAITING_RESPONSE_BROADCAST = 2;
|
|
32
|
+
|
|
33
|
+ // TO REMOVE when we use broadcast
|
|
34
|
+ final static int portUDPlistening_usr1 = 31001;
|
|
35
|
+ final static int portUDPlistening_remoteUsr2 = 31002;
|
|
36
|
+ final static int portUDPlistening_remoteUsr3 = 31003;
|
|
37
|
+ final static int [] tabBroadcast = {portUDPlistening_usr1,portUDPlistening_remoteUsr2,portUDPlistening_remoteUsr3};
|
|
38
|
+
|
|
39
|
+ public Boolean interfaceRunning = false;
|
35
|
40
|
/*** ATTRIBUTS ***/
|
36
|
41
|
protected LocalUser myUser;
|
37
|
42
|
protected Interface view;
|
38
|
43
|
private ListeningThreadUDP udp_connect_thread;
|
39
|
44
|
private ListeningThreadTCPConnection tcp_connect_thread;
|
40
|
45
|
private Historique histoire;
|
41
|
|
- private Chat activeChat;
|
|
46
|
+ protected Chat activeChat;
|
42
|
47
|
|
43
|
48
|
/**
|
44
|
49
|
* Constructor of Controller
|
|
@@ -82,7 +87,8 @@ public class Controller {
|
82
|
87
|
|
83
|
88
|
notify_remote_users();
|
84
|
89
|
|
85
|
|
-
|
|
90
|
+ interfaceRunning =true;
|
|
91
|
+ view=Interface.createAndShowGUI(this);
|
86
|
92
|
}
|
87
|
93
|
|
88
|
94
|
/**
|
|
@@ -110,13 +116,32 @@ public class Controller {
|
110
|
116
|
catch(UnknownHostException e) {
|
111
|
117
|
JOptionPane.showMessageDialog(null ,"Could not find local address!");
|
112
|
118
|
}
|
|
119
|
+
|
113
|
120
|
this.myUser = new LocalUser(pseudo,addIP,portUDPsend,portUDPlistening,portTCP);
|
|
121
|
+ try {
|
|
122
|
+ if(this.validatePseudo(pseudo)) {
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+ this.udp_connect_thread = new ListeningThreadUDP("UDP Listening thread",this);
|
|
126
|
+ this.udp_connect_thread.start();
|
|
127
|
+
|
|
128
|
+ this.tcp_connect_thread = new ListeningThreadTCPConnection("TCP main Listening thread",this);
|
|
129
|
+ this.tcp_connect_thread.start();
|
|
130
|
+
|
|
131
|
+ interfaceRunning =true;
|
|
132
|
+ view=Interface.createAndShowGUI(this);
|
|
133
|
+ }
|
|
134
|
+ else {
|
|
135
|
+ System.out.println("Unavailable "+pseudo);
|
|
136
|
+ }
|
|
137
|
+ } catch (IOException e) {
|
|
138
|
+ e.printStackTrace();
|
|
139
|
+ }
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
|
114
|
143
|
|
115
|
|
- this.udp_connect_thread = new ListeningThreadUDP("UDP Listening thread",this);
|
116
|
|
- this.udp_connect_thread.start();
|
117
|
144
|
|
118
|
|
- this.tcp_connect_thread = new ListeningThreadTCPConnection("TCP main Listening thread",this);
|
119
|
|
- this.tcp_connect_thread.start();
|
120
|
145
|
|
121
|
146
|
notify_remote_users();
|
122
|
147
|
}
|
|
@@ -188,11 +213,11 @@ public class Controller {
|
188
|
213
|
public void changePseudo() throws IOException {
|
189
|
214
|
String oldPseudo = this.myUser.getPseudo(); //Saves the old one for comparison
|
190
|
215
|
|
191
|
|
- String tmpPseudo = view.PseudotextField.getText(); // Read user input
|
|
216
|
+ String tmpPseudo = view.ChangePseudotextField.getText(); // Read user input
|
192
|
217
|
|
193
|
218
|
if(!(this.validatePseudo(tmpPseudo)) || tmpPseudo.equals(oldPseudo)) {
|
194
|
219
|
view.Pseudolabel.setText("Already exists, enter another nickname. Your current username is: " + oldPseudo); // Read user input
|
195
|
|
- tmpPseudo = view.PseudotextField.getText(); // Read user input
|
|
220
|
+ tmpPseudo = view.ChangePseudotextField.getText(); // Read user input
|
196
|
221
|
}
|
197
|
222
|
else {
|
198
|
223
|
this.myUser.setPseudo(tmpPseudo);
|
|
@@ -221,46 +246,45 @@ public class Controller {
|
221
|
246
|
|
222
|
247
|
// Call broadcast
|
223
|
248
|
InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast
|
224
|
|
- //System.out.println(broadcastIP);
|
|
249
|
+ //System.out.println(this.myUser.getPortUDPsend());
|
225
|
250
|
DatagramSocket dgramSocket = new DatagramSocket(this.myUser.getPortUDPsend(),this.myUser.getAddIP());
|
226
|
251
|
byte[] buffer = new byte[256];
|
227
|
252
|
|
228
|
253
|
// Création du message à envoyer
|
229
|
|
- String toSend = this.myUser.getAddIP()+":"+this.myUser.getPortUDPsend()+":test";
|
230
|
|
-
|
231
|
|
- // Send to remote user 1
|
232
|
|
- DatagramPacket outPacket= new DatagramPacket(toSend.getBytes(), toSend.length(),broadcastIP, portUDPlistening_remoteUsr2);
|
233
|
|
- dgramSocket.send(outPacket);
|
234
|
|
-
|
235
|
|
- // Send to remote user 2
|
236
|
|
- outPacket= new DatagramPacket(toSend.getBytes(), toSend.length(),broadcastIP, portUDPlistening_remoteUsr3);
|
237
|
|
- dgramSocket.send(outPacket);
|
238
|
|
-
|
239
|
|
- // Initialisation des variables de la boucle
|
240
|
|
- Boolean valid = true;
|
|
254
|
+ String toSend = this.myUser.getAddIP()+":"+this.myUser.getPortUDPsend()+":info";
|
|
255
|
+
|
|
256
|
+ // Send to other active user (simulation of broadcast)
|
|
257
|
+ DatagramPacket outPacket = null;
|
|
258
|
+ int tabBroadcastSize = tabBroadcast.length;
|
|
259
|
+ for(int i=0;i<tabBroadcastSize;i++) {
|
|
260
|
+ if(tabBroadcast[i]!=myUser.getPortUDPlistening()) {
|
|
261
|
+ outPacket= new DatagramPacket(toSend.getBytes(), toSend.length(),broadcastIP, tabBroadcast[i]);
|
|
262
|
+ dgramSocket.send(outPacket);
|
|
263
|
+ }
|
|
264
|
+ }
|
241
|
265
|
|
|
266
|
+ /*** For 5 seconds wait for answer : validate pseudo & add to userlist ***/
|
|
267
|
+ Boolean valid = true;
|
242
|
268
|
DatagramPacket inPacket;
|
243
|
269
|
String response = null;
|
244
|
270
|
String[] tabresponse= new String [4];
|
245
|
271
|
dgramSocket.setSoTimeout(1000);
|
246
|
272
|
Boolean arecu;
|
247
|
|
-
|
248
|
|
- System.out.print("Wait for pseudo validation ...\n");
|
249
|
|
-
|
250
|
273
|
int nbReponse =0;
|
|
274
|
+ System.out.println("("+myUser.getPseudo()+") Waiting for pseudo validation ...");
|
251
|
275
|
Date oldDate = new Date();
|
252
|
276
|
Date newDate = new Date();
|
253
|
|
- while( (newDate.getTime()-oldDate.getTime()) < 5000 && valid) {
|
254
|
|
-
|
|
277
|
+
|
|
278
|
+ while( (newDate.getTime()-oldDate.getTime()) < NB_SECOND_WAITING_RESPONSE_BROADCAST*1000 && valid) {
|
255
|
279
|
nbReponse++;
|
256
|
280
|
inPacket= new DatagramPacket(buffer, buffer.length);
|
257
|
|
-
|
258
|
281
|
arecu=true;
|
259
|
282
|
try{
|
260
|
283
|
dgramSocket.receive(inPacket);
|
261
|
284
|
}catch (Exception e) {
|
262
|
285
|
arecu=false;
|
263
|
286
|
}
|
|
287
|
+
|
264
|
288
|
buffer = inPacket.getData();
|
265
|
289
|
response = new String(buffer);
|
266
|
290
|
|
|
@@ -268,9 +292,6 @@ public class Controller {
|
268
|
292
|
// On découpe la réponse en tableau de string ([adresseIP,tcpPort,nickname])
|
269
|
293
|
tabresponse = response.split(":");
|
270
|
294
|
|
271
|
|
- // Affichage de la réponse
|
272
|
|
- //System.out.println("Remote user n°"+nbReponse+"\nIP : "+tabresponse[0]+"\nn°Port : "+tabresponse[1]+"\npseudo : "+tabresponse[2]);
|
273
|
|
-
|
274
|
295
|
// Si reception on ajoute l'utilisateur à notre liste d'utilisateur distant
|
275
|
296
|
this.myUser.addRemoteUser(InetAddress.getByName(tabresponse[0].split("/")[1]),Integer.parseInt(tabresponse[1]),tabresponse[2]);
|
276
|
297
|
valid= (tmpPseudo.compareTo(tabresponse[2])!=0); // On regarde la différence entre notre pseudo et le pseudo reçu
|
|
@@ -287,46 +308,35 @@ public class Controller {
|
287
|
308
|
|
288
|
309
|
return valid;
|
289
|
310
|
}
|
|
311
|
+
|
290
|
312
|
/** notify_remote_users
|
291
|
313
|
* <p>
|
292
|
314
|
* En utilisant le port UDP d'envoi, on envoie en broadcast les informations nous concernant
|
293
|
315
|
* </p>
|
294
|
316
|
*/
|
295
|
317
|
public void notify_remote_users() {
|
296
|
|
- // Création du socket d'envoi d'information
|
297
|
318
|
DatagramSocket dgramSocket= null;
|
298
|
319
|
try {
|
299
|
320
|
dgramSocket= new DatagramSocket(this.myUser.getPortUDPsend(),this.myUser.getAddIP());
|
300
|
321
|
} catch (IOException e) {
|
301
|
322
|
e.printStackTrace();
|
302
|
323
|
}
|
303
|
|
-
|
304
|
|
- // Construction du message à envoyer
|
305
|
|
- String toSend = this.myUser.getAddIP().toString()+":"+this.myUser.getPortTCP()+":"+this.myUser.getPseudo()+":test";
|
306
|
324
|
|
|
325
|
+ // Send to other active user (simulation of broadcast)
|
|
326
|
+ String toSend = this.myUser.getAddIP().toString()+":"+this.myUser.getPortTCP()+":"+this.myUser.getPseudo()+":notify";
|
307
|
327
|
DatagramPacket outPacket =null;
|
308
|
|
-
|
309
|
|
- // Send information to usr2
|
310
|
|
- if(this.myUser.getPortUDPlistening()!=portUDPlistening_remoteUsr2) {
|
311
|
|
- outPacket = new DatagramPacket(toSend.getBytes(), toSend.length(),this.myUser.getAddIP(), portUDPlistening_remoteUsr2);
|
312
|
|
- try {
|
313
|
|
- dgramSocket.send(outPacket);
|
314
|
|
- } catch (IOException e) {
|
315
|
|
- e.printStackTrace();
|
316
|
|
- }
|
317
|
|
- }
|
318
|
|
-
|
319
|
|
-
|
320
|
|
- // Send information to usr3
|
321
|
|
- if(this.myUser.getPortUDPlistening()!=portUDPlistening_remoteUsr3) {
|
322
|
|
-
|
323
|
|
- outPacket= new DatagramPacket(toSend.getBytes(), toSend.length(),this.myUser.getAddIP(), portUDPlistening_remoteUsr3);
|
324
|
|
- try {
|
325
|
|
- dgramSocket.send(outPacket);
|
326
|
|
- } catch (IOException e) {
|
327
|
|
- e.printStackTrace();
|
328
|
|
- }
|
329
|
|
- }
|
|
328
|
+ int tabBroadcastSize = tabBroadcast.length;
|
|
329
|
+ for(int i=0;i<tabBroadcastSize;i++) {
|
|
330
|
+ if(tabBroadcast[i]!=myUser.getPortUDPlistening()) {
|
|
331
|
+ outPacket = new DatagramPacket(toSend.getBytes(), toSend.length(),this.myUser.getAddIP(), tabBroadcast[i]);
|
|
332
|
+ try {
|
|
333
|
+ dgramSocket.send(outPacket);
|
|
334
|
+ } catch (IOException e) {
|
|
335
|
+ e.printStackTrace();
|
|
336
|
+ }
|
|
337
|
+ }
|
|
338
|
+
|
|
339
|
+ }
|
330
|
340
|
dgramSocket.close();
|
331
|
341
|
}
|
332
|
342
|
|
|
@@ -342,7 +352,7 @@ public class Controller {
|
342
|
352
|
link=new Socket(c.getRemoteUser().getAddIP(),c.getRemoteUser().getPortTCP()/*, InetAddress.getLocalHost() ,myUser.getPortTCP()*/);
|
343
|
353
|
c.setSocket(link);
|
344
|
354
|
// Sending data for identification (TO REMOVE IF != IP)
|
345
|
|
- sendMessage(new Msg_Text(myUser.getAddIP(),Integer.toString(myUser.getPortTCP())),c);
|
|
355
|
+ sendInfoProcess(Integer.toString(myUser.getPortTCP()), c); // tell rm session STOP
|
346
|
356
|
}catch(IOException e) {
|
347
|
357
|
System.out.println("Error linking to TCP server of "+ c.getRemoteUser().getPortTCP());
|
348
|
358
|
}
|
|
@@ -376,9 +386,10 @@ public class Controller {
|
376
|
386
|
}
|
377
|
387
|
|
378
|
388
|
// Look for history
|
|
389
|
+
|
379
|
390
|
int nbMessage = c.getMessages().size();
|
380
|
391
|
for(int i=0;i<nbMessage;i++) {
|
381
|
|
- history+="\n"+c.getMessages().get(i).getMessage();
|
|
392
|
+ history+="\n("+c.getMessages().get(i).getDate()+")\n"+c.getMessages().get(i).getAutor().getPseudo()+" : "+c.getMessages().get(i).getMessage();
|
382
|
393
|
}
|
383
|
394
|
this.activeChat = c;
|
384
|
395
|
|
|
@@ -390,13 +401,13 @@ public class Controller {
|
390
|
401
|
this.activeChat = null;
|
391
|
402
|
}
|
392
|
403
|
public void closeSession(Chat c) {
|
393
|
|
- sendMessage(new Msg_Text(myUser.getAddIP(),"end"), c); // tell rm session STOP
|
|
404
|
+ sendInfoProcess("end", c); // tell rm session STOP
|
394
|
405
|
JOptionPane.showMessageDialog(null ,"Close session with "+c.getRemoteUser().getPseudo());
|
395
|
406
|
this.myUser.closeChat(c);
|
396
|
407
|
}
|
397
|
408
|
|
398
|
409
|
public void askToSend(String textMessage){
|
399
|
|
- sendMessage(new Msg_Text(myUser.getAddIP(),textMessage), this.activeChat);
|
|
410
|
+ sendMessage(new Msg_Text(myUser,textMessage), this.activeChat);
|
400
|
411
|
}
|
401
|
412
|
public void sendMessage(Message msg,Chat c) {
|
402
|
413
|
/*** Init send process ***/
|
|
@@ -408,29 +419,57 @@ public class Controller {
|
408
|
419
|
}
|
409
|
420
|
c.addMessage(msg);
|
410
|
421
|
|
411
|
|
- /*** Gestion des différentes instance de Message ***/
|
412
|
422
|
// TODO check if instance of Msg_Text or anything else and threat it given a type
|
413
|
423
|
String message = (String) msg.getMessage();
|
414
|
|
-
|
415
|
|
- // Date l'envoie du message
|
416
|
|
- DateFormat dateFormat = new SimpleDateFormat("yyyy MM dd HH mm ss");
|
417
|
|
- Date date=new Date();
|
418
|
424
|
|
419
|
425
|
// Sauvegarde dans la base de données
|
|
426
|
+ DateFormat dateFormat = new SimpleDateFormat("yyyy MM dd HH mm ss");
|
|
427
|
+ Date date=new Date();
|
420
|
428
|
//this.getHistory().saveMessage(getMyUser(), c.getRemoteUser(),message ,dateFormat.format(date));
|
421
|
429
|
|
422
|
430
|
// Send message
|
423
|
|
- //out.println(dateFormat.format(date));
|
424
|
431
|
out.println(message);
|
425
|
432
|
|
426
|
433
|
}
|
427
|
434
|
|
|
435
|
+ public void sendInfoProcess(String msg,Chat c) {
|
|
436
|
+ PrintWriter out=null;
|
|
437
|
+ try {
|
|
438
|
+ out = new PrintWriter(c.getUserSocket().getOutputStream(),true);
|
|
439
|
+ } catch (IOException e) {
|
|
440
|
+ e.printStackTrace();
|
|
441
|
+ }
|
|
442
|
+
|
|
443
|
+ out.println(msg);
|
|
444
|
+
|
|
445
|
+ }
|
428
|
446
|
|
429
|
447
|
public String askNewMessage() {
|
430
|
448
|
String message = "";
|
431
|
449
|
return message;
|
432
|
450
|
}
|
433
|
451
|
|
|
452
|
+ public String [] askUpdateActiveUsers() {
|
|
453
|
+ String[] pseudotab = new String[myUser.getRemoteUsersList().size()];
|
|
454
|
+ int size = myUser.getRemoteUsersList().size();
|
|
455
|
+ for(int i=0; i < size; i++) {
|
|
456
|
+ pseudotab[i] = myUser.getRemoteUsersList().get(i).getPseudo();
|
|
457
|
+ }
|
|
458
|
+ return pseudotab;
|
|
459
|
+ }
|
|
460
|
+
|
|
461
|
+ public void askForClose() {
|
|
462
|
+ interfaceRunning = false;
|
|
463
|
+ close();
|
|
464
|
+ }
|
|
465
|
+ public void close() {
|
|
466
|
+ if(interfaceRunning) {
|
|
467
|
+ myUser.closeAllChat();
|
|
468
|
+ tcp_connect_thread.close();
|
|
469
|
+ udp_connect_thread.close();
|
|
470
|
+ }
|
|
471
|
+
|
|
472
|
+ }
|
434
|
473
|
|
435
|
474
|
public static void main(String[] args) throws IOException, InterruptedException {
|
436
|
475
|
|
|
@@ -440,13 +479,16 @@ public class Controller {
|
440
|
479
|
|
441
|
480
|
/************************* INIT *******************************/
|
442
|
481
|
/** Création des utilisateurs **/
|
|
482
|
+ // LOCAL USER
|
|
483
|
+ Controller ctr1 = new Controller(31011,portUDPlistening_usr1,31021,"Theau",histoire);
|
|
484
|
+
|
443
|
485
|
// REMOTEUSER_1 - MIKE
|
444
|
|
- Controller ctr2 = new Controller(31012,portUDPlistening_remoteUsr2,31022,"Mike",histoire);
|
|
486
|
+ Controller ctr2 = new Controller(31012,portUDPlistening_remoteUsr2,31022,"Leonie",histoire);
|
|
487
|
+
|
445
|
488
|
// REMOTEUSER_2 - ALICE
|
446
|
|
- Controller ctr3 = new Controller(31013,portUDPlistening_remoteUsr3,31023,"Alice",histoire);
|
447
|
|
- // LOCAL USER
|
448
|
|
- Controller ctr1 = new Controller(31011,31001,31021,histoire);
|
449
|
|
-
|
|
489
|
+ Controller ctr3 = new Controller(31013,portUDPlistening_remoteUsr3,31023,"Alexandre",histoire);
|
|
490
|
+
|
|
491
|
+
|
450
|
492
|
|
451
|
493
|
|
452
|
494
|
/*********************** TEST AREA ********************/
|
|
@@ -476,17 +518,12 @@ public class Controller {
|
476
|
518
|
|
477
|
519
|
/************************* LOOP *******************************/
|
478
|
520
|
|
479
|
|
- /** Création de l'interface graphique **/
|
480
|
|
- ctr1.interfaceRunning =true;
|
481
|
|
- ctr1.view=Interface.createAndShowGUI(ctr1);
|
482
|
|
- ctr2.interfaceRunning =true;
|
483
|
|
- ctr2.view=Interface.createAndShowGUI(ctr2);
|
484
|
|
- ctr3.interfaceRunning =true;
|
485
|
|
- ctr3.view=Interface.createAndShowGUI(ctr3);
|
|
521
|
+
|
486
|
522
|
|
487
|
523
|
/** loop **/
|
488
|
|
- while(ctr1.interfaceRunning) {
|
489
|
|
- //ctr1.view.update(info)
|
|
524
|
+ Boolean running = ctr1.interfaceRunning || ctr2.interfaceRunning || ctr3.interfaceRunning;
|
|
525
|
+ while(running) {
|
|
526
|
+ running = ctr1.interfaceRunning || ctr2.interfaceRunning || ctr3.interfaceRunning;
|
490
|
527
|
}
|
491
|
528
|
|
492
|
529
|
System.out.println("Fin de la boucle");
|
|
@@ -497,17 +534,14 @@ public class Controller {
|
497
|
534
|
|
498
|
535
|
/** Close thread and socket **/
|
499
|
536
|
// REMOTEUSER_1 - MIKE
|
500
|
|
- ctr2.myUser.closeAllChat();
|
501
|
|
- ctr2.tcp_connect_thread.close();
|
502
|
|
- ctr2.udp_connect_thread.close();
|
|
537
|
+ ctr2.close();
|
|
538
|
+
|
503
|
539
|
// REMOTEUSER_2 - ALICE
|
504
|
|
- ctr3.myUser.closeAllChat();
|
505
|
|
- ctr3.tcp_connect_thread.close();
|
506
|
|
- ctr3.udp_connect_thread.close();
|
|
540
|
+ ctr3.close();
|
|
541
|
+
|
507
|
542
|
// LOCAL USER
|
508
|
|
- ctr1.myUser.closeAllChat();
|
509
|
|
- ctr1.tcp_connect_thread.close();
|
510
|
|
- ctr1.udp_connect_thread.close();
|
|
543
|
+ ctr1.close();
|
|
544
|
+
|
511
|
545
|
// AFFICHAGE
|
512
|
546
|
System.out.println("end program");
|
513
|
547
|
JOptionPane.showMessageDialog(null ,"END");
|