Skip to content
This repository was archived by the owner on Sep 10, 2018. It is now read-only.

Commit 91c813f

Browse files
committed
Ajout d'un système permettant l'arrêt du client lorsqu'il saisi 'exit' dans la console
1 parent 0314290 commit 91c813f

File tree

15 files changed

+370
-184
lines changed

15 files changed

+370
-184
lines changed

src/fr/imie/formations/client/Client.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
import java.net.UnknownHostException;
99

1010
public class Client {
11+
/**
12+
* Point d'entrée
13+
*
14+
* @param args
15+
*/
1116
public static void main( String[] args ) {
1217
Client client = new Client( "Toto" );
1318
}
@@ -31,7 +36,7 @@ public Client( final String pNom ) {
3136
// Réception de l'accusé du serveur
3237
System.out.println( in.readLine() );
3338

34-
Thread discution = new Thread( new DiscutionAvecServeur( socket, this ) );
39+
Thread discution = new Thread( new DiscutionAvecServeur( socket ) );
3540
discution.start();
3641
} catch ( UnknownHostException e ) {
3742
e.printStackTrace();

src/fr/imie/formations/client/DiscutionAvecServeur.java

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,37 @@
66
import java.io.PrintWriter;
77
import java.net.Socket;
88

9+
import fr.imie.formations.communications.EmissionServeur;
10+
import fr.imie.formations.communications.ReceptionServeur;
11+
12+
/**
13+
* Service de discution du client avec un serveur.
14+
*
15+
* @author takiguchi
16+
*
17+
*/
918
public class DiscutionAvecServeur implements Runnable {
10-
private Socket socket;
11-
private PrintWriter out;
12-
private BufferedReader in;
13-
private Client client;
14-
private Thread emission;
15-
private Thread reception;
19+
/** Socket entre le client et son serveur. */
20+
private Socket socket;
21+
/** Flux de réception des messages du serveur. */
22+
private PrintWriter out;
23+
/** Flux d'envoi de messages vers le serveur. */
24+
private BufferedReader in;
25+
/** Service d'émission de messages vers le serveur. */
26+
private ReceptionServeur reception;
27+
/** Thread de réception des messages du serveur. */
28+
private Thread threadReception;
29+
/** Thread d'émission de messages vers le serveur. */
30+
private Thread threadEmission;
1631

1732
/**
1833
* Constructeur
1934
*
2035
* @param pSocket
36+
* Socket entre le client et le serveur.
2137
*/
22-
public DiscutionAvecServeur( final Socket pSocket, final Client pClient ) {
38+
public DiscutionAvecServeur( final Socket pSocket ) {
2339
socket = pSocket;
24-
client = pClient;
2540
}
2641

2742
@Override
@@ -30,11 +45,12 @@ public void run() {
3045
out = new PrintWriter( socket.getOutputStream() );
3146
in = new BufferedReader( new InputStreamReader( socket.getInputStream() ) );
3247

33-
emission = new Thread( new Emission( client, out ) );
34-
emission.start();
48+
reception = new ReceptionServeur( in );
49+
threadReception = new Thread( reception );
50+
threadReception.start();
3551

36-
reception = new Thread( new Reception( in, emission ) );
37-
reception.start();
52+
threadEmission = new Thread( new EmissionServeur( out, reception ) );
53+
threadEmission.start();
3854
} catch ( final IOException e ) {
3955
e.printStackTrace();
4056
}

src/fr/imie/formations/client/Emission.java

Lines changed: 0 additions & 45 deletions
This file was deleted.

src/fr/imie/formations/client/Reception.java

Lines changed: 0 additions & 48 deletions
This file was deleted.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package fr.imie.formations.communications;
2+
3+
import java.io.PrintWriter;
4+
5+
public abstract class Emission implements Runnable {
6+
private PrintWriter out;
7+
private boolean running;
8+
private String message;
9+
10+
public Emission( final PrintWriter pOut ) {
11+
out = pOut;
12+
running = true;
13+
}
14+
15+
public boolean isRunning() {
16+
return running;
17+
}
18+
19+
public void stop() {
20+
running = false;
21+
}
22+
23+
public PrintWriter getOut() {
24+
return out;
25+
}
26+
27+
public String getMessage() {
28+
return message;
29+
}
30+
31+
public void setMessage( final String pMessage ) {
32+
message = pMessage;
33+
}
34+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package fr.imie.formations.communications;
2+
3+
import java.io.PrintWriter;
4+
5+
/**
6+
* Service d'émission de message à un client, pour un serveur.
7+
*
8+
* @author takiguchi
9+
*
10+
*/
11+
public class EmissionClient extends Emission {
12+
/**
13+
* Constructeur
14+
*
15+
* @param pOut
16+
*/
17+
public EmissionClient( PrintWriter pOut ) {
18+
super( pOut );
19+
setMessage( "Commande reçue" );
20+
}
21+
22+
@Override
23+
public void run() {
24+
getOut().println( getMessage() );
25+
getOut().flush();
26+
}
27+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package fr.imie.formations.communications;
2+
3+
import java.io.PrintWriter;
4+
import java.util.Scanner;
5+
6+
/**
7+
* Service d'émission de message à un serveur, pour un client.
8+
*
9+
* @author takiguchi
10+
*
11+
*/
12+
public class EmissionServeur extends Emission {
13+
/** Le scanner pour saisir des messages. */
14+
private Scanner scanner;
15+
/** Le service de réception des messages du serveur. */
16+
private ReceptionServeur reception;
17+
18+
/**
19+
* Constructeur
20+
*
21+
* @param pOut
22+
* Flux d'envoi de message vers le serveur.
23+
*/
24+
public EmissionServeur( final PrintWriter pOut, final ReceptionServeur pReception ) {
25+
super( pOut );
26+
scanner = new Scanner( System.in );
27+
reception = pReception;
28+
}
29+
30+
@Override
31+
public void run() {
32+
// String temp;
33+
while ( isRunning() ) {
34+
System.out.print( "Vous : " );
35+
// temp = scanner.next();
36+
setMessage( scanner.next() );
37+
if ( "exit".equals( getMessage().toLowerCase() ) ) {
38+
setMessage( "over" );
39+
stop();
40+
}
41+
getOut().println( getMessage() );
42+
getOut().flush();
43+
}
44+
45+
System.out.println( "Arrêt de la communication avec le serveur..." );
46+
}
47+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package fr.imie.formations.communications;
2+
3+
import java.io.BufferedReader;
4+
5+
// TODO: Auto-generated Javadoc
6+
/**
7+
* Service de réception d'un serveur ou d'un client.
8+
*
9+
* @author takiguchi
10+
*
11+
*/
12+
public abstract class Reception implements Runnable {
13+
/** Buffer pour recevoir les messages. */
14+
private BufferedReader in;
15+
/** Variable permettant d'arrêter le thread. */
16+
private boolean running;
17+
/** Message reçu par l'expéditeur. */
18+
private String messageRecu;
19+
20+
/**
21+
* Constructeur.
22+
*
23+
* @param pIn
24+
* Flux de réception de messages.
25+
*/
26+
public Reception( final BufferedReader pIn ) {
27+
in = pIn;
28+
running = true;
29+
}
30+
31+
/**
32+
* Checks if the thread is running.
33+
*
34+
* @return true, if it is running
35+
*/
36+
public boolean isRunning() {
37+
return running;
38+
}
39+
40+
/**
41+
* Stoppe le thread de réception de messages.
42+
*/
43+
public void stop() {
44+
running = false;
45+
}
46+
47+
/**
48+
* Gets the in.
49+
*
50+
* @return the in
51+
*/
52+
public BufferedReader getIn() {
53+
return in;
54+
}
55+
56+
/**
57+
* Gets the message recu.
58+
*
59+
* @return the message recu
60+
*/
61+
public String getMessageRecu() {
62+
return messageRecu;
63+
}
64+
65+
/**
66+
* Sets the message recu.
67+
*
68+
* @param messageRecu
69+
* the new message recu
70+
*/
71+
public void setMessageRecu( String messageRecu ) {
72+
this.messageRecu = messageRecu;
73+
}
74+
75+
}

0 commit comments

Comments
 (0)