194 lines
5.7 KiB
Java
194 lines
5.7 KiB
Java
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>
|
|
* Classe representant les échanges TCP entre utilisateurs.
|
|
* </p>
|
|
*/
|
|
public class TCPEchange {
|
|
|
|
public static void demarrerSession(ChatApp app,Utilisateur User2 ) throws IOException {
|
|
System.out.println("Demmarrage d'une session de clavardage");
|
|
Socket s = new Socket(User2.getIp(),5000);
|
|
System.out.println("Socket de demarrage d'une session cree");
|
|
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, 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());
|
|
PrintStream output = new PrintStream(s.getOutputStream());
|
|
output.println(mh.toString());
|
|
output.close();
|
|
s.close();
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
public static void ecouteTCP(ChatApp app) {
|
|
ServerSocket ss = null;
|
|
ExecutorService exec = Executors.newFixedThreadPool(1000);
|
|
System.out.println("Ecoute TCP activee");
|
|
try {
|
|
ss = new ServerSocket(5000);
|
|
System.out.println("Socket d'ecoute cree");
|
|
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()));
|
|
}
|
|
}
|
|
catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
//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("Creation d'un thread d'envoi");
|
|
String msg;
|
|
while(true){
|
|
msg = sc.nextLine();
|
|
if(msg.equals("--STOP--")) {
|
|
MessageHorodate mh = new MessageHorodate(Destinataire,app.getMe(),msg,0);
|
|
break;
|
|
}
|
|
MessageHorodate mh = new MessageHorodate(Destinataire,app.getMe(),msg,1);
|
|
out.println(mh);
|
|
out.flush();
|
|
}
|
|
try {
|
|
System.out.println("Fermeture du thread d'envoi");
|
|
in.close();
|
|
link.close();
|
|
}catch(Exception e) {
|
|
// Gestion de l'exception de la fermeture de la socket
|
|
}
|
|
}
|
|
}
|
|
|
|
// Reception
|
|
class RunnerTCPEcoute implements Runnable {
|
|
final Socket link;
|
|
private ChatApp app ;
|
|
|
|
public RunnerTCPEcoute(Socket link,ChatApp app ) {
|
|
this.link = link;
|
|
this.app = app;
|
|
}
|
|
|
|
@Override
|
|
public void run() {
|
|
System.out.println("Creation d'un thread d'ecoute");
|
|
try {
|
|
PrintStream output = new PrintStream(link.getOutputStream());
|
|
//InputStream is = link.getInputStream();
|
|
BufferedReader in = new BufferedReader (new InputStreamReader (link.getInputStream()));
|
|
|
|
String line = "";
|
|
String dest = "";
|
|
String src = "";
|
|
String type = "";
|
|
String date = "";
|
|
String payload = "";
|
|
String msg = "";
|
|
while (line != null) {
|
|
line = in.readLine();
|
|
if(line.split(":")[0].equals("Destinataire")) {
|
|
if(msg.equals("")) {
|
|
dest = line+"\n";
|
|
}
|
|
else {
|
|
msg=dest+src+type+date+payload;
|
|
payload = "";
|
|
MessageHorodate mh = MessageHorodate.stringToMessageHorodate(msg);
|
|
if(mh.getType()==1) {
|
|
Historique h = app.getHist(mh.getSource().getPseudo());
|
|
h.addMessage(mh);
|
|
app.majHistorique(mh.getSource().getPseudo(), h);
|
|
}
|
|
else {
|
|
break;
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
else if(line.split(":")[0].equals("Source")) {
|
|
src = line+"\n";
|
|
}
|
|
else if(line.split(":")[0].equals("Type")) {
|
|
type = line+"\n";
|
|
}
|
|
else if(line.split(":")[0].equals("Date")) {
|
|
date = line+"\n";
|
|
}
|
|
else {
|
|
payload += line+"\n";
|
|
}
|
|
|
|
System.out.println("Received: "+ line);
|
|
|
|
|
|
|
|
}
|
|
System.out.println("Finishing thread");
|
|
|
|
|
|
in.close();
|
|
link.close();
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|