Compare commits

..

No commits in common. "413bb6f896ffa865c16f5466e705cf884743f396" and "38e8703d5a2d51f3d14994cc5187ae98c4939901" have entirely different histories.

2 changed files with 28 additions and 130 deletions

View file

@ -1,5 +1,4 @@
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
@ -12,7 +11,6 @@ public class MessageHorodate {
private Utilisateur destinataire ;
private Utilisateur source ;
private Date dateHorodatage ;
private int type; // 0 = debut de la communication, 1= message de communication, 2 = fin de la communicataion
private String Message;
/**
@ -23,16 +21,11 @@ public class MessageHorodate {
* @param Message - Message envoye
* </p>
*/
public MessageHorodate(Utilisateur destinataire, Utilisateur source, String Message, int type) {
public MessageHorodate(Utilisateur destinataire , Utilisateur source , String Message) {
this.destinataire = destinataire ;
this.source = source ;
this.Message = Message ;
this.dateHorodatage = new Date();
this.type = type;
}
public void setDate(Date d) {
this.dateHorodatage=d;
}
/**
@ -44,10 +37,9 @@ public class MessageHorodate {
@Override
public String toString() {
String Msg = "";
Msg += ("Destinataire:" + this.destinataire + "\n") ;
Msg += ("Source:" + this.source+ "\n") ;
Msg += ("Type:"+ this.type+ "\n");
Msg += ("Date:" + this.dateToString() + "\n") ;
Msg += ("Destinataire: " + this.destinataire + "\n") ;
Msg += ("Source: " + this.source+ "\n") ;
Msg += ("Date: " + this.dateToString() + "\n") ;
Msg += ("Message:" + this.Message + "\n" );
return Msg ;
}
@ -70,28 +62,9 @@ public class MessageHorodate {
* @return un messageHorodate
* </p>
*/
public static MessageHorodate stringToMessageHorodate(String s) {
String mots[] = s.split("\n");
Utilisateur destinataire = Utilisateur.stringToUtilisateur(mots[0].split(":")[1]);
Utilisateur source = Utilisateur.stringToUtilisateur(mots[1].split(":")[1]);
int type = Integer.parseInt(mots[2].split(":")[1]);
DateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
try {
date = format.parse(mots[3].split(":")[1]);
} catch (ParseException e) {
e.printStackTrace();
}
String payload = "";
for(int i=4; i< mots.length; i++) {
payload += mots[i]+"\n";
}
MessageHorodate mh = new MessageHorodate(destinataire, source, payload, type);
mh.setDate(date);
/* public static MessageHorodate stringToMessageHorodate(String s) {
MessageHorodate mh ;
return mh ;
}
}*/
}

View file

@ -1,16 +1,7 @@
import java.io.InputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.OutputStream;
import java.net.DatagramSocket;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* <p>
@ -19,24 +10,13 @@ import java.io.InputStreamReader;
*/
public class TCPEchange {
public static void demarrerSession(ChatApp app,Utilisateur User2 ) throws IOException {
Socket s = new Socket(User2.getIp(),5000);
ExecutorService exec = Executors.newFixedThreadPool(1000);
exec.submit(new RunnerTCPEcoute(s,app));
exec.submit(new RunnerTCPEnvoi(s,app,app.getMe()));
}
public static void envoiTCP(ChatApp app,Utilisateur User2, String Msg, int type ) {
public static void envoiTCP(ChatApp app,Utilisateur User2, String Msg ) {
// On cree un messagehorodate
MessageHorodate mh = new MessageHorodate(app.getMe(), User2, Msg, type);
if( type == 1 ) {
MessageHorodate mh = new MessageHorodate(app.getMe(), User2, Msg);
// on ajoute le msg à son historique
Historique h = app.getHist(User2.getPseudo());
h.addMessage(mh);
// on update la liste des historiques de app
app.majHistorique(User2.getPseudo(), h);
}
try {
Socket s = new Socket(User2.getIp(), User2.getPort());
@ -50,15 +30,12 @@ public class TCPEchange {
}
public static void ecouteTCP(ChatApp app) {
ServerSocket ss = null;
ExecutorService exec = Executors.newFixedThreadPool(1000);
try {
ss = new ServerSocket(5000);
ServerSocket ss = new ServerSocket(1234);
while(true) {
System.out.println("Attente Session de clavardage");
Socket link = ss.accept();
exec.submit(new RunnerTCPEcoute(link,app));
exec.submit(new RunnerTCPEnvoi(link,app,app.getMe()));
new Thread(new RunnerTCP(link,app)).start();
}
}
catch (Exception e) {
@ -67,43 +44,11 @@ public class TCPEchange {
}
}
//ENVOI
class RunnerTCPEnvoi implements Runnable {
final Socket link;
private ChatApp app ;
private Utilisateur Destinataire;
final BufferedReader in;
final PrintWriter out;
final Scanner sc=new Scanner(System.in);
public RunnerTCPEnvoi(Socket link,ChatApp app, Utilisateur user2 ) throws IOException {
this.link = link;
this.app = app;
this.Destinataire = user2;
this.out = new PrintWriter(link.getOutputStream());
this.in = new BufferedReader (new InputStreamReader (link.getInputStream()));
}
@Override
public void run() {
System.out.println("Thread started");
String msg;
while(true){
msg = sc.nextLine();
MessageHorodate mh = new MessageHorodate(Destinataire,app.getMe(),msg,1);
out.println(mh.toString());
out.flush();
}
}
}
// Reception
class RunnerTCPEcoute implements Runnable {
class RunnerTCP implements Runnable {
final Socket link;
private ChatApp app ;
public RunnerTCPEcoute(Socket link,ChatApp app ) {
public RunnerTCP(Socket link,ChatApp app ) {
this.link = link;
this.app = app;
}
@ -112,43 +57,23 @@ class RunnerTCPEcoute implements Runnable {
public void run() {
System.out.println("Thread started");
try {
PrintStream output = new PrintStream(link.getOutputStream());
//InputStream is = link.getInputStream();
BufferedReader in = new BufferedReader (new InputStreamReader (link.getInputStream()));
String line = in.readLine();
String msg = "";
while (line != null) {
System.out.println("Received: "+ msg);
/*if((line.split(" ")[0].equals("Destinataire"))) {
if(msg != "") {
MessageHorodate mh = MessageHorodate.stringToMessageHorodate(msg);
// on ajoute le msg à son historique
Historique h = app.getHist(mh.);
h.addMessage(mh);
// on update la liste des historiques de app
app.majHistorique(User2.getPseudo(), h);
InputStream is = link.getInputStream();
int rcv = 0;
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");
in.close();
is.close();
link.close();
} catch (IOException e) {
} catch (Exception e) {
e.printStackTrace();
}
}
}
}