code.
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
import java.io.Serializable;
import java.util.Calendar;
+import java.text.SimpleDateFormat;
public class Message implements Serializable {
// L'emeteur du message
// Les méthodes
- Message(String name, String msg) {
+ Message(String name, String msg, Calendar c) {
emetteur = name;
texte = msg;
- heure = Calendar.getInstance();
+ heure = c;
}
/**
}
public String toString() {
- return "<" + emetteur + ":" + heure + "> " + texte;
+ SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
+ return "<" + emetteur + "|" + dateFormat.format(heure.getTime()) + "> " + texte;
}
}
// puis établie les chaînages de flot nécessaires
// pour l'envoi et la reception de messages
sock = new Socket(adresseIPServeur, portServeur);
- InputStream IStream = null;
- IStream = sock.getInputStream();
- InputStreamReader IMesg = new InputStreamReader(IStream);
- lecture = new BufferedReader(IMesg);
- oLecture = new ObjectInputStream(IStream);
- OutputStream OStream = null;
- OStream = sock.getOutputStream();
+ OutputStream OStream = sock.getOutputStream();
ecriture = new PrintWriter(OStream);
oEcriture = new ObjectOutputStream(OStream);
+
+ InputStream IStream = sock.getInputStream();
+ InputStreamReader IMesg = new InputStreamReader(IStream);
+ lecture = new BufferedReader(IMesg);
+ oLecture = new ObjectInputStream(IStream);
}
private void attributesInit() {
import java.io.*;
+import java.util.Calendar;
+import java.util.Random;
public class ThreadClientoSend implements Runnable {
private SocketClient client;
+ //FIXME: use a random name by thread for now
+ // should setable
+ private String randName;
ThreadClientoSend(SocketClient c) {
client = c;
+ randName = randomName();
+ }
+
+ private String randomName() {
+
+ int leftLimit = 97; // letter 'a'
+ int rightLimit = 122; // letter 'z'
+ int targetStringLength = 8;
+ Random random = new Random();
+ StringBuilder buffer = new StringBuilder(targetStringLength);
+ for (int i = 0; i < targetStringLength; i++) {
+ int randomLimitedInt = leftLimit + (int)(random.nextFloat() * (rightLimit - leftLimit + 1));
+ buffer.append((char)randomLimitedInt);
+ }
+ String generatedString = buffer.toString();
+
+ return generatedString;
}
public void run() {
if (line.equals(".")) {
end = true;
}
- Message oMsg = new Message("Name", line);
+ Message oMsg = new Message(randName, line, Calendar.getInstance());
client.sendoMsg(oMsg);
System.out.println(Thread.currentThread().getName() + " a envoye " + oMsg);
}
}
}
+ /**
+ * Be careful, this function must be thread-safe
+ * @param clientSocket [description]
+ * @param sharedList [description]
+ * @throws IOException [description]
+ */
public void doService(Socket clientSocket, ArrayList<PrintWriter> sharedList) throws IOException {
BufferedReader in;
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String theLine = in.readLine();
if (theLine.equals(".")) {
end = true; // le thread de service doit terminer
- break; // do not broadcast the dot that will close clients threads
+ break; // do not broadcast the dot that will close all clients threads
}
broadcastMsg(theLine);
}
--- /dev/null
+import java.net.*;
+import java.io.*;
+import java.util.*;
+
+public class BroadcastoThreadService implements Runnable {
+
+ private Socket clientSocket;
+ private static ArrayList<ObjectOutputStream> listoWriter;
+
+ BroadcastoThreadService(Socket clientSocket) {
+ System.out.println("Creation d'un thread pour repondre a un client, port "
+ + clientSocket.getPort());
+ this.clientSocket = clientSocket;
+ if (listoWriter == null)
+ listoWriter = new ArrayList<ObjectOutputStream>();
+ }
+
+ public void run() {
+ try {
+ doService(clientSocket, listoWriter);
+ clientSocket.close();
+ } catch (IOException e) {
+ System.err.println("IOException : " + e);
+ e.printStackTrace();
+ }
+ catch (ClassNotFoundException e) {
+ System.err.println("ClassNotFoundException: " + e);
+ e.printStackTrace();
+ }
+ finally {
+ try {
+ if (this.clientSocket != null)
+ this.clientSocket.close();
+ } catch (IOException e) {
+ System.err.println("IOException : " + e);
+ e.printStackTrace();
+ }
+ }
+ }
+
+ public void doService(Socket clientSocket, ArrayList<ObjectOutputStream> sharedList) throws IOException, ClassNotFoundException {
+ ObjectInputStream OReader = new ObjectInputStream(clientSocket.getInputStream());
+ ObjectOutputStream OWriter = new ObjectOutputStream(clientSocket.getOutputStream());
+ sharedList.add(OWriter);
+ boolean end = false;
+ while (!end) {
+ Message roMsg = (Message)OReader.readObject();
+ if (roMsg.getTexte().equals(".")) {
+ end = true; // le thread de service doit terminer
+ break; // do not broadcast the dot that will close all clients threads
+ }
+ broadcastoMsg(roMsg);
+ }
+ sharedList.remove(OWriter);
+ if (OReader != null)
+ OReader.close();
+ if (OWriter != null)
+ OWriter.close();
+ System.out.println("Fin du thread repondant au client, port "
+ + clientSocket.getPort());
+ }
+
+ private void broadcastoMsg(Message oMsg) throws IOException, ClassNotFoundException {
+ ListIterator<ObjectOutputStream> iter = listoWriter.listIterator();
+ while (iter.hasNext()) {
+ ObjectOutputStream cursorOOS = iter.next();
+ cursorOOS.writeObject(oMsg);
+ cursorOOS.flush();
+ }
+ }
+
+}
while (true) { // le dispatcher est le thread qui execute main()
Socket clientSocket = listenSocket.accept();
System.out.println("Connexion de :" + clientSocket.getInetAddress());
- Thread serviceThread = new Thread(new BroadcastThreadService(clientSocket));
+ //Thread serviceThread = new Thread(new BroadcastThreadService(clientSocket));
+ Thread serviceThread = new Thread(new BroadcastoThreadService(clientSocket));
serviceThread.start();
}
}
CLASSES = \
Message.java \
BroadcastThreadService.java \
+ BroadcastoThreadService.java \
Main.java
#
--- /dev/null
+import java.io.Serializable;
+import java.util.Calendar;
+import java.text.SimpleDateFormat;
+
+public class Message implements Serializable {
+ // L'emeteur du message
+ private String emetteur ;
+ // Le contenu du message
+ private String texte ;
+ // Heure du message
+ private Calendar heure ;
+
+ // Les méthodes
+
+ Message(String name, String msg, Calendar c) {
+ emetteur = name;
+ texte = msg;
+ heure = c;
+ }
+
+ /**
+ * @param name the emetteur to set
+ */
+ public void setEmetteur(String name) {
+ emetteur = name;
+ }
+
+ /**
+ * @return the emetteur
+ */
+ public String getEmetteur() {
+ return emetteur;
+ }
+
+ /**
+ * @param texte the texte to set
+ */
+ public void setTexte(String texte) {
+ this.texte = texte;
+ }
+
+ /**
+ * @return the texte
+ */
+ public String getTexte() {
+ return texte;
+ }
+
+ /**
+ * @param heure the heure to set
+ */
+ public void setHeure(Calendar heure) {
+ this.heure = heure;
+ }
+
+ /**
+ * @return the heure
+ */
+ public Calendar getHeure() {
+ return heure;
+ }
+
+ public String toString() {
+ SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
+ return "<" + emetteur + "|" + dateFormat.format(heure.getTime()) + "> " + texte;
+ }
+}