From 735ae6cfb3fb2ba0bb4fdab9e8e6cbafe7e18fb4 Mon Sep 17 00:00:00 2001 From: benassai Date: Wed, 2 Dec 2020 10:53:08 +0100 Subject: [PATCH] =?UTF-8?q?Mise=20en=20place=20de=20la=20cr=C3=A9ation=20d?= =?UTF-8?q?es=20sessions=20(en=20local)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../clavardage/gestionnaireClavardage.java | 65 ++++++++++++++++--- .../src/clavardage/sessionClavardage.java | 16 ++++- Projet_POO/src/clavardage/test.java | 28 ++++++++ Projet_POO/src/reseau/TCPClient.java | 33 ++++++---- 4 files changed, 118 insertions(+), 24 deletions(-) create mode 100644 Projet_POO/src/clavardage/test.java diff --git a/Projet_POO/src/clavardage/gestionnaireClavardage.java b/Projet_POO/src/clavardage/gestionnaireClavardage.java index 288adf2..bad1b33 100644 --- a/Projet_POO/src/clavardage/gestionnaireClavardage.java +++ b/Projet_POO/src/clavardage/gestionnaireClavardage.java @@ -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 sessions = new ArrayList(); 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 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) { - //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()) { + //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(); } diff --git a/Projet_POO/src/clavardage/sessionClavardage.java b/Projet_POO/src/clavardage/sessionClavardage.java index 1824960..a8ec0e9 100644 --- a/Projet_POO/src/clavardage/sessionClavardage.java +++ b/Projet_POO/src/clavardage/sessionClavardage.java @@ -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() { + + //} } diff --git a/Projet_POO/src/clavardage/test.java b/Projet_POO/src/clavardage/test.java new file mode 100644 index 0000000..36c2129 --- /dev/null +++ b/Projet_POO/src/clavardage/test.java @@ -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(); + + } + +} diff --git a/Projet_POO/src/reseau/TCPClient.java b/Projet_POO/src/reseau/TCPClient.java index 7369ec0..b5803a8 100644 --- a/Projet_POO/src/reseau/TCPClient.java +++ b/Projet_POO/src/reseau/TCPClient.java @@ -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) {