Progrès sur les sessions

This commit is contained in:
Marino Benassai 2020-12-04 15:48:06 +01:00
parent c4cb34c14d
commit 99dcf24958
6 changed files with 104 additions and 42 deletions

View file

@ -1,4 +1,5 @@
package clavardage;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
@ -15,6 +16,7 @@ public class gestionnaireClavardage implements Runnable{
private String host;
private Thread thread;
//Ajouter la supppression des sessions de la liste après déconnexion
//Remplacer par un singleton
public gestionnaireClavardage(long id, int backlog, String host) {
this.id = id;
@ -53,13 +55,24 @@ public class gestionnaireClavardage implements Runnable{
while (true) {
Socket ssocket = this.requestServer.accept();
System.out.print("Données reçues sur le serveur de " + this.id + "\n");
TCPClient client = new TCPClient(ssocket);
long idClient = Long.parseLong(client.receive());
TCPServerThread client = new TCPServerThread(ssocket,false);
long idClient = -1;
try {
idClient = Long.parseLong(client.receive());
}
catch (IOException e) {
client.stop();
}
Thread t = new Thread(client);
t.start();
int i = 0;
while (i<this.sessions.size() && this.sessions.get(i).getIdDestination() != idClient) {
while (idClient>0 && i<this.sessions.size() && this.sessions.get(i).getIdDestination() != idClient) {
i+=1;
}
if (i >= this.sessions.size()) {
if (idClient == -1);
else if (i >= this.sessions.size()) {
//Il n'existe pas encore de session entre les deux utilisateurs
System.out.print("Nouvelle session sur le serveur de " + this.id + "\n");
TCPClient clientEnvoi = new TCPClient(client.getAdresseCible(), PORT_REQUETE_NOUVELLE_SESSION + (int) idClient);

View file

@ -6,21 +6,33 @@ public class sessionClavardage {
private long idDestination = 0;
private TCPClient clientEmission = null;
private TCPClient clientReception = null;
private String etat = null;
public sessionClavardage(long idSource, long idDestination, TCPClient clientEmission, TCPClient clientReception) {
this.idSource = idSource;
this.idDestination = idDestination;
this.clientEmission = clientEmission;
this.clientReception = clientReception;
this.etat = "ReceptionConfig";
}
public sessionClavardage(long idSource, long idDestination, TCPClient clientReception) {
this.idSource = idSource;
this.idDestination = idDestination;
this.clientReception = clientReception;
this.etat = "Config";
}
public void send(String message) {
this.clientEmission.send(message);
}
public void stop() {
if (this.clientEmission != null) this.clientEmission.stop();
if (this.clientReception != null) this.clientReception.stop();
}
public String toString() {
return "Session entre " + this.idSource + " à l'adresse " + this.clientEmission.getAdresseSource()
+ ", et " + this.idDestination + " à l'adresse " + this.clientEmission.getAdresseCible() + "\n";
}
public long getIdSource() {
@ -39,16 +51,6 @@ public class sessionClavardage {
this.clientReception = client;
}
public void stop() {
if (this.clientEmission != null) this.clientEmission.stop();
if (this.clientReception != null) this.clientReception.stop();
}
public String toString() {
return "Session entre " + this.idSource + " à l'adresse " + this.clientEmission.getAdresseSource()
+ ", et " + this.idDestination + " à l'adresse " + this.clientEmission.getAdresseCible() + "\n";
}
//public void run() {
//}

View file

@ -1,5 +1,7 @@
package clavardage;
import java.util.ArrayList;
public class test {
public test() {
@ -7,22 +9,38 @@ public class test {
}
public static void main(String[] args) {
gestionnaireClavardage gc1 = new gestionnaireClavardage(14, 5, "localhost");
gestionnaireClavardage gc2 = new gestionnaireClavardage(24, 5, "localhost");
gc2.createSession(14);
ArrayList<gestionnaireClavardage> l = new ArrayList<gestionnaireClavardage>();
int i;
for (i = 0; i < 15; i++) {
gestionnaireClavardage gc = new gestionnaireClavardage(i * 10 + 4, 5, "localhost");
l.add(gc);
if (i>=1) gc.createSession(4);
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.print("Nb de sessions de gc1: " + gc1.getSessions().size() + "\n");
gc1.printSessions();
System.out.print("Nb de sessions de gc2: " + gc2.getSessions().size() + "\n");
gc2.printSessions();
gc1.fermerSessions();
gc2.fermerSessions();
System.out.print("Nb de sessions de gc0: " + l.get(0).getSessions().size() + "\n");
l.get(0).printSessions();
System.out.print("Nb de sessions de gc2: " + l.get(1).getSessions().size() + "\n");
l.get(1).printSessions();
for (i=0; i<l.get(0).getSessions().size();i++) {
sessionClavardage session = l.get(0).getSessions().get(i);
String message = "Message de " + session.getIdSource() +" vers " + session.getIdDestination() +".\n";
session.send(message);
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (i = 1; i<l.size();i++) l.get(i).fermerSessions();
}
}
}

View file

@ -71,14 +71,10 @@ public class TCPClient {
this.output.flush();
}
public String receive() {
public String receive() throws IOException{
String message = null;
try {
message = input.readLine();
}
catch (IOException e) {
System.out.print("Error while reading buffer");
}
message = input.readLine();
return message;
}
@ -87,7 +83,7 @@ public class TCPClient {
this.socket.close();
}
catch (IOException e) {
System.out.print("Error while closing connection");
System.out.print("Error while closing connection\n");
}
@ -116,7 +112,7 @@ public class TCPClient {
return this.adresseCible.getHostAddress();
}
public static void main(String[] args) {
public static void main(String[] args) throws IOException{
TCPClient client = new TCPClient("localhost", 1999);
String message = "Bonjour.\n";
System.out.printf("Message envoyé: %s", message);

View file

@ -45,7 +45,7 @@ public class TCPServer {
}
public void createThread(Socket ssocket) {
new TCPServerThread(ssocket);
new TCPServerThread(ssocket, true);
this.clients.add(ssocket);
}

View file

@ -10,16 +10,49 @@ import java.net.UnknownHostException;
public class TCPServerThread extends TCPClient implements Runnable{
Thread thread;
boolean terminé = false;
public TCPServerThread(Socket socket) {
public TCPServerThread(Socket socket, boolean start) {
super(socket);
thread = new Thread(this);
thread.start();
if (start) {
thread = new Thread(this);
thread.start();
}
}
public TCPServerThread(String host, int port, boolean start) {
super(host,port);
if (start) {
thread = new Thread(this);
thread.start();
}
}
public void stop() {
this.terminé = true;
try {
this.socket.close();
}
catch (IOException e) {
System.out.print("Error while closing connection\n");
}
}
public void run() {
String message = this.receive();
this.send(message);
while (!terminé) {
try {
String message = this.receive();
System.out.print("Status : " + terminé + " " + message + "\n");
}
catch (IOException e) {
this.terminé = true;
System.out.print("Connection sur le port " + this.port + " interrompue par " + this.adresseCible +"\n");
}
catch (Exception e) {
this.terminé = true;
System.out.print(e);
}
}
this.stop();
}
}