import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
-import java.io.IOException;
/**
* Applications réparties
private JTextArea entrants;
private JTextField sortants;
private ArrayList<String> sendMessages;
- private SocketClient socketCl;
IHM() {
sendMessages = new ArrayList<String>();
- socketCl = new SocketClient();
}
public void go() {
this.notify();
}
- synchronized public void getAndSendNextMessage() {
+ synchronized public String getNextMessageToSend() {
try {
if (sendMessages.isEmpty())
this.wait();
e.printStackTrace();
}
String mess = (String)sendMessages.remove(0);
- //System.out.println("IHM -> message a envoyer : " + mess);
- socketCl.sendMsg(mess);
+ System.out.println("IHM -> message a envoyer : " + mess);
+ return mess;
}
- public void writeMessage() throws IOException {
- String mess = socketCl.receiveMsg();
- //System.out.println("IHM -> message a ecrire : " + mess);
+ public void writeMessage(String mess) {
+ System.out.println("IHM -> message a ecrire : " + mess);
entrants.append(mess + "\n");
}
- public void close() {
- socketCl.closeRWIO();
- }
-
- public static void main (String[] args) {
- IHM client = new IHM();
- try {
- client.go();
- while (true) {
- client.getAndSendNextMessage();
- client.writeMessage();
- }
- }
- catch (IOException e) {
- System.err.println("IOException : " + e);
- e.printStackTrace();
- }
- finally {
- client.close();
- }
- }
-
} // fin classe SimpleClientDiscussion
--- /dev/null
+import java.io.*;
+
+
+public class Main {
+
+ public static void main(String[] args) {
+ SocketClient client = null;
+ Thread thS = null;
+ Thread thR = null;
+ IHM clientIHM = null;
+
+ try {
+ clientIHM = new IHM();
+ clientIHM.go();
+ client = new SocketClient();
+ thS = new Thread(new ThreadIHMSend(client, clientIHM));
+ thR = new Thread(new ThreadIHMReceive(client, clientIHM));
+ thS.setName("thS");
+ thS.start();
+ thR.setName("thR");
+ thR.start();
+ }
+ catch (Exception e) {
+ System.err.println("Exception : " + e);
+ e.printStackTrace();
+ }
+ finally {
+ try {
+ thS.join();
+ thR.join();
+ }
+ catch (InterruptedException e) {
+ System.err.println("InterruptedException: " + e);
+ e.printStackTrace();
+ }
+ client.closeRWIO();
+ }
+ }
+
+}
CLASSES = \
Message.java \
SocketClient.java \
- IHM.java
+ ThreadIHMReceive.java \
+ ThreadIHMSend.java \
+ IHM.java \
+ Main.java
#
# MAIN is a variable with the name of the file containing the main method
#
-MAIN = IHM
+MAIN = Main
#
# the default make target entry
--- /dev/null
+import java.io.*;
+
+public class ThreadIHMReceive implements Runnable {
+ private SocketClient client;
+ private IHM clientIHM;
+
+ ThreadIHMReceive(SocketClient c, IHM ui) {
+ client = c;
+ clientIHM = ui;
+ }
+
+ public void run() {
+ try {
+ boolean end = false;
+ //FIXME: not exiting properly randomly from that loop!
+ while (!end) {
+ String rline = client.receiveMsg();
+ if (rline.equals(".")) {
+ end = true;
+ }
+ clientIHM.writeMessage(rline);
+ System.out.println(Thread.currentThread().getName() + " a recu " + rline);
+ }
+ }
+ catch (IOException e) {
+ System.err.println("IOException: " + e);
+ e.printStackTrace();
+ }
+ }
+}
--- /dev/null
+import java.io.*;
+
+public class ThreadIHMSend implements Runnable {
+ private SocketClient client;
+ private IHM clientIHM;
+
+ ThreadIHMSend(SocketClient c, IHM ui) {
+ client = c;
+ clientIHM = ui;
+ }
+
+ public void run() {
+ BufferedReader userInput = null;
+ try {
+ boolean end = false;
+ while (!end) {
+ String line = clientIHM.getNextMessageToSend();
+ if (line.equals(".")) {
+ end = true;
+ }
+ client.sendMsg(line);
+ System.out.println(Thread.currentThread().getName() + " a envoye " + line);
+ }
+ }
+ catch (Exception e) {
+ System.err.println("Exception: " + e);
+ e.printStackTrace();
+ }
+ }
+}
end = true; // le thread de service doit terminer
break; // do not broadcast the dot that will close all clients threads
}
+ System.out.println("Broadcasting the message <" + theLine + "> received from " + clientSocket.toString());
broadcastMsg(theLine);
}
sharedList.remove(OWriter);
end = true; // le thread de service doit terminer
break; // do not broadcast the dot that will close all clients threads
}
+ System.out.println("Broadcasting the message <" + roMsg + "> received from " + clientSocket.toString());
broadcastoMsg(roMsg);
}
sharedList.remove(OWriter);
public static void main(String[] args) {
ServerSocket listenSocket = null;
try {
+ System.out.println("Demarrage du serveur sur le port " + Integer.parseInt(args[0]) + ", mise en attente de connexion :");
listenSocket = new ServerSocket(Integer.parseInt(args[0])); // port
while (true) { // le dispatcher est le thread qui execute main()
Socket clientSocket = listenSocket.accept();