Browse Source

Echange de messages ok entre deux PCs

Morgane Foussats 3 years ago
commit
ed4f39a6a0
61 changed files with 1806 additions and 0 deletions
  1. 15
    0
      chat/.classpath
  2. 17
    0
      chat/.project
  3. 15
    0
      chat/.settings/org.eclipse.jdt.core.prefs
  4. BIN
      chat/bin/controller/Agent.class
  5. BIN
      chat/bin/controller/DataBase.class
  6. BIN
      chat/bin/controller/Test.class
  7. BIN
      chat/bin/controller/package-info.class
  8. BIN
      chat/bin/gui/FenetreAcc$1.class
  9. BIN
      chat/bin/gui/FenetreAcc.class
  10. BIN
      chat/bin/gui/FenetreChat$1.class
  11. BIN
      chat/bin/gui/FenetreChat$2.class
  12. BIN
      chat/bin/gui/FenetreChat.class
  13. BIN
      chat/bin/gui/FenetreMenu$1.class
  14. BIN
      chat/bin/gui/FenetreMenu$2.class
  15. BIN
      chat/bin/gui/FenetreMenu$3$1.class
  16. BIN
      chat/bin/gui/FenetreMenu$3.class
  17. BIN
      chat/bin/gui/FenetreMenu$4$1.class
  18. BIN
      chat/bin/gui/FenetreMenu$4$2.class
  19. BIN
      chat/bin/gui/FenetreMenu$4.class
  20. BIN
      chat/bin/gui/FenetreMenu$5.class
  21. BIN
      chat/bin/gui/FenetreMenu.class
  22. BIN
      chat/bin/gui/package-info.class
  23. BIN
      chat/bin/gui/panda.png
  24. BIN
      chat/bin/images/panda.png
  25. BIN
      chat/bin/model/Contact.class
  26. BIN
      chat/bin/model/ListeContacts.class
  27. BIN
      chat/bin/model/Message.class
  28. BIN
      chat/bin/model/MessageChat.class
  29. BIN
      chat/bin/model/MessageDeconnexion.class
  30. BIN
      chat/bin/model/MessagePseudo.class
  31. BIN
      chat/bin/module-info.class
  32. BIN
      chat/bin/network/Manager.class
  33. BIN
      chat/bin/network/TCPChat.class
  34. BIN
      chat/bin/network/TCPServer.class
  35. BIN
      chat/bin/network/Tools.class
  36. BIN
      chat/bin/network/UDPInput.class
  37. BIN
      chat/bin/network/UDPOutput.class
  38. BIN
      chat/lib/mysql-connector-java-5.0.5-bin.jar
  39. 293
    0
      chat/src/controller/Agent.java
  40. 200
    0
      chat/src/controller/DataBase.java
  41. 15
    0
      chat/src/controller/Test.java
  42. 1
    0
      chat/src/controller/package-info.java
  43. 125
    0
      chat/src/gui/FenetreAcc.java
  44. 193
    0
      chat/src/gui/FenetreChat.java
  45. 234
    0
      chat/src/gui/FenetreMenu.java
  46. 1
    0
      chat/src/gui/package-info.java
  47. BIN
      chat/src/gui/panda.png
  48. BIN
      chat/src/images/panda.png
  49. 66
    0
      chat/src/model/Contact.java
  50. 96
    0
      chat/src/model/ListeContacts.java
  51. 75
    0
      chat/src/model/Message.java
  52. 22
    0
      chat/src/model/MessageChat.java
  53. 26
    0
      chat/src/model/MessageDeconnexion.java
  54. 23
    0
      chat/src/model/MessagePseudo.java
  55. 5
    0
      chat/src/module-info.java
  56. 19
    0
      chat/src/network/Manager.java
  57. 112
    0
      chat/src/network/TCPChat.java
  58. 61
    0
      chat/src/network/TCPServer.java
  59. 69
    0
      chat/src/network/Tools.java
  60. 73
    0
      chat/src/network/UDPInput.java
  61. 50
    0
      chat/src/network/UDPOutput.java

+ 15
- 0
chat/.classpath View File

@@ -0,0 +1,15 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<classpath>
3
+	<classpathentry kind="src" path="src"/>
4
+	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
5
+		<attributes>
6
+			<attribute name="module" value="true"/>
7
+		</attributes>
8
+	</classpathentry>
9
+	<classpathentry kind="lib" path="lib/mysql-connector-java-5.0.5-bin.jar">
10
+		<attributes>
11
+			<attribute name="module" value="true"/>
12
+		</attributes>
13
+	</classpathentry>
14
+	<classpathentry kind="output" path="bin"/>
15
+</classpath>

+ 17
- 0
chat/.project View File

@@ -0,0 +1,17 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<projectDescription>
3
+	<name>chat</name>
4
+	<comment></comment>
5
+	<projects>
6
+	</projects>
7
+	<buildSpec>
8
+		<buildCommand>
9
+			<name>org.eclipse.jdt.core.javabuilder</name>
10
+			<arguments>
11
+			</arguments>
12
+		</buildCommand>
13
+	</buildSpec>
14
+	<natures>
15
+		<nature>org.eclipse.jdt.core.javanature</nature>
16
+	</natures>
17
+</projectDescription>

+ 15
- 0
chat/.settings/org.eclipse.jdt.core.prefs View File

@@ -0,0 +1,15 @@
1
+eclipse.preferences.version=1
2
+org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3
+org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate
4
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=13
5
+org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
6
+org.eclipse.jdt.core.compiler.compliance=13
7
+org.eclipse.jdt.core.compiler.debug.lineNumber=generate
8
+org.eclipse.jdt.core.compiler.debug.localVariable=generate
9
+org.eclipse.jdt.core.compiler.debug.sourceFile=generate
10
+org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
11
+org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
12
+org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
13
+org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
14
+org.eclipse.jdt.core.compiler.release=enabled
15
+org.eclipse.jdt.core.compiler.source=13

BIN
chat/bin/controller/Agent.class View File


BIN
chat/bin/controller/DataBase.class View File


BIN
chat/bin/controller/Test.class View File


BIN
chat/bin/controller/package-info.class View File


BIN
chat/bin/gui/FenetreAcc$1.class View File


BIN
chat/bin/gui/FenetreAcc.class View File


BIN
chat/bin/gui/FenetreChat$1.class View File


BIN
chat/bin/gui/FenetreChat$2.class View File


BIN
chat/bin/gui/FenetreChat.class View File


BIN
chat/bin/gui/FenetreMenu$1.class View File


BIN
chat/bin/gui/FenetreMenu$2.class View File


BIN
chat/bin/gui/FenetreMenu$3$1.class View File


BIN
chat/bin/gui/FenetreMenu$3.class View File


BIN
chat/bin/gui/FenetreMenu$4$1.class View File


BIN
chat/bin/gui/FenetreMenu$4$2.class View File


BIN
chat/bin/gui/FenetreMenu$4.class View File


BIN
chat/bin/gui/FenetreMenu$5.class View File


BIN
chat/bin/gui/FenetreMenu.class View File


BIN
chat/bin/gui/package-info.class View File


BIN
chat/bin/gui/panda.png View File


BIN
chat/bin/images/panda.png View File


BIN
chat/bin/model/Contact.class View File


BIN
chat/bin/model/ListeContacts.class View File


BIN
chat/bin/model/Message.class View File


BIN
chat/bin/model/MessageChat.class View File


BIN
chat/bin/model/MessageDeconnexion.class View File


BIN
chat/bin/model/MessagePseudo.class View File


BIN
chat/bin/module-info.class View File


BIN
chat/bin/network/Manager.class View File


BIN
chat/bin/network/TCPChat.class View File


BIN
chat/bin/network/TCPServer.class View File


BIN
chat/bin/network/Tools.class View File


BIN
chat/bin/network/UDPInput.class View File


BIN
chat/bin/network/UDPOutput.class View File


BIN
chat/lib/mysql-connector-java-5.0.5-bin.jar View File


+ 293
- 0
chat/src/controller/Agent.java View File

@@ -0,0 +1,293 @@
1
+package controller;
2
+
3
+import java.awt.Color;
4
+import java.beans.PropertyChangeEvent;
5
+import java.beans.PropertyChangeListener;
6
+import java.beans.PropertyChangeSupport;
7
+import java.io.IOException;
8
+import java.net.InetAddress;
9
+import java.net.SocketException;
10
+import java.util.ArrayList;
11
+
12
+import javax.swing.text.BadLocationException;
13
+import javax.swing.text.SimpleAttributeSet;
14
+import javax.swing.text.StyleConstants;
15
+import javax.swing.text.StyledDocument;
16
+
17
+import gui.FenetreChat;
18
+import model.*;
19
+import network.*;
20
+
21
+public class Agent implements PropertyChangeListener{
22
+	private Contact user;
23
+	private UDPInput BIn;
24
+	private UDPOutput BOut;
25
+	//private TCPChat TCPSend;
26
+	private TCPServer server;
27
+	private ListeContacts list;
28
+	private InetAddress localAddress;
29
+	private InetAddress broadcast;
30
+	private ArrayList<TCPChat> listTCP;
31
+	private DataBase db;
32
+	
33
+	private PropertyChangeSupport support;
34
+	
35
+	
36
+	
37
+	public Agent(InetAddress address, int portIn, int portOut) throws IOException {
38
+		this.user = new Contact("", address, portIn);
39
+		this.BIn = new UDPInput(user.getAddress(), portIn);
40
+		this.BOut = new UDPOutput(user, portOut);
41
+		this.server = new TCPServer(user.getPort(), user.getAddress());
42
+		this.server.addPropertyChangeListener(this);
43
+		//this.TCPSend = new TCPChat(user);
44
+		this.list = ListeContacts.getInstance();
45
+		this.localAddress=Tools.getAdress()[0];
46
+		this.broadcast= Tools.getAdress()[1];
47
+		this.db = DataBase.getInstance();
48
+		this.listTCP = new ArrayList<TCPChat>();
49
+		this.support = new PropertyChangeSupport(this);
50
+		this.BIn.addPropertyChangeListener(this);
51
+		BIn.start();
52
+	}
53
+	
54
+	
55
+	
56
+	public void propertyChange(PropertyChangeEvent evt) {
57
+    	switch (evt.getPropertyName()) {
58
+    	case "Msg UDP Recu" : 
59
+    		System.out.println("envoi message");
60
+    		Message msg = BIn.getMessage();
61
+    		try {
62
+				handleMessage(msg);
63
+			} catch (IOException e) {
64
+				e.printStackTrace();
65
+			}
66
+    		break;
67
+    	case "session chat demarree" :
68
+    		System.out.println("dans session demarree");
69
+    		TCPChat tcpChat = this.server.getConnexionTCPChat();
70
+    		tcpChat.addPropertyChangeListener(this);
71
+    		this.listTCP.add(tcpChat);
72
+    		break;
73
+    	
74
+		case "Msg TCP Recu" : 
75
+			SimpleAttributeSet left = new SimpleAttributeSet();
76
+	        StyleConstants.setAlignment(left, StyleConstants.ALIGN_LEFT);
77
+	        StyleConstants.setForeground(left, Color.RED);
78
+			System.out.println("dans property change");
79
+			TCPChat tcpChatCo = this.listTCP.get(0);
80
+			try {
81
+				Thread.sleep(1000);
82
+			} catch (InterruptedException e1) {
83
+				e1.printStackTrace();
84
+			}
85
+			MessageChat msgRecu = tcpChatCo.getMessageRecu();
86
+			//FenetreChat.getTa().append("kevin"+ " : "+msgRecu.getMessage()+"\n");
87
+			StyledDocument doc = FenetreChat.getDoc();
88
+			try {
89
+				doc.insertString(doc.getLength(), "\n"+msgRecu.getMessage(), left );
90
+			} catch (BadLocationException e) {
91
+				e.printStackTrace();
92
+			}
93
+            doc.setParagraphAttributes(doc.getLength(), 1, left, false);
94
+		break;
95
+    	}
96
+    	
97
+    }
98
+	
99
+	
100
+	
101
+	//Envoi sur 5001 et reçoit sur 5000
102
+	public void testSendBroadcast() throws IOException, InterruptedException {
103
+		MessagePseudo msg = new MessagePseudo(this.user.getAddress(), Tools.getLocalIp(), user.getPort(), 14000, 1, "Michel");
104
+		BOut.send(msg, user.getAddress(), user.getPort());
105
+		Thread.sleep(10);
106
+		MessagePseudo msgRecu = BIn.getMessage();
107
+		handleMessage(msgRecu);
108
+		MessagePseudo msg2 = new MessagePseudo(this.user.getAddress(), Tools.getLocalIp(), user.getPort(), 14000, 1, "Jacques");
109
+		BOut.send(msg2, user.getAddress(), user.getPort());
110
+		Thread.sleep(10);
111
+		MessagePseudo msgRecu2 = BIn.getMessage();
112
+		handleMessage(msgRecu2);
113
+		MessagePseudo msg3 = new MessagePseudo(this.user.getAddress(), Tools.getLocalIp(), user.getPort(), 14000, 1, "Bob");
114
+		BOut.send(msg3, user.getAddress(), user.getPort());
115
+		Thread.sleep(10);
116
+		MessagePseudo msgRecu3 = BIn.getMessage();
117
+		handleMessage(msgRecu3);
118
+		choisirPseudo("Bob");
119
+		choisirPseudo("Patrick");
120
+	}
121
+	
122
+	
123
+	/*public void testSendTCP() throws IOException, InterruptedException {
124
+		
125
+		TCPSend.sendMsg("COUCOU");
126
+		Thread.sleep(1000);
127
+		TCPSend.sendMsg("Ca va ?");
128
+		Thread.sleep(1000);
129
+		TCPSend.sendMsg("Non je suis KO");
130
+		Thread.sleep(1000);
131
+		TCPSend.sendMsg("Moi aussi :'(");
132
+		Thread.sleep(1000);
133
+		
134
+	}*/
135
+	
136
+	/*type 0 : premier message de broadcast
137
+	 * type, 1 : deuxieme message de broadcast avec pseudo
138
+	 * type 2 : Message de déconnexion
139
+	 */
140
+	
141
+	public void handleMessage(Message msg) throws IOException {
142
+		switch(msg.getTypeMessage()) {
143
+		case 0 : 
144
+			System.out.println("Envoi message présentation");
145
+			MessagePseudo msgPresentation = new MessagePseudo(this.localAddress, msg.getAddressSrc(), this.user.getPort(), msg.getPortSrc(), 1, this.user.getPseudo());
146
+			BOut.send(msgPresentation, msg.getAddressSrc(), msg.getPortSrc());
147
+			break;
148
+		case 1 : 
149
+			MessagePseudo msgPseudo = (MessagePseudo) msg;
150
+			if(!list.pseudoExist(msgPseudo.getPseudo())) {
151
+				Contact newUser = new Contact(msgPseudo.getPseudo(), msgPseudo.getAddressSrc(), msgPseudo.getPortSrc());
152
+				list.addContact(newUser);
153
+			}
154
+			break;
155
+		/*case 2 :
156
+			MessageDeconnexion messageDeconnexion = (MessageDeconnexion) msg;
157
+			if(list.pseudoExist(messageDeconnexion.getPseudo())){
158
+				Contact contact = list.findContact(messageDeconnexion.getPseudo());
159
+				list.deleteContact(contact);
160
+			}*/
161
+		}
162
+		
163
+	}
164
+	
165
+	public boolean connect(String pseudo) throws IOException, InterruptedException {
166
+		MessagePseudo msg_connexion = new MessagePseudo(this.localAddress, this.broadcast, this.user.getPort(), 25000, 0, "");
167
+		BOut.send(msg_connexion, this.broadcast, 25000);
168
+		Thread.sleep(5000);
169
+		ArrayList<MessagePseudo> msg_recus = BIn.getListMessage();
170
+		for(int i =0; i<msg_recus.size(); i++) {
171
+			handleMessage(msg_recus.get(i));
172
+		}
173
+		boolean pseudoOK = choisirPseudo(pseudo);
174
+		list.addContact(user);
175
+		if(pseudoOK){
176
+			int id = db.addUser(pseudo);
177
+			this.user.setId(id);
178
+			MessagePseudo msgPresentation = new MessagePseudo(this.localAddress, this.broadcast, this.user.getPort(), 25000, 1, this.user.getPseudo());
179
+			BOut.send(msgPresentation, this.broadcast, 25000);
180
+			this.user.setStatut("En ligne");
181
+			System.out.println("Connexion réussie avec le pseudo : "+pseudo);
182
+		}else {
183
+			System.out.println("Echec de la connexion, veuillez choisir un autre pseudo");
184
+		}
185
+		return pseudoOK;
186
+	}
187
+	
188
+	public boolean choisirPseudo(String pseudo) {
189
+		if(!list.pseudoExist(pseudo)){
190
+			user.setPseudo(pseudo);
191
+			System.out.println("Pseudo set : "+pseudo);
192
+			return true;
193
+		}else {
194
+			System.out.println("Pseudo déjà utilisé");
195
+			return false;
196
+		}
197
+	}
198
+	
199
+	public void deconnexion() throws IOException {
200
+		MessageDeconnexion msgDeconnexion = new MessageDeconnexion(this.localAddress, this.broadcast, this.user.getPort(), 25000, 3, user.getPseudo());
201
+		BOut.send(msgDeconnexion, this.broadcast, this.user.getPort());
202
+		System.exit(1);
203
+	}
204
+	
205
+	public boolean changerPseudo(String pseudo) {
206
+        boolean changeOK = choisirPseudo(pseudo);
207
+        if(changeOK){
208
+            System.out.println("Pseudo changé à : "+pseudo);
209
+            db.updatePseudo(this.user.getId(), pseudo);
210
+            return true;
211
+        }else {
212
+            System.out.println("Veuillez choisir un autre pseudo");
213
+            return false;
214
+        }
215
+    }
216
+
217
+    public boolean chatExists(int destID) {
218
+        boolean chatExists = false;
219
+        for(TCPChat tcp : listTCP) {
220
+            if(tcp.getDestID()==destID) {
221
+                chatExists = true;
222
+            }
223
+        }
224
+        return chatExists;
225
+    }
226
+
227
+    public void createChat (int destId, String destPseudo) throws IOException {
228
+        boolean chatExists = chatExists(destId);
229
+        if(!chatExists) {
230
+            for(Contact user : list.getListe()) {
231
+                if(user.getPseudo().equals(destPseudo)) {
232
+                    TCPChat connexionTCP = new TCPChat(user, destId);
233
+                    listTCP.add(connexionTCP);
234
+                    connexionTCP.addPropertyChangeListener(this);
235
+                }
236
+            }
237
+        }
238
+    }
239
+    
240
+    
241
+    
242
+    public void addPropertyChangeListener(PropertyChangeListener pcl, String propertyName){
243
+        this.support.addPropertyChangeListener(propertyName, pcl);
244
+    }
245
+
246
+    public void deletePropertyChangeListener(PropertyChangeListener pcl, String propertyName){
247
+        this.support.removePropertyChangeListener(propertyName,pcl);
248
+    }
249
+
250
+
251
+
252
+	public Contact getUser() {
253
+		return user;
254
+	}
255
+
256
+
257
+
258
+	public void setUser(Contact user) {
259
+		this.user = user;
260
+	}
261
+
262
+
263
+
264
+	public ListeContacts getList() {
265
+		return list;
266
+	}
267
+
268
+
269
+
270
+	public void setList(ListeContacts list) {
271
+		this.list = list;
272
+	}
273
+
274
+
275
+	public DataBase getDb() {
276
+		return db;
277
+	}
278
+
279
+
280
+
281
+	public void setDb(DataBase db) {
282
+		this.db = db;
283
+	}
284
+	
285
+	
286
+	public ArrayList<TCPChat> getListeTCPChat(){
287
+		return listTCP;
288
+	}
289
+	
290
+	public TCPChat getTCPChat() {
291
+		return this.listTCP.remove(0);
292
+	}
293
+}

+ 200
- 0
chat/src/controller/DataBase.java View File

@@ -0,0 +1,200 @@
1
+package controller;
2
+
3
+import java.sql.*;
4
+
5
+public class DataBase {
6
+	
7
+	//Connexion serveur GEI
8
+	//login : tp_servlet_018
9
+	//password : Taey2Aje
10
+	//Plus d'infs connexion sur moodle
11
+	
12
+	private static DataBase instance = null;
13
+	
14
+	public Connection connexion;
15
+	
16
+	private DataBase() {
17
+		try {
18
+			Class.forName("com.mysql.jdbc.Driver");
19
+		}catch (ClassNotFoundException e){
20
+			e.printStackTrace();
21
+		}
22
+		try {
23
+			connexion = DriverManager.getConnection("jdbc:mysql://srv-bdens.insa-toulouse.fr:3306/tp_servlet_018?useSSL=false", "tp_servlet_018", "Taey2Aje");
24
+			//connexion = DriverManager.getConnection("jdbc:mysql://localhost/test?characterEncoding=utf8", "root","root");
25
+		}catch(SQLException e) {
26
+			e.printStackTrace();
27
+		}
28
+	}
29
+	
30
+	public static DataBase getInstance() {
31
+		synchronized (DataBase.class) {
32
+			DataBase db = instance;
33
+			if(db == null) {
34
+				db = new DataBase();
35
+			}
36
+			return db;
37
+		}
38
+	}
39
+	
40
+	//retourne l'id associé au user ajouté
41
+	public int addUser(String pseudo) {
42
+		String query = "INSERT INTO users (pseudo, statut) VALUES (?, ?)";
43
+		PreparedStatement pStat = null;
44
+		try {
45
+			pStat = connexion.prepareStatement(query);
46
+			pStat.setString(1, pseudo);
47
+			pStat.setString(2, "En ligne");
48
+			pStat.executeUpdate();
49
+		}catch (SQLException e) {
50
+			e.printStackTrace();
51
+		}
52
+		query = "SELECT * FROM users WHERE pseudo=?";
53
+		int id =0;
54
+		try {
55
+			pStat = connexion.prepareStatement(query);
56
+			pStat.setString(1, pseudo);
57
+			ResultSet resId = pStat.executeQuery();
58
+			if(resId.first()) {
59
+				id = resId.getInt("id");
60
+			}
61
+		}catch(SQLException e) {
62
+			e.printStackTrace();
63
+		}
64
+		return id;
65
+	}
66
+
67
+	public String getPseudoFromId(int id) {
68
+		String pseudo = null;
69
+		String query = "SELECT * FROM users WHERE id=?";
70
+		PreparedStatement pStat = null;
71
+		try {
72
+			pStat = connexion.prepareStatement(query);
73
+			pStat.setInt(1, id);
74
+			ResultSet resPseudo = pStat.executeQuery();
75
+			if(resPseudo.first()) {
76
+				pseudo = resPseudo.getString("pseudo");
77
+			}
78
+		}catch(SQLException e) {
79
+			e.printStackTrace();
80
+		}
81
+		return pseudo;
82
+	}
83
+	
84
+	public int getIdFromPseudo(String pseudo) {
85
+		int id = -1;
86
+		String query = "SELECT * FROM users WHERE pseudo='" + pseudo + "'";
87
+		PreparedStatement pStat = null;
88
+		try {
89
+			pStat = connexion.prepareStatement(query);
90
+			//pStat.setString(1, pseudo);
91
+			ResultSet resId = pStat.executeQuery();
92
+			if(resId.next()) {
93
+				id = resId.getInt(1);
94
+			}
95
+		}catch(SQLException e) {
96
+			e.printStackTrace();
97
+		}
98
+		return id;
99
+	}
100
+	
101
+	public void deleteFromId(int id) {
102
+		String query = "DELETE FROM `users` WHERE `id` = ?";
103
+		PreparedStatement pStat = null;
104
+		try {
105
+			pStat = connexion.prepareStatement(query);
106
+			pStat.setInt(1, id);
107
+			pStat.executeUpdate();
108
+		}catch(SQLException e) {
109
+			e.printStackTrace();
110
+		}
111
+		
112
+	}
113
+	
114
+	public void updatePseudo(int id, String pseudo) {
115
+		String query = "UPDATE `users` SET `pseudo`=? WHERE id=?";
116
+		PreparedStatement pStat = null;
117
+		try {
118
+			pStat = connexion.prepareStatement(query);
119
+			pStat.setString(1, pseudo);
120
+			pStat.setInt(2, id);
121
+			pStat.executeUpdate();
122
+		}catch(SQLException e) {
123
+			e.printStackTrace();
124
+		}
125
+	}
126
+	
127
+	//chat table : nom : id1id2
128
+    //colonnes :
129
+    //id (l'id unique du message)
130
+    //id1 et id2 les id des deux users qui se parlent
131
+    //message : le message envoyé 
132
+    //date : la date d'envoi du msg
133
+    public void newChatTable (int id1, int id2) {
134
+        int idSrc = id1;
135
+        int idDest = id2;
136
+        if(id1>id2) {
137
+            idSrc = id2;
138
+            idDest = id1;
139
+        }
140
+        String nameTable = idSrc+"_"+idDest;
141
+        String query = "CREATE TABLE " +nameTable+" (\n" +
142
+        "id INT PRIMARY KEY NOT NULL AUTO_INCREMENT, \n" +
143
+        "idSrc INT NOT NULL,\n" + 
144
+        "idDest INT NOT NULL,\n" +
145
+        "message MEDIUMTEXT NOT NULL,\n" +
146
+        "date DATE NOT NULL\n" +
147
+        ");";
148
+        PreparedStatement pStat = null;
149
+        try {
150
+            pStat = connexion.prepareStatement(query);
151
+            pStat.executeUpdate();
152
+        }catch(SQLException e) {
153
+            e.printStackTrace();
154
+        }
155
+
156
+    }
157
+    
158
+    public boolean tableChatExists(int id1, int id2) {
159
+        int idSrc = id1;
160
+        int idDest = id2;
161
+        boolean res = true;
162
+        if(id1>id2) {
163
+            idSrc = id2;
164
+            idDest = id1;
165
+        }
166
+        String nameTable = idSrc+"_"+idDest;
167
+        String query = "SELECT * FROM " + nameTable;
168
+        PreparedStatement pStat = null;
169
+        try {
170
+            pStat = connexion.prepareStatement(query);
171
+            pStat.executeQuery();
172
+        }catch(SQLException e) {
173
+            res = false;
174
+            //e.printStackTrace();
175
+        }
176
+        return res;
177
+    }
178
+    
179
+//il faut mettre dans l'ordre idSrc pour id1 et id Dest pour id2 dans les params
180
+    public void addMessage(int id1, int id2, String message) {
181
+        int idSrc = id1;
182
+        int idDest = id2;
183
+        if(id1>id2) {
184
+            idSrc = id2;
185
+            idDest = id1;
186
+        }
187
+        String nameTable = idSrc+"_"+idDest;
188
+        String query = "INSERT INTO " + nameTable + "(idSrc, idDest, message, date) VALUES (?, ?, ?, CURRENT_TIME)";
189
+        PreparedStatement pStat = null;
190
+        try {
191
+            pStat = connexion.prepareStatement(query);
192
+            pStat.setInt(1, id1);
193
+            pStat.setInt(2, id2);
194
+            pStat.setString(3, message);
195
+            pStat.executeUpdate();
196
+        }catch(SQLException e) {
197
+            e.printStackTrace();
198
+        }
199
+    }
200
+}

+ 15
- 0
chat/src/controller/Test.java View File

@@ -0,0 +1,15 @@
1
+package controller;
2
+
3
+import java.io.IOException;
4
+
5
+import network.Tools;
6
+
7
+public class Test {
8
+	public static  void main(String args[]) throws IOException, InterruptedException {
9
+		Agent agent1 = new Agent(Tools.getAdress()[0], 22001, 22000);
10
+		int id = agent1.getDb().getIdFromPseudo(agent1.getUser().getPseudo());
11
+		System.out.println("ID : "+id);
12
+		
13
+		//Tools.printInterfaces();
14
+	}
15
+}

+ 1
- 0
chat/src/controller/package-info.java View File

@@ -0,0 +1 @@
1
+package controller;

+ 125
- 0
chat/src/gui/FenetreAcc.java View File

@@ -0,0 +1,125 @@
1
+package gui;
2
+
3
+import java.awt.BorderLayout;
4
+import java.awt.Dimension;
5
+import java.awt.GridLayout;
6
+import java.awt.event.ActionEvent;
7
+import java.awt.event.ActionListener;
8
+
9
+import javax.swing.BorderFactory;
10
+import javax.swing.JButton;
11
+import javax.swing.JFrame;
12
+import javax.swing.JLabel;
13
+import javax.swing.JPanel;
14
+import javax.swing.JTextField;
15
+import javax.swing.SwingConstants;
16
+import javax.swing.JOptionPane;
17
+
18
+import java.util.concurrent.ExecutorService;
19
+import java.util.concurrent.Executors;
20
+import java.io.IOException;
21
+
22
+import controller.*;
23
+import network.Tools;
24
+
25
+public class FenetreAcc implements ActionListener {
26
+	JFrame frame;
27
+    JPanel panel;
28
+    JTextField pseudofield;
29
+    JLabel Text;
30
+    JButton Connexion;
31
+    
32
+    /*
33
+     * Constructeur d'une fenetre d'affichage pour la connexion d'un utilisateur.
34
+     * Cette fenetre sera munie d'un bouton de connexion et d'une zone de saisie de pseudo.
35
+     */
36
+    public FenetreAcc() {
37
+		// TODO Auto-generated constructor stub
38
+		//creer une instance JFrame 
39
+		frame = new JFrame("Fenetre accueil");
40
+        //sortir quand l’utilisateur ferme le frame
41
+		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
42
+		// fixer les dimensions de la fenetre 
43
+        frame.setSize(new Dimension(120, 40));
44
+        //Creer le JPanel
45
+        panel = new JPanel(new GridLayout(3,1));
46
+        //Ajouter les elements 
47
+        this.addWidgets();
48
+        //Set the default button.
49
+        frame.getRootPane().setDefaultButton(Connexion);
50
+        //Ajouter le panel a la window
51
+        frame.getContentPane().add(panel, BorderLayout.CENTER);
52
+        //L'utilisateur ne pourra pas aggrandir la fenetre
53
+        this.frame.setResizable(false);
54
+        //Afficher la fenetre
55
+        frame.pack();
56
+        frame.setVisible(true);
57
+	}
58
+    
59
+    private void addWidgets() {
60
+	 	// Créer Zone d'insertion de texte pour le pseudo
61
+        this.pseudofield = new JTextField(2); 
62
+        // creation d'un label qui contiendra un txt au centre
63
+        this.Text = new JLabel("Type your user name", SwingConstants.CENTER); 
64
+        //Ajout d'un bouton Connexion 
65
+        this.Connexion = new JButton("Connexion");
66
+        //On associe au bouton Connexion des actions a realiser
67
+        this.Connexion.addActionListener(this);
68
+        // On ajouter les differents elements au panel
69
+        panel.add(pseudofield);
70
+        panel.add(Text);
71
+        panel.add(Connexion);
72
+        //ajouter un effet de bord transparent au composant Jlabel
73
+        Text.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
74
+ }
75
+    
76
+    public void actionPerformed(ActionEvent event) {
77
+		 // on recupere le texte entree dans la zone de saisie
78
+		 String pseudo =  pseudofield.getText();
79
+		 // On crée un objet de type ChatApp
80
+		 Agent agent;
81
+		 boolean connexion;
82
+		 
83
+		try {
84
+			agent = new Agent(Tools.getAdress()[0], 25001, 25000);
85
+			// on tente une connexion avec ce pseudo
86
+			connexion = agent.connect(pseudo);
87
+			// Dans les deux cas de figures (reussite ou echec) on affiche un pop-up pour expliquer la situation
88
+			 if(connexion) {
89
+				 	// La connexion a reussi
90
+					JOptionPane.showMessageDialog(frame, "Bonjour " + pseudo) ;
91
+					frame.dispose();
92
+					// on lance une nouvelle fenetre de type View_Menu
93
+					FenetreMenu fenetreCourante= new FenetreMenu(agent);
94
+			}
95
+			else {
96
+					// La connexion a echoue, il est possible de rentrer un nouveau pseudo
97
+					JOptionPane.showMessageDialog(frame, "Echec de Connexion , ce pseudo est deja pris !");
98
+			}
99
+		} catch (IOException e) {
100
+			System.out.println("Création thread impossible");
101
+			e.printStackTrace();
102
+		} catch (InterruptedException e) {
103
+			System.out.println("connect impossible");
104
+			e.printStackTrace();
105
+		}
106
+		 
107
+    }
108
+    
109
+    private static void createAndShowGUI() {
110
+		 // Etre certain d'avoir une joli fenetre 
111
+	        JFrame.setDefaultLookAndFeelDecorated(true);
112
+	        // On crée une fentre d'acceuil
113
+	        FenetreAcc  fenetre = new FenetreAcc();
114
+	 }
115
+    
116
+    public static void main(String[] args) {
117
+        //Schedule a job for the event-dispatching thread:
118
+        //creating and showing this application's GUI.
119
+        javax.swing.SwingUtilities.invokeLater(new Runnable() {
120
+            public void run() {
121
+                createAndShowGUI();
122
+            }
123
+        });
124
+    }
125
+}

+ 193
- 0
chat/src/gui/FenetreChat.java View File

@@ -0,0 +1,193 @@
1
+package gui;
2
+
3
+import java.awt.event.ActionEvent;
4
+import java.awt.event.ActionListener;
5
+import java.awt.event.WindowAdapter;
6
+import java.awt.event.WindowEvent;
7
+import java.beans.PropertyChangeEvent;
8
+import java.beans.PropertyChangeListener;
9
+import java.io.IOException;
10
+import java.util.List;
11
+
12
+import javax.swing.*;
13
+import javax.swing.text.BadLocationException;
14
+import javax.swing.text.SimpleAttributeSet;
15
+import javax.swing.text.StyleConstants;
16
+import javax.swing.text.StyledDocument;
17
+
18
+import controller.Agent;
19
+import model.Contact;
20
+import model.MessageChat;
21
+import network.TCPChat;
22
+
23
+import java.awt.*;
24
+
25
+public class FenetreChat extends JFrame {
26
+	
27
+	JFrame frame;
28
+	Agent agent;
29
+	Contact dest; //destinataire du message
30
+	Contact user; //utilisateur
31
+	
32
+	WindowAdapter wa;
33
+	JPanel panel;
34
+	static JTextArea ta;
35
+
36
+    static StyledDocument doc;
37
+	
38
+	
39
+	public FenetreChat(Agent agent, String userString) {
40
+		this.frame = new JFrame("Fenetre Chat avec "+ userString);
41
+		this.agent = agent;
42
+		this.user = agent.getUser();
43
+		this.dest = agent.getList().findContact(userString);
44
+		
45
+		this.frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
46
+		
47
+		this.frame.setSize(new Dimension(400, 400));
48
+		this.frame.setResizable(true);
49
+		
50
+		this.wa = new WindowAdapter() {
51
+				public void windowClosing(WindowEvent e) {
52
+					int quitter = FenetreMenu.fenetreFermeture();
53
+					if(quitter == 0) {
54
+						frame.dispose();
55
+					}
56
+		}};
57
+		
58
+		addWidgets();
59
+		frame.addWindowListener(wa);
60
+		frame.setVisible(true);
61
+		
62
+	}
63
+
64
+
65
+	private void addWidgets() {
66
+		
67
+		JTextPane textPane = new JTextPane();
68
+
69
+        this.doc = textPane.getStyledDocument();
70
+
71
+
72
+        SimpleAttributeSet right = new SimpleAttributeSet();
73
+        StyleConstants.setAlignment(right, StyleConstants.ALIGN_RIGHT);
74
+        StyleConstants.setForeground(right, Color.BLUE);
75
+		
76
+		GridBagLayout gridBagLayout = new GridBagLayout();
77
+		gridBagLayout.columnWidths = new int[]{394, 0};
78
+		gridBagLayout.rowHeights = new int[]{16, 220, 74, 0};
79
+		gridBagLayout.columnWeights = new double[]{0.0, Double.MIN_VALUE};
80
+		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, Double.MIN_VALUE};
81
+		this.frame.getContentPane().setLayout(gridBagLayout);
82
+		this.panel = new JPanel();
83
+		
84
+		//zone pour rentrer les message à envoyer
85
+		JTextArea text = new JTextArea(10, 3);
86
+		text.setForeground(Color.BLACK);
87
+		text.setFont(new Font("Century Schoolbook", Font.PLAIN, 11));
88
+		text.setText("Enter your message");
89
+		text.setLineWrap(true);
90
+		
91
+		JScrollPane sp = new JScrollPane(text);
92
+		
93
+		//zone affichage texte
94
+		//this.ta = new JTextArea(20, 20);
95
+		//this.ta.setLineWrap(true);
96
+		//this.ta.setText("");
97
+		JScrollPane sp2 = new JScrollPane(textPane);
98
+		//this.ta.setEditable(false);
99
+		GridBagConstraints gbc = new GridBagConstraints();
100
+		gbc.fill = GridBagConstraints.BOTH;
101
+		gbc.insets = new Insets(0, 0, 5, 0);
102
+		gbc.gridx = 0;
103
+		gbc.gridy = 1;
104
+		frame.getContentPane().add(sp2, gbc);
105
+		
106
+		//bouton envoyer
107
+		JButton envoyer = new JButton("Send");
108
+		frame.getRootPane().setDefaultButton(envoyer);
109
+		envoyer.addActionListener(new ActionListener() {
110
+			public void actionPerformed(ActionEvent e) {
111
+				String message = text.getText();
112
+				System.out.println("Message :"+ message);
113
+				System.out.println(dest);
114
+				int idUser = agent.getDb().getIdFromPseudo(user.getPseudo());
115
+				System.out.println("IDuser = "+idUser);
116
+				int idDest = agent.getDb().getIdFromPseudo(dest.getPseudo());
117
+				System.out.println("IDdest = "+idDest);
118
+				try {
119
+					TCPChat tcpChat = null;
120
+					for(TCPChat tcp : agent.getListeTCPChat()) {
121
+						if (tcp.getDestID() == idDest) {
122
+							tcpChat = tcp;
123
+						}
124
+					}
125
+					//text.append(message);
126
+					text.setText("");
127
+					//ta.append(agent.getUser().getPseudo()+ " : "+message+"\n");
128
+					//ta.setText("");
129
+					tcpChat.sendMsg(message);
130
+					doc.insertString(doc.getLength(), "\n"+message, right );
131
+		            doc.setParagraphAttributes(doc.getLength(), 1, right, false);
132
+					if(agent.getDb().tableChatExists(idUser, idDest)) {
133
+						agent.getDb().addMessage(idUser, idDest, message);
134
+					}else {
135
+						agent.getDb().newChatTable(idUser, idDest);
136
+						agent.getDb().addMessage(idUser, idDest, message);
137
+					}
138
+				} catch (IOException | BadLocationException e1) {
139
+					System.out.println("Envoi du message impossible");
140
+					e1.printStackTrace();
141
+				}
142
+				
143
+			}});
144
+		
145
+		panel.add(BorderLayout.CENTER, sp);
146
+		panel.add(BorderLayout.SOUTH,envoyer);
147
+		panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
148
+		
149
+		GridBagConstraints gbc_panel = new GridBagConstraints();
150
+		gbc_panel.anchor = GridBagConstraints.NORTH;
151
+		gbc_panel.fill = GridBagConstraints.HORIZONTAL;
152
+		gbc_panel.gridx = 0;
153
+		gbc_panel.gridy = 2;
154
+		frame.getContentPane().add(panel, gbc_panel);
155
+	}
156
+
157
+
158
+	public static JTextArea getTa() {
159
+		return ta;
160
+	}
161
+
162
+
163
+	public void setTa(JTextArea ta) {
164
+		this.ta = ta;
165
+	}
166
+
167
+
168
+	public static StyledDocument getDoc() {
169
+		return doc;
170
+	}
171
+
172
+
173
+	public static void setDoc(StyledDocument doc) {
174
+		FenetreChat.doc = doc;
175
+	}
176
+
177
+
178
+	/*@Override
179
+	public void propertyChange(PropertyChangeEvent evt) {
180
+		switch (evt.getPropertyName()) {
181
+		case "Msg TCP Recu" : 
182
+			System.out.println("dans property change");
183
+			TCPChat tcpChat = agent.getTCPChat();
184
+			MessageChat msgRecu = tcpChat.getMessageRecu();
185
+			ta.append(dest.getPseudo()+ " : "+msgRecu.getMessage()+"\n");
186
+			break;
187
+		}
188
+	}*/
189
+	
190
+	
191
+	
192
+}
193
+		

+ 234
- 0
chat/src/gui/FenetreMenu.java View File

@@ -0,0 +1,234 @@
1
+package gui;
2
+
3
+import java.awt.BorderLayout;
4
+import java.awt.Color;
5
+import java.awt.Dimension;
6
+import java.awt.Font;
7
+import java.awt.GridLayout;
8
+import java.awt.SystemColor;
9
+import java.awt.event.ActionEvent;
10
+import java.awt.event.ActionListener;
11
+import java.awt.event.WindowAdapter;
12
+import java.awt.event.WindowEvent;
13
+import java.beans.PropertyChangeListener;
14
+import java.io.IOException;
15
+import java.util.Vector;
16
+
17
+import javax.swing.ImageIcon;
18
+import javax.swing.JButton;
19
+import javax.swing.JComboBox;
20
+import javax.swing.JFrame;
21
+import javax.swing.JLabel;
22
+import javax.swing.JMenu;
23
+import javax.swing.JMenuBar;
24
+import javax.swing.JMenuItem;
25
+import javax.swing.JOptionPane;
26
+import javax.swing.JPanel;
27
+import javax.swing.JScrollPane;
28
+import javax.swing.JTextArea;
29
+import javax.swing.JTextField;
30
+import javax.swing.SwingConstants;
31
+
32
+import controller.Agent;
33
+import model.Contact;
34
+
35
+public class FenetreMenu {
36
+	JFrame frame;
37
+    JPanel panel;
38
+    JMenuBar menu;
39
+    Agent agent;
40
+    JLabel jlabel;
41
+    JLabel texte;
42
+    JLabel liste;
43
+    WindowAdapter wa ;
44
+    
45
+    public FenetreMenu(Agent agent) {
46
+    	this.agent = agent;
47
+    	frame = new JFrame("Fenêtre menu");
48
+		frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
49
+		
50
+		 frame.setSize(new Dimension(100, 100));
51
+		 
52
+		 wa = new WindowAdapter(){
53
+	            public void windowClosing(WindowEvent e){
54
+	                  int reponse = fenetreFermeture();
55
+	                  if (reponse==0){
56
+	                	  	 try {
57
+	                	  		 // on deconnecte l'app avant de quitter 
58
+	                	  		 // Tout les utilisateurs sont donc prevenus du depart
59
+								agent.deconnexion();
60
+							} catch (IOException e1) {
61
+								e1.printStackTrace();
62
+							}
63
+	                          frame.dispose();
64
+	                  }
65
+	            }};
66
+	            
67
+	            
68
+	        frame.addWindowListener(wa); 
69
+	        menu = new JMenuBar();
70
+	        //Creation d'un JPanel
71
+	    	panel = new JPanel(new GridLayout(3,1));
72
+	    	panel.setForeground(SystemColor.menuText);
73
+	    	// Ajouter tout les elements a la fenetre
74
+	        this.addWidgets();
75
+	        // ajouter le panel a la fenetre
76
+	        frame.getContentPane().add(panel, BorderLayout.CENTER);
77
+	        // Afficher la fenetre
78
+	        frame.pack();
79
+	        frame.setVisible(true);
80
+		}
81
+    
82
+    static int fenetreFermeture() {
83
+    	return JOptionPane.showConfirmDialog(
84
+ 		       null,
85
+ 		       "Are you sure you wanna quit?",
86
+ 		       "Quitter",
87
+ 		       JOptionPane.YES_NO_OPTION);
88
+    }
89
+    
90
+    private void addWidgets() {
91
+    	jlabel = new JLabel(new ImageIcon("/Users/momof/eclipse-workspace/chat/src/images/panda.png"), JLabel.CENTER);
92
+    	texte = new JLabel("Bienvenue sur ce magnifique chat " + agent.getUser().getPseudo());
93
+    	texte.setFont(new Font("Century Schoolbook", Font.PLAIN, 40));
94
+	 	texte.setHorizontalAlignment(SwingConstants.CENTER);
95
+	 	liste = new JLabel("Active users", JLabel.LEFT);
96
+	 	
97
+	 	JMenu contenu = new JMenu("Actions");
98
+	 	contenu.setForeground(Color.BLUE);
99
+	 	contenu.setHorizontalAlignment(SwingConstants.CENTER);
100
+	 	
101
+	 	JButton refresh = new JButton("Refresh");
102
+	 	refresh.setForeground(Color.GRAY);
103
+	 	refresh.setHorizontalAlignment(SwingConstants.CENTER);
104
+    
105
+	 	//JMenuItem usersActifs = new JMenuItem("Active users");
106
+	 	JMenuItem modifierPseudo = new JMenuItem("Modify your pseudo");
107
+	    JMenuItem deco = new JMenuItem("Deconnection");
108
+	    
109
+	    //contenu.add(usersActifs);  
110
+	    contenu.add(modifierPseudo);
111
+	    contenu.add(deco);
112
+	    
113
+	    
114
+	    menu.add(contenu);
115
+	    menu.add(refresh);
116
+	    frame.setJMenuBar(menu);
117
+	    
118
+	    /*liste déroulante des utilsateurs actifs, 
119
+	     * cliquer dessus pour créer une fenêtre de chat*/
120
+	    JTextArea ta = new JTextArea(20,20);
121
+   		ta.insert("Active users\n",0);
122
+		JScrollPane scrollPane = new JScrollPane(ta); 
123
+		
124
+	    String users = agent.getList().actifUsers();
125
+	    Vector<String> v = new Vector<String>();
126
+		for(String pseudo : users.split("\n")) {
127
+   			v.add(pseudo);
128
+   		 }
129
+		
130
+		JComboBox cb = new JComboBox(v);
131
+  	    cb.addActionListener(new ActionListener() {
132
+  	    	public void actionPerformed(ActionEvent event) {
133
+                Object selected = cb.getSelectedItem();
134
+                int idDest = agent.getDb().getIdFromPseudo(selected.toString());
135
+                try {
136
+					agent.createChat(idDest, selected.toString());
137
+				} catch (IOException e) {
138
+					System.out.println("Creation tcp chat impossible");
139
+					e.printStackTrace();
140
+				}
141
+                new FenetreChat(agent, selected.toString());
142
+
143
+            }
144
+  	    });
145
+  	    
146
+  	    
147
+  	  /*actions pour refresh*/
148
+	  	 refresh.addActionListener(new ActionListener() {
149
+	  	    	public void actionPerformed(ActionEvent e) {
150
+	  	    		String users = agent.getList().actifUsers();
151
+	  	    		for(String pseudo : users.split("\n")) {
152
+	  	    			if (!v.contains(pseudo)) {
153
+	  	    				v.add(pseudo);
154
+	  	    			}
155
+	  	     		 }
156
+	  	    		JComboBox cb = new JComboBox(v);
157
+	  	    	    cb.addActionListener(new ActionListener() {
158
+	  	    	    	public void actionPerformed(ActionEvent event) {
159
+	  	                  Object selected = cb.getSelectedItem();
160
+	  	                  int idDest = agent.getDb().getIdFromPseudo(selected.toString());
161
+	  	                  try {
162
+	  	  					agent.createChat(idDest, selected.toString());
163
+	  	  				} catch (IOException e) {
164
+	  	  					System.out.println("Creation tcp chat impossible");
165
+	  	  					e.printStackTrace();
166
+	  	  				}
167
+	  	                  new FenetreChat(agent, selected.toString());	  	                  	
168
+	  	              }
169
+	  	    	    });
170
+	  	    			}});
171
+	  	 
172
+	  	 frame.getContentPane().add(refresh, BorderLayout.NORTH);
173
+	  	  panel.add(BorderLayout.CENTER, jlabel);
174
+	  	  panel.add(BorderLayout.SOUTH , texte );
175
+	  	  panel.add(BorderLayout.WEST, cb);
176
+	  	  
177
+	  	  
178
+	  	
179
+  	    
180
+  	    /*actions pour modifier le pseudo*/
181
+  	    modifierPseudo.addActionListener(new ActionListener() {
182
+  	    	public void actionPerformed(ActionEvent e) {
183
+  	    		JLabel Text = new JLabel("Enter your new pseudo", SwingConstants.CENTER);
184
+  	    		JTextField field = new JTextField(2);
185
+  	    		JButton cancel = new JButton("Cancel");
186
+  	    		cancel.addActionListener(new ActionListener() {
187
+  	    			public void actionPerformed(ActionEvent e) {
188
+  	    				frame.dispose();
189
+  		        		new FenetreMenu(agent);
190
+  	    			}});
191
+  	    		JButton ok = new JButton("Continue");
192
+  	    		frame.getRootPane().setDefaultButton(ok);
193
+  	    		ok.addActionListener(new ActionListener() {
194
+  	    			public void actionPerformed(ActionEvent e) {
195
+  	    				String newPseudo = field.getText();
196
+  	    				if(agent.changerPseudo(newPseudo)) {
197
+  	    					JOptionPane.showMessageDialog(frame, "Your new pseudo is "+newPseudo); 
198
+							frame.dispose();
199
+							new FenetreMenu(agent);
200
+  	    				}else {
201
+  	    					JOptionPane.showMessageDialog(frame, "Pseudo already, use choose another one");
202
+  	    				};
203
+  	    			}
204
+  	    		});
205
+  	    		JPanel panel1 = new JPanel(new GridLayout(4,1));
206
+			    panel1.add(cancel);
207
+		       	panel1.add(Text);
208
+		       	panel1.add(field);
209
+		       	panel1.add(ok);   
210
+		       	frame.getContentPane().removeAll();
211
+	            frame.getContentPane().add(panel1,BorderLayout.CENTER);
212
+	            frame.setVisible(true);
213
+  	    	}
214
+  	    });
215
+  	    
216
+	    deco.addActionListener(new ActionListener() {
217
+	    	public void actionPerformed(ActionEvent e) {
218
+	    		try {
219
+					agent.deconnexion();
220
+					frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
221
+				} catch (IOException e1) {
222
+					System.out.println("Deconnexion impossible");
223
+					e1.printStackTrace();
224
+				}
225
+	    	}
226
+	    });
227
+	    //panel.add(BorderLayout.NORTH, menu);
228
+	 	
229
+	 	
230
+	 	
231
+    }
232
+}
233
+    
234
+  	 

+ 1
- 0
chat/src/gui/package-info.java View File

@@ -0,0 +1 @@
1
+package gui;

BIN
chat/src/gui/panda.png View File


BIN
chat/src/images/panda.png View File


+ 66
- 0
chat/src/model/Contact.java View File

@@ -0,0 +1,66 @@
1
+package model;
2
+
3
+import java.io.*;
4
+import java.net.InetAddress;
5
+
6
+public class Contact {
7
+	
8
+	private String pseudo;
9
+	private String status;
10
+	private int port;
11
+	private int id;
12
+	
13
+	private InetAddress address;
14
+	
15
+	
16
+	public Contact(String pseudo, InetAddress address, int port) {
17
+		this.pseudo = pseudo;
18
+		this.address = address;
19
+		this.status = "";
20
+		this.port = port;
21
+	}
22
+	
23
+	public String getPseudo() {
24
+		return this.pseudo;
25
+	}
26
+	
27
+	public void setPseudo(String pseudo) {
28
+		this.pseudo = pseudo;
29
+	}
30
+	
31
+	public InetAddress getAddress() {
32
+		return this.address;
33
+	}
34
+	
35
+	public void setAddress(InetAddress address) {
36
+		this.address = address;
37
+	}
38
+	
39
+	public String getStatut() {
40
+		return this.status;
41
+	}
42
+	
43
+	public void setStatut(String status) {
44
+		this.status = status;
45
+	}
46
+
47
+	public int getPort() {
48
+		return port;
49
+	}
50
+
51
+	public void setPort(int port) {
52
+		this.port = port;
53
+	}
54
+
55
+	public int getId() {
56
+		return id;
57
+	}
58
+
59
+	public void setId(int id) {
60
+		this.id = id;
61
+	}
62
+	
63
+	public String toString() {
64
+		return "Pseudo : " + pseudo + " status : " + status + " \n";
65
+	}
66
+}

+ 96
- 0
chat/src/model/ListeContacts.java View File

@@ -0,0 +1,96 @@
1
+package model;
2
+
3
+import network.*;
4
+import java.net.InetAddress;
5
+import java.util.ArrayList;
6
+
7
+public class ListeContacts {
8
+	private static Contact user;
9
+	private ArrayList<Contact> listeContact;
10
+	
11
+	
12
+	//la liste doit être un singleton
13
+	
14
+	private static final ListeContacts instance = new ListeContacts();
15
+	
16
+	public ListeContacts() {
17
+		this.listeContact = new ArrayList<Contact>();
18
+	}
19
+	
20
+	public static final ListeContacts getInstance() {
21
+		return instance;
22
+	}
23
+	
24
+	public static void createUser(String pseudo, int port) {
25
+		user = new Contact(pseudo, Tools.getLocalIp(), port);
26
+	}
27
+	
28
+	public boolean contactExist(Contact contact) {
29
+		for(Contact c : listeContact) {
30
+			if(c.getPseudo().equals(contact.getPseudo())){
31
+				return true;
32
+			}
33
+		}
34
+		return false;
35
+	}
36
+	
37
+	public boolean pseudoExist(String pseudo) {
38
+		for(Contact c : listeContact) {
39
+			if(c.getPseudo().equals(pseudo)){
40
+				return true;
41
+			}
42
+		}
43
+		return false;
44
+	}
45
+	
46
+	public void addContact(Contact contact) {
47
+		if(!contactExist(contact)){
48
+			System.out.println("Pseudo : "+contact.getPseudo()+" ajouté\n");
49
+			listeContact.add(contact);
50
+		}
51
+	}
52
+	
53
+	public void deleteContact(Contact contact) {
54
+		if(contactExist(contact)){
55
+			System.out.println("Pseudo : "+contact.getPseudo()+"supprimé\n");
56
+			listeContact.remove(contact);
57
+		}
58
+	}
59
+	
60
+	public int length() {
61
+		int n = 0;
62
+		while(!listeContact.isEmpty()) {
63
+			n++;
64
+		}
65
+		return n;
66
+	}
67
+	
68
+	public Contact findContact(String pseudo) {
69
+		Contact contact = null;
70
+		for(Contact c : listeContact) {
71
+			if(c.getPseudo().equals(pseudo)){
72
+				contact = c;
73
+				break;
74
+			}
75
+		}
76
+		return contact;
77
+	}
78
+	
79
+	
80
+	public String actifUsers() {
81
+		String users = "";
82
+		for(Contact c : this.listeContact) {
83
+			users += (c.getPseudo()+"\n");
84
+		}
85
+		return users;
86
+	}
87
+
88
+	public ArrayList<Contact> getListe() {
89
+		return listeContact;
90
+	}
91
+
92
+	public void setListeContact(ArrayList<Contact> listeContact) {
93
+		this.listeContact = listeContact;
94
+	}
95
+
96
+}

+ 75
- 0
chat/src/model/Message.java View File

@@ -0,0 +1,75 @@
1
+package model;
2
+
3
+import java.io.Serializable;
4
+import java.net.*;
5
+
6
+public class Message implements Serializable{
7
+
8
+	protected InetAddress addressSrc;
9
+	protected InetAddress addressDest;
10
+	protected int portSrc;
11
+	protected int portDest;
12
+	protected int typeMessage;
13
+	
14
+	
15
+	public Message(InetAddress addressSrc, InetAddress addressDest, int portSrc, int portDest, int typeMessage) {
16
+		this.addressSrc = addressSrc;
17
+		this.addressDest = addressDest;
18
+		this.portSrc = portSrc;
19
+		this.portDest = portDest;
20
+		this.typeMessage = typeMessage;
21
+	}
22
+
23
+
24
+	public InetAddress getAddressSrc() {
25
+		return addressSrc;
26
+	}
27
+
28
+
29
+	public void setAddressSrc(InetAddress addressSrc) {
30
+		this.addressSrc = addressSrc;
31
+	}
32
+
33
+
34
+	public InetAddress getAddressDest() {
35
+		return addressDest;
36
+	}
37
+
38
+
39
+	public void setAddressDest(InetAddress addressDest) {
40
+		this.addressDest = addressDest;
41
+	}
42
+
43
+
44
+	public int getPortSrc() {
45
+		return portSrc;
46
+	}
47
+
48
+
49
+	public void setPortSrc(int portSrc) {
50
+		this.portSrc = portSrc;
51
+	}
52
+
53
+
54
+	public int getPortDest() {
55
+		return portDest;
56
+	}
57
+
58
+
59
+	public void setPortDest(int portDest) {
60
+		this.portDest = portDest;
61
+	}
62
+
63
+
64
+	public int getTypeMessage() {
65
+		return typeMessage;
66
+	}
67
+
68
+
69
+	public void setTypeMessage(int typeMessage) {
70
+		this.typeMessage = typeMessage;
71
+	}
72
+	
73
+	
74
+	
75
+}

+ 22
- 0
chat/src/model/MessageChat.java View File

@@ -0,0 +1,22 @@
1
+package model;
2
+
3
+import java.net.InetAddress;
4
+
5
+public class MessageChat extends Message{
6
+
7
+	private String message;
8
+	
9
+	public MessageChat(InetAddress addressSrc, InetAddress addressDest, int portSrc, int portDest, int typeMessage, String message) {
10
+		super(addressSrc, addressDest, portSrc, portDest, typeMessage);
11
+		this.message = message;
12
+	}
13
+
14
+	public String getMessage() {
15
+		return message;
16
+	}
17
+
18
+	public void setMessage(String message) {
19
+		this.message = message;
20
+	}
21
+	
22
+}

+ 26
- 0
chat/src/model/MessageDeconnexion.java View File

@@ -0,0 +1,26 @@
1
+package model;
2
+
3
+import java.net.InetAddress;
4
+
5
+public class MessageDeconnexion extends Message{
6
+	private String msg;
7
+	private String pseudo;
8
+	
9
+	public MessageDeconnexion(InetAddress addressSrc, InetAddress addressDest, int portSrc, int portDest, int typeMessage, String pseudo) {
10
+		super(addressSrc, addressDest, portSrc, portDest, typeMessage);
11
+		this.pseudo = pseudo;
12
+		this.msg = "Deconnexion";
13
+	}
14
+
15
+	public String getPseudo() {
16
+		return pseudo;
17
+	}
18
+
19
+	public void setPseudo(String pseudo) {
20
+		this.pseudo = pseudo;
21
+	}
22
+
23
+	public String getMsg() {
24
+		return msg;
25
+	}
26
+}

+ 23
- 0
chat/src/model/MessagePseudo.java View File

@@ -0,0 +1,23 @@
1
+package model;
2
+
3
+import java.net.InetAddress;
4
+
5
+public class MessagePseudo extends Message{
6
+	
7
+	private String pseudo;
8
+	
9
+	public MessagePseudo(InetAddress addressSrc, InetAddress addressDest, int portSrc, int portDest, int typeMessage,String pseudo) {
10
+		super(addressSrc, addressDest, portSrc, portDest, typeMessage);
11
+		this.pseudo = pseudo;
12
+	}
13
+
14
+	public String getPseudo() {
15
+		return pseudo;
16
+	}
17
+
18
+	public void setPseudo(String pseudo) {
19
+		this.pseudo = pseudo;
20
+	}
21
+	
22
+	
23
+}

+ 5
- 0
chat/src/module-info.java View File

@@ -0,0 +1,5 @@
1
+module chat {
2
+	requires java.desktop;
3
+	requires java.sql;
4
+	requires mysql.connector.java;
5
+}

+ 19
- 0
chat/src/network/Manager.java View File

@@ -0,0 +1,19 @@
1
+package network;
2
+
3
+import java.net.*;
4
+import java.util.*;
5
+
6
+
7
+
8
+public class Manager {
9
+	private static int port;
10
+	
11
+	
12
+	private DatagramSocket broadcast;
13
+	
14
+	private HashMap<String, Integer> portMap;
15
+	
16
+	//network
17
+	
18
+	
19
+}

+ 112
- 0
chat/src/network/TCPChat.java View File

@@ -0,0 +1,112 @@
1
+package network;
2
+
3
+import java.beans.PropertyChangeListener;
4
+import java.beans.PropertyChangeSupport;
5
+import java.io.BufferedReader;
6
+import java.io.IOException;
7
+import java.io.InputStreamReader;
8
+import java.io.PrintWriter;
9
+import java.net.Socket;
10
+import java.util.ArrayList;
11
+
12
+import controller.Agent;
13
+import model.Contact;
14
+import model.MessageChat;
15
+
16
+public class TCPChat extends Thread{
17
+	
18
+	private ArrayList<MessageChat> listeMessagesRecu;
19
+	private Socket socket; 
20
+	private int destID;
21
+	private PropertyChangeSupport support;
22
+	
23
+	
24
+	//connexion faite par un autre user
25
+	public TCPChat(Socket socket) {
26
+		this.socket = socket;
27
+		listeMessagesRecu = new ArrayList<MessageChat>();
28
+		this.support = new PropertyChangeSupport(this);
29
+		this.start();
30
+	}
31
+	
32
+	//connexion faite par le user
33
+	public TCPChat(Contact destUser, int destID) throws IOException {
34
+		this.socket = new Socket(destUser.getAddress(), destUser.getPort());
35
+		listeMessagesRecu = new ArrayList<MessageChat>();
36
+		this.destID = destID;
37
+		this.support = new PropertyChangeSupport(this);
38
+		this.start();
39
+	}
40
+	
41
+	
42
+	
43
+	public void addPropertyChangeListener(PropertyChangeListener pcl) {
44
+		this.support.addPropertyChangeListener("Msg TCP Recu", pcl);
45
+	}
46
+	
47
+	
48
+	@Override
49
+	public void run() {
50
+		BufferedReader in;
51
+		try {
52
+			in = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
53
+			
54
+			while(true) {			
55
+				try {
56
+					String input = in.readLine();
57
+					MessageChat msgRecu = new MessageChat(this.socket.getInetAddress(), this.socket.getLocalAddress(), this.socket.getPort(), this.socket.getLocalPort(), 0, input);
58
+					listeMessagesRecu.add(msgRecu);
59
+					System.out.println("fire msg tcp recu");
60
+					this.support.firePropertyChange("Msg TCP Recu", true, false);
61
+					//if (!msgRecu.getMessage().equals(null)) {
62
+						System.out.println("Message reçu : "+msgRecu.getMessage() );
63
+					//}
64
+				} catch (IOException e) {
65
+					System.out.println("Création et read du in impossible");
66
+					e.printStackTrace();
67
+				}
68
+				
69
+			}
70
+		} catch (IOException e1) {
71
+			System.out.println("pb création in");
72
+			e1.printStackTrace();
73
+		}
74
+	}
75
+	
76
+	public void sendMsg(String message) throws IOException {
77
+		PrintWriter out = new PrintWriter(this.socket.getOutputStream(),true);
78
+		out.println(message);
79
+		System.out.println("Message envoyé : "+message);
80
+	}
81
+	
82
+	
83
+	public MessageChat getMessageRecu() {
84
+		return listeMessagesRecu.remove(0);
85
+	}
86
+
87
+	public ArrayList<MessageChat> getListeMessagesRecu() {
88
+		return listeMessagesRecu;
89
+	}
90
+
91
+	public void setListeMessagesRecu(ArrayList<MessageChat> listeMessagesRecu) {
92
+		this.listeMessagesRecu = listeMessagesRecu;
93
+	}
94
+
95
+	public Socket getSocket() {
96
+		return socket;
97
+	}
98
+
99
+	public void setSocket(Socket socket) {
100
+		this.socket = socket;
101
+	}
102
+
103
+	public int getDestID() {
104
+		return destID;
105
+	}
106
+
107
+	public void setDestID(int destID) {
108
+		this.destID = destID;
109
+	}
110
+	
111
+	
112
+}

+ 61
- 0
chat/src/network/TCPServer.java View File

@@ -0,0 +1,61 @@
1
+package network;
2
+
3
+import java.beans.PropertyChangeEvent;
4
+import java.beans.PropertyChangeListener;
5
+import java.beans.PropertyChangeSupport;
6
+import java.io.IOException;
7
+import java.net.InetAddress;
8
+import java.net.ServerSocket;
9
+import java.net.Socket;
10
+import java.util.ArrayList;
11
+
12
+public class TCPServer extends Thread{
13
+	
14
+	private ServerSocket socket;
15
+	private InetAddress address;
16
+	private int port;
17
+	private ArrayList<TCPChat> listeConnexions;
18
+	private PropertyChangeSupport support;
19
+	
20
+	public TCPServer(int port, InetAddress address) throws IOException {
21
+		this.socket = new ServerSocket(port, 1000, address);
22
+		this.address = address;
23
+		this.port = port;
24
+		this.listeConnexions = new ArrayList<TCPChat>();
25
+		this.support = new PropertyChangeSupport(this);
26
+		this.start();
27
+	}
28
+	
29
+	
30
+	public void addPropertyChangeListener(PropertyChangeListener pcl){
31
+        this.support.addPropertyChangeListener("session chat demarree",pcl);
32
+    }
33
+	
34
+	@Override
35
+	public void run() {
36
+		Socket link;
37
+		try {
38
+			link = socket.accept();
39
+			TCPChat tcpChat = new TCPChat(link);	
40
+			this.listeConnexions.add(tcpChat);
41
+			this.support.firePropertyChange("session chat demarree", true, false);
42
+			//link.close();// A APPELER QUELQUE PART D'AUTRE
43
+		} catch (IOException e) {
44
+			System.out.println("Socket non créé ou non fermé");
45
+			e.printStackTrace();
46
+		}
47
+		
48
+	}
49
+
50
+	public InetAddress getAddress() {
51
+		return address;
52
+	}
53
+
54
+	public void setAddress(InetAddress address) {
55
+		this.address = address;
56
+	}
57
+	
58
+	public TCPChat getConnexionTCPChat() {
59
+		return this.listeConnexions.remove(0);
60
+	}
61
+}

+ 69
- 0
chat/src/network/Tools.java View File

@@ -0,0 +1,69 @@
1
+package network;
2
+import java.net.*;
3
+import java.util.Enumeration;
4
+
5
+public class Tools {
6
+	
7
+	public static InetAddress getLocalIp(){
8
+		try{
9
+			InetAddress address = InetAddress.getLocalHost();
10
+			return address;
11
+		}
12
+		catch (UnknownHostException e){
13
+			System.out.println ("Could not find local address!");
14
+		}
15
+		return null;
16
+	}
17
+	
18
+	
19
+	//Renvoie un tableau d'InetAdress le premier élement est l'adresse locale de la machine le deuxieme l'adresse de broadcast
20
+	public static InetAddress[] getAdress() {
21
+        Enumeration<NetworkInterface> interfaces = null;
22
+        InetAddress localAdress = null;
23
+        InetAddress broadcastAddress = null;
24
+        InetAddress[] tab = {null,null};
25
+        try {
26
+            interfaces = NetworkInterface.getNetworkInterfaces();
27
+            while (interfaces.hasMoreElements())
28
+            {
29
+                NetworkInterface networkInterface = interfaces.nextElement();
30
+                if (networkInterface.getName().equals("wlan3")){
31
+                    localAdress = networkInterface.getInterfaceAddresses().get(0).getAddress();
32
+                    tab[0] = localAdress;
33
+                    broadcastAddress = networkInterface.getInterfaceAddresses().get(0).getBroadcast();
34
+                    tab[1] = broadcastAddress;
35
+                    System.out.println("Found adress : " + localAdress);
36
+                    break;
37
+                }
38
+            }
39
+        } catch (SocketException e) {
40
+            e.printStackTrace();
41
+        }
42
+        return tab;
43
+    }
44
+	
45
+	public static void printInterfaces() {
46
+        Enumeration<NetworkInterface> interfaces = null;
47
+        try {
48
+            interfaces = NetworkInterface.getNetworkInterfaces();
49
+            while (interfaces.hasMoreElements())
50
+            {
51
+                NetworkInterface networkInterface = interfaces.nextElement();
52
+                try {
53
+                InetAddress adr = networkInterface.getInterfaceAddresses().get(0).getAddress();
54
+                if(adr!=null) {
55
+                    System.out.println(networkInterface.getName() + adr ); 
56
+                }
57
+                } catch (Exception e) {
58
+                    e.printStackTrace();
59
+                }
60
+
61
+            }
62
+        } catch (SocketException e) {
63
+            e.printStackTrace();
64
+        }
65
+    }
66
+}
67
+
68
+
69
+

+ 73
- 0
chat/src/network/UDPInput.java View File

@@ -0,0 +1,73 @@
1
+package network;
2
+
3
+import model.*;
4
+
5
+import java.beans.PropertyChangeListener;
6
+import java.beans.PropertyChangeSupport;
7
+import java.io.ByteArrayInputStream;
8
+import java.io.IOException;
9
+import java.io.ObjectInputStream;
10
+import java.net.*;
11
+import java.util.ArrayList;
12
+
13
+
14
+public class UDPInput extends Thread{
15
+	private DatagramSocket socket;
16
+	private ArrayList<MessagePseudo> messageReceived;
17
+	private PropertyChangeSupport support;
18
+	
19
+	public UDPInput(InetAddress localAddress, int port) throws SocketException {
20
+		this.socket = new DatagramSocket(port, localAddress);
21
+		this.socket.setBroadcast(true);
22
+		this.messageReceived = new ArrayList<MessagePseudo>();
23
+		this.support = new PropertyChangeSupport(this);
24
+	}
25
+	
26
+	public ArrayList<MessagePseudo> getListMessage (){
27
+		return this.messageReceived;
28
+	}
29
+	
30
+	public MessagePseudo getMessage() {
31
+		return this.messageReceived.remove(0);
32
+	}
33
+	
34
+	
35
+	public void addPropertyChangeListener(PropertyChangeListener pcl) {
36
+		this.support.addPropertyChangeListener("Msg UDP Recu", pcl);
37
+	}
38
+	
39
+	
40
+	@Override
41
+	public void run() {
42
+		byte[] buffer = new byte[5000];
43
+		while(true) {
44
+			DatagramPacket inPacket = new DatagramPacket(buffer, buffer.length);
45
+			try {
46
+				socket.receive(inPacket);
47
+			} catch (IOException e) {
48
+				System.out.println("Packet UDP non reçu\n");
49
+				e.printStackTrace();
50
+			}
51
+			/* Charset charset = StandardCharsets.US_ASCII;
52
+			 String pseudo = charset.decode(ByteBuffer.wrap(inPacket.getData())).toString();
53
+			 MessagePseudo msg = new MessagePseudo(null, null, 0, 0, 0, "");
54
+			 msg.setPseudo(pseudo);*/
55
+			ByteArrayInputStream byteInStream = new ByteArrayInputStream(inPacket.getData());
56
+                ObjectInputStream objectInStream;
57
+				try {
58
+					objectInStream = new ObjectInputStream(byteInStream);
59
+					MessagePseudo msg = (MessagePseudo) objectInStream.readObject();
60
+					messageReceived.add(msg);
61
+					System.out.println("fire property change");
62
+					this.support.firePropertyChange("Msg UDP Recu", this.messageReceived.size() -1, this.messageReceived.size());
63
+					 System.out.println("Message reçu, pseudo = "+msg.getPseudo()+" et num port = "+msg.getPortSrc()+"\n");
64
+				} catch (IOException e) {
65
+					System.out.println("Objet pas créé");
66
+					e.printStackTrace();
67
+				} catch (ClassNotFoundException e) {
68
+					System.out.println("Objet pas lu");
69
+					e.printStackTrace();
70
+				}	
71
+		}
72
+	}
73
+}

+ 50
- 0
chat/src/network/UDPOutput.java View File

@@ -0,0 +1,50 @@
1
+package network;
2
+import model.*;
3
+
4
+import java.io.ByteArrayOutputStream;
5
+import java.io.IOException;
6
+import java.io.ObjectOutputStream;
7
+import java.net.*;
8
+
9
+
10
+
11
+
12
+public class UDPOutput {
13
+	private Contact user;
14
+	private DatagramSocket socket;
15
+	private int port;
16
+	
17
+	public UDPOutput(Contact user, int port) throws SocketException {
18
+		this.user = user;
19
+		this.port = port;
20
+		this.socket = new DatagramSocket(port, user.getAddress());
21
+		this.socket.setBroadcast(true);
22
+	}
23
+	
24
+	
25
+	public void send(MessagePseudo msg, InetAddress addressDest, int portSrc) throws IOException {
26
+		byte[] buffer = "".getBytes();
27
+        ByteArrayOutputStream byteOutStream = new ByteArrayOutputStream();
28
+            ObjectOutputStream objectOutStream = new ObjectOutputStream(byteOutStream);
29
+            objectOutStream.writeObject(msg);
30
+            objectOutStream.close();
31
+            buffer = byteOutStream.toByteArray();
32
+            DatagramPacket packet = new DatagramPacket(buffer,buffer.length,msg.getAddressDest(), msg.getPortDest());
33
+            this.socket.send(packet);	
34
+		 System.out.println("Message envoyé, pseudo = "+msg.getPseudo()+" et num port destination = "+portSrc+"\n");	
35
+	}
36
+	
37
+	public void send(MessageDeconnexion msg, InetAddress addressDest, int portSrc) throws IOException {
38
+		byte[] buffer = "".getBytes();
39
+        ByteArrayOutputStream byteOutStream = new ByteArrayOutputStream();
40
+            ObjectOutputStream objectOutStream = new ObjectOutputStream(byteOutStream);
41
+            objectOutStream.writeObject(msg);
42
+            objectOutStream.close();
43
+            buffer = byteOutStream.toByteArray();
44
+            DatagramPacket packet = new DatagramPacket(buffer,buffer.length,msg.getAddressDest(),msg.getPortDest());
45
+            this.socket.send(packet);	
46
+		 System.out.println("Message envoyé, pseudo = "+msg.getMsg()+" et num port destination = "+portSrc+"\n");	
47
+	}
48
+}
49
+
50
+

Loading…
Cancel
Save