diff --git a/Application/Clavardage/src/controller/Controller.java b/Application/Clavardage/src/controller/Controller.java
index 560acba..1696bf1 100644
--- a/Application/Clavardage/src/controller/Controller.java
+++ b/Application/Clavardage/src/controller/Controller.java
@@ -9,28 +9,34 @@ import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JOptionPane;
+import model.Chat;
import model.LocalUser;
+import model.Message;
+import model.Msg_Text;
+import model.RemoteUser;
import view.Interface;
public class Controller {
/*** CONSTANTES ***/
- final static int portUDPlistening_remoteUsr2 = 20002; // TO REMOVE when we use broadcast
- final static int portUDPlistening_remoteUsr3 = 20003; // TO REMOVE when we use broadcast
+ final static int portUDPlistening_remoteUsr2 = 31002; // TO REMOVE when we use broadcast
+ final static int portUDPlistening_remoteUsr3 = 31003; // TO REMOVE when we use broadcast
/*** ATTRIBUTS ***/
- private LocalUser myUser;
- private Interface hisView;
+ protected LocalUser myUser;
+ protected Interface hisView;
private ListeningThreadUDP udp_connect_thread;
private ListeningThreadTCPConnection tcp_connect_thread;
- private ArrayList tcp_chats_threads = new ArrayList(); // listes des utilisateurs actifs
private Historique histoire;
+
/**
* Constructor of Controller
* @parametres
@@ -46,7 +52,7 @@ public class Controller {
* - notification aux autres utilisateurs actifs
*
*/
- private Controller(int portUDPsend,int portUDPlistening,int portTCP,Historique histoire) {
+ private Controller(int portUDPsend,int portUDPlistening,int portTCP) {
this.histoire=histoire;
InetAddress addIP = null;
try
@@ -64,10 +70,10 @@ public class Controller {
e.printStackTrace();
}
- this.udp_connect_thread = new ListeningThreadUDP("UDP Listening thread",this.myUser);
+ this.udp_connect_thread = new ListeningThreadUDP("UDP Listening thread",this);
this.udp_connect_thread.start();
- this.tcp_connect_thread = new ListeningThreadTCPConnection("TCP main Listening thread",this.myUser);
+ this.tcp_connect_thread = new ListeningThreadTCPConnection("TCP main Listening thread",this);
this.tcp_connect_thread.start();
notify_remote_users();
@@ -90,8 +96,7 @@ public class Controller {
* - notification aux autres utilisateurs actifs
*
*/
- private Controller(int portUDPsend,int portUDPlistening,int portTCP,String pseudo,Historique histoire) {
- this.histoire=histoire;
+ private Controller(int portUDPsend,int portUDPlistening,int portTCP,String pseudo) {
InetAddress addIP = null;
try
{
@@ -102,10 +107,10 @@ public class Controller {
}
this.myUser = new LocalUser(pseudo,addIP,portUDPsend,portUDPlistening,portTCP);
- this.udp_connect_thread = new ListeningThreadUDP("UDP Listening thread",this.myUser);
+ this.udp_connect_thread = new ListeningThreadUDP("UDP Listening thread",this);
this.udp_connect_thread.start();
- this.tcp_connect_thread = new ListeningThreadTCPConnection("TCP main Listening thread",this.myUser);
+ this.tcp_connect_thread = new ListeningThreadTCPConnection("TCP main Listening thread",this);
this.tcp_connect_thread.start();
notify_remote_users();
@@ -127,12 +132,10 @@ public class Controller {
public ListeningThreadTCPConnection getTcp_connect_thread() {
return tcp_connect_thread;
}
- public ArrayList getTcp_chats_threads() {
- return tcp_chats_threads;
- }
public Historique getHistory() {
return histoire;
}
+
/*** SETTERS ***/
public void setMyUser(LocalUser myUser) {
this.myUser = myUser;
@@ -146,9 +149,6 @@ public class Controller {
public void setTcp_connect_thread(ListeningThreadTCPConnection tcp_connect_thread) {
this.tcp_connect_thread = tcp_connect_thread;
}
- public void setTcp_chats_threads(ArrayList tcp_chats_threads) {
- this.tcp_chats_threads = tcp_chats_threads;
- }
public void setHistory(Historique histoire) {
this.histoire=histoire;
}
@@ -169,7 +169,7 @@ public class Controller {
//sc1.close();
JOptionPane.showMessageDialog(null ,"Your nickname : " + tmpPseudo + " is valid !");
- hisView.Pseudolabel.setText("Your current username is: " + tmpPseudo);
+ //hisView.Pseudolabel.setText("Your current username is: " + tmpPseudo);
return tmpPseudo;
}
@@ -263,10 +263,7 @@ public class Controller {
tabresponse = response.split(":");
// Affichage de la réponse
- System.out.println("Remote user n°"+nbReponse+
- "\nIP : "+tabresponse[0]+
- "\nn°Port : "+tabresponse[1]+
- "\npseudo : "+tabresponse[2]);
+ //System.out.println("Remote user n°"+nbReponse+"\nIP : "+tabresponse[0]+"\nn°Port : "+tabresponse[1]+"\npseudo : "+tabresponse[2]);
// Si reception on ajoute l'utilisateur à notre liste d'utilisateur distant
this.myUser.addRemoteUser(InetAddress.getByName(tabresponse[0].split("/")[1]),Integer.parseInt(tabresponse[1]),tabresponse[2]);
@@ -328,6 +325,41 @@ public class Controller {
dgramSocket.close();
}
+ public void openSession(Chat c) {
+ Socket link=null;
+ try {
+ System.out.println("("+this.myUser.getPseudo()+") Connecting to "+c.getRemoteUser().getPortTCP()+" of " + c.getRemoteUser().getPseudo());
+ link=new Socket(c.getRemoteUser().getAddIP(),c.getRemoteUser().getPortTCP());
+ }catch(IOException e) {
+ System.out.println("Error linking to TCP server of "+ c.getRemoteUser().getPortTCP());
+ }
+ c.setSocket(link);
+ JOptionPane.showMessageDialog(null ,"New chat with "+c.getRemoteUser().getPseudo());
+
+ // TODO Récupération de la conversation (historique)
+ }
+
+ public void closeSession(Chat c) {
+ c.closeSocket();
+ // TODO Serait mieux d'enlever le chat de la liste de myUser
+ }
+
+ public void sendMsg(Message msg,Chat c) {
+ PrintWriter out=null;
+ try {
+ out = new PrintWriter(c.getUserSocket().getOutputStream(),true);
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+
+ DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
+ Date date = new Date();
+ out.println(dateFormat.format(date));
+
+ out.println(msg.getMessage());
+ }
+
+ // Plus utilisé
public void TCPmessage(int index) throws IOException {
Socket link=null;
String s1;
@@ -390,36 +422,28 @@ public class Controller {
* Laisse l'utilisateur choisir parmis la liste d'utilisateurs actifs celui avec lequel il souhaite échanger via un chat
*
*/
- public void getOneActiveUser() throws IOException {
+ public void selectActiveUser() throws IOException {
this.printRemoteUserList();
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:"));
if (index >= 0 && index
*
*/
- public ListeningThreadTCPChat(String s,LocalUser myUser,Socket socket) {
- super(s,myUser);
+ public ListeningThreadTCPChat(String s,Controller controller,Socket socket) {
+ super(s,controller);
this.socket=socket;
}
@@ -36,35 +36,28 @@ public class ListeningThreadTCPChat extends ListeningThread{
*
*/
public void run() {
- Socket link = this.socket;
- try {
- BufferedReader in =new BufferedReader(new InputStreamReader(link.getInputStream()));
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- PrintWriter out=null;
- try {
- out = new PrintWriter(link.getOutputStream(),true);
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
- Date date = new Date();
- System.out.println(myUser.getPseudo()+" envoie un message");
- out.println(dateFormat.format(date));
- out.println("end");
- String input;
- /*while (!(input=in.readLine()).equals("end")) {
- System.out.print("server_recoit:"+input);
- }*/
- try {
- link.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
+ BufferedReader in = null;
+ String msg = null;
+ try {
+ System.out.println("("+this.controller.myUser.getPseudo()+") WAIT FOR NEW MESSAGE ");
+ in =new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ String input;
+ try {
+ while (!(input=in.readLine()).equals("end")) {
+ System.out.println("("+this.controller.myUser.getPseudo()+") recoit : "+input);
+ }
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+
+
+
+
}
/* close
@@ -80,7 +73,7 @@ public class ListeningThreadTCPChat extends ListeningThread{
} catch (IOException e1) {
e1.printStackTrace();
}
- System.out.println("End of listing thread UDP ("+this.myUser.getPseudo()+")");
+ System.out.println("End of listing thread UDP ("+this.controller.myUser.getPseudo()+")");
try {
this.interrupt();
}catch (Exception e){
diff --git a/Application/Clavardage/src/controller/ListeningThreadTCPConnection.java b/Application/Clavardage/src/controller/ListeningThreadTCPConnection.java
index 9b5a96e..fa31e9c 100644
--- a/Application/Clavardage/src/controller/ListeningThreadTCPConnection.java
+++ b/Application/Clavardage/src/controller/ListeningThreadTCPConnection.java
@@ -18,14 +18,14 @@ public class ListeningThreadTCPConnection extends ListeningThread{
*
*
*/
- public ListeningThreadTCPConnection(String s,LocalUser myUser) {
- super(s,myUser);
+ public ListeningThreadTCPConnection(String s,Controller controller) {
+ super(s,controller);
}
public void accept(ServerSocket servSocket) throws IOException {
Socket socket_tcp= servSocket.accept();
- ListeningThreadTCPChat threadtcpchat = new ListeningThreadTCPChat("Chat_with_"+myUser.getPseudo(),myUser,socket_tcp);
+ ListeningThreadTCPChat threadtcpchat = new ListeningThreadTCPChat("Chat_with_"+controller.myUser.getPseudo(),controller,socket_tcp);
threadtcpchat.start();
threadtcpchat.interrupt();
@@ -42,18 +42,21 @@ public class ListeningThreadTCPConnection extends ListeningThread{
public void run(){
// Tant que l'utilisateur est actif on attends la demande de nouvelle conversation
+ ServerSocket servSocket=null;
+ try {
+ servSocket = new ServerSocket(controller.myUser.getPortTCP());
+ System.out.println("("+this.controller.myUser.getPseudo()+") Server is listening on port "+this.controller.myUser.getPortTCP());
+ } catch (IOException e) {
+ System.out.println("("+this.controller.myUser.getPseudo()+") Server is not listening on port "+this.controller.myUser.getPortTCP());
+ e.printStackTrace();
+ }
+ // TODO changer le true
while(true) {
-
- ServerSocket servSocket=null;
- try {
- servSocket = new ServerSocket(myUser.getPortTCP());
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
-
+ System.out.println("("+this.controller.myUser.getPseudo()+") WAITING FOR NEW CONNECTION");
+
try {
this.accept(servSocket);
+ System.out.println("("+this.controller.myUser.getPseudo()+") NEW CONNECTION");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
@@ -77,7 +80,7 @@ public class ListeningThreadTCPConnection extends ListeningThread{
} catch (IOException e1) {
e1.printStackTrace();
}
- System.out.println("End of listing thread TCP ("+this.myUser.getPseudo()+")");
+ System.out.println("End of listing thread TCP ("+this.controller.myUser.getPseudo()+")");
try {
this.interrupt();
}catch (Exception e){
diff --git a/Application/Clavardage/src/controller/ListeningThreadUDP.java b/Application/Clavardage/src/controller/ListeningThreadUDP.java
index 3089b26..808103e 100644
--- a/Application/Clavardage/src/controller/ListeningThreadUDP.java
+++ b/Application/Clavardage/src/controller/ListeningThreadUDP.java
@@ -23,10 +23,10 @@ public class ListeningThreadUDP extends ListeningThread{
* Création d'un socket d'écoute UDP
*
*/
- public ListeningThreadUDP(String s,LocalUser myUser) {
- super(s,myUser);
+ public ListeningThreadUDP(String s,Controller controller) {
+ super(s,controller);
try {
- this.dgramSocket = new DatagramSocket(this.myUser.getPortUDPlistening(),this.myUser.getAddIP());
+ this.dgramSocket = new DatagramSocket(this.controller.myUser.getPortUDPlistening(),this.controller.myUser.getAddIP());
} catch (IOException e) {
e.printStackTrace();
}
@@ -72,7 +72,7 @@ public class ListeningThreadUDP extends ListeningThread{
}
int senderUDPport = Integer.parseInt(tabMsg[1]); // On récupère le port UDP de l'utilisateur distant
- String toSend = myUser.getAddIP().toString()+":"+myUser.getPortTCP()+":"+myUser.getPseudo()+":test";
+ String toSend = controller.myUser.getAddIP().toString()+":"+controller.myUser.getPortTCP()+":"+controller.myUser.getPseudo()+":test";
DatagramPacket outPacket= new DatagramPacket(toSend.getBytes(), toSend.length(),itsIP, senderUDPport);
try {
@@ -86,7 +86,7 @@ public class ListeningThreadUDP extends ListeningThread{
else {
try {
// 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
- this.myUser.addRemoteUser(InetAddress.getByName(tabMsg[0].split("/")[1]),Integer.parseInt(tabMsg[1]),tabMsg[2]);
+ this.controller.myUser.addRemoteUser(InetAddress.getByName(tabMsg[0].split("/")[1]),Integer.parseInt(tabMsg[1]),tabMsg[2]);
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
@@ -111,7 +111,7 @@ public class ListeningThreadUDP extends ListeningThread{
*/
public void close() {
this.dgramSocket.close();
- System.out.println("End of listing thread UDP ("+this.myUser.getPseudo()+")");
+ System.out.println("End of listing thread UDP ("+this.controller.myUser.getPseudo()+")");
try {
this.interrupt();
}catch (Exception e){
diff --git a/Application/Clavardage/src/model/Chat.java b/Application/Clavardage/src/model/Chat.java
index 9479352..baba4a8 100644
--- a/Application/Clavardage/src/model/Chat.java
+++ b/Application/Clavardage/src/model/Chat.java
@@ -1,54 +1,57 @@
package model;
+import java.io.IOException;
+import java.net.Socket;
import java.util.ArrayList;
public class Chat {
/*** ATTRIBUTS ***/
- private int localUser_portTCP;
- private int remoteUser_portTCP;
- private ArrayList remoteUsersChatList = new ArrayList(); // listes des utilisateurs sur ce chat
+ private RemoteUser remoteUser;
private ArrayList messages = new ArrayList();
-
+ private Socket userSocket;
+
/**
* Constructor of Chat (local)
* @parametres
* @param remoteUser : remoteUser => référence de l'utilisateur distant
- * @param localUser_portTCP : int => le numéro de port TCP d'écoute de la conversation de l'utilisateur local
- * @param remoteUser_portTCP : int => le numéro de port TCP d'écoute de la conversation de l'utilisateur distant
- * @description
+ * @description
*
*
*
*/
- public Chat(RemoteUser rm,int localUser_portTCP,int remoteUser_portTCP) {
- this.localUser_portTCP = localUser_portTCP;
- this.remoteUser_portTCP = remoteUser_portTCP;
- remoteUsersChatList.add(rm);
+ public Chat(RemoteUser rm) {
+ this.remoteUser=rm;
}
/*** GETTERS ***/
- public int getLocalUser_portTCP() {
- return localUser_portTCP;
- }
- public int getRemoteUser_portTCP() {
- return remoteUser_portTCP;
- }
- public ArrayList getRemoteUsersChatList() {
- return remoteUsersChatList;
+ public RemoteUser getRemoteUser() {
+ return remoteUser;
}
public ArrayList getMessages() {
return messages;
}
-
+ public Socket getUserSocket() {
+ return userSocket;
+ }
+
/*** SETTERS ***/
- public void setLocalUser_portTCP(int localUser_portTCP) {
- this.localUser_portTCP = localUser_portTCP;
+ public void setRemoteUser(RemoteUser rm) {
+ this.remoteUser = rm;
}
- public void setRemoteUser_portTCP(int remoteUser_portTCP) {
- this.remoteUser_portTCP = remoteUser_portTCP;
+ public void setSocket(Socket link) {
+ this.userSocket = link;
}
- public void setRemoteUsersChatList(ArrayList remoteUsersChatList) {
- this.remoteUsersChatList = remoteUsersChatList;
+ public void addMessage(Message msg) {
+ this.messages.add(msg);
+ }
+
+ public void closeSocket() {
+ try {
+ this.userSocket.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+
}
}
diff --git a/Application/Clavardage/src/model/LocalUser.java b/Application/Clavardage/src/model/LocalUser.java
index a38c52e..8f8f6a0 100644
--- a/Application/Clavardage/src/model/LocalUser.java
+++ b/Application/Clavardage/src/model/LocalUser.java
@@ -88,8 +88,30 @@ public class LocalUser extends User{
* Ajout ou mise à jour l'utilisateur distant dans notre liste d'utilisateur distant
*
*/
- public void addChats(RemoteUser rm,int localUser_portTCP,int remoteUser_portTCP) {
- Chat newChat= new Chat(rm,localUser_portTCP,remoteUser_portTCP);
+ public Chat addChats(RemoteUser rm) {
+ Chat newChat= new Chat(rm);
this.chats.add(newChat);
+ return newChat;
+ }
+
+ public int getIndexOf(RemoteUser remoteUser) {
+ int i=0;
+ Boolean found = (this.chats.get(i).getRemoteUser() == remoteUser);
+ while(i