Debut de l'implementation de la phase TCP
This commit is contained in:
parent
f436ef2c18
commit
46ad15fecc
4 changed files with 130 additions and 28 deletions
|
@ -1,4 +1,5 @@
|
|||
import java.text.DateFormat;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
|
@ -11,6 +12,7 @@ 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;
|
||||
|
||||
/**
|
||||
|
@ -21,11 +23,16 @@ public class MessageHorodate {
|
|||
* @param Message - Message envoye
|
||||
* </p>
|
||||
*/
|
||||
public MessageHorodate(Utilisateur destinataire , Utilisateur source , String Message) {
|
||||
public MessageHorodate(Utilisateur destinataire, Utilisateur source, String Message, int type) {
|
||||
this.destinataire = destinataire ;
|
||||
this.source = source ;
|
||||
this.Message = Message ;
|
||||
this.dateHorodatage = new Date();
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public void setDate(Date d) {
|
||||
this.dateHorodatage=d;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -37,9 +44,10 @@ public class MessageHorodate {
|
|||
@Override
|
||||
public String toString() {
|
||||
String Msg = "";
|
||||
Msg += ("Destinataire: " + this.destinataire + "\n") ;
|
||||
Msg += ("Source: " + this.source+ "\n") ;
|
||||
Msg += ("Date: " + this.dateToString() + "\n") ;
|
||||
Msg += ("Destinataire:" + this.destinataire + "\n") ;
|
||||
Msg += ("Source:" + this.source+ "\n") ;
|
||||
Msg += ("Type:"+ this.type+ "\n");
|
||||
Msg += ("Date:" + this.dateToString() + "\n") ;
|
||||
Msg += ("Message:" + this.Message + "\n" );
|
||||
return Msg ;
|
||||
}
|
||||
|
@ -62,9 +70,28 @@ public class MessageHorodate {
|
|||
* @return un messageHorodate
|
||||
* </p>
|
||||
*/
|
||||
/* public static MessageHorodate stringToMessageHorodate(String s) {
|
||||
MessageHorodate mh ;
|
||||
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);
|
||||
|
||||
return mh ;
|
||||
}*/
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Binary file not shown.
|
@ -1,7 +1,16 @@
|
|||
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>
|
||||
|
@ -10,13 +19,24 @@ import java.net.Socket;
|
|||
*/
|
||||
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
|
||||
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);
|
||||
MessageHorodate mh = new MessageHorodate(app.getMe(), User2, Msg, type);
|
||||
if( type == 1 ) {
|
||||
// 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());
|
||||
|
@ -30,12 +50,15 @@ public class TCPEchange {
|
|||
}
|
||||
|
||||
public static void ecouteTCP(ChatApp app) {
|
||||
ServerSocket ss = null;
|
||||
ExecutorService exec = Executors.newFixedThreadPool(1000);
|
||||
try {
|
||||
ServerSocket ss = new ServerSocket(1234);
|
||||
ss = new ServerSocket(5000);
|
||||
while(true) {
|
||||
System.out.println("Attente Session de clavardage");
|
||||
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) {
|
||||
|
@ -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;
|
||||
private ChatApp app ;
|
||||
|
||||
public RunnerTCP(Socket link,ChatApp app ) {
|
||||
public RunnerTCPEcoute(Socket link,ChatApp app ) {
|
||||
this.link = link;
|
||||
this.app = app;
|
||||
}
|
||||
|
@ -57,23 +112,43 @@ class RunnerTCP implements Runnable {
|
|||
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);
|
||||
}
|
||||
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);
|
||||
|
||||
}
|
||||
}*/
|
||||
// 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();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
|
||||
|
||||
in.close();
|
||||
link.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
|
Binary file not shown.
Loading…
Reference in a new issue