première version de l'interface graphique pour l'échange de messages
entre deux sessions terminée.
This commit is contained in:
parent
208728205f
commit
329d8f4f3c
5 changed files with 105 additions and 13 deletions
|
@ -1,4 +1,5 @@
|
||||||
package clavardage;
|
package clavardage;
|
||||||
|
import java.awt.EventQueue;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
import java.net.ServerSocket;
|
import java.net.ServerSocket;
|
||||||
|
@ -7,18 +8,27 @@ import java.net.SocketAddress;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
import reseau.*;
|
import reseau.*;
|
||||||
|
import ui.DiscussionUI;
|
||||||
|
|
||||||
public class gestionnaireClavardage implements Runnable{
|
public class gestionnaireClavardage implements Runnable{
|
||||||
public static final int PORT_REQUETE_NOUVELLE_SESSION = 19999;
|
public static final int PORT_REQUETE_NOUVELLE_SESSION = 19999;
|
||||||
|
private static gestionnaireClavardage uniqueInstance = null;
|
||||||
private ArrayList<sessionClavardage> sessions = new ArrayList<sessionClavardage>();
|
private ArrayList<sessionClavardage> sessions = new ArrayList<sessionClavardage>();
|
||||||
private TCPServer requestServer;
|
private TCPServer requestServer;
|
||||||
private long id;
|
private long id;
|
||||||
private String host;
|
private String host;
|
||||||
private Thread thread;
|
private Thread thread;
|
||||||
|
|
||||||
|
public static gestionnaireClavardage instance(long id, int backlog, String host) {
|
||||||
|
if (uniqueInstance == null) {
|
||||||
|
uniqueInstance = new gestionnaireClavardage(id, backlog, host);
|
||||||
|
}
|
||||||
|
return gestionnaireClavardage.uniqueInstance;
|
||||||
|
}
|
||||||
|
|
||||||
//Ajouter la supppression des sessions de la liste après déconnexion
|
//Ajouter la supppression des sessions de la liste après déconnexion
|
||||||
//Remplacer par un singleton
|
//Remplacer par un singleton
|
||||||
public gestionnaireClavardage(long id, int backlog, String host) {
|
private gestionnaireClavardage(long id, int backlog, String host) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.host = host;
|
this.host = host;
|
||||||
this.requestServer = new TCPServer(this.host, backlog, PORT_REQUETE_NOUVELLE_SESSION + (int) id);
|
this.requestServer = new TCPServer(this.host, backlog, PORT_REQUETE_NOUVELLE_SESSION + (int) id);
|
||||||
|
@ -32,6 +42,18 @@ public class gestionnaireClavardage implements Runnable{
|
||||||
this.sessions.add(session);
|
this.sessions.add(session);
|
||||||
client.send(Long.toString(this.id)+"\n");
|
client.send(Long.toString(this.id)+"\n");
|
||||||
System.out.print("Paramètres de session envoyée à " + id + "\n");
|
System.out.print("Paramètres de session envoyée à " + id + "\n");
|
||||||
|
|
||||||
|
//Lancement de la fenêtre de session
|
||||||
|
EventQueue.invokeLater(new Runnable() {
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
DiscussionUI frame = new DiscussionUI(session);
|
||||||
|
frame.setVisible(true);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public ArrayList<sessionClavardage> getSessions() {
|
public ArrayList<sessionClavardage> getSessions() {
|
||||||
|
@ -80,6 +102,18 @@ public class gestionnaireClavardage implements Runnable{
|
||||||
this.sessions.add(session);
|
this.sessions.add(session);
|
||||||
clientEnvoi.send(Long.toString(id)+"\n");
|
clientEnvoi.send(Long.toString(id)+"\n");
|
||||||
System.out.print("Fin de la configuration de " + this.id + "\n");
|
System.out.print("Fin de la configuration de " + this.id + "\n");
|
||||||
|
|
||||||
|
//Lancement de la fenêtre de session
|
||||||
|
EventQueue.invokeLater(new Runnable() {
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
DiscussionUI frame = new DiscussionUI(session);
|
||||||
|
frame.setVisible(true);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
else if (this.sessions.get(i).getClientReception() == null) {
|
else if (this.sessions.get(i).getClientReception() == null) {
|
||||||
System.out.print("Suite de la configuration sur le serveur de " + this.id + "\n");
|
System.out.print("Suite de la configuration sur le serveur de " + this.id + "\n");
|
||||||
|
|
|
@ -1,14 +1,17 @@
|
||||||
package clavardage;
|
package clavardage;
|
||||||
|
|
||||||
|
import java.awt.EventQueue;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
import ui.DiscussionUI;
|
||||||
|
|
||||||
public class test {
|
public class test {
|
||||||
|
|
||||||
public test() {
|
public test() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
/*
|
||||||
public static void main(String[] args) {
|
public static void main2(String[] args) {
|
||||||
ArrayList<gestionnaireClavardage> l = new ArrayList<gestionnaireClavardage>();
|
ArrayList<gestionnaireClavardage> l = new ArrayList<gestionnaireClavardage>();
|
||||||
int i;
|
int i;
|
||||||
for (i = 0; i < 15; i++) {
|
for (i = 0; i < 15; i++) {
|
||||||
|
@ -41,6 +44,13 @@ public class test {
|
||||||
for (i = 1; i<l.size();i++) l.get(i).fermerSessions();
|
for (i = 1; i<l.size();i++) l.get(i).fermerSessions();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
gestionnaireClavardage gc = gestionnaireClavardage.instance(11, 5, "localhost");
|
||||||
|
gc.createSession(10);
|
||||||
|
|
||||||
}
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package reseau;
|
package reseau;
|
||||||
import java.net.*;
|
import java.net.*;
|
||||||
|
import java.beans.PropertyChangeListener;
|
||||||
|
import java.beans.PropertyChangeSupport;
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
|
|
||||||
public class TCPClient {
|
public class TCPClient {
|
||||||
|
@ -9,12 +11,14 @@ public class TCPClient {
|
||||||
Socket socket = null;
|
Socket socket = null;
|
||||||
BufferedReader input = null;
|
BufferedReader input = null;
|
||||||
PrintWriter output = null;
|
PrintWriter output = null;
|
||||||
|
protected PropertyChangeSupport support;
|
||||||
|
|
||||||
public TCPClient() {
|
public TCPClient() {
|
||||||
this.port = 0;
|
this.port = 0;
|
||||||
this.socket = null;
|
this.socket = null;
|
||||||
this.input = null;
|
this.input = null;
|
||||||
this.output = null;
|
this.output = null;
|
||||||
|
support = new PropertyChangeSupport(this);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,6 +47,7 @@ public class TCPClient {
|
||||||
catch (IOException e) {
|
catch (IOException e) {
|
||||||
System.out.print("Error while reading output stream\n");
|
System.out.print("Error while reading output stream\n");
|
||||||
}
|
}
|
||||||
|
support = new PropertyChangeSupport(this);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -63,6 +68,7 @@ public class TCPClient {
|
||||||
catch (IOException e) {
|
catch (IOException e) {
|
||||||
System.out.print("Error while reading output stream");
|
System.out.print("Error while reading output stream");
|
||||||
}
|
}
|
||||||
|
support = new PropertyChangeSupport(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void send(String message) {
|
public void send(String message) {
|
||||||
|
@ -112,6 +118,14 @@ public class TCPClient {
|
||||||
return this.adresseCible.getHostAddress();
|
return this.adresseCible.getHostAddress();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void addPropertyChangeListener(PropertyChangeListener pcl) {
|
||||||
|
support.addPropertyChangeListener(pcl);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void removePropertyChangeListener(PropertyChangeListener pcl) {
|
||||||
|
support.removePropertyChangeListener(pcl);
|
||||||
|
}
|
||||||
|
|
||||||
public static void main(String[] args) throws IOException{
|
public static void main(String[] args) throws IOException{
|
||||||
TCPClient client = new TCPClient("localhost", 1999);
|
TCPClient client = new TCPClient("localhost", 1999);
|
||||||
String message = "Bonjour.\n";
|
String message = "Bonjour.\n";
|
||||||
|
|
|
@ -1,16 +1,22 @@
|
||||||
package reseau;
|
package reseau;
|
||||||
|
|
||||||
|
import java.beans.PropertyChangeListener;
|
||||||
|
import java.beans.PropertyChangeSupport;
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.io.PrintWriter;
|
import java.io.PrintWriter;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class TCPServerThread extends TCPClient implements Runnable{
|
public class TCPServerThread extends TCPClient implements Runnable{
|
||||||
|
|
||||||
Thread thread;
|
Thread thread;
|
||||||
boolean terminé = false;
|
boolean terminé = false;
|
||||||
|
List<String> messages = new ArrayList<String>();
|
||||||
|
|
||||||
|
|
||||||
public TCPServerThread(Socket socket, boolean start) {
|
public TCPServerThread(Socket socket, boolean start) {
|
||||||
super(socket);
|
super(socket);
|
||||||
|
@ -38,11 +44,20 @@ public class TCPServerThread extends TCPClient implements Runnable{
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void setMessages(String message) {
|
||||||
|
List<String> newMessages = new ArrayList<String>(this.messages);
|
||||||
|
newMessages.add(message);
|
||||||
|
this.support.firePropertyChange("messages", this.messages, newMessages);
|
||||||
|
this.messages = newMessages;
|
||||||
|
}
|
||||||
|
|
||||||
public void run() {
|
public void run() {
|
||||||
while (!terminé) {
|
while (!terminé) {
|
||||||
try {
|
try {
|
||||||
String message = this.receive();
|
String message = this.receive();
|
||||||
System.out.print("Status : " + terminé + " " + message + "\n");
|
setMessages(message);
|
||||||
|
System.out.print("Status : " + terminé + ", message reçu : " + message + "\n");
|
||||||
}
|
}
|
||||||
catch (IOException e) {
|
catch (IOException e) {
|
||||||
this.terminé = true;
|
this.terminé = true;
|
||||||
|
|
|
@ -7,22 +7,28 @@ import javax.swing.JFrame;
|
||||||
import javax.swing.JPanel;
|
import javax.swing.JPanel;
|
||||||
import javax.swing.border.EmptyBorder;
|
import javax.swing.border.EmptyBorder;
|
||||||
import javax.swing.JScrollPane;
|
import javax.swing.JScrollPane;
|
||||||
|
import javax.swing.JTextArea;
|
||||||
import javax.swing.JTextField;
|
import javax.swing.JTextField;
|
||||||
import javax.swing.JButton;
|
import javax.swing.JButton;
|
||||||
import java.awt.event.ActionListener;
|
import java.awt.event.ActionListener;
|
||||||
|
import java.beans.PropertyChangeEvent;
|
||||||
|
import java.beans.PropertyChangeListener;
|
||||||
|
import java.util.List;
|
||||||
import java.awt.event.ActionEvent;
|
import java.awt.event.ActionEvent;
|
||||||
import javax.swing.JScrollBar;
|
import javax.swing.JScrollBar;
|
||||||
|
import clavardage.*;
|
||||||
|
|
||||||
public class DiscussionUI extends JFrame {
|
public class DiscussionUI extends JFrame implements PropertyChangeListener{
|
||||||
|
|
||||||
private JPanel contentPane;
|
private JPanel contentPane;
|
||||||
private JTextField textField;
|
private JTextField textField;
|
||||||
private JTextField historicField;
|
private JTextArea historicField;
|
||||||
|
private sessionClavardage session;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Launch the application.
|
* Launch the application.
|
||||||
*/
|
*/
|
||||||
public static void main(String[] args) {
|
/*public static void main(String[] args) {
|
||||||
EventQueue.invokeLater(new Runnable() {
|
EventQueue.invokeLater(new Runnable() {
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
|
@ -34,11 +40,22 @@ public class DiscussionUI extends JFrame {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
public void propertyChange(PropertyChangeEvent evt) {
|
||||||
|
List<String> list = ((List<String>) evt.getNewValue());
|
||||||
|
historicField.setText(historicField.getText() + (list.get(list.size()-1)) + "\n");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create the frame.
|
* Create the frame.
|
||||||
*/
|
*/
|
||||||
public DiscussionUI() {
|
public DiscussionUI(sessionClavardage session) {
|
||||||
|
this.session = session;
|
||||||
|
this.session.getClientReception().addPropertyChangeListener(this);;
|
||||||
|
|
||||||
setTitle("Discussion");
|
setTitle("Discussion");
|
||||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
setBounds(100, 100, 450, 300);
|
setBounds(100, 100, 450, 300);
|
||||||
|
@ -51,13 +68,11 @@ public class DiscussionUI extends JFrame {
|
||||||
scrollPane.setBounds(21, 11, 403, 164);
|
scrollPane.setBounds(21, 11, 403, 164);
|
||||||
contentPane.add(scrollPane);
|
contentPane.add(scrollPane);
|
||||||
|
|
||||||
historicField = new JTextField();
|
historicField = new JTextArea();
|
||||||
historicField.setEditable(false);
|
historicField.setEditable(false);
|
||||||
scrollPane.setViewportView(historicField);
|
scrollPane.setViewportView(historicField);
|
||||||
historicField.setColumns(10);
|
historicField.setColumns(10);
|
||||||
|
|
||||||
JScrollBar scrollBar = new JScrollBar();
|
|
||||||
scrollPane.setRowHeaderView(scrollBar);
|
|
||||||
|
|
||||||
textField = new JTextField();
|
textField = new JTextField();
|
||||||
textField.setBounds(21, 197, 301, 42);
|
textField.setBounds(21, 197, 301, 42);
|
||||||
|
@ -67,6 +82,10 @@ public class DiscussionUI extends JFrame {
|
||||||
JButton sendButton = new JButton("Send");
|
JButton sendButton = new JButton("Send");
|
||||||
sendButton.addActionListener(new ActionListener() {
|
sendButton.addActionListener(new ActionListener() {
|
||||||
public void actionPerformed(ActionEvent arg0) {
|
public void actionPerformed(ActionEvent arg0) {
|
||||||
|
String message = textField.getText();
|
||||||
|
textField.setText("");
|
||||||
|
historicField.setText(historicField.getText() + session.getIdSource() +": " + message + "\n");
|
||||||
|
session.send(session.getIdSource() + ": " + message+"\n");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
sendButton.setBounds(335, 207, 89, 23);
|
sendButton.setBounds(335, 207, 89, 23);
|
||||||
|
|
Loading…
Reference in a new issue