Reception du message
This commit is contained in:
parent
a168df58fb
commit
90d485801d
5 changed files with 73 additions and 27 deletions
|
@ -337,12 +337,16 @@ public class Controller {
|
|||
/*** Create socket send => ask connection to server of rm ***/
|
||||
Socket link=null;
|
||||
try {
|
||||
// Connection => server tcp rm
|
||||
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()*/);
|
||||
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) {
|
||||
System.out.println("Error linking to TCP server of "+ c.getRemoteUser().getPortTCP());
|
||||
}
|
||||
c.setSocket(link);
|
||||
|
||||
|
||||
/*** recup history and put it in model ***/
|
||||
/*
|
||||
|
@ -374,7 +378,7 @@ public class Controller {
|
|||
// Look for history
|
||||
int nbMessage = c.getMessages().size();
|
||||
for(int i=0;i<nbMessage;i++) {
|
||||
history+="<br>"+c.getMessages().get(i).getMessage();
|
||||
history+="\n"+c.getMessages().get(i).getMessage();
|
||||
}
|
||||
this.activeChat = c;
|
||||
|
||||
|
@ -416,13 +420,16 @@ public class Controller {
|
|||
//this.getHistory().saveMessage(getMyUser(), c.getRemoteUser(),message ,dateFormat.format(date));
|
||||
|
||||
// Send message
|
||||
out.println(dateFormat.format(date));
|
||||
//out.println(dateFormat.format(date));
|
||||
out.println(message);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
public String askNewMessage() {
|
||||
String message = "";
|
||||
return message;
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) throws IOException, InterruptedException {
|
||||
|
|
|
@ -51,12 +51,37 @@ public class ListeningThreadTCPChat extends ListeningThread{
|
|||
|
||||
|
||||
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 **********/
|
||||
/*
|
||||
RemoteUser rm = new RemoteUser("Unknown",this.socket.getInetAddress(),this.socket.getPort());
|
||||
// check data identification from rm user | TO REMOVE IF !=IP
|
||||
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);
|
||||
// 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) {
|
||||
rm = controller.myUser.getRemoteUsersList().get(indexRM);
|
||||
// Check if chat already created
|
||||
|
@ -66,27 +91,24 @@ public class ListeningThreadTCPChat extends ListeningThread{
|
|||
c = controller.myUser.getChats().get(indexChat);
|
||||
}
|
||||
else {
|
||||
c = this.controller.myUser.addChats(rm);
|
||||
this.controller.openSession(c);
|
||||
this.controller.openSession(rm);
|
||||
}
|
||||
}
|
||||
else {
|
||||
System.out.println("("+this.controller.myUser.getPseudo()+") Remote User unidentifiable => CLOSING CONNECTION");
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
/*** listening tcp message from rm until session is close ***/
|
||||
|
||||
try {
|
||||
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 {
|
||||
System.out.println("("+this.controller.myUser.getPseudo()+") WAIT FOR NEW MESSAGE FROM "+rm.getPseudo());
|
||||
|
||||
while (!(input=in.readLine()).equals("end")/* && c.getActive()*/) {
|
||||
System.out.println("("+this.controller.myUser.getPseudo()+") recoit : "+input);
|
||||
//c.addMessage(new Msg_Text(rm.getAddIP(),input));
|
||||
System.out.println("("+this.controller.myUser.getPseudo()+") recoit => "+rm.getPseudo()+" : "+input);
|
||||
c.addMessage(new Msg_Text(rm.getAddIP(),input));
|
||||
// TODO Prévenir l'interface de la réception d'un nouveau message
|
||||
controller.view.notifyNewMessage();
|
||||
|
||||
}
|
||||
} catch (IOException e) {
|
||||
|
|
|
@ -112,8 +112,15 @@ public class LocalUser extends User{
|
|||
public int getActiveUserIndexOf(RemoteUser rm) {
|
||||
int index = 0;
|
||||
Boolean found = false;
|
||||
//System.out.println(rm);
|
||||
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++;
|
||||
}
|
||||
if(found) {
|
||||
|
|
|
@ -56,8 +56,9 @@ public abstract class User {
|
|||
this.portTCP = portTCP;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
|
@ -67,13 +68,12 @@ public abstract class User {
|
|||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
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)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -224,6 +224,16 @@ public class Interface extends JFrame implements ActionListener, WindowListener
|
|||
public void windowClosed(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 {
|
||||
//Schedule a job for the event-dispatching thread:
|
||||
|
|
Loading…
Reference in a new issue