This commit is contained in:
basti 2020-11-30 18:37:25 +01:00
commit 5d762d1632
5 changed files with 107 additions and 22 deletions

View file

@ -1,5 +1,42 @@
package clavardage;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
public class gestionnaireClavardage {
sessions =
import reseau.*;
public class gestionnaireClavardage implements Runnable{
public static final int PORT_REQUETE_NOUVELLE_SESSION = 19999;
private sessionClavardage[] sessions;
private TCPServer requestServer;
private long id;
public void run() {
while (true) {
Socket ssocket = this.requestServer.accept();
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);
}
else if (this.sessions[i].getClientReception() == null) {
//il existe une session entre les deux utilisateurs, mais la phase de configuration n'est pas terminée
this.sessions[i].setClientReception(client);
}
else {
//Il existe déjà une connexion établie entre les deux machines
client.stop();
}
}
}
}

View file

@ -1,10 +1,45 @@
package clavardage;
import reseau.*;
public class sessionClavardage {
public class sessionClavardage implements Runnable{
private long idSource = 0;
private long idDestination = 0;
private TCPClient clientEmission = null;
private TCPClient clientReception = null;
private String etat = null;
public sessionClavardage() {
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 long getIdSource() {
return this.idSource;
}
public long getIdDestination() {
return this.idDestination;
}
public TCPClient getClientReception() {
return this.clientReception;
}
public void setClientReception(TCPClient client) {
this.clientReception = client;
}
public void run() {
}
}

View file

@ -44,6 +44,24 @@ public class TCPClient {
}
public TCPClient(Socket socket) {
this.host = socket.getInetAddress().getHostName();
this.port = socket.getPort();
this.socket = socket;
try {
this.input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
}
catch (IOException e) {
System.out.print("Error while reading input stream");
}
try {
this.output = new PrintWriter(socket.getOutputStream(),true);
}
catch (IOException e) {
System.out.print("Error while reading output stream");
}
}
public void send(String message) {
//Le message doit se terminer par \n ?
this.output.print(message);
@ -83,6 +101,10 @@ public class TCPClient {
}
}
public String getHost() {
return this.host;
}
public static void main(String[] args) {
TCPClient client = new TCPClient("localhost", 1999);
String message = "Bonjour.\n";

View file

@ -33,7 +33,7 @@ public class TCPServer {
}
public void accept() {
public Socket accept() {
Socket ssocket = null;
try {
ssocket = this.socket.accept();
@ -41,6 +41,10 @@ public class TCPServer {
catch (IOException e) {
System.out.print("Erreur lors de la connexion avec un client");
}
return ssocket;
}
public void createThread(Socket ssocket) {
new TCPServerThread(ssocket);
this.clients.add(ssocket);
@ -52,7 +56,8 @@ public class TCPServer {
TCPServer server = new TCPServer("localhost", 5, 1999);
while(true) {
server.accept();
Socket ssocket = server.accept();
server.createThread(ssocket);
}
}

View file

@ -12,21 +12,7 @@ public class TCPServerThread extends TCPClient implements Runnable{
Thread thread;
public TCPServerThread(Socket socket) {
this.host = socket.getInetAddress().getHostName();
this.port = socket.getPort();
this.socket = socket;
try {
this.input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
}
catch (IOException e) {
System.out.print("Error while reading input stream");
}
try {
this.output = new PrintWriter(socket.getOutputStream(),true);
}
catch (IOException e) {
System.out.print("Error while reading output stream");
}
super(socket);
thread = new Thread(this);
thread.start();
}