Compare commits
No commits in common. "413bb6f896ffa865c16f5466e705cf884743f396" and "38e8703d5a2d51f3d14994cc5187ae98c4939901" have entirely different histories.
413bb6f896
...
38e8703d5a
2 changed files with 28 additions and 130 deletions
|
@ -1,5 +1,4 @@
|
||||||
import java.text.DateFormat;
|
import java.text.DateFormat;
|
||||||
import java.text.ParseException;
|
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
|
@ -12,7 +11,6 @@ public class MessageHorodate {
|
||||||
private Utilisateur destinataire ;
|
private Utilisateur destinataire ;
|
||||||
private Utilisateur source ;
|
private Utilisateur source ;
|
||||||
private Date dateHorodatage ;
|
private Date dateHorodatage ;
|
||||||
private int type; // 0 = debut de la communication, 1= message de communication, 2 = fin de la communicataion
|
|
||||||
private String Message;
|
private String Message;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -23,16 +21,11 @@ public class MessageHorodate {
|
||||||
* @param Message - Message envoye
|
* @param Message - Message envoye
|
||||||
* </p>
|
* </p>
|
||||||
*/
|
*/
|
||||||
public MessageHorodate(Utilisateur destinataire, Utilisateur source, String Message, int type) {
|
public MessageHorodate(Utilisateur destinataire , Utilisateur source , String Message) {
|
||||||
this.destinataire = destinataire ;
|
this.destinataire = destinataire ;
|
||||||
this.source = source ;
|
this.source = source ;
|
||||||
this.Message = Message ;
|
this.Message = Message ;
|
||||||
this.dateHorodatage = new Date();
|
this.dateHorodatage = new Date();
|
||||||
this.type = type;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setDate(Date d) {
|
|
||||||
this.dateHorodatage=d;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -46,7 +39,6 @@ public class MessageHorodate {
|
||||||
String Msg = "";
|
String Msg = "";
|
||||||
Msg += ("Destinataire: " + this.destinataire + "\n") ;
|
Msg += ("Destinataire: " + this.destinataire + "\n") ;
|
||||||
Msg += ("Source: " + this.source+ "\n") ;
|
Msg += ("Source: " + this.source+ "\n") ;
|
||||||
Msg += ("Type:"+ this.type+ "\n");
|
|
||||||
Msg += ("Date: " + this.dateToString() + "\n") ;
|
Msg += ("Date: " + this.dateToString() + "\n") ;
|
||||||
Msg += ("Message:" + this.Message + "\n" );
|
Msg += ("Message:" + this.Message + "\n" );
|
||||||
return Msg ;
|
return Msg ;
|
||||||
|
@ -70,28 +62,9 @@ public class MessageHorodate {
|
||||||
* @return un messageHorodate
|
* @return un messageHorodate
|
||||||
* </p>
|
* </p>
|
||||||
*/
|
*/
|
||||||
public static MessageHorodate stringToMessageHorodate(String s) {
|
/* public static MessageHorodate stringToMessageHorodate(String s) {
|
||||||
|
MessageHorodate mh ;
|
||||||
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);
|
|
||||||
|
|
||||||
return mh ;
|
return mh ;
|
||||||
}
|
}*/
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,16 +1,7 @@
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.PrintStream;
|
import java.io.PrintStream;
|
||||||
import java.io.PrintWriter;
|
|
||||||
import java.io.OutputStream;
|
|
||||||
import java.net.DatagramSocket;
|
|
||||||
import java.net.ServerSocket;
|
import java.net.ServerSocket;
|
||||||
import java.net.Socket;
|
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>
|
* <p>
|
||||||
|
@ -19,24 +10,13 @@ import java.io.InputStreamReader;
|
||||||
*/
|
*/
|
||||||
public class TCPEchange {
|
public class TCPEchange {
|
||||||
|
|
||||||
public static void demarrerSession(ChatApp app,Utilisateur User2 ) throws IOException {
|
public static void envoiTCP(ChatApp app,Utilisateur User2, String Msg ) {
|
||||||
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 ) {
|
|
||||||
// On cree un messagehorodate
|
// On cree un messagehorodate
|
||||||
MessageHorodate mh = new MessageHorodate(app.getMe(), User2, Msg, type);
|
MessageHorodate mh = new MessageHorodate(app.getMe(), User2, Msg);
|
||||||
if( type == 1 ) {
|
|
||||||
// on ajoute le msg à son historique
|
// on ajoute le msg à son historique
|
||||||
Historique h = app.getHist(User2.getPseudo());
|
Historique h = app.getHist(User2.getPseudo());
|
||||||
h.addMessage(mh);
|
|
||||||
// on update la liste des historiques de app
|
// on update la liste des historiques de app
|
||||||
app.majHistorique(User2.getPseudo(), h);
|
app.majHistorique(User2.getPseudo(), h);
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Socket s = new Socket(User2.getIp(), User2.getPort());
|
Socket s = new Socket(User2.getIp(), User2.getPort());
|
||||||
|
@ -50,15 +30,12 @@ public class TCPEchange {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void ecouteTCP(ChatApp app) {
|
public static void ecouteTCP(ChatApp app) {
|
||||||
ServerSocket ss = null;
|
|
||||||
ExecutorService exec = Executors.newFixedThreadPool(1000);
|
|
||||||
try {
|
try {
|
||||||
ss = new ServerSocket(5000);
|
ServerSocket ss = new ServerSocket(1234);
|
||||||
while(true) {
|
while(true) {
|
||||||
System.out.println("Attente Session de clavardage");
|
System.out.println("Attente Session de clavardage");
|
||||||
Socket link = ss.accept();
|
Socket link = ss.accept();
|
||||||
exec.submit(new RunnerTCPEcoute(link,app));
|
new Thread(new RunnerTCP(link,app)).start();
|
||||||
exec.submit(new RunnerTCPEnvoi(link,app,app.getMe()));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception e) {
|
catch (Exception e) {
|
||||||
|
@ -67,43 +44,11 @@ public class TCPEchange {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class RunnerTCP implements Runnable {
|
||||||
//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 {
|
|
||||||
final Socket link;
|
final Socket link;
|
||||||
private ChatApp app ;
|
private ChatApp app ;
|
||||||
|
|
||||||
public RunnerTCPEcoute(Socket link,ChatApp app ) {
|
public RunnerTCP(Socket link,ChatApp app ) {
|
||||||
this.link = link;
|
this.link = link;
|
||||||
this.app = app;
|
this.app = app;
|
||||||
}
|
}
|
||||||
|
@ -112,43 +57,23 @@ class RunnerTCPEcoute implements Runnable {
|
||||||
public void run() {
|
public void run() {
|
||||||
System.out.println("Thread started");
|
System.out.println("Thread started");
|
||||||
try {
|
try {
|
||||||
PrintStream output = new PrintStream(link.getOutputStream());
|
InputStream is = link.getInputStream();
|
||||||
//InputStream is = link.getInputStream();
|
int rcv = 0;
|
||||||
BufferedReader in = new BufferedReader (new InputStreamReader (link.getInputStream()));
|
while ((rcv = is.read()) != -1) {
|
||||||
|
System.out.println("Received: "+ rcv);
|
||||||
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);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}*/
|
|
||||||
// On recree un messagehorodate à partir du message reçu
|
// On recree un messagehorodate à partir du message reçu
|
||||||
|
|
||||||
// on ajoute le msg à son historique
|
// on ajoute le msg à son historique
|
||||||
|
|
||||||
// on update la liste des historiques de app
|
// on update la liste des historiques de app
|
||||||
|
|
||||||
}
|
|
||||||
System.out.println("Finishing thread");
|
System.out.println("Finishing thread");
|
||||||
|
is.close();
|
||||||
|
|
||||||
in.close();
|
|
||||||
link.close();
|
link.close();
|
||||||
} catch (IOException e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue