diff --git a/Readme.md b/Readme.md index 2fbbc381f5a6e091ba1d447ba3cb9f49da12e697..e711a227ec5539020cc656364c89ee0ae40e1dfb 100644 --- a/Readme.md +++ b/Readme.md @@ -11,7 +11,9 @@ retournés par le côté serveur. <valeur> ::= <char> | <chaine> | [0-9]* | [0-9]* "." [0-9]* <chaine> ::= "<char>*" <char> ::= un charactère alphanumérique -<reponse> ::= "resultat:" <type> "," <valeur> + +<exception> ::= "<char>*" | "none" +<reponse> ::= "resultat:" <type> "," <valeur> "|" "Exception:" <exception> ``` ## Exemples de ce qui devra être transmis @@ -20,4 +22,6 @@ Voici deux exemples de messages transmis par le côté client pour une invocation : + CALL:nombreMots:param[string,"Travail à la chaîne"] + EXEMPLE: "resultat:int,4|Exception:none" + + CALL:compteChar:param[string,"arrête ton char"]:param[char,"a"] diff --git a/src/fr/ulille/iut/m4102/AccesService.java b/src/fr/ulille/iut/m4102/AccesService.java index bfb1c19963ad2339267142b350c27c3167f2ef59..8041a6c2d9173fdf0d2e48d5a7ccd27855b91dbe 100644 --- a/src/fr/ulille/iut/m4102/AccesService.java +++ b/src/fr/ulille/iut/m4102/AccesService.java @@ -1,5 +1,11 @@ package fr.ulille.iut.m4102; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.PrintWriter; +import java.net.ServerSocket; +import java.net.Socket; import java.util.Arrays; /** @@ -11,18 +17,25 @@ import java.util.Arrays; * renverra le résultat sous forme de chaîne * de caractères. */ -public class AccesService { +public class AccesService implements Runnable { private AlaChaine alc; - - public AccesService() { + private Socket s; + + public AccesService() throws IOException { + alc = new AlaChaine(); + } + public AccesService(Socket sock) { alc = new AlaChaine(); + s = sock; } - public String traiteInvocation(String invocation) { + public String traiteInvocation(String invocation) { + System.out.println("REQUEST: " + invocation); String[] invocDivisee = invocation.replace("param[","").replace("]","").split(":"); String nomMethode = invocDivisee[1]; String[] paramTab = Arrays.copyOfRange(invocDivisee, 2, invocDivisee.length); + String resultatFormate = "resultat:"; switch(nomMethode) { @@ -57,8 +70,31 @@ public class AccesService { break; } + System.out.println("RETURN: " + resultatFormate); return resultatFormate; } + + + public void traiteRequete() throws IOException { + BufferedReader reception = new BufferedReader(new InputStreamReader(s.getInputStream())); + PrintWriter envoi = new PrintWriter(s.getOutputStream(), true); + String str = traiteInvocation(reception.readLine()); + System.out.println(str); + envoi.println(str); + reception.close(); + envoi.close(); + } + + public void run() { + PrintWriter envoi = null; + try { + envoi = new PrintWriter(s.getOutputStream(), true); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + envoi.println("Salut client"); + } } diff --git a/src/fr/ulille/iut/m4102/Client.java b/src/fr/ulille/iut/m4102/Client.java index f4016046af88ec739060d79e07bf1332cf83c55e..706e01b6529294848b0051492bce69b5f9ce9c7c 100644 --- a/src/fr/ulille/iut/m4102/Client.java +++ b/src/fr/ulille/iut/m4102/Client.java @@ -13,49 +13,50 @@ import java.net.UnknownHostException; * au serveur via la socket */ public class Client { - private Socket clientSocket = null; - private PrintWriter envoi = null; - private BufferedReader reception = null; - - // La classe doit être instanciée avec le nom - // ou l'adresse de la machine où se trouve le serveur - // ainsi que le numéro de port qu'il utilise. - public Client(String host, int port) { - try { - // Instanciation de la socket client - // S'il n'y a pas d'exception, la connexion est établie - clientSocket = new Socket(host, port); - } - catch (UnknownHostException e) { - e.printStackTrace(); - System.exit(1); - } - catch (IOException e) { - e.printStackTrace(); - System.exit(1); + private Socket clientSocket = null; + private PrintWriter envoi = null; + private BufferedReader reception = null; + + // La classe doit être instanciée avec le nom + // ou l'adresse de la machine où se trouve le serveur + // ainsi que le numéro de port qu'il utilise. + public Client(String host, int port) { + try { + // Instanciation de la socket client + // S'il n'y a pas d'exception, la connexion est établie + clientSocket = new Socket(host, port); + } catch (UnknownHostException e) { + e.printStackTrace(); + System.exit(1); + } catch (IOException e) { + e.printStackTrace(); + System.exit(1); + } + + try { + // Récupération d'un flux orienté texte pour écrire dans la socket + envoi = new PrintWriter(clientSocket.getOutputStream(), true); + + // Récupération d'un flux orienté texte pour lire dans la socket + reception = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); + } catch (IOException e) { + e.printStackTrace(); + System.exit(1); + } } - - try { - // Récupération d'un flux orienté texte pour écrire dans la socket - envoi = new PrintWriter(clientSocket.getOutputStream(), true); - - // Récupération d'un flux orienté texte pour lire dans la socket - reception = new BufferedReader( - new InputStreamReader(clientSocket.getInputStream())); - } catch (IOException e) { - e.printStackTrace(); - System.exit(1); + + public String envoyer(String message) { + envoi.println(message); + try { + return reception.readLine(); + } catch (IOException e) { + e.printStackTrace(); + } + return null; } - } - - public String envoyer(String message) { - envoi.println(message); - - try { - return reception.readLine(); - } catch (IOException e) { - e.printStackTrace(); + + public Socket getClientSocket() { + return clientSocket; } - return null; - } -} + +} \ No newline at end of file diff --git a/src/fr/ulille/iut/m4102/Intermediaire.java b/src/fr/ulille/iut/m4102/Intermediaire.java index bdcde113b00768df9abe215874226e91141f717f..1a740fec3ceff01c7c65362db0b58c577664d3b0 100644 --- a/src/fr/ulille/iut/m4102/Intermediaire.java +++ b/src/fr/ulille/iut/m4102/Intermediaire.java @@ -1,5 +1,7 @@ package fr.ulille.iut.m4102; +import java.io.IOException; + /** * Cette classe introduit un intermédiaire entre la classe utilisatrice * et l'implémentation du traitement des chaînes. @@ -10,23 +12,35 @@ package fr.ulille.iut.m4102; * appelait directement la classe AlaChaine. */ public class Intermediaire implements AlaChaineInterface { - private AlaChaine alc; + private AccesService aService; - public Intermediaire() { + private Client client; + public Intermediaire() throws IOException { aService = new AccesService(); - alc = new AlaChaine(); + //alc = new AlaChaine(); } public int nombreMots(String chaine) { /* System.out.println("Méthode: NombreMots ; Paramètre(String): "+chaine +" ; Resultat(int): "+alc.nombreMots(chaine)); return alc.nombreMots(chaine); */ + + client = new Client("localhost", 8080); String invocation = "Call:nombreMots:param[string," + chaine + "]"; String resInv = aService.traiteInvocation(invocation); String valeur = resInv.split(":")[1].split(",")[1]; + + String res = null; + try { + res = client.envoyer(resInv); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + System.out.println(res); - + return Integer.parseInt(valeur); @@ -35,11 +49,23 @@ public class Intermediaire implements AlaChaineInterface { public String asphyxie(String chaine) throws PasDAirException { /* System.out.println("Méthode: asphyxie ; Paramètre(String): "+chaine +" ; Resultat(String): "+alc.asphyxie(chaine)+" ; PasDAirException: Déjà asphyxiée"); return alc.asphyxie(chaine);*/ - + + client = new Client("localhost", 8080); + String invocation = "Call:asphyxie:param[string," + chaine + "]"; String resInv = aService.traiteInvocation(invocation); String valeur = resInv.split(":")[1].split(",")[1]; + + String res = null; + try { + res = client.envoyer(resInv); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + System.out.println(res); + return valeur; } @@ -47,22 +73,50 @@ public class Intermediaire implements AlaChaineInterface { public String leetSpeak(String chaine) { /* System.out.println("Méthode: leetSpeak ; Paramètre(String): "+chaine +" ; Resultat(String): "+alc.leetSpeak(chaine)); return alc.leetSpeak(chaine);*/ + + client = new Client("localhost", 8080); + String invocation = "Call:leetSpeak:param[string," + chaine + "]"; - String res = aService.traiteInvocation(invocation); - String valeur = res.split(":")[1].split(",")[1]; + String resInv = aService.traiteInvocation(invocation); + String valeur = resInv.split(":")[1].split(",")[1]; + + String res = null; + try { + res = client.envoyer(resInv); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + System.out.println(res); + + return valeur; } public int compteChar(String chaine, char c) { /* System.out.println("Méthode: compteChar ; Paramètre(String, char): "+chaine+", "+c+" ; Resultat(int): "+alc.compteChar(chaine, c)); return alc.compteChar(chaine, c);*/ + + client = new Client("localhost", 8080); + String invocation = "Call:compteChar:param[string, " + chaine + "]:param[char," + c + "]"; - String res = aService.traiteInvocation(invocation); - String valeur = res.split(":")[1].split(",")[1]; - - return Integer.parseInt(valeur); + String resInv = aService.traiteInvocation(invocation); + String valeur = resInv.split(":")[1].split(",")[1]; + + String res = null; + try { + res = client.envoyer(resInv); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + System.out.println(res); + + + return Integer.parseInt(valeur); + } - + } diff --git a/src/fr/ulille/iut/m4102/Serveur.java b/src/fr/ulille/iut/m4102/Serveur.java index 41825360e88c9fbf1e2530a5c01d9762f7c35ca2..db7b46eefb23e63966db27d7ad2ff6e64cfc3362 100644 --- a/src/fr/ulille/iut/m4102/Serveur.java +++ b/src/fr/ulille/iut/m4102/Serveur.java @@ -8,52 +8,57 @@ import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; + /** - * Cette classe démarre une socket serveur et - * attend les connexions des clients. - * Quand un client se connecte, elle délègue - * le travail à la classe AccesService. + * Cette classe démarre une socket serveur et attend les connexions des clients. + * Quand un client se connecte, elle délègue le travail à la classe + * AccesService. */ public class Serveur { - private ServerSocket serveurSocket = null; - - public Serveur(int port) { - try { - // Création de la Socket Serveur qui permettra d'attendre les connexions - serveurSocket = new ServerSocket(port); - } catch (IOException e) { - e.printStackTrace(); - System.exit(1); - } - } - - public void miseEnService() { - Socket unClient = null; + private ServerSocket serveurSocket = null; - // Boucle d'attente des clients - while (true ) { - try { - // accept() est bloquant. Quand on en sort, on a un nouveau - // client avec une nouvelle instance de socket - unClient = serveurSocket.accept(); - } catch (IOException e) { - e.printStackTrace(); - System.exit(1); - } - - // quand on a un client, on peut instancier la - // classe AccesService et lui demander de traiter - // la requête. - - //AccesService as = new AccesService(unClient); - //as.traiteRequete(); + public Serveur(int port) { + try { + // Création de la Socket Serveur qui permettra d'attendre les connexions + serveurSocket = new ServerSocket(port); + } catch (IOException e) { + //System.out.println(e.getMessage()); + e.printStackTrace(); + System.exit(1); + } } - } - - // La classe doit être exécutée en passant le port serveur à utiliser en paramètre - public static void main(String[] args) { - Serveur serveur = new Serveur (Integer.parseInt(args[0])); - - serveur.miseEnService(); - } -} + + public void miseEnService() { + Socket unClient = null; + + // Boucle d'attente des clients + while (true) { + try { + // accept() est bloquant. Quand on en sort, on a un nouveau + // client avec une nouvelle instance de socket + unClient = serveurSocket.accept(); + } catch (IOException e) { + e.printStackTrace(); + System.exit(1); + } + + // quand on a un client, on peut instancier la + // classe AccesService et lui demander de traiter + // la requête. + + AccesService as = new AccesService(unClient); + try { + as.traiteRequete(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + // La classe doit être exécutée en passant le port serveur à utiliser en + // paramètre + public static void main(String[] args) { + Serveur serv = new Serveur(Integer.parseInt(args[0])); + serv.miseEnService(); + } +} \ No newline at end of file diff --git a/src/fr/ulille/iut/m4102/Utilisatrice.java b/src/fr/ulille/iut/m4102/Utilisatrice.java index ebd29b4dec386f7b9ae8b39a11f2dad59f6b64de..22556420411c2f17fe26b32ec174579a7faa2e63 100644 --- a/src/fr/ulille/iut/m4102/Utilisatrice.java +++ b/src/fr/ulille/iut/m4102/Utilisatrice.java @@ -13,7 +13,7 @@ public class Utilisatrice { // utilisation normale. // On instancie la classe Intermediaire au lieu // d'instancier directement AlaChaine. - Intermediaire i = new Intermediaire(); + AlaChaineInterface i = new Intermediaire(); System.out.println(i.nombreMots("Travail à la chaîne"));