Reception du message

This commit is contained in:
Alexandre Gonzalvez 2020-12-18 11:38:33 +01:00
parent a168df58fb
commit 90d485801d
5 changed files with 73 additions and 27 deletions

View file

@ -337,12 +337,16 @@ public class Controller {
/*** Create socket send => ask connection to server of rm ***/ /*** Create socket send => ask connection to server of rm ***/
Socket link=null; Socket link=null;
try { try {
// Connection => server tcp rm
System.out.println("("+this.myUser.getPseudo()+") Connecting to "+c.getRemoteUser().getPortTCP()+" of " + c.getRemoteUser().getPseudo()); System.out.println("("+this.myUser.getPseudo()+") Connecting to "+c.getRemoteUser().getPortTCP()+" of " + c.getRemoteUser().getPseudo());
link=new Socket(c.getRemoteUser().getAddIP(),c.getRemoteUser().getPortTCP()/*, InetAddress.getLocalHost() ,myUser.getPortTCP()*/); link=new Socket(c.getRemoteUser().getAddIP(),c.getRemoteUser().getPortTCP()/*, InetAddress.getLocalHost() ,myUser.getPortTCP()*/);
c.setSocket(link);
// Sending data for identification (TO REMOVE IF != IP)
sendMessage(new Msg_Text(myUser.getAddIP(),Integer.toString(myUser.getPortTCP())),c);
}catch(IOException e) { }catch(IOException e) {
System.out.println("Error linking to TCP server of "+ c.getRemoteUser().getPortTCP()); System.out.println("Error linking to TCP server of "+ c.getRemoteUser().getPortTCP());
} }
c.setSocket(link);
/*** recup history and put it in model ***/ /*** recup history and put it in model ***/
/* /*
@ -374,7 +378,7 @@ public class Controller {
// Look for history // Look for history
int nbMessage = c.getMessages().size(); int nbMessage = c.getMessages().size();
for(int i=0;i<nbMessage;i++) { for(int i=0;i<nbMessage;i++) {
history+="<br>"+c.getMessages().get(i).getMessage(); history+="\n"+c.getMessages().get(i).getMessage();
} }
this.activeChat = c; this.activeChat = c;
@ -416,13 +420,16 @@ public class Controller {
//this.getHistory().saveMessage(getMyUser(), c.getRemoteUser(),message ,dateFormat.format(date)); //this.getHistory().saveMessage(getMyUser(), c.getRemoteUser(),message ,dateFormat.format(date));
// Send message // Send message
out.println(dateFormat.format(date)); //out.println(dateFormat.format(date));
out.println(message); out.println(message);
} }
public String askNewMessage() {
String message = "";
return message;
}
public static void main(String[] args) throws IOException, InterruptedException { public static void main(String[] args) throws IOException, InterruptedException {

View file

@ -51,12 +51,37 @@ public class ListeningThreadTCPChat extends ListeningThread{
Chat c = null; Chat c = null;
try {
in =new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/************ Check rm user information **********/ /************ Check rm user information **********/
/* // check data identification from rm user | TO REMOVE IF !=IP
RemoteUser rm = new RemoteUser("Unknown",this.socket.getInetAddress(),this.socket.getPort()); int rmPortTCP = -1;
try {
rmPortTCP = Integer.parseInt(in.readLine());
} catch (NumberFormatException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
RemoteUser rm = new RemoteUser("Unknown",this.socket.getInetAddress(),rmPortTCP);
int indexRM = controller.myUser.getActiveUserIndexOf(rm); int indexRM = controller.myUser.getActiveUserIndexOf(rm);
// Check if rm is identifiable // Check if rm is identifiable
System.out.println(rm); /*System.out.println(rm);
for(int i=0;i<controller.myUser.getRemoteUsersList().size();i++) {
System.out.println(controller.myUser.getRemoteUsersList().get(i));
}
System.out.println(indexRM);*/
if(indexRM!=-1) { if(indexRM!=-1) {
rm = controller.myUser.getRemoteUsersList().get(indexRM); rm = controller.myUser.getRemoteUsersList().get(indexRM);
// Check if chat already created // Check if chat already created
@ -66,27 +91,24 @@ public class ListeningThreadTCPChat extends ListeningThread{
c = controller.myUser.getChats().get(indexChat); c = controller.myUser.getChats().get(indexChat);
} }
else { else {
c = this.controller.myUser.addChats(rm); this.controller.openSession(rm);
this.controller.openSession(c);
} }
} }
else { else {
System.out.println("("+this.controller.myUser.getPseudo()+") Remote User unidentifiable => CLOSING CONNECTION"); System.out.println("("+this.controller.myUser.getPseudo()+") Remote User unidentifiable => CLOSING CONNECTION");
} }
*/
/*** listening tcp message from rm until session is close ***/ /*** listening tcp message from rm until session is close ***/
try { try {
System.out.println("("+this.controller.myUser.getPseudo()+") WAIT FOR NEW MESSAGE FROM ");//+rm.getPseudo()); System.out.println("("+this.controller.myUser.getPseudo()+") WAIT FOR NEW MESSAGE FROM "+rm.getPseudo());
in =new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
while (!(input=in.readLine()).equals("end")/* && c.getActive()*/) { while (!(input=in.readLine()).equals("end")/* && c.getActive()*/) {
System.out.println("("+this.controller.myUser.getPseudo()+") recoit : "+input); System.out.println("("+this.controller.myUser.getPseudo()+") recoit => "+rm.getPseudo()+" : "+input);
//c.addMessage(new Msg_Text(rm.getAddIP(),input)); c.addMessage(new Msg_Text(rm.getAddIP(),input));
// TODO Prévenir l'interface de la réception d'un nouveau message // TODO Prévenir l'interface de la réception d'un nouveau message
controller.view.notifyNewMessage();
} }
} catch (IOException e) { } catch (IOException e) {

View file

@ -112,8 +112,15 @@ public class LocalUser extends User{
public int getActiveUserIndexOf(RemoteUser rm) { public int getActiveUserIndexOf(RemoteUser rm) {
int index = 0; int index = 0;
Boolean found = false; Boolean found = false;
//System.out.println(rm);
while(index<this.remoteUsersList.size() && !found) { while(index<this.remoteUsersList.size() && !found) {
found = (this.remoteUsersList.get(index) == rm); found = (this.remoteUsersList.get(index).equals(rm));
/*
System.out.println(found);
System.out.println(this.remoteUsersList.get(index));
System.out.println(this.remoteUsersList.get(index).getPortTCP()==rm.getPortTCP());
System.out.println(this.remoteUsersList.get(index).getAddIP()==rm.getAddIP());
*/
index++; index++;
} }
if(found) { if(found) {

View file

@ -56,8 +56,9 @@ public abstract class User {
this.portTCP = portTCP; this.portTCP = portTCP;
} }
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (this == obj) if (this == obj)
@ -67,13 +68,12 @@ public abstract class User {
if (getClass() != obj.getClass()) if (getClass() != obj.getClass())
return false; return false;
User other = (User) obj; User other = (User) obj;
if (addIP == null) {
if (other.addIP != null)
return false;
} else if (!addIP.equals(other.addIP))
return false;
if (portTCP != other.portTCP) if (portTCP != other.portTCP)
return false; return false;
return true; return true;
} }
} }

View file

@ -224,6 +224,16 @@ public class Interface extends JFrame implements ActionListener, WindowListener
public void windowClosed(WindowEvent e) {} public void windowClosed(WindowEvent e) {}
public void windowClosing(WindowEvent e) {} public void windowClosing(WindowEvent e) {}
public void notifyNewMessage() {
String newMessage = controller.askNewMessage();
//TOCHECK maybe better way to do it
if(newMessage!="") {
//TODO afficher le message
}
}
/* /*
public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException {
//Schedule a job for the event-dispatching thread: //Schedule a job for the event-dispatching thread: