Suite echange TCP
This commit is contained in:
parent
1cea2246c3
commit
9b173cd22d
8 changed files with 89 additions and 8 deletions
Binary file not shown.
|
@ -2,6 +2,7 @@ import java.io.IOException;
|
|||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
@ -17,7 +18,7 @@ public class ChatApp {
|
|||
/* Liste des utilisateurs actifs */
|
||||
private ListUtilisateurs actifUsers ;
|
||||
private static ArrayList<Integer> ListPort = new ArrayList<Integer>();
|
||||
private Map<Historique,String> mapHistorique ;
|
||||
private Map<String,Historique> mapHistorique ;
|
||||
|
||||
|
||||
|
||||
|
@ -49,8 +50,13 @@ public class ChatApp {
|
|||
//ip.getHostAddress();
|
||||
this.me = new Utilisateur(pseudo,port,ip);
|
||||
this.actifUsers.addList(getMe());
|
||||
this.mapHistorique = new HashMap<String,Historique>() ;
|
||||
}
|
||||
|
||||
public void majHistorique(String pseudo,Historique h) {
|
||||
getMapHistorique().put(h.getUser2().getPseudo(),h);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
|
@ -112,6 +118,8 @@ public class ChatApp {
|
|||
public static void main (String[] args) throws IOException {
|
||||
//Integer p = 2345 ;
|
||||
ChatApp app = new ChatApp(args[0],Integer.parseInt(args[1])) ;
|
||||
InetAddress localHost = InetAddress.getLocalHost() ;
|
||||
System.out.println("Mon adress: "+ localHost.toString());
|
||||
ListPort.add(1234);
|
||||
ListPort.add(3000);
|
||||
ListPort.add(4000);
|
||||
|
@ -151,6 +159,22 @@ public class ChatApp {
|
|||
public ListUtilisateurs getActifUsers() {
|
||||
return actifUsers;
|
||||
}
|
||||
|
||||
public Map<String,Historique> getMapHistorique() {
|
||||
return mapHistorique;
|
||||
}
|
||||
|
||||
public Historique getHist(String pseudo) {
|
||||
Historique h = this.mapHistorique.get(pseudo);
|
||||
if( h != null) {
|
||||
return h ;
|
||||
}
|
||||
else {
|
||||
h = new Historique(this.me, this.getActifUsers().getPseudoList(pseudo));
|
||||
return h ;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
class Runner implements Runnable {
|
||||
|
|
BIN
Implementation/src/Historique.class
Normal file
BIN
Implementation/src/Historique.class
Normal file
Binary file not shown.
|
@ -1,17 +1,47 @@
|
|||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Classe permettant de sauvegarder les échanges entre deux utilisateurs
|
||||
* </p>
|
||||
*/
|
||||
public class Historique {
|
||||
private Utilisateur User1;
|
||||
private Utilisateur User2;
|
||||
// Liste conservant les differents échanges
|
||||
private ArrayList<MessageHorodate> HistoriqueHorodate ;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Constructeur : un historique sauvegarde les echanges entre
|
||||
* User1 et User2
|
||||
* @param User1 - un utilisateur
|
||||
* @param User2 - un second utilisateur
|
||||
* </p>
|
||||
*/
|
||||
public Historique(Utilisateur User1, Utilisateur User2) {
|
||||
this.User1 = User1;
|
||||
this.User2 = User2;
|
||||
this.HistoriqueHorodate = new ArrayList<MessageHorodate>();
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* On ajoute un message échangé dans la liste 'HistoriqueHorodate'
|
||||
*
|
||||
* @param mh - le message horodate échangé
|
||||
* </p>
|
||||
*/
|
||||
public void addMessage(MessageHorodate mh) {
|
||||
this.HistoriqueHorodate.add(mh);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* getter pour recuperer le second utilisateur
|
||||
* </p>
|
||||
*/
|
||||
public Utilisateur getUser2() {
|
||||
return User2;
|
||||
}
|
||||
}
|
||||
|
|
BIN
Implementation/src/MessageHorodate.class
Normal file
BIN
Implementation/src/MessageHorodate.class
Normal file
Binary file not shown.
|
@ -56,9 +56,16 @@ public class MessageHorodate {
|
|||
return format.format(this.dateHorodatage);
|
||||
}
|
||||
|
||||
public static MessageHorodate stringToMessageHorodate(String s) {
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Permet de re creer un message horodate a partir d'un string
|
||||
* @return un messageHorodate
|
||||
* </p>
|
||||
*/
|
||||
/* public static MessageHorodate stringToMessageHorodate(String s) {
|
||||
MessageHorodate mh ;
|
||||
return mh ;
|
||||
}
|
||||
}*/
|
||||
|
||||
}
|
||||
|
|
Binary file not shown.
|
@ -4,10 +4,21 @@ import java.io.PrintStream;
|
|||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Classe representant les échanges TCP entre utilisateurs.
|
||||
* </p>
|
||||
*/
|
||||
public class TCPEchange {
|
||||
|
||||
public static void envoiTCP(Utilisateur User1,Utilisateur User2, String Msg ) {
|
||||
MessageHorodate mh = new MessageHorodate(User1, User2, Msg);
|
||||
public static void envoiTCP(ChatApp app,Utilisateur User2, String Msg ) {
|
||||
// On cree un messagehorodate
|
||||
MessageHorodate mh = new MessageHorodate(app.getMe(), User2, Msg);
|
||||
// on ajoute le msg à son historique
|
||||
Historique h = app.getHist(User2.getPseudo());
|
||||
// on update la liste des historiques de app
|
||||
app.majHistorique(User2.getPseudo(), h);
|
||||
|
||||
try {
|
||||
Socket s = new Socket(User2.getIp(), User2.getPort());
|
||||
PrintStream output = new PrintStream(s.getOutputStream());
|
||||
|
@ -19,13 +30,13 @@ public class TCPEchange {
|
|||
}
|
||||
}
|
||||
|
||||
public static void ecouteTCP() {
|
||||
public static void ecouteTCP(ChatApp app) {
|
||||
try {
|
||||
ServerSocket ss = new ServerSocket(1234);
|
||||
while(true) {
|
||||
System.out.println("Attente Session de clavardage");
|
||||
Socket link = ss.accept();
|
||||
new Thread(new RunnerTCP(link)).start();
|
||||
new Thread(new RunnerTCP(link,app)).start();
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
|
@ -36,9 +47,11 @@ public class TCPEchange {
|
|||
|
||||
class RunnerTCP implements Runnable {
|
||||
final Socket link;
|
||||
private ChatApp app ;
|
||||
|
||||
public RunnerTCP(Socket link) {
|
||||
public RunnerTCP(Socket link,ChatApp app ) {
|
||||
this.link = link;
|
||||
this.app = app;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -50,6 +63,13 @@ class RunnerTCP implements Runnable {
|
|||
while ((rcv = is.read()) != -1) {
|
||||
System.out.println("Received: "+ rcv);
|
||||
}
|
||||
// On recree un messagehorodate à partir du message reçu
|
||||
|
||||
// on ajoute le msg à son historique
|
||||
|
||||
// on update la liste des historiques de app
|
||||
|
||||
|
||||
System.out.println("Finishing thread");
|
||||
is.close();
|
||||
link.close();
|
||||
|
|
Loading…
Reference in a new issue