Merge branch 'master' of https://git.etud.insa-toulouse.fr/benassai/Projet_POO.git
This commit is contained in:
commit
4bfd657e6c
5 changed files with 105 additions and 13 deletions
|
@ -1,4 +1,5 @@
|
|||
package clavardage;
|
||||
import java.awt.EventQueue;
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.ServerSocket;
|
||||
|
@ -7,18 +8,27 @@ import java.net.SocketAddress;
|
|||
import java.util.ArrayList;
|
||||
|
||||
import reseau.*;
|
||||
import ui.DiscussionUI;
|
||||
|
||||
public class gestionnaireClavardage implements Runnable{
|
||||
public static final int PORT_REQUETE_NOUVELLE_SESSION = 19999;
|
||||
private static gestionnaireClavardage uniqueInstance = null;
|
||||
private ArrayList<sessionClavardage> sessions = new ArrayList<sessionClavardage>();
|
||||
private TCPServer requestServer;
|
||||
private long id;
|
||||
private String host;
|
||||
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
|
||||
//Remplacer par un singleton
|
||||
public gestionnaireClavardage(long id, int backlog, String host) {
|
||||
private gestionnaireClavardage(long id, int backlog, String host) {
|
||||
this.id = id;
|
||||
this.host = host;
|
||||
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);
|
||||
client.send(Long.toString(this.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() {
|
||||
|
@ -80,6 +102,18 @@ public class gestionnaireClavardage implements Runnable{
|
|||
this.sessions.add(session);
|
||||
clientEnvoi.send(Long.toString(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) {
|
||||
System.out.print("Suite de la configuration sur le serveur de " + this.id + "\n");
|
||||
|
|
|
@ -1,14 +1,17 @@
|
|||
package clavardage;
|
||||
|
||||
import java.awt.EventQueue;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import ui.DiscussionUI;
|
||||
|
||||
public class test {
|
||||
|
||||
public test() {
|
||||
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
/*
|
||||
public static void main2(String[] args) {
|
||||
ArrayList<gestionnaireClavardage> l = new ArrayList<gestionnaireClavardage>();
|
||||
int 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();
|
||||
|
||||
}
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
||||
}
|
||||
gestionnaireClavardage gc = gestionnaireClavardage.instance(11, 5, "localhost");
|
||||
gc.createSession(10);
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
package reseau;
|
||||
import java.net.*;
|
||||
import java.beans.PropertyChangeListener;
|
||||
import java.beans.PropertyChangeSupport;
|
||||
import java.io.*;
|
||||
|
||||
public class TCPClient {
|
||||
|
@ -9,12 +11,14 @@ public class TCPClient {
|
|||
Socket socket = null;
|
||||
BufferedReader input = null;
|
||||
PrintWriter output = null;
|
||||
protected PropertyChangeSupport support;
|
||||
|
||||
public TCPClient() {
|
||||
this.port = 0;
|
||||
this.socket = null;
|
||||
this.input = null;
|
||||
this.output = null;
|
||||
support = new PropertyChangeSupport(this);
|
||||
|
||||
}
|
||||
|
||||
|
@ -43,6 +47,7 @@ public class TCPClient {
|
|||
catch (IOException e) {
|
||||
System.out.print("Error while reading output stream\n");
|
||||
}
|
||||
support = new PropertyChangeSupport(this);
|
||||
|
||||
}
|
||||
|
||||
|
@ -63,6 +68,7 @@ public class TCPClient {
|
|||
catch (IOException e) {
|
||||
System.out.print("Error while reading output stream");
|
||||
}
|
||||
support = new PropertyChangeSupport(this);
|
||||
}
|
||||
|
||||
public void send(String message) {
|
||||
|
@ -112,6 +118,14 @@ public class TCPClient {
|
|||
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{
|
||||
TCPClient client = new TCPClient("localhost", 1999);
|
||||
String message = "Bonjour.\n";
|
||||
|
|
|
@ -1,16 +1,22 @@
|
|||
package reseau;
|
||||
|
||||
import java.beans.PropertyChangeListener;
|
||||
import java.beans.PropertyChangeSupport;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.PrintWriter;
|
||||
import java.net.Socket;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class TCPServerThread extends TCPClient implements Runnable{
|
||||
|
||||
Thread thread;
|
||||
boolean terminé = false;
|
||||
List<String> messages = new ArrayList<String>();
|
||||
|
||||
|
||||
public TCPServerThread(Socket socket, boolean start) {
|
||||
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() {
|
||||
while (!terminé) {
|
||||
try {
|
||||
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) {
|
||||
this.terminé = true;
|
||||
|
|
|
@ -7,22 +7,28 @@ import javax.swing.JFrame;
|
|||
import javax.swing.JPanel;
|
||||
import javax.swing.border.EmptyBorder;
|
||||
import javax.swing.JScrollPane;
|
||||
import javax.swing.JTextArea;
|
||||
import javax.swing.JTextField;
|
||||
import javax.swing.JButton;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.beans.PropertyChangeEvent;
|
||||
import java.beans.PropertyChangeListener;
|
||||
import java.util.List;
|
||||
import java.awt.event.ActionEvent;
|
||||
import javax.swing.JScrollBar;
|
||||
import clavardage.*;
|
||||
|
||||
public class DiscussionUI extends JFrame {
|
||||
public class DiscussionUI extends JFrame implements PropertyChangeListener{
|
||||
|
||||
private JPanel contentPane;
|
||||
private JTextField textField;
|
||||
private JTextField historicField;
|
||||
private JTextArea historicField;
|
||||
private sessionClavardage session;
|
||||
|
||||
/**
|
||||
* Launch the application.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
/*public static void main(String[] args) {
|
||||
EventQueue.invokeLater(new Runnable() {
|
||||
public void run() {
|
||||
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.
|
||||
*/
|
||||
public DiscussionUI() {
|
||||
public DiscussionUI(sessionClavardage session) {
|
||||
this.session = session;
|
||||
this.session.getClientReception().addPropertyChangeListener(this);;
|
||||
|
||||
setTitle("Discussion");
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
setBounds(100, 100, 450, 300);
|
||||
|
@ -51,13 +68,11 @@ public class DiscussionUI extends JFrame {
|
|||
scrollPane.setBounds(21, 11, 403, 164);
|
||||
contentPane.add(scrollPane);
|
||||
|
||||
historicField = new JTextField();
|
||||
historicField = new JTextArea();
|
||||
historicField.setEditable(false);
|
||||
scrollPane.setViewportView(historicField);
|
||||
historicField.setColumns(10);
|
||||
|
||||
JScrollBar scrollBar = new JScrollBar();
|
||||
scrollPane.setRowHeaderView(scrollBar);
|
||||
|
||||
textField = new JTextField();
|
||||
textField.setBounds(21, 197, 301, 42);
|
||||
|
@ -67,6 +82,10 @@ public class DiscussionUI extends JFrame {
|
|||
JButton sendButton = new JButton("Send");
|
||||
sendButton.addActionListener(new ActionListener() {
|
||||
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);
|
||||
|
|
Loading…
Reference in a new issue