|
@@ -31,9 +31,10 @@ public class Controller {
|
31
|
31
|
final static int portUDPlistening_remoteUsr2 = 31002; // TO REMOVE when we use broadcast
|
32
|
32
|
final static int portUDPlistening_remoteUsr3 = 31003; // TO REMOVE when we use broadcast
|
33
|
33
|
|
|
34
|
+ public Boolean interfaceRunning;
|
34
|
35
|
/*** ATTRIBUTS ***/
|
35
|
36
|
protected LocalUser myUser;
|
36
|
|
- protected Interface hisView;
|
|
37
|
+ protected Interface view;
|
37
|
38
|
private ListeningThreadUDP udp_connect_thread;
|
38
|
39
|
private ListeningThreadTCPConnection tcp_connect_thread;
|
39
|
40
|
private Historique histoire;
|
|
@@ -126,8 +127,8 @@ public class Controller {
|
126
|
127
|
public LocalUser getMyUser() {
|
127
|
128
|
return myUser;
|
128
|
129
|
}
|
129
|
|
- public Interface getHisView() {
|
130
|
|
- return hisView;
|
|
130
|
+ public Interface getview() {
|
|
131
|
+ return view;
|
131
|
132
|
}
|
132
|
133
|
public ListeningThreadUDP getUdp_connect_thread() {
|
133
|
134
|
return udp_connect_thread;
|
|
@@ -143,8 +144,8 @@ public class Controller {
|
143
|
144
|
public void setMyUser(LocalUser myUser) {
|
144
|
145
|
this.myUser = myUser;
|
145
|
146
|
}
|
146
|
|
- public void setHisView(Interface hisView) {
|
147
|
|
- this.hisView = hisView;
|
|
147
|
+ public void setview(Interface view) {
|
|
148
|
+ this.view = view;
|
148
|
149
|
}
|
149
|
150
|
public void setUdp_connect_thread(ListeningThreadUDP udp_connect_thread) {
|
150
|
151
|
this.udp_connect_thread = udp_connect_thread;
|
|
@@ -186,11 +187,11 @@ public class Controller {
|
186
|
187
|
public void changePseudo() throws IOException {
|
187
|
188
|
String oldPseudo = this.myUser.getPseudo(); //Saves the old one for comparison
|
188
|
189
|
|
189
|
|
- String tmpPseudo = hisView.PseudotextField.getText(); // Read user input
|
|
190
|
+ String tmpPseudo = view.PseudotextField.getText(); // Read user input
|
190
|
191
|
|
191
|
192
|
while(!(this.validatePseudo(tmpPseudo)) || tmpPseudo.equals(oldPseudo)) {
|
192
|
|
- hisView.Pseudolabel.setText("Already exists, enter another nickname. Your current username is: " + oldPseudo); // Read user input
|
193
|
|
- tmpPseudo = hisView.PseudotextField.getText(); // Read user input
|
|
193
|
+ view.Pseudolabel.setText("Already exists, enter another nickname. Your current username is: " + oldPseudo); // Read user input
|
|
194
|
+ tmpPseudo = view.PseudotextField.getText(); // Read user input
|
194
|
195
|
}
|
195
|
196
|
|
196
|
197
|
this.myUser.setPseudo(tmpPseudo);
|
|
@@ -328,29 +329,48 @@ public class Controller {
|
328
|
329
|
dgramSocket.close();
|
329
|
330
|
}
|
330
|
331
|
|
331
|
|
- public void openSession(Chat c) {
|
332
|
|
- Socket link=null;
|
333
|
|
- try {
|
334
|
|
- System.out.println("("+this.myUser.getPseudo()+") Connecting to "+c.getRemoteUser().getPortTCP()+" of " + c.getRemoteUser().getPseudo());
|
335
|
|
- link=new Socket(c.getRemoteUser().getAddIP(),c.getRemoteUser().getPortTCP());
|
336
|
|
- }catch(IOException e) {
|
337
|
|
- System.out.println("Error linking to TCP server of "+ c.getRemoteUser().getPortTCP());
|
338
|
|
- }
|
|
332
|
+ /**
|
|
333
|
+ * @parameters
|
|
334
|
+ * @param chat : Chat
|
|
335
|
+ * <p>
|
|
336
|
+ * Ajout ou mise à jour l'utilisateur distant dans notre liste d'utilisateur distant
|
|
337
|
+ * </p>
|
|
338
|
+ */
|
|
339
|
+
|
|
340
|
+
|
|
341
|
+ public void openSession(RemoteUser rm) {
|
|
342
|
+ myUser.addChats(rm);
|
|
343
|
+ Chat c = myUser.getChats().get(myUser.getChatIndexOf(rm));
|
339
|
344
|
|
340
|
|
- c.setSocket(link);
|
341
|
|
- JOptionPane.showMessageDialog(null ,"New chat with "+c.getRemoteUser().getPseudo());
|
342
|
|
- try {
|
343
|
|
- System.out.println(this.getHistory().retrieveMessage(getMyUser(), c.getRemoteUser()));
|
344
|
|
- } catch (SQLException e) {
|
345
|
|
- System.out.println("souci avec le retrieveMsg");
|
346
|
|
- e.printStackTrace();
|
|
345
|
+ if(c.getActive()) {
|
|
346
|
+ JOptionPane.showMessageDialog(null ,"Already a session with "+c.getRemoteUser().getPseudo());
|
|
347
|
+ }
|
|
348
|
+ else {
|
|
349
|
+ Socket link=null;
|
|
350
|
+ try {
|
|
351
|
+ System.out.println("("+this.myUser.getPseudo()+") Connecting to "+c.getRemoteUser().getPortTCP()+" of " + c.getRemoteUser().getPseudo());
|
|
352
|
+ link=new Socket(c.getRemoteUser().getAddIP(),c.getRemoteUser().getPortTCP()/*, InetAddress.getLocalHost() ,myUser.getPortTCP()*/);
|
|
353
|
+ }catch(IOException e) {
|
|
354
|
+ System.out.println("Error linking to TCP server of "+ c.getRemoteUser().getPortTCP());
|
|
355
|
+ }
|
|
356
|
+
|
|
357
|
+ c.setSocket(link);
|
|
358
|
+ JOptionPane.showMessageDialog(null ,"New session with "+c.getRemoteUser().getPseudo());
|
|
359
|
+ // TODO Récupération de la conversation (historique)
|
|
360
|
+ try {
|
|
361
|
+ System.out.println(this.getHistory().retrieveMessage(getMyUser(), c.getRemoteUser()));
|
|
362
|
+ } catch (SQLException e) {
|
|
363
|
+ System.out.println("souci avec le retrieveMsg");
|
|
364
|
+ e.printStackTrace();
|
|
365
|
+ }
|
|
366
|
+ c.activate();
|
347
|
367
|
}
|
348
|
|
- // TODO Récupération de la conversation (historique)
|
|
368
|
+
|
349
|
369
|
}
|
350
|
370
|
|
351
|
371
|
public void closeSession(Chat c) {
|
352
|
|
- c.closeSocket();
|
353
|
|
- // TODO Serait mieux d'enlever le chat de la liste de myUser
|
|
372
|
+ sendMsg(new Msg_Text(myUser.getAddIP(),"end"), c);
|
|
373
|
+ this.myUser.closeChat(c);
|
354
|
374
|
}
|
355
|
375
|
|
356
|
376
|
public void sendMsg(Message msg,Chat c) {
|
|
@@ -377,8 +397,11 @@ public class Controller {
|
377
|
397
|
//System.out.println(message);
|
378
|
398
|
out.println(dateFormat.format(date));
|
379
|
399
|
out.println(message);
|
|
400
|
+ c.addMessage(msg);
|
|
401
|
+
|
380
|
402
|
}
|
381
|
403
|
|
|
404
|
+
|
382
|
405
|
// Plus utilisé
|
383
|
406
|
public void TCPmessage(int index) throws IOException {
|
384
|
407
|
Socket link=null;
|
|
@@ -468,12 +491,11 @@ public class Controller {
|
468
|
491
|
|
469
|
492
|
if (index >= 0 && index<this.myUser.getRemoteUsersList().size()) {
|
470
|
493
|
|
471
|
|
- if(this.myUser.getIndexOf(this.myUser.getRemoteUsersList().get(index))==-1){
|
|
494
|
+ if(this.myUser.getChatIndexOf(this.myUser.getRemoteUsersList().get(index))==-1){
|
472
|
495
|
JOptionPane.showMessageDialog(null ,"User "+this.myUser.getRemoteUsersList().get(index).getPseudo()+" is already in chat with you");
|
473
|
496
|
}
|
474
|
497
|
else {
|
475
|
|
- Chat chat=this.myUser.addChats(this.myUser.getRemoteUsersList().get(index));
|
476
|
|
- this.openSession(chat);
|
|
498
|
+ this.openSession(myUser.getRemoteUsersList().get(index));
|
477
|
499
|
}
|
478
|
500
|
}
|
479
|
501
|
else {
|
|
@@ -499,21 +521,23 @@ public class Controller {
|
499
|
521
|
Controller ctr1 = new Controller(31011,31001,31021,histoire);
|
500
|
522
|
|
501
|
523
|
/** Création de l'interface graphique **/
|
502
|
|
- Boolean interfaceRunning = true;
|
503
|
|
- ctr1.hisView=Interface.createAndShowGUI(ctr1);
|
|
524
|
+ ctr1.interfaceRunning =true;
|
|
525
|
+ ctr1.view=Interface.createAndShowGUI(ctr1);
|
504
|
526
|
|
505
|
527
|
/** Simulation of a session **/
|
506
|
528
|
|
507
|
|
- // SELECT REMOTE USER
|
508
|
|
- Chat chatwithrm0 = ctr1.myUser.addChats(ctr1.myUser.getRemoteUsersList().get(0));
|
|
529
|
+
|
509
|
530
|
// AFFICHAGE REMOTE USER CHOISIE
|
510
|
531
|
System.out.println("("+ctr1.myUser.getPseudo()+" ) OPEN SESSION WITH "+ctr1.myUser.getRemoteUsersList().get(0).getPseudo());
|
|
532
|
+ // SELECTION DE L UTILISATEUR
|
|
533
|
+ RemoteUser rm0 = ctr1.myUser.getRemoteUsersList().get(0);
|
511
|
534
|
// OPEN SESSION
|
512
|
|
- ctr1.openSession(chatwithrm0);
|
|
535
|
+ ctr1.openSession(rm0);
|
|
536
|
+ // RECUPERATION DE LA CONVERSATION
|
|
537
|
+ Chat chatwithrm0 = ctr1.myUser.getChats().get(ctr1.myUser.getChatIndexOf(rm0));
|
513
|
538
|
// SEND MESSAGE
|
514
|
539
|
ctr1.sendMsg(new Msg_Text(ctr1.myUser.getAddIP(),"test"), chatwithrm0);
|
515
|
540
|
// CLOSE SESSION
|
516
|
|
- ctr1.sendMsg(new Msg_Text(ctr1.myUser.getAddIP(),"end"), chatwithrm0);
|
517
|
541
|
ctr1.closeSession(chatwithrm0);
|
518
|
542
|
|
519
|
543
|
|
|
@@ -524,26 +548,24 @@ public class Controller {
|
524
|
548
|
//ctr1.changePseudo();
|
525
|
549
|
|
526
|
550
|
|
527
|
|
- /*
|
528
|
|
- while(interfaceRunning) {
|
529
|
|
-
|
530
|
|
- }*/
|
531
|
551
|
|
532
|
|
- /** Close thread and socket **/
|
533
|
|
- // SLEEP 20 SEC
|
534
|
|
- System.out.println("Sleep mode for 20 seconds ...");
|
535
|
|
- Thread.sleep(20000);
|
|
552
|
+ while(ctr1.interfaceRunning) {
|
|
553
|
+ //ctr1.view
|
|
554
|
+ }
|
|
555
|
+
|
|
556
|
+ System.out.println("Fin de la boucle");
|
536
|
557
|
|
|
558
|
+ /** Close thread and socket **/
|
537
|
559
|
// REMOTEUSER_1 - MIKE
|
538
|
|
- ctr2.myUser.closeAllRemainingChatSocket();
|
|
560
|
+ ctr2.myUser.closeAllChat();
|
539
|
561
|
ctr2.tcp_connect_thread.close();
|
540
|
562
|
ctr2.udp_connect_thread.close();
|
541
|
563
|
// REMOTEUSER_2 - ALICE
|
542
|
|
- ctr3.myUser.closeAllRemainingChatSocket();
|
|
564
|
+ ctr3.myUser.closeAllChat();
|
543
|
565
|
ctr3.tcp_connect_thread.close();
|
544
|
566
|
ctr3.udp_connect_thread.close();
|
545
|
567
|
// LOCAL USER
|
546
|
|
- ctr1.myUser.closeAllRemainingChatSocket();
|
|
568
|
+ ctr1.myUser.closeAllChat();
|
547
|
569
|
ctr1.tcp_connect_thread.close();
|
548
|
570
|
ctr1.udp_connect_thread.close();
|
549
|
571
|
// AFFICHAGE
|