Mise en place de la création des sessions (en local)

This commit is contained in:
Marino Benassai 2020-12-02 10:53:08 +01:00
parent 5d762d1632
commit 735ae6cfb3
4 changed files with 118 additions and 24 deletions

View file

@ -1,38 +1,83 @@
package clavardage;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;
import java.util.ArrayList;
import reseau.*;
public class gestionnaireClavardage implements Runnable{
public static final int PORT_REQUETE_NOUVELLE_SESSION = 19999;
private sessionClavardage[] sessions;
private ArrayList<sessionClavardage> sessions = new ArrayList<sessionClavardage>();
private TCPServer requestServer;
private long id;
private String host;
private Thread thread;
//Remplacer par un singleton
public gestionnaireClavardage(long id, int backlog, String host) {
this.id = id;
this.host = host;
this.requestServer = new TCPServer(this.host, backlog, PORT_REQUETE_NOUVELLE_SESSION + (int) id);
this.thread = new Thread(this);
this.thread.start();
}
public void createSession(long id) {
TCPClient client = new TCPClient("localhost", PORT_REQUETE_NOUVELLE_SESSION + (int) id);
sessionClavardage session = new sessionClavardage(this.id, id, client, null);
this.sessions.add(session);
client.send(Long.toString(this.id)+"\n");
System.out.print("Paramètres de session envoyée à " + id + "\n");
}
public ArrayList<sessionClavardage> getSessions() {
return this.sessions;
}
public void fermerSessions() {
for (sessionClavardage s : this.sessions) {
s.stop();
}
}
public void printSessions() {
for (sessionClavardage s : sessions) {
System.out.print(s);
}
}
public void run() {
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());
int i = 0;
while (i<this.sessions.length && this.sessions[i].getIdDestination() != idClient) i+=1;
if (i >= this.sessions.length) {
//Il n'existe pas encore de session entre les deux utilisateurs
TCPClient clientEnvoi = new TCPClient(client.getHost(), PORT_REQUETE_NOUVELLE_SESSION);
sessionClavardage session = new sessionClavardage(this.id, idClient, null, client);
while (i<this.sessions.size() && this.sessions.get(i).getIdDestination() != idClient) {
i+=1;
}
else if (this.sessions[i].getClientReception() == null) {
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);
sessionClavardage session = new sessionClavardage(this.id, idClient, clientEnvoi, client);
this.sessions.add(session);
clientEnvoi.send(Long.toString(id)+"\n");
System.out.print("Fin de la configuration de " + this.id + "\n");
}
else if (this.sessions.get(i).getClientReception() == null) {
System.out.print("Suite de la configuration sur le serveur de " + this.id + "\n");
//il existe une session entre les deux utilisateurs, mais la phase de configuration n'est pas terminée
this.sessions[i].setClientReception(client);
this.sessions.get(i).setClientReception(client);
System.out.print("Fin de la configuration de " + this.id + "\n");
}
else {
//Il existe déjà une connexion établie entre les deux machines
System.out.print("Tentative de session multiple sur le serveur de " + this.id + "\n");
client.stop();
}

View file

@ -1,7 +1,7 @@
package clavardage;
import reseau.*;
public class sessionClavardage implements Runnable{
public class sessionClavardage {
private long idSource = 0;
private long idDestination = 0;
private TCPClient clientEmission = null;
@ -39,7 +39,17 @@ public class sessionClavardage implements Runnable{
this.clientReception = client;
}
public void run() {
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

@ -0,0 +1,28 @@
package clavardage;
public class test {
public test() {
}
public static void main(String[] args) {
gestionnaireClavardage gc1 = new gestionnaireClavardage(14, 5, "localhost");
gestionnaireClavardage gc2 = new gestionnaireClavardage(24, 5, "localhost");
gc2.createSession(14);
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();
}
}

View file

@ -3,51 +3,54 @@ import java.net.*;
import java.io.*;
public class TCPClient {
String host = null;
InetAddress adresseCible = null;
InetAddress adresseSource = null;
int port = 0;
Socket socket = null;
BufferedReader input = null;
PrintWriter output = null;
public TCPClient() {
this.host = null;
this.port = 0;
this.socket = null;
this.input = null;
this.output = null;
}
public TCPClient(String host, int port) {
this.host = host;
this.port = port;
try {
this.socket = new Socket(host, port);
}
catch (UnknownHostException e) {
System.out.print("Could not find host");
System.out.print("Could not find host\n");
}
catch (IOException e) {
System.out.print("Could not create socket");
System.out.print("Could not create socket\n");
}
this.adresseCible = socket.getInetAddress();
this.adresseSource = socket.getLocalAddress();
try {
this.input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
}
catch (IOException e) {
System.out.print("Error while reading input stream");
System.out.print("Error while reading input stream\n");
}
try {
this.output = new PrintWriter(socket.getOutputStream(),true);
}
catch (IOException e) {
System.out.print("Error while reading output stream");
System.out.print("Error while reading output stream\n");
}
}
public TCPClient(Socket socket) {
this.host = socket.getInetAddress().getHostName();
this.port = socket.getPort();
this.socket = socket;
this.adresseCible = socket.getInetAddress();
this.adresseSource = socket.getLocalAddress();
try {
this.input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
}
@ -94,15 +97,23 @@ public class TCPClient {
this.stop();
this.port = port;
try {
this.socket = new Socket(host, port);
this.socket = new Socket(this.adresseCible.getHostName(), port);
}
catch (UnknownHostException e) {
System.out.print("Could not find host");
}
}
public String getHost() {
return this.host;
public String getHostSource() {
return this.adresseSource.getHostName();
}
public String getAdresseSource() {
return this.adresseSource.getHostAddress();
}
public String getAdresseCible() {
return this.adresseCible.getHostAddress();
}
public static void main(String[] args) {