Compare commits
2 commits
38e8703d5a
...
413bb6f896
Author | SHA1 | Date | |
---|---|---|---|
413bb6f896 | |||
46ad15fecc |
2 changed files with 130 additions and 28 deletions
|
@ -1,4 +1,5 @@
|
||||||
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;
|
||||||
|
|
||||||
|
@ -11,6 +12,7 @@ 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;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -21,11 +23,16 @@ public class MessageHorodate {
|
||||||
* @param Message - Message envoye
|
* @param Message - Message envoye
|
||||||
* </p>
|
* </p>
|
||||||
*/
|
*/
|
||||||
public MessageHorodate(Utilisateur destinataire , Utilisateur source , String Message) {
|
public MessageHorodate(Utilisateur destinataire, Utilisateur source, String Message, int type) {
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -37,9 +44,10 @@ public class MessageHorodate {
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
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 += ("Date: " + this.dateToString() + "\n") ;
|
Msg += ("Type:"+ this.type+ "\n");
|
||||||
|
Msg += ("Date:" + this.dateToString() + "\n") ;
|
||||||
Msg += ("Message:" + this.Message + "\n" );
|
Msg += ("Message:" + this.Message + "\n" );
|
||||||
return Msg ;
|
return Msg ;
|
||||||
}
|
}
|
||||||
|
@ -62,9 +70,28 @@ 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,7 +1,16 @@
|
||||||
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>
|
||||||
|
@ -10,13 +19,24 @@ import java.net.Socket;
|
||||||
*/
|
*/
|
||||||
public class TCPEchange {
|
public class TCPEchange {
|
||||||
|
|
||||||
public static void envoiTCP(ChatApp app,Utilisateur User2, String Msg ) {
|
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 ) {
|
||||||
// On cree un messagehorodate
|
// On cree un messagehorodate
|
||||||
MessageHorodate mh = new MessageHorodate(app.getMe(), User2, Msg);
|
MessageHorodate mh = new MessageHorodate(app.getMe(), User2, Msg, type);
|
||||||
|
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());
|
||||||
|
@ -30,12 +50,15 @@ 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 {
|
||||||
ServerSocket ss = new ServerSocket(1234);
|
ss = new ServerSocket(5000);
|
||||||
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();
|
||||||
new Thread(new RunnerTCP(link,app)).start();
|
exec.submit(new RunnerTCPEcoute(link,app));
|
||||||
|
exec.submit(new RunnerTCPEnvoi(link,app,app.getMe()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception e) {
|
catch (Exception e) {
|
||||||
|
@ -44,11 +67,43 @@ 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 RunnerTCP(Socket link,ChatApp app ) {
|
public RunnerTCPEcoute(Socket link,ChatApp app ) {
|
||||||
this.link = link;
|
this.link = link;
|
||||||
this.app = app;
|
this.app = app;
|
||||||
}
|
}
|
||||||
|
@ -57,23 +112,43 @@ class RunnerTCP implements Runnable {
|
||||||
public void run() {
|
public void run() {
|
||||||
System.out.println("Thread started");
|
System.out.println("Thread started");
|
||||||
try {
|
try {
|
||||||
InputStream is = link.getInputStream();
|
PrintStream output = new PrintStream(link.getOutputStream());
|
||||||
int rcv = 0;
|
//InputStream is = link.getInputStream();
|
||||||
while ((rcv = is.read()) != -1) {
|
BufferedReader in = new BufferedReader (new InputStreamReader (link.getInputStream()));
|
||||||
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 (Exception e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue