Browse Source

fix connection tcp, send message + reception

Alexandre Gonzalvez 3 years ago
parent
commit
c9e317bd27

+ 110
- 57
Application/Clavardage/src/controller/Controller.java View File

@@ -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
 }

+ 3
- 4
Application/Clavardage/src/controller/ListeningThread.java View File

@@ -1,14 +1,13 @@
1 1
 package controller;
2 2
 
3
-import model.LocalUser;
4 3
 
5 4
 public abstract class ListeningThread extends Thread{
6 5
 	
7
-	protected LocalUser myUser;
6
+	protected Controller controller;
8 7
 	
9
-	public ListeningThread(String s,LocalUser myUser) {
8
+	public ListeningThread(String s,Controller controller) {
10 9
 		super(s);
11
-		this.myUser = myUser;
10
+		this.controller = controller;
12 11
 	}
13 12
 	
14 13
 	public abstract void run();

+ 25
- 32
Application/Clavardage/src/controller/ListeningThreadTCPChat.java View File

@@ -23,8 +23,8 @@ public class ListeningThreadTCPChat extends ListeningThread{
23 23
 	 * 	<p>
24 24
 	 * 	</p>
25 25
 	 */
26
-	public ListeningThreadTCPChat(String s,LocalUser myUser,Socket socket) { 
27
-		super(s,myUser);
26
+	public ListeningThreadTCPChat(String s,Controller controller,Socket socket) { 
27
+		super(s,controller);
28 28
 		this.socket=socket;
29 29
 		
30 30
 	}
@@ -36,35 +36,28 @@ public class ListeningThreadTCPChat extends ListeningThread{
36 36
 	 * </p>
37 37
 	 */
38 38
 	public void run() {
39
-		Socket link = this.socket;
40
-		try {
41
-			BufferedReader in =new BufferedReader(new InputStreamReader(link.getInputStream()));
42
-		} catch (IOException e) {
43
-			// TODO Auto-generated catch block
44
-			e.printStackTrace();
45
-		}
46
-		PrintWriter out=null;
47
-		try {
48
-			out = new PrintWriter(link.getOutputStream(),true);
49
-		} catch (IOException e) {
50
-			// TODO Auto-generated catch block
51
-			e.printStackTrace();
52
-		}
53
-		DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
54
-		Date date = new Date();
55
-		System.out.println(myUser.getPseudo()+" envoie un message");
56
-		out.println(dateFormat.format(date));
57
-		out.println("end");
58
-		String input;
59
-		/*while (!(input=in.readLine()).equals("end")) {
60
-			System.out.print("server_recoit:"+input);
61
-		}*/
62
-		try {
63
-			link.close();
64
-		} catch (IOException e) {
65
-			// TODO Auto-generated catch block
66
-			e.printStackTrace();
67
-		}		
39
+			BufferedReader in = null;
40
+			String msg = null;
41
+			try {
42
+				System.out.println("("+this.controller.myUser.getPseudo()+") WAIT FOR NEW MESSAGE ");
43
+				in =new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
44
+			} catch (IOException e) {
45
+				// TODO Auto-generated catch block
46
+				e.printStackTrace();
47
+			}
48
+			String input;
49
+			try {
50
+				while (!(input=in.readLine()).equals("end")) {
51
+					System.out.println("("+this.controller.myUser.getPseudo()+") recoit : "+input);
52
+				}
53
+			} catch (IOException e) {
54
+				// TODO Auto-generated catch block
55
+				e.printStackTrace();
56
+			}
57
+
58
+			
59
+
60
+	
68 61
 	}
69 62
 	
70 63
 	/* close
@@ -80,7 +73,7 @@ public class ListeningThreadTCPChat extends ListeningThread{
80 73
 		} catch (IOException e1) {
81 74
 			e1.printStackTrace();
82 75
 		}
83
-		System.out.println("End of listing thread UDP ("+this.myUser.getPseudo()+")");
76
+		System.out.println("End of listing thread UDP ("+this.controller.myUser.getPseudo()+")");
84 77
 		try {
85 78
 			this.interrupt();
86 79
 		}catch (Exception e){

+ 16
- 13
Application/Clavardage/src/controller/ListeningThreadTCPConnection.java View File

@@ -18,14 +18,14 @@ public class ListeningThreadTCPConnection extends ListeningThread{
18 18
 	 * 	<p>
19 19
 	 * 	</p>
20 20
 	 */
21
-	public ListeningThreadTCPConnection(String s,LocalUser myUser) { 
22
-		super(s,myUser);
21
+	public ListeningThreadTCPConnection(String s,Controller controller) { 
22
+		super(s,controller);
23 23
 		
24 24
 	}
25 25
 	
26 26
 	public void accept(ServerSocket servSocket) throws IOException {
27 27
 		Socket socket_tcp= servSocket.accept();
28
-		ListeningThreadTCPChat threadtcpchat = new ListeningThreadTCPChat("Chat_with_"+myUser.getPseudo(),myUser,socket_tcp);
28
+		ListeningThreadTCPChat threadtcpchat = new ListeningThreadTCPChat("Chat_with_"+controller.myUser.getPseudo(),controller,socket_tcp);
29 29
 		threadtcpchat.start();
30 30
 		threadtcpchat.interrupt();
31 31
 		
@@ -42,18 +42,21 @@ public class ListeningThreadTCPConnection extends ListeningThread{
42 42
 	public void run(){ 
43 43
 		
44 44
 		// Tant que l'utilisateur est actif on attends la demande de nouvelle conversation
45
+		ServerSocket servSocket=null;
46
+		try {
47
+			servSocket = new ServerSocket(controller.myUser.getPortTCP());
48
+			System.out.println("("+this.controller.myUser.getPseudo()+") Server is listening on port "+this.controller.myUser.getPortTCP());
49
+		} catch (IOException e) {
50
+			System.out.println("("+this.controller.myUser.getPseudo()+") Server is not listening on port "+this.controller.myUser.getPortTCP());
51
+			e.printStackTrace();
52
+		}
53
+		// TODO changer le true
45 54
 		while(true) {
46
-			
47
-			ServerSocket servSocket=null;
48
-			try {
49
-				servSocket = new ServerSocket(myUser.getPortTCP());
50
-			} catch (IOException e) {
51
-				// TODO Auto-generated catch block
52
-				e.printStackTrace();
53
-			}
54
-			
55
+			System.out.println("("+this.controller.myUser.getPseudo()+") WAITING FOR NEW CONNECTION");
56
+
55 57
 			try {
56 58
 				this.accept(servSocket);
59
+				System.out.println("("+this.controller.myUser.getPseudo()+") NEW CONNECTION");
57 60
 			} catch (IOException e) {
58 61
 				// TODO Auto-generated catch block
59 62
 				e.printStackTrace();
@@ -77,7 +80,7 @@ public class ListeningThreadTCPConnection extends ListeningThread{
77 80
 		} catch (IOException e1) {
78 81
 			e1.printStackTrace();
79 82
 		}
80
-		System.out.println("End of listing thread TCP ("+this.myUser.getPseudo()+")");
83
+		System.out.println("End of listing thread TCP ("+this.controller.myUser.getPseudo()+")");
81 84
 		try {
82 85
 			this.interrupt();
83 86
 		}catch (Exception e){

+ 6
- 6
Application/Clavardage/src/controller/ListeningThreadUDP.java View File

@@ -23,10 +23,10 @@ public class ListeningThreadUDP extends ListeningThread{
23 23
 	 * 		Création d'un socket d'écoute UDP
24 24
 	 * </p>
25 25
 	 */
26
-	public ListeningThreadUDP(String s,LocalUser myUser) { 
27
-		super(s,myUser);
26
+	public ListeningThreadUDP(String s,Controller controller) { 
27
+		super(s,controller);
28 28
 		try {
29
-			this.dgramSocket = new DatagramSocket(this.myUser.getPortUDPlistening(),this.myUser.getAddIP());
29
+			this.dgramSocket = new DatagramSocket(this.controller.myUser.getPortUDPlistening(),this.controller.myUser.getAddIP());
30 30
 		} catch (IOException e) {
31 31
 			e.printStackTrace();
32 32
 		}	
@@ -72,7 +72,7 @@ public class ListeningThreadUDP extends ListeningThread{
72 72
 				}
73 73
 				int senderUDPport = Integer.parseInt(tabMsg[1]); // On récupère le port UDP de l'utilisateur distant
74 74
 				
75
-				String toSend = myUser.getAddIP().toString()+":"+myUser.getPortTCP()+":"+myUser.getPseudo()+":test";
75
+				String toSend = controller.myUser.getAddIP().toString()+":"+controller.myUser.getPortTCP()+":"+controller.myUser.getPseudo()+":test";
76 76
 				DatagramPacket outPacket= new DatagramPacket(toSend.getBytes(), toSend.length(),itsIP, senderUDPport);
77 77
 
78 78
 				try {
@@ -86,7 +86,7 @@ public class ListeningThreadUDP extends ListeningThread{
86 86
 			else { 
87 87
 				try {
88 88
 					// On récupère l'adresse IP et le port TCP de l'utilisateur distant et ajout à la liste de l'utilisateur utilisant ce thread 
89
-					this.myUser.addRemoteUser(InetAddress.getByName(tabMsg[0].split("/")[1]),Integer.parseInt(tabMsg[1]),tabMsg[2]);
89
+					this.controller.myUser.addRemoteUser(InetAddress.getByName(tabMsg[0].split("/")[1]),Integer.parseInt(tabMsg[1]),tabMsg[2]);
90 90
 				} catch (NumberFormatException e) {
91 91
 					// TODO Auto-generated catch block
92 92
 					e.printStackTrace();
@@ -111,7 +111,7 @@ public class ListeningThreadUDP extends ListeningThread{
111 111
 	 */
112 112
 	public void close() {
113 113
 		this.dgramSocket.close();
114
-		System.out.println("End of listing thread UDP ("+this.myUser.getPseudo()+")");
114
+		System.out.println("End of listing thread UDP ("+this.controller.myUser.getPseudo()+")");
115 115
 		try {
116 116
 			this.interrupt();
117 117
 		}catch (Exception e){

+ 29
- 26
Application/Clavardage/src/model/Chat.java View File

@@ -1,54 +1,57 @@
1 1
 package model;
2 2
 
3
+import java.io.IOException;
4
+import java.net.Socket;
3 5
 import java.util.ArrayList;
4 6
 
5 7
 public class Chat {
6 8
 	
7 9
 	/*** ATTRIBUTS ***/
8
-	private int localUser_portTCP;
9
-	private int remoteUser_portTCP;
10
-	private ArrayList<RemoteUser> remoteUsersChatList = new ArrayList<RemoteUser>(); // listes des utilisateurs sur ce chat
10
+	private RemoteUser remoteUser;
11 11
 	private ArrayList<Message> messages = new ArrayList<Message>();
12
-
12
+	private Socket userSocket;
13
+	
13 14
 	/**
14 15
 	 *  Constructor of Chat (local)
15 16
 	 * @parametres
16 17
 	 * 	 	@param remoteUser : remoteUser => référence de l'utilisateur distant
17
-	 * 		@param localUser_portTCP : int => le numéro de port TCP d'écoute de la conversation de l'utilisateur local
18
-	 * 		@param remoteUser_portTCP : int => le numéro de port TCP d'écoute de la conversation de l'utilisateur distant
19
-	 * @description
18
+		 * @description
20 19
 	 * <p>
21 20
 	 * 		
22 21
 	 * </p>
23 22
 	 */
24
-	public Chat(RemoteUser rm,int localUser_portTCP,int remoteUser_portTCP) {
25
-		this.localUser_portTCP = localUser_portTCP;
26
-		this.remoteUser_portTCP = remoteUser_portTCP;
27
-		remoteUsersChatList.add(rm);
23
+	public Chat(RemoteUser rm) {
24
+		this.remoteUser=rm;
28 25
 	}
29 26
 	
30 27
 	/*** GETTERS ***/ 
31
-	public int getLocalUser_portTCP() {
32
-		return localUser_portTCP;
33
-	}
34
-	public int getRemoteUser_portTCP() {
35
-		return remoteUser_portTCP;
36
-	}
37
-	public ArrayList<RemoteUser> getRemoteUsersChatList() {
38
-		return remoteUsersChatList;
28
+	public RemoteUser getRemoteUser() {
29
+		return remoteUser;
39 30
 	}
40 31
 	public ArrayList<Message> getMessages() {
41 32
 		return messages;
42 33
 	}
43
-
34
+	public Socket getUserSocket() {
35
+		return userSocket;
36
+	}
37
+	
44 38
 	/*** SETTERS ***/
45
-	public void setLocalUser_portTCP(int localUser_portTCP) {
46
-		this.localUser_portTCP = localUser_portTCP;
39
+	public void setRemoteUser(RemoteUser rm) {
40
+		this.remoteUser = rm;
47 41
 	}
48
-	public void setRemoteUser_portTCP(int remoteUser_portTCP) {
49
-		this.remoteUser_portTCP = remoteUser_portTCP;
42
+	public void setSocket(Socket link) {
43
+		this.userSocket = link;
50 44
 	}
51
-	public void setRemoteUsersChatList(ArrayList<RemoteUser> remoteUsersChatList) {
52
-		this.remoteUsersChatList = remoteUsersChatList;
45
+	public void addMessage(Message msg) {
46
+		this.messages.add(msg);
47
+	}
48
+	
49
+	public void closeSocket() {
50
+		try {
51
+			this.userSocket.close();
52
+		} catch (IOException e) {
53
+			e.printStackTrace();
54
+		}
55
+		
53 56
 	}
54 57
 }

+ 24
- 2
Application/Clavardage/src/model/LocalUser.java View File

@@ -88,8 +88,30 @@ public class LocalUser extends User{
88 88
 	 * 		Ajout ou mise à jour l'utilisateur distant dans notre liste d'utilisateur distant
89 89
 	 * </p>
90 90
 	 */
91
-	public void addChats(RemoteUser rm,int localUser_portTCP,int remoteUser_portTCP) {
92
-		Chat newChat= new Chat(rm,localUser_portTCP,remoteUser_portTCP);
91
+	public Chat addChats(RemoteUser rm) {
92
+		Chat newChat= new Chat(rm);
93 93
 		this.chats.add(newChat);
94
+		return newChat;
95
+	}
96
+
97
+	public int getIndexOf(RemoteUser remoteUser) {
98
+		int i=0;
99
+		Boolean found  = (this.chats.get(i).getRemoteUser() == remoteUser);
100
+		while(i<this.chats.size() && !found) {
101
+			i++;
102
+			found = (this.chats.get(i).getRemoteUser() == remoteUser);
103
+		}
104
+		if(found) {
105
+			return i;
106
+		}
107
+		else {
108
+			return -1;
109
+		}
110
+	}
111
+	
112
+	public void closeAllRemainingChatSocket() {
113
+		for(int i=0;i<this.chats.size();i++) {
114
+			this.chats.get(i).closeSocket();
115
+		}
94 116
 	}
95 117
 }

+ 20
- 18
Application/Clavardage/src/model/User.java View File

@@ -4,24 +4,7 @@ import java.net.*;
4 4
 
5 5
 public abstract class User {
6 6
 	
7
-	@Override
8
-	public boolean equals(Object obj) {
9
-		if (this == obj)
10
-			return true;
11
-		if (obj == null)
12
-			return false;
13
-		if (getClass() != obj.getClass())
14
-			return false;
15
-		User other = (User) obj;
16
-		if (addIP == null) {
17
-			if (other.addIP != null)
18
-				return false;
19
-		} else if (!addIP.equals(other.addIP))
20
-			return false;
21
-		if (portTCP != other.portTCP)
22
-			return false;
23
-		return true;
24
-	}
7
+	
25 8
 	/*** ATTRIBUTS ***/
26 9
 	protected String pseudo;
27 10
 	protected InetAddress addIP;
@@ -73,4 +56,23 @@ public abstract class User {
73 56
 		this.portTCP = portTCP;
74 57
 	}
75 58
 
59
+	
60
+	@Override
61
+	public boolean equals(Object obj) {
62
+		if (this == obj)
63
+			return true;
64
+		if (obj == null)
65
+			return false;
66
+		if (getClass() != obj.getClass())
67
+			return false;
68
+		User other = (User) obj;
69
+		if (addIP == null) {
70
+			if (other.addIP != null)
71
+				return false;
72
+		} else if (!addIP.equals(other.addIP))
73
+			return false;
74
+		if (portTCP != other.portTCP)
75
+			return false;
76
+		return true;
77
+	}
76 78
 }

+ 1
- 1
Application/Clavardage/src/view/Interface.java View File

@@ -24,7 +24,7 @@ public class Interface implements ActionListener {
24 24
 	final static String LOOKANDFEEL = "System";
25 25
 	
26 26
 	public Interface(Controller controller) {
27
-		this.controller = controller;
27
+		this.hisController = controller;
28 28
 	}
29 29
 	
30 30
 	public Component createComponents() {

Loading…
Cancel
Save