ajout classe MessageHorodate Historique et TCPEchange
This commit is contained in:
parent
717a2427fb
commit
a6fdfff6f8
5 changed files with 143 additions and 1 deletions
|
@ -2,6 +2,7 @@ import java.io.IOException;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
|
|
||||||
|
@ -16,7 +17,7 @@ public class ChatApp {
|
||||||
/* Liste des utilisateurs actifs */
|
/* Liste des utilisateurs actifs */
|
||||||
private ListUtilisateurs actifUsers ;
|
private ListUtilisateurs actifUsers ;
|
||||||
private static ArrayList<Integer> ListPort = new ArrayList<Integer>();
|
private static ArrayList<Integer> ListPort = new ArrayList<Integer>();
|
||||||
|
private Map<Historique,String> mapHistorique ;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
17
Implementation/src/Historique.java
Normal file
17
Implementation/src/Historique.java
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
public class Historique {
|
||||||
|
private Utilisateur User1;
|
||||||
|
private Utilisateur User2;
|
||||||
|
private ArrayList<MessageHorodate> HistoriqueHorodate ;
|
||||||
|
|
||||||
|
public Historique(Utilisateur User1, Utilisateur User2) {
|
||||||
|
this.User1 = User1;
|
||||||
|
this.User2 = User2;
|
||||||
|
this.HistoriqueHorodate = new ArrayList<MessageHorodate>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addMessage(MessageHorodate mh) {
|
||||||
|
this.HistoriqueHorodate.add(mh);
|
||||||
|
}
|
||||||
|
}
|
64
Implementation/src/MessageHorodate.java
Normal file
64
Implementation/src/MessageHorodate.java
Normal file
|
@ -0,0 +1,64 @@
|
||||||
|
import java.net.InetAddress;
|
||||||
|
import java.net.UnknownHostException;
|
||||||
|
import java.text.DateFormat;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>
|
||||||
|
* Classe representant les messages envoyés en TCP lors d'une session de clavardage
|
||||||
|
* </p>
|
||||||
|
*/
|
||||||
|
public class MessageHorodate {
|
||||||
|
private Utilisateur destinataire ;
|
||||||
|
private Utilisateur source ;
|
||||||
|
private Date dateHorodatage ;
|
||||||
|
private String Message;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>
|
||||||
|
* Constructeur , le message va etre horodate
|
||||||
|
* @param destinataire - Destinataire du message
|
||||||
|
* @param source - Source du message
|
||||||
|
* @param Message - Message envoye
|
||||||
|
* </p>
|
||||||
|
*/
|
||||||
|
public MessageHorodate(Utilisateur destinataire , Utilisateur source , String Message) {
|
||||||
|
this.destinataire = destinataire ;
|
||||||
|
this.source = source ;
|
||||||
|
this.Message = Message ;
|
||||||
|
this.dateHorodatage = new Date();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>
|
||||||
|
* permet de creer une representation string du message
|
||||||
|
* @return Les differents attributs de la classe sous forme de string
|
||||||
|
* </p>
|
||||||
|
*/
|
||||||
|
public String toString() {
|
||||||
|
String Msg = "";
|
||||||
|
Msg += ("Destinataire: " + this.destinataire + "\n") ;
|
||||||
|
Msg += ("Source: " + this.source+ "\n") ;
|
||||||
|
Msg += ("Date: " + this.dateToString() + "\n") ;
|
||||||
|
Msg += ("Message:" + this.Message + "\n" );
|
||||||
|
return Msg ;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>
|
||||||
|
* permet de creer une representation string de la date d'horodatage
|
||||||
|
* @return La date d'horodatage du message en format yyyy/MM/dd HH:mm:ss
|
||||||
|
* </p>
|
||||||
|
*/
|
||||||
|
public String dateToString() {
|
||||||
|
DateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
|
||||||
|
return format.format(this.dateHorodatage);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static MessageHorodate stringToMessageHorodate(String s) {
|
||||||
|
MessageHorodate mh ;
|
||||||
|
return mh ;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Binary file not shown.
60
Implementation/src/TCPEchange.java
Normal file
60
Implementation/src/TCPEchange.java
Normal file
|
@ -0,0 +1,60 @@
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.io.PrintStream;
|
||||||
|
import java.net.ServerSocket;
|
||||||
|
import java.net.Socket;
|
||||||
|
|
||||||
|
public class TCPEchange {
|
||||||
|
|
||||||
|
public static void envoiTCP(Utilisateur User1,Utilisateur User2, String Msg ) {
|
||||||
|
MessageHorodate mh = new MessageHorodate(User1, User2, Msg);
|
||||||
|
try {
|
||||||
|
Socket s = new Socket(User2.getIp(), User2.getPort());
|
||||||
|
PrintStream output = new PrintStream(s.getOutputStream());
|
||||||
|
output.println(mh.toString());
|
||||||
|
output.close();
|
||||||
|
s.close();
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void ecouteTCP() {
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class RunnerTCP implements Runnable {
|
||||||
|
final Socket link;
|
||||||
|
|
||||||
|
public RunnerTCP(Socket link) {
|
||||||
|
this.link = link;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
System.out.println("Thread started");
|
||||||
|
try {
|
||||||
|
InputStream is = link.getInputStream();
|
||||||
|
int rcv = 0;
|
||||||
|
while ((rcv = is.read()) != -1) {
|
||||||
|
System.out.println("Received: "+ rcv);
|
||||||
|
}
|
||||||
|
System.out.println("Finishing thread");
|
||||||
|
is.close();
|
||||||
|
link.close();
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue