Progrès sur les sessions
This commit is contained in:
parent
c4cb34c14d
commit
99dcf24958
6 changed files with 104 additions and 42 deletions
|
@ -1,4 +1,5 @@
|
||||||
package clavardage;
|
package clavardage;
|
||||||
|
import java.io.IOException;
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
import java.net.ServerSocket;
|
import java.net.ServerSocket;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
|
@ -15,6 +16,7 @@ public class gestionnaireClavardage implements Runnable{
|
||||||
private String host;
|
private String host;
|
||||||
private Thread thread;
|
private Thread thread;
|
||||||
|
|
||||||
|
//Ajouter la supppression des sessions de la liste après déconnexion
|
||||||
//Remplacer par un singleton
|
//Remplacer par un singleton
|
||||||
public gestionnaireClavardage(long id, int backlog, String host) {
|
public gestionnaireClavardage(long id, int backlog, String host) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
|
@ -53,13 +55,24 @@ public class gestionnaireClavardage implements Runnable{
|
||||||
while (true) {
|
while (true) {
|
||||||
Socket ssocket = this.requestServer.accept();
|
Socket ssocket = this.requestServer.accept();
|
||||||
System.out.print("Données reçues sur le serveur de " + this.id + "\n");
|
System.out.print("Données reçues sur le serveur de " + this.id + "\n");
|
||||||
TCPClient client = new TCPClient(ssocket);
|
TCPServerThread client = new TCPServerThread(ssocket,false);
|
||||||
long idClient = Long.parseLong(client.receive());
|
long idClient = -1;
|
||||||
|
|
||||||
|
try {
|
||||||
|
idClient = Long.parseLong(client.receive());
|
||||||
|
}
|
||||||
|
catch (IOException e) {
|
||||||
|
client.stop();
|
||||||
|
}
|
||||||
|
|
||||||
|
Thread t = new Thread(client);
|
||||||
|
t.start();
|
||||||
int i = 0;
|
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;
|
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
|
//Il n'existe pas encore de session entre les deux utilisateurs
|
||||||
System.out.print("Nouvelle session sur le serveur de " + this.id + "\n");
|
System.out.print("Nouvelle session sur le serveur de " + this.id + "\n");
|
||||||
TCPClient clientEnvoi = new TCPClient(client.getAdresseCible(), PORT_REQUETE_NOUVELLE_SESSION + (int) idClient);
|
TCPClient clientEnvoi = new TCPClient(client.getAdresseCible(), PORT_REQUETE_NOUVELLE_SESSION + (int) idClient);
|
||||||
|
|
|
@ -6,21 +6,33 @@ public class sessionClavardage {
|
||||||
private long idDestination = 0;
|
private long idDestination = 0;
|
||||||
private TCPClient clientEmission = null;
|
private TCPClient clientEmission = null;
|
||||||
private TCPClient clientReception = null;
|
private TCPClient clientReception = null;
|
||||||
private String etat = null;
|
|
||||||
|
|
||||||
public sessionClavardage(long idSource, long idDestination, TCPClient clientEmission, TCPClient clientReception) {
|
public sessionClavardage(long idSource, long idDestination, TCPClient clientEmission, TCPClient clientReception) {
|
||||||
this.idSource = idSource;
|
this.idSource = idSource;
|
||||||
this.idDestination = idDestination;
|
this.idDestination = idDestination;
|
||||||
this.clientEmission = clientEmission;
|
this.clientEmission = clientEmission;
|
||||||
this.clientReception = clientReception;
|
this.clientReception = clientReception;
|
||||||
this.etat = "ReceptionConfig";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public sessionClavardage(long idSource, long idDestination, TCPClient clientReception) {
|
public sessionClavardage(long idSource, long idDestination, TCPClient clientReception) {
|
||||||
this.idSource = idSource;
|
this.idSource = idSource;
|
||||||
this.idDestination = idDestination;
|
this.idDestination = idDestination;
|
||||||
this.clientReception = clientReception;
|
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() {
|
public long getIdSource() {
|
||||||
|
@ -39,16 +51,6 @@ public class sessionClavardage {
|
||||||
this.clientReception = client;
|
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() {
|
//public void run() {
|
||||||
|
|
||||||
//}
|
//}
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package clavardage;
|
package clavardage;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
public class test {
|
public class test {
|
||||||
|
|
||||||
public test() {
|
public test() {
|
||||||
|
@ -7,22 +9,38 @@ public class test {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
gestionnaireClavardage gc1 = new gestionnaireClavardage(14, 5, "localhost");
|
ArrayList<gestionnaireClavardage> l = new ArrayList<gestionnaireClavardage>();
|
||||||
gestionnaireClavardage gc2 = new gestionnaireClavardage(24, 5, "localhost");
|
int i;
|
||||||
gc2.createSession(14);
|
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 {
|
try {
|
||||||
Thread.sleep(100);
|
Thread.sleep(100);
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
// TODO Auto-generated catch block
|
// TODO Auto-generated catch block
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
System.out.print("Nb de sessions de gc1: " + gc1.getSessions().size() + "\n");
|
System.out.print("Nb de sessions de gc0: " + l.get(0).getSessions().size() + "\n");
|
||||||
gc1.printSessions();
|
l.get(0).printSessions();
|
||||||
System.out.print("Nb de sessions de gc2: " + gc2.getSessions().size() + "\n");
|
System.out.print("Nb de sessions de gc2: " + l.get(1).getSessions().size() + "\n");
|
||||||
gc2.printSessions();
|
l.get(1).printSessions();
|
||||||
gc1.fermerSessions();
|
for (i=0; i<l.get(0).getSessions().size();i++) {
|
||||||
gc2.fermerSessions();
|
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();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
|
@ -71,14 +71,10 @@ public class TCPClient {
|
||||||
this.output.flush();
|
this.output.flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
public String receive() {
|
public String receive() throws IOException{
|
||||||
String message = null;
|
String message = null;
|
||||||
try {
|
|
||||||
message = input.readLine();
|
message = input.readLine();
|
||||||
}
|
|
||||||
catch (IOException e) {
|
|
||||||
System.out.print("Error while reading buffer");
|
|
||||||
}
|
|
||||||
return message;
|
return message;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -87,7 +83,7 @@ public class TCPClient {
|
||||||
this.socket.close();
|
this.socket.close();
|
||||||
}
|
}
|
||||||
catch (IOException e) {
|
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();
|
return this.adresseCible.getHostAddress();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) throws IOException{
|
||||||
TCPClient client = new TCPClient("localhost", 1999);
|
TCPClient client = new TCPClient("localhost", 1999);
|
||||||
String message = "Bonjour.\n";
|
String message = "Bonjour.\n";
|
||||||
System.out.printf("Message envoyé: %s", message);
|
System.out.printf("Message envoyé: %s", message);
|
||||||
|
|
|
@ -45,7 +45,7 @@ public class TCPServer {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void createThread(Socket ssocket) {
|
public void createThread(Socket ssocket) {
|
||||||
new TCPServerThread(ssocket);
|
new TCPServerThread(ssocket, true);
|
||||||
this.clients.add(ssocket);
|
this.clients.add(ssocket);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,16 +10,49 @@ import java.net.UnknownHostException;
|
||||||
public class TCPServerThread extends TCPClient implements Runnable{
|
public class TCPServerThread extends TCPClient implements Runnable{
|
||||||
|
|
||||||
Thread thread;
|
Thread thread;
|
||||||
|
boolean terminé = false;
|
||||||
|
|
||||||
public TCPServerThread(Socket socket) {
|
public TCPServerThread(Socket socket, boolean start) {
|
||||||
super(socket);
|
super(socket);
|
||||||
|
if (start) {
|
||||||
thread = new Thread(this);
|
thread = new Thread(this);
|
||||||
thread.start();
|
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() {
|
public void run() {
|
||||||
|
while (!terminé) {
|
||||||
|
try {
|
||||||
String message = this.receive();
|
String message = this.receive();
|
||||||
this.send(message);
|
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();
|
this.stop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue