import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
+import java.util.Random;
/**
* Applications réparties
private JTextArea entrants;
private JTextField sortants;
private ArrayList<String> sendMessages;
+ //FIXME: use a random name by thread for now, should be setable
+ private String randName;
IHM() {
sendMessages = new ArrayList<String>();
+ 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 go() {
- JFrame cadre = new JFrame("Client de discussion");
+ JFrame cadre = new JFrame("Client de discussion de " + randName);
JPanel panneau = new JPanel();
entrants = new JTextArea(15, 30);
entrants.setLineWrap(true);
}
String mess = (String)sendMessages.remove(0);
System.out.println("IHM -> message a envoyer : " + mess);
- return mess;
+ return randName + "> " + mess;
}
public void writeMessage(String mess) {
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());
+ 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());
+ 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 :");
+ System.out.println("Demarrage du serveur sur le port " + Integer.parseInt(args[0]) + " et 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();