|
@@ -9,28 +9,34 @@ import java.net.DatagramSocket;
|
9
|
9
|
import java.net.InetAddress;
|
10
|
10
|
import java.net.Socket;
|
11
|
11
|
import java.net.UnknownHostException;
|
|
12
|
+import java.text.DateFormat;
|
|
13
|
+import java.text.SimpleDateFormat;
|
12
|
14
|
import java.util.ArrayList;
|
13
|
15
|
import java.util.Calendar;
|
14
|
16
|
import java.util.Date;
|
15
|
17
|
|
16
|
18
|
import javax.swing.JOptionPane;
|
17
|
19
|
|
|
20
|
+import model.Chat;
|
18
|
21
|
import model.LocalUser;
|
|
22
|
+import model.Message;
|
|
23
|
+import model.Msg_Text;
|
|
24
|
+import model.RemoteUser;
|
19
|
25
|
import view.Interface;
|
20
|
26
|
|
21
|
27
|
public class Controller {
|
22
|
28
|
|
23
|
29
|
/*** CONSTANTES ***/
|
24
|
|
- final static int portUDPlistening_remoteUsr2 = 20002; // TO REMOVE when we use broadcast
|
25
|
|
- final static int portUDPlistening_remoteUsr3 = 20003; // TO REMOVE when we use broadcast
|
|
30
|
+ final static int portUDPlistening_remoteUsr2 = 31002; // TO REMOVE when we use broadcast
|
|
31
|
+ final static int portUDPlistening_remoteUsr3 = 31003; // TO REMOVE when we use broadcast
|
26
|
32
|
|
27
|
33
|
/*** ATTRIBUTS ***/
|
28
|
|
- private LocalUser myUser;
|
29
|
|
- private Interface hisView;
|
|
34
|
+ protected LocalUser myUser;
|
|
35
|
+ protected Interface hisView;
|
30
|
36
|
private ListeningThreadUDP udp_connect_thread;
|
31
|
37
|
private ListeningThreadTCPConnection tcp_connect_thread;
|
32
|
|
- private ArrayList<ListeningThreadTCPChat> tcp_chats_threads = new ArrayList<ListeningThreadTCPChat>(); // listes des utilisateurs actifs
|
33
|
38
|
private Historique histoire;
|
|
39
|
+
|
34
|
40
|
/**
|
35
|
41
|
* Constructor of Controller
|
36
|
42
|
* @parametres
|
|
@@ -46,7 +52,7 @@ public class Controller {
|
46
|
52
|
* - notification aux autres utilisateurs actifs
|
47
|
53
|
* </p>
|
48
|
54
|
*/
|
49
|
|
- private Controller(int portUDPsend,int portUDPlistening,int portTCP,Historique histoire) {
|
|
55
|
+ private Controller(int portUDPsend,int portUDPlistening,int portTCP) {
|
50
|
56
|
this.histoire=histoire;
|
51
|
57
|
InetAddress addIP = null;
|
52
|
58
|
try
|
|
@@ -64,10 +70,10 @@ public class Controller {
|
64
|
70
|
e.printStackTrace();
|
65
|
71
|
}
|
66
|
72
|
|
67
|
|
- this.udp_connect_thread = new ListeningThreadUDP("UDP Listening thread",this.myUser);
|
|
73
|
+ this.udp_connect_thread = new ListeningThreadUDP("UDP Listening thread",this);
|
68
|
74
|
this.udp_connect_thread.start();
|
69
|
75
|
|
70
|
|
- this.tcp_connect_thread = new ListeningThreadTCPConnection("TCP main Listening thread",this.myUser);
|
|
76
|
+ this.tcp_connect_thread = new ListeningThreadTCPConnection("TCP main Listening thread",this);
|
71
|
77
|
this.tcp_connect_thread.start();
|
72
|
78
|
|
73
|
79
|
notify_remote_users();
|
|
@@ -90,8 +96,7 @@ public class Controller {
|
90
|
96
|
* - notification aux autres utilisateurs actifs
|
91
|
97
|
* </p>
|
92
|
98
|
*/
|
93
|
|
- private Controller(int portUDPsend,int portUDPlistening,int portTCP,String pseudo,Historique histoire) {
|
94
|
|
- this.histoire=histoire;
|
|
99
|
+ private Controller(int portUDPsend,int portUDPlistening,int portTCP,String pseudo) {
|
95
|
100
|
InetAddress addIP = null;
|
96
|
101
|
try
|
97
|
102
|
{
|
|
@@ -102,10 +107,10 @@ public class Controller {
|
102
|
107
|
}
|
103
|
108
|
this.myUser = new LocalUser(pseudo,addIP,portUDPsend,portUDPlistening,portTCP);
|
104
|
109
|
|
105
|
|
- this.udp_connect_thread = new ListeningThreadUDP("UDP Listening thread",this.myUser);
|
|
110
|
+ this.udp_connect_thread = new ListeningThreadUDP("UDP Listening thread",this);
|
106
|
111
|
this.udp_connect_thread.start();
|
107
|
112
|
|
108
|
|
- this.tcp_connect_thread = new ListeningThreadTCPConnection("TCP main Listening thread",this.myUser);
|
|
113
|
+ this.tcp_connect_thread = new ListeningThreadTCPConnection("TCP main Listening thread",this);
|
109
|
114
|
this.tcp_connect_thread.start();
|
110
|
115
|
|
111
|
116
|
notify_remote_users();
|
|
@@ -127,12 +132,10 @@ public class Controller {
|
127
|
132
|
public ListeningThreadTCPConnection getTcp_connect_thread() {
|
128
|
133
|
return tcp_connect_thread;
|
129
|
134
|
}
|
130
|
|
- public ArrayList<ListeningThreadTCPChat> getTcp_chats_threads() {
|
131
|
|
- return tcp_chats_threads;
|
132
|
|
- }
|
133
|
135
|
public Historique getHistory() {
|
134
|
136
|
return histoire;
|
135
|
137
|
}
|
|
138
|
+
|
136
|
139
|
/*** SETTERS ***/
|
137
|
140
|
public void setMyUser(LocalUser myUser) {
|
138
|
141
|
this.myUser = myUser;
|
|
@@ -146,9 +149,6 @@ public class Controller {
|
146
|
149
|
public void setTcp_connect_thread(ListeningThreadTCPConnection tcp_connect_thread) {
|
147
|
150
|
this.tcp_connect_thread = tcp_connect_thread;
|
148
|
151
|
}
|
149
|
|
- public void setTcp_chats_threads(ArrayList<ListeningThreadTCPChat> tcp_chats_threads) {
|
150
|
|
- this.tcp_chats_threads = tcp_chats_threads;
|
151
|
|
- }
|
152
|
152
|
public void setHistory(Historique histoire) {
|
153
|
153
|
this.histoire=histoire;
|
154
|
154
|
}
|
|
@@ -169,7 +169,7 @@ public class Controller {
|
169
|
169
|
|
170
|
170
|
//sc1.close();
|
171
|
171
|
JOptionPane.showMessageDialog(null ,"Your nickname : " + tmpPseudo + " is valid !");
|
172
|
|
- hisView.Pseudolabel.setText("Your current username is: " + tmpPseudo);
|
|
172
|
+ //hisView.Pseudolabel.setText("Your current username is: " + tmpPseudo);
|
173
|
173
|
|
174
|
174
|
return tmpPseudo;
|
175
|
175
|
}
|
|
@@ -263,10 +263,7 @@ public class Controller {
|
263
|
263
|
tabresponse = response.split(":");
|
264
|
264
|
|
265
|
265
|
// Affichage de la réponse
|
266
|
|
- System.out.println("Remote user n°"+nbReponse+
|
267
|
|
- "\nIP : "+tabresponse[0]+
|
268
|
|
- "\nn°Port : "+tabresponse[1]+
|
269
|
|
- "\npseudo : "+tabresponse[2]);
|
|
266
|
+ //System.out.println("Remote user n°"+nbReponse+"\nIP : "+tabresponse[0]+"\nn°Port : "+tabresponse[1]+"\npseudo : "+tabresponse[2]);
|
270
|
267
|
|
271
|
268
|
// Si reception on ajoute l'utilisateur à notre liste d'utilisateur distant
|
272
|
269
|
this.myUser.addRemoteUser(InetAddress.getByName(tabresponse[0].split("/")[1]),Integer.parseInt(tabresponse[1]),tabresponse[2]);
|
|
@@ -328,6 +325,41 @@ public class Controller {
|
328
|
325
|
dgramSocket.close();
|
329
|
326
|
}
|
330
|
327
|
|
|
328
|
+ public void openSession(Chat c) {
|
|
329
|
+ Socket link=null;
|
|
330
|
+ try {
|
|
331
|
+ System.out.println("("+this.myUser.getPseudo()+") Connecting to "+c.getRemoteUser().getPortTCP()+" of " + c.getRemoteUser().getPseudo());
|
|
332
|
+ link=new Socket(c.getRemoteUser().getAddIP(),c.getRemoteUser().getPortTCP());
|
|
333
|
+ }catch(IOException e) {
|
|
334
|
+ System.out.println("Error linking to TCP server of "+ c.getRemoteUser().getPortTCP());
|
|
335
|
+ }
|
|
336
|
+ c.setSocket(link);
|
|
337
|
+ JOptionPane.showMessageDialog(null ,"New chat with "+c.getRemoteUser().getPseudo());
|
|
338
|
+
|
|
339
|
+ // TODO Récupération de la conversation (historique)
|
|
340
|
+ }
|
|
341
|
+
|
|
342
|
+ public void closeSession(Chat c) {
|
|
343
|
+ c.closeSocket();
|
|
344
|
+ // TODO Serait mieux d'enlever le chat de la liste de myUser
|
|
345
|
+ }
|
|
346
|
+
|
|
347
|
+ public void sendMsg(Message msg,Chat c) {
|
|
348
|
+ PrintWriter out=null;
|
|
349
|
+ try {
|
|
350
|
+ out = new PrintWriter(c.getUserSocket().getOutputStream(),true);
|
|
351
|
+ } catch (IOException e) {
|
|
352
|
+ e.printStackTrace();
|
|
353
|
+ }
|
|
354
|
+
|
|
355
|
+ DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
|
|
356
|
+ Date date = new Date();
|
|
357
|
+ out.println(dateFormat.format(date));
|
|
358
|
+
|
|
359
|
+ out.println(msg.getMessage());
|
|
360
|
+ }
|
|
361
|
+
|
|
362
|
+ // Plus utilisé
|
331
|
363
|
public void TCPmessage(int index) throws IOException {
|
332
|
364
|
Socket link=null;
|
333
|
365
|
String s1;
|
|
@@ -390,36 +422,28 @@ public class Controller {
|
390
|
422
|
* Laisse l'utilisateur choisir parmis la liste d'utilisateurs actifs celui avec lequel il souhaite échanger via un chat
|
391
|
423
|
*</p>
|
392
|
424
|
*/
|
393
|
|
- public void getOneActiveUser() throws IOException {
|
|
425
|
+ public void selectActiveUser() throws IOException {
|
394
|
426
|
this.printRemoteUserList();
|
395
|
427
|
int index=Integer.parseInt(JOptionPane.showInputDialog(null, "Please, enter index of one active user that you saw on the list to start a conversation with:"));
|
396
|
428
|
|
397
|
429
|
if (index >= 0 && index<this.myUser.getRemoteUsersList().size()) {
|
398
|
|
- /*
|
399
|
|
- if(userChatList.contains(this.myUser.getRemoteUsersList().get(index))) {
|
|
430
|
+
|
|
431
|
+ if(this.myUser.getIndexOf(this.myUser.getRemoteUsersList().get(index))==-1){
|
400
|
432
|
JOptionPane.showMessageDialog(null ,"User "+this.myUser.getRemoteUsersList().get(index).getPseudo()+" is already in chat with you");
|
401
|
433
|
}
|
402
|
434
|
else {
|
403
|
|
-
|
|
435
|
+ Chat chat=this.myUser.addChats(this.myUser.getRemoteUsersList().get(index));
|
|
436
|
+ this.openSession(chat);
|
404
|
437
|
}
|
405
|
|
- */
|
406
|
|
- // IF
|
407
|
|
- // TODO regarder si ils sont déjà en conversation
|
408
|
|
- // ELSE
|
409
|
|
- // TODO Créer un nouveau thread tcp
|
410
|
|
- int localUser_portTCP;
|
411
|
|
- int remoteUser_portTCP;
|
412
|
|
- //this.myUser.addChats(this.myUser.getRemoteUsersList().get(index), localUser_portTCP, remoteUser_portTCP);
|
413
|
|
- JOptionPane.showMessageDialog(null ,"New chat with "+this.myUser.getRemoteUsersList().get(index).getPseudo());
|
414
|
438
|
}
|
415
|
439
|
else {
|
416
|
440
|
JOptionPane.showMessageDialog(null ,"Wrong index (no active at index number "+index+" )");
|
|
441
|
+ this.selectActiveUser();
|
417
|
442
|
}
|
418
|
|
-
|
419
|
|
- this.TCPmessage(index);
|
420
|
|
- //sc2.close();
|
421
|
443
|
}
|
422
|
444
|
|
|
445
|
+
|
|
446
|
+
|
423
|
447
|
/********************************** A mettre dans l'interface ******************************************/
|
424
|
448
|
|
425
|
449
|
/**
|
|
@@ -439,31 +463,60 @@ public class Controller {
|
439
|
463
|
|
440
|
464
|
/*************************************************************************************************************************/
|
441
|
465
|
public static void main(String[] args) throws IOException, InterruptedException {
|
|
466
|
+
|
|
467
|
+ System.out.println("start program");
|
|
468
|
+
|
442
|
469
|
Historique histoire=new Historique();
|
443
|
|
- Controller ctr2 = new Controller(12226,portUDPlistening_remoteUsr2,22222,"Mike",histoire);
|
444
|
|
- Controller ctr3 = new Controller(12224,portUDPlistening_remoteUsr3,22223,"Alice",histoire);
|
445
|
|
- Controller ctr1 = new Controller(12225,20001,22221,histoire); // Notre utilisateur local
|
446
|
470
|
|
447
|
|
- // Création de l'interface
|
|
471
|
+ /** Création des utilisateurs **/
|
|
472
|
+ // REMOTEUSER_1 - MIKE
|
|
473
|
+ Controller ctr2 = new Controller(31012,portUDPlistening_remoteUsr2,31022,"Mike");
|
|
474
|
+ // REMOTEUSER_2 - ALICE
|
|
475
|
+ Controller ctr3 = new Controller(31013,portUDPlistening_remoteUsr3,31023,"Alice");
|
|
476
|
+ // LOCAL USER
|
|
477
|
+ Controller ctr1 = new Controller(31011,31001,31021);
|
|
478
|
+
|
|
479
|
+ /** Création de l'interface graphique **/
|
448
|
480
|
ctr1.hisView=Interface.createAndShowGUI(ctr1);
|
449
|
|
-
|
450
|
|
- // Fonction appelé par notre utilisateur local
|
451
|
|
- ctr1.getOneActiveUser();
|
452
|
|
- ctr1.changePseudo();
|
453
|
|
-
|
454
|
|
- // On attends 5 secondes
|
455
|
|
- System.out.println("Sleep mode for 5 seconds");
|
456
|
|
- Thread.sleep(5000);
|
457
|
481
|
|
458
|
|
- // On ferme les différents threads et socket d'écoute
|
459
|
|
- ctr1.udp_connect_thread.close();
|
|
482
|
+ /** Simulation of a session **/
|
|
483
|
+ // SELECT REMOTE USER
|
|
484
|
+ Chat chatwithrm0 = ctr1.myUser.addChats(ctr1.myUser.getRemoteUsersList().get(0));
|
|
485
|
+ // AFFICHAGE REMOTE USER CHOISIE
|
|
486
|
+ System.out.println("("+ctr1.myUser.getPseudo()+" ) OPEN SESSION WITH "+ctr1.myUser.getRemoteUsersList().get(0).getPseudo());
|
|
487
|
+ // OPEN SESSION
|
|
488
|
+ ctr1.openSession(chatwithrm0);
|
|
489
|
+ // SEND MESSAGE
|
|
490
|
+ ctr1.sendMsg(new Msg_Text(ctr1.myUser.getAddIP(),"test"), chatwithrm0);
|
|
491
|
+ // CLOSE SESSION
|
|
492
|
+ ctr1.sendMsg(new Msg_Text(ctr1.myUser.getAddIP(),"end"), chatwithrm0);
|
|
493
|
+ ctr1.closeSession(chatwithrm0);
|
|
494
|
+
|
|
495
|
+ /** Unused function **/
|
|
496
|
+ // MANUAL SELECTION OF ACTIVE USER
|
|
497
|
+ //ctr1.selectActiveUser();
|
|
498
|
+ // CHANGE USER NICKNAME
|
|
499
|
+ //ctr1.changePseudo();
|
|
500
|
+
|
|
501
|
+ /** Close thread and socket **/
|
|
502
|
+ // SLEEP 5 SEC
|
|
503
|
+ System.out.println("Sleep mode for 5 seconds ...");
|
|
504
|
+ Thread.sleep(5000);
|
|
505
|
+ // REMOTEUSER_1 - MIKE
|
|
506
|
+ ctr2.myUser.closeAllRemainingChatSocket();
|
|
507
|
+ ctr2.tcp_connect_thread.close();
|
460
|
508
|
ctr2.udp_connect_thread.close();
|
|
509
|
+ // REMOTEUSER_2 - ALICE
|
|
510
|
+ ctr3.myUser.closeAllRemainingChatSocket();
|
|
511
|
+ ctr3.tcp_connect_thread.close();
|
461
|
512
|
ctr3.udp_connect_thread.close();
|
462
|
|
-
|
|
513
|
+ // LOCAL USER
|
|
514
|
+ ctr1.myUser.closeAllRemainingChatSocket();
|
463
|
515
|
ctr1.tcp_connect_thread.close();
|
464
|
|
- ctr2.tcp_connect_thread.close();
|
465
|
|
- ctr3.tcp_connect_thread.close();
|
466
|
|
- JOptionPane.showMessageDialog(null ,"End");
|
|
516
|
+ ctr1.udp_connect_thread.close();
|
|
517
|
+ // AFFICHAGE
|
|
518
|
+ System.out.println("end program");
|
|
519
|
+ JOptionPane.showMessageDialog(null ,"END");
|
467
|
520
|
}
|
468
|
521
|
|
469
|
522
|
}
|