From b9170dba0f135ab8e84d4a56d7f53a834d26215f Mon Sep 17 00:00:00 2001 From: rhabban Date: Sun, 5 Mar 2017 17:05:27 +0100 Subject: [PATCH 01/10] ClientsData updated --- src/client/ClientThread.java | 16 +++++++++------- src/client/ReceivingThread.java | 7 ++----- src/model/Message.java | 6 ++++++ 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/client/ClientThread.java b/src/client/ClientThread.java index 4126da1..48902d6 100644 --- a/src/client/ClientThread.java +++ b/src/client/ClientThread.java @@ -50,6 +50,9 @@ public synchronized void run() { } else { streamOut.writeObject(new Message( Message._NAME_, "Bonjour " + clientName + " et bienvenue dans le chat. Pour communiquer avec les utilisateurs, il est nécessaire de se positionner à leur portée", clientName, 0, 0, null)); } + ArrayList clients = getClients(); + + //streamOut.flush(); } @@ -86,10 +89,12 @@ public synchronized void run() { } public synchronized void sendMessages(ArrayList messages){ + ArrayList clients = getClients(); for(ClientThread thread : threads){ for(Message message : messages){ try { + message.clients = clients; /*MessageValidator val = new MessageValidator(this.clientData, thread.clientData); if(this == thread || val.isClientsNear() == true) thread.streamOut.writeObject(message); @@ -108,7 +113,6 @@ public synchronized void sendMessage(Message message){ messages.add(message); sendMessages(messages); - //TODO : Le message est sensé contenir la listes des clients lq cette méthode est appelée par refreshClientData System.out.println("ClientThread.sendMessage :" + messages); } @@ -117,14 +121,12 @@ public synchronized void refreshClientData(String name, int posX, int posY){ this.clientData.setX(posX); this.clientData.setY(posY); - System.out.println("ClientThread.refreshClientData :"+this.clientData); System.out.println(name+" position has been updated"); - + } + + public synchronized Message createClientsMessage(){ ArrayList clients = getClients(); - - System.out.println("ClientThread.refreshClientData getClients() :"+ clients); - - sendMessage(new Message(Message._CLIENTS_, "", "", 0, 0, clients)); + return (new Message(Message._CLIENTS_, clients)); } public ArrayList getClients(){ diff --git a/src/client/ReceivingThread.java b/src/client/ReceivingThread.java index 18ff0a8..0c7c312 100644 --- a/src/client/ReceivingThread.java +++ b/src/client/ReceivingThread.java @@ -32,12 +32,9 @@ public void run() if(msg.type == Message._NAME_){ this.client.setName(msg.clientName); } - if(msg.type == Message._CLIENTS_){ - this.client.setClientsData(msg.clients); - continue; - } + this.client.setClientsData(msg.clients); this.client.notifyObservers("<"+msg.clientName+">"+msg.text); - System.out.println("ReceivingThread.run thisClients : "+this.client.getClientsData()); + System.out.println("ReceivingThread.run Message :" + msg); } } catch (IOException | ClassNotFoundException e) { diff --git a/src/model/Message.java b/src/model/Message.java index f935cf7..2d875bb 100644 --- a/src/model/Message.java +++ b/src/model/Message.java @@ -15,6 +15,7 @@ public class Message implements Serializable { public final static int _CLIENTS_ = 2; public final static int _DISCONNECT_ = 3; public final static int _POSITION_ = 4; + public final static int _CONNECT_ = 5; public int type; public String text; @@ -33,6 +34,11 @@ public Message(int type, String text, String clientName, int posX, int posY, Arr this.posY = posY; this.clients = clients; } + + public Message(int type, ArrayList clients){ + this.type = type; + this.clients = clients; + } @Override public String toString() { From d11c2b9d839970c7a6cfff6b48efc9a7f3cda348 Mon Sep 17 00:00:00 2001 From: Raphael-Erfani Date: Sun, 5 Mar 2017 17:07:10 +0100 Subject: [PATCH 02/10] Interface update --- src/View/ClientUI.java | 13 +++++++++++-- src/View/GridView.java | 8 +++----- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/View/ClientUI.java b/src/View/ClientUI.java index 5603b15..1ccba5e 100644 --- a/src/View/ClientUI.java +++ b/src/View/ClientUI.java @@ -1,5 +1,6 @@ package View; +import java.util.ArrayList; import java.util.Observable; import java.util.Observer; @@ -49,7 +50,6 @@ public ClientUI(Client client) throws IOException { * @throws IOException */ private void createUI() throws IOException { - // TODO :: supprimer la mapView de la Frame à part et l'intégrer à ClientUI JFrame map = new MapView(this.client); ImagePanel mapPanel = (ImagePanel) map.getContentPane(); mapPanel.setBorder(BorderFactory.createLineBorder(Color.black)); @@ -66,10 +66,13 @@ private void createUI() throws IOException { add(box, BorderLayout.SOUTH); add(new JScrollPane(boxUsers), BorderLayout.EAST); inputTextField = new JTextField(); - for(int i=0; i<5;i++){ + + // TODO :: Afficher la liste des clients connectés dynamiquement + for (int i=0;i<5;i++) { JLabel userName = new JLabel("user"+i); boxUsers.add(userName); } + //JTextArea usersList = new JTextArea("users List"); sendButton = new JButton("Send"); sendButton.setBackground(new Color(59,89,182)); @@ -110,6 +113,12 @@ public void update(Observable o, Object arg) { public void run() { textArea.append(arg.toString()); textArea.append("\n"); + ArrayList clientsList = client.getClientsData(); + for (Client c : clientsList) { + JLabel userName = new JLabel(c.getName()); + boxUsers.add(userName); + } + System.out.println(clientsList); } }); } diff --git a/src/View/GridView.java b/src/View/GridView.java index dab0ba0..b4d8be4 100644 --- a/src/View/GridView.java +++ b/src/View/GridView.java @@ -33,8 +33,8 @@ public class GridView extends JPanel { /** Draw borders for the client scope */ public void paintComponent(Graphics g){ super.paintComponent(g); - g.drawOval(-200, -200, 400, 400); - //g.drawRect(0, -200, 400, 400); + //g.drawOval(-200, -200, 400, 400); + g.drawRect(-200, -200, 400, 400); //g.drawRoundRect(000, -200, 400, 400,100,100); // TODO :: La portée est affichée en brut ici, à changer dynamiquement ! @@ -43,8 +43,6 @@ public void paintComponent(Graphics g){ public GridView(MapManager mapManager, Client currentClient) { super(); this.mapManager = mapManager; - - // Image du marqueur ImageIcon pinIcon = new ImageIcon(getClass().getResource("/res/pin.png")); // load the image to a imageIcon @@ -54,7 +52,7 @@ public GridView(MapManager mapManager, Client currentClient) { // Listener permettant de gérer le drag n drop MouseListener ml = new MouseListener() { - + @Override public void mouseClicked(MouseEvent e) {} From cc61ac3293b09bf0a6b11049800eac3984f092e6 Mon Sep 17 00:00:00 2001 From: rhabban Date: Sun, 5 Mar 2017 17:23:48 +0100 Subject: [PATCH 03/10] update --- src/View/ClientUI.java | 6 ++++-- src/client/ClientThread.java | 11 ++++++----- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/View/ClientUI.java b/src/View/ClientUI.java index 1ccba5e..ed5c6fe 100644 --- a/src/View/ClientUI.java +++ b/src/View/ClientUI.java @@ -115,8 +115,10 @@ public void run() { textArea.append("\n"); ArrayList clientsList = client.getClientsData(); for (Client c : clientsList) { - JLabel userName = new JLabel(c.getName()); - boxUsers.add(userName); + if(c != null){ + JLabel userName = new JLabel(c.getName()); + boxUsers.add(userName); + } } System.out.println(clientsList); } diff --git a/src/client/ClientThread.java b/src/client/ClientThread.java index 48902d6..ebce6b9 100644 --- a/src/client/ClientThread.java +++ b/src/client/ClientThread.java @@ -37,8 +37,9 @@ public synchronized void run() { ObjectInputStream streamIn = new ObjectInputStream(is); streamOut = new ObjectOutputStream(clientSocket.getOutputStream()); - streamOut.writeObject(new Message(Message._TEXT_,"Quel est votre nom ?", "", 0, 0, null)); - streamOut.flush(); + streamOut.writeObject(new Message(Message._TEXT_,"Quel est votre nom ?", "", 0, 0, getClients())); + + //streamOut.flush(); Message msgName = (Message)streamIn.readObject(); String clientName = msgName.text; @@ -46,9 +47,9 @@ public synchronized void run() { for(ClientThread thread : threads){ if(thread != this){ - streamOut.writeObject(new Message(Message._TEXT_," s'est connecté !", clientName, 0, 0, null)); + streamOut.writeObject(new Message(Message._TEXT_," s'est connecté !", clientName, 0, 0, getClients())); } else { - streamOut.writeObject(new Message( Message._NAME_, "Bonjour " + clientName + " et bienvenue dans le chat. Pour communiquer avec les utilisateurs, il est nécessaire de se positionner à leur portée", clientName, 0, 0, null)); + streamOut.writeObject(new Message( Message._NAME_, "Bonjour " + clientName + " et bienvenue dans le chat. Pour communiquer avec les utilisateurs, il est nécessaire de se positionner à leur portée", clientName, 0, 0, getClients())); } ArrayList clients = getClients(); @@ -141,7 +142,7 @@ public ArrayList getClients(){ public synchronized void disconnect(){ try{ for(ClientThread thread : threads){ - thread.streamOut.writeObject(new Message(Message._TEXT_, clientData.getName() + "s'est déconnecté !", "", 0, 0, null)); + thread.streamOut.writeObject(new Message(Message._TEXT_, clientData.getName() + "s'est déconnecté !", "", 0, 0, getClients())); if(thread == this) thread = null; } From 58ac92665f8801a6a7ede4190fbf716fa93ad1c8 Mon Sep 17 00:00:00 2001 From: rhabban Date: Sun, 5 Mar 2017 17:36:34 +0100 Subject: [PATCH 04/10] minor ajustements --- src/View/ClientUI.java | 2 +- src/client/ClientThread.java | 12 +++++------- src/client/ReceivingThread.java | 1 + 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/View/ClientUI.java b/src/View/ClientUI.java index ed5c6fe..9ed4a02 100644 --- a/src/View/ClientUI.java +++ b/src/View/ClientUI.java @@ -120,7 +120,7 @@ public void run() { boxUsers.add(userName); } } - System.out.println(clientsList); + System.out.println("ClientUI update : " + clientsList); } }); } diff --git a/src/client/ClientThread.java b/src/client/ClientThread.java index ebce6b9..064f888 100644 --- a/src/client/ClientThread.java +++ b/src/client/ClientThread.java @@ -37,7 +37,7 @@ public synchronized void run() { ObjectInputStream streamIn = new ObjectInputStream(is); streamOut = new ObjectOutputStream(clientSocket.getOutputStream()); - streamOut.writeObject(new Message(Message._TEXT_,"Quel est votre nom ?", "", 0, 0, getClients())); + streamOut.writeObject(new Message(Message._TEXT_,"Quel est votre nom ?", "BOT", 0, 0, getClients())); //streamOut.flush(); Message msgName = (Message)streamIn.readObject(); @@ -47,7 +47,7 @@ public synchronized void run() { for(ClientThread thread : threads){ if(thread != this){ - streamOut.writeObject(new Message(Message._TEXT_," s'est connecté !", clientName, 0, 0, getClients())); + streamOut.writeObject(new Message(Message._TEXT_," s'est connecté !", "BOT", 0, 0, getClients())); } else { streamOut.writeObject(new Message( Message._NAME_, "Bonjour " + clientName + " et bienvenue dans le chat. Pour communiquer avec les utilisateurs, il est nécessaire de se positionner à leur portée", clientName, 0, 0, getClients())); } @@ -101,7 +101,8 @@ public synchronized void sendMessages(ArrayList messages){ thread.streamOut.writeObject(message); else System.out.println("Hors de portée");*/ - thread.streamOut.writeObject(message); + thread.streamOut.writeObject(message); + System.out.println("ClientThread.sendMessages :" + message); } catch (IOException e) { e.printStackTrace(); } @@ -113,8 +114,6 @@ public synchronized void sendMessage(Message message){ ArrayList messages = new ArrayList<>(); messages.add(message); sendMessages(messages); - - System.out.println("ClientThread.sendMessage :" + messages); } public synchronized void refreshClientData(String name, int posX, int posY){ @@ -122,7 +121,7 @@ public synchronized void refreshClientData(String name, int posX, int posY){ this.clientData.setX(posX); this.clientData.setY(posY); - System.out.println(name+" position has been updated"); + System.out.println(name+" position updated" + clientData); } public synchronized Message createClientsMessage(){ @@ -135,7 +134,6 @@ public ArrayList getClients(){ for(ClientThread thread : threads){ clients.add(thread.clientData); } - System.out.println("ClientThread.getClients :"+clients); return clients; } diff --git a/src/client/ReceivingThread.java b/src/client/ReceivingThread.java index 0c7c312..d5b0f7e 100644 --- a/src/client/ReceivingThread.java +++ b/src/client/ReceivingThread.java @@ -31,6 +31,7 @@ public void run() msg = (Message)streamIn.readObject(); if(msg.type == Message._NAME_){ this.client.setName(msg.clientName); + msg.clientName = "BOT"; } this.client.setClientsData(msg.clients); this.client.notifyObservers("<"+msg.clientName+">"+msg.text); From 153d5f078a2f1d1949330bab0344c8435c7ba5f5 Mon Sep 17 00:00:00 2001 From: Raphael-Erfani Date: Sun, 5 Mar 2017 17:36:50 +0100 Subject: [PATCH 05/10] Update clients list --- src/View/ClientUI.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/View/ClientUI.java b/src/View/ClientUI.java index ed5c6fe..fab08e2 100644 --- a/src/View/ClientUI.java +++ b/src/View/ClientUI.java @@ -66,12 +66,12 @@ private void createUI() throws IOException { add(box, BorderLayout.SOUTH); add(new JScrollPane(boxUsers), BorderLayout.EAST); inputTextField = new JTextField(); - + /* // TODO :: Afficher la liste des clients connectés dynamiquement for (int i=0;i<5;i++) { JLabel userName = new JLabel("user"+i); boxUsers.add(userName); - } + }*/ //JTextArea usersList = new JTextArea("users List"); sendButton = new JButton("Send"); @@ -113,6 +113,7 @@ public void update(Observable o, Object arg) { public void run() { textArea.append(arg.toString()); textArea.append("\n"); + boxUsers.removeAll(); ArrayList clientsList = client.getClientsData(); for (Client c : clientsList) { if(c != null){ From c90d539a8391e53bafbf4cabbfddc3a2078d8c91 Mon Sep 17 00:00:00 2001 From: rhabban Date: Sun, 5 Mar 2017 18:22:09 +0100 Subject: [PATCH 06/10] Minor adj --- src/View/GridView.java | 2 ++ src/client/ClientThread.java | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/View/GridView.java b/src/View/GridView.java index b4d8be4..ac38e8e 100644 --- a/src/View/GridView.java +++ b/src/View/GridView.java @@ -61,6 +61,8 @@ public void mousePressed(MouseEvent e) { GridCase jc = (GridCase)e.getSource(); TransferHandler th = jc.getTransferHandler(); + + currentClient.sendPosition(); // Permet d'éviter de pouvoir déplacer une case vide if(jc.getIcon() != null) diff --git a/src/client/ClientThread.java b/src/client/ClientThread.java index 064f888..2e788a8 100644 --- a/src/client/ClientThread.java +++ b/src/client/ClientThread.java @@ -47,7 +47,7 @@ public synchronized void run() { for(ClientThread thread : threads){ if(thread != this){ - streamOut.writeObject(new Message(Message._TEXT_," s'est connecté !", "BOT", 0, 0, getClients())); + streamOut.writeObject(new Message(Message._TEXT_," s'est connecté !", clientName, 0, 0, getClients())); } else { streamOut.writeObject(new Message( Message._NAME_, "Bonjour " + clientName + " et bienvenue dans le chat. Pour communiquer avec les utilisateurs, il est nécessaire de se positionner à leur portée", clientName, 0, 0, getClients())); } From 0cfc3ca5d6985f1b3410b04d29226d18c3ddf418 Mon Sep 17 00:00:00 2001 From: Raphael-Erfani Date: Sun, 5 Mar 2017 18:37:04 +0100 Subject: [PATCH 07/10] Minors UI fixes --- src/View/ClientUI.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/View/ClientUI.java b/src/View/ClientUI.java index 027a3c5..64c4f45 100644 --- a/src/View/ClientUI.java +++ b/src/View/ClientUI.java @@ -66,6 +66,7 @@ private void createUI() throws IOException { add(box, BorderLayout.SOUTH); add(new JScrollPane(boxUsers), BorderLayout.EAST); inputTextField = new JTextField(); + /* // TODO :: Afficher la liste des clients connectés dynamiquement for (int i=0;i<5;i++) { @@ -114,6 +115,8 @@ public void run() { textArea.append(arg.toString()); textArea.append("\n"); boxUsers.removeAll(); + JLabel titre = new JLabel("Liste des utilisateurs connectés : "); + boxUsers.add(titre); ArrayList clientsList = client.getClientsData(); for (Client c : clientsList) { if(c != null){ From 40f05b86bc246a3f21f1baa390205f7c67f83722 Mon Sep 17 00:00:00 2001 From: rhabban Date: Sun, 5 Mar 2017 19:41:55 +0100 Subject: [PATCH 08/10] Refacto and commenting Client and Server classes --- bin/client/Client.class | Bin 3945 -> 4058 bytes src/View/ClientUI.java | 17 +++-------------- src/client/Client.java | 30 ++++++++++++++---------------- src/client/ClientThread.java | 20 +++++++++----------- src/client/ReceivingThread.java | 5 ++++- src/server/Server.java | 2 +- 6 files changed, 31 insertions(+), 43 deletions(-) diff --git a/bin/client/Client.class b/bin/client/Client.class index 266b8fca3006073ee9779b8302867d8a57a3df36..c05f89fb42200f850f0bccf53f64cc2c4bfac10c 100644 GIT binary patch literal 4058 zcmaJ@`F9iN6}{s{W5j@o5^MvmBLW3WGKi3rB(^bj9B7=#h9crbNr8^#i5ZY3MH(4Q znxrkH`2zi?)#Sy*6fcF!h3pN-Gw^`Ek$1dgS}k$YDpb5;#Uo0` zUNtbFkI=F6fpwmX6qc?#SueP1V7PI*ZC4CWx_Q@|GSEF5TO>KNg=Hs zHMcCMo5?URII0l`BQrM_TNE3k7EJVs6t}PgVUflx>_nKMnxi9{Gjh~Pi8vTOdn@Y{ zCB$VDkJg+cmviUqJ0>0@Fy(b44q=?c=Y*8wB0XW@Nj$|6DmP#e#hImzr@jm!ZbSr0mZZJonXgUC&O5X|gvMPtBMz*^L)1yd);WWyhPn z#%gw!C4XPGa0xFkt?klEvlag~F_oSCa*Jxe6T}uxe2ML5mDP|-&O7C@y~47%iX|Z- zYv9c2Tzi(bugv+UTAGBAH3`SUs{(Q7&64Xm8djJ|@-m@$&Mnequ4>C!C-Gei-MQy;$qDy9uPp^k9vn7TfHwop;(z_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wxp@xej&9(^ zd)P4_=fM4p2f;9m(V1Qbqn;eo%&gO#KZ8RM*Uko9m0(G75C%7ut3&+1g**u!qN}SY za8%?vvwX?KBYgxZHB$8+H;do1!qJSr)J`(}4^*~L%SOMpiLWO%@X7|hy@Brvb-oGp zbesd9`YAl#pspizfl+#zaY|9UXZXRCrV|T_{;1NX9;tnJNRc+UN0T<{q-#EDHep}b zcYV?#B@^il;U3o9dwWIN^fvqsGQaE6YJM5Yas~UD4+r_RaG3cJM>fDO;mf<<7nPs3 z$b;P$<8`uNx@8Q7FMg7;8_2SGu~*Zy6esTjVM!Nfw<@_4ixW spQO_Uejoi4|2Ssd-HZNBkJ%;fML*=2_1O;eS^(%oBjY}%=^lLYKc|Z)yZ`_I diff --git a/src/View/ClientUI.java b/src/View/ClientUI.java index 027a3c5..ca3ba84 100644 --- a/src/View/ClientUI.java +++ b/src/View/ClientUI.java @@ -10,16 +10,13 @@ import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; -import java.awt.image.BufferedImage; import java.io.IOException; -import javax.imageio.ImageIO; import javax.swing.BorderFactory; import javax.swing.Box; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; -import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; @@ -29,7 +26,7 @@ import utils.ImagePanel; /** - * ClientUI + * ClientUI is the user interface. Each ClientUI create and observe a new Client. * @author Corentin */ public class ClientUI extends JFrame implements Observer{ @@ -42,6 +39,7 @@ public class ClientUI extends JFrame implements Observer{ public ClientUI(Client client) throws IOException { this.client = client; + // Client is observed by ClientUI client.addObserver(this); createUI(); } @@ -66,14 +64,6 @@ private void createUI() throws IOException { add(box, BorderLayout.SOUTH); add(new JScrollPane(boxUsers), BorderLayout.EAST); inputTextField = new JTextField(); - /* - // TODO :: Afficher la liste des clients connectés dynamiquement - for (int i=0;i<5;i++) { - JLabel userName = new JLabel("user"+i); - boxUsers.add(userName); - }*/ - - //JTextArea usersList = new JTextArea("users List"); sendButton = new JButton("Send"); sendButton.setBackground(new Color(59,89,182)); sendButton.setForeground(Color.WHITE); @@ -82,9 +72,8 @@ private void createUI() throws IOException { inputTextField.setForeground(Color.BLACK); box.add(inputTextField); box.add(sendButton); - //boxUsers.add(usersList); - // Action for the inputTextField and the goButton + // Send text line message to client ActionListener sendListener = new ActionListener() { public void actionPerformed(ActionEvent e) { String str = inputTextField.getText(); diff --git a/src/client/Client.java b/src/client/Client.java index 9b629b0..abcea9f 100644 --- a/src/client/Client.java +++ b/src/client/Client.java @@ -12,19 +12,19 @@ /** - * Client allows a user to send messages to the server. + * Client allows a user to send messages to the server. This class communicates with ClientUI (Obs) and ClientThread with ObjectStream + * This is a serializable class that allow it to be send in ObjectStream (only with position and name) * @author Corentin - * */ public class Client extends Observable implements Serializable { - private String name = "test"; + // Serializabled attributes + private String name; private int position_x; private int position_y; + private ArrayList clientsData = new ArrayList<>(); // Client knows its Client's pair. - - private ArrayList clientsData = new ArrayList<>(); - + // Sockets and streams attributes private Socket socket = null; private OutputStream outputStream; private ObjectOutputStream objectOutputStream; @@ -33,6 +33,7 @@ public Client(){ super(); } + /** This constructor is used to send this into socket **/ public Client(String name, int position_x, int position_y){ this.name = name; this.position_x = position_x; @@ -49,29 +50,28 @@ public void InitSocket(String server, int port) throws IOException { receivingThread.start(); } + /** ClientUI is updating with arg by this method **/ public void notifyObservers(Object arg) { super.setChanged(); super.notifyObservers(arg); } - /** Send a line of text */ + /** Send a text Message to ClientThread */ public void send(String text) { try { Message message = new Message(Message._TEXT_, text, this.name, this.position_x, this.position_y, null); objectOutputStream.writeObject(message); - //objectOutputStream.flush(); } catch (IOException e) { notifyObservers(e); } } - /** Send a line of text */ + /** Send a position Message to ClientThread */ public void sendPosition() { try { Message message = new Message(Message._POSITION_, "", this.name, this.position_x, this.position_y, null); System.out.println("Client.SendPosition :" +message); objectOutputStream.writeObject(message); - //objectOutputStream.flush(); } catch (IOException e) { notifyObservers(e); } @@ -117,10 +117,9 @@ public int getY() { public void setY(int position_y) { this.position_y = position_y; } - + public void setClientsData(ArrayList clients){ this.clientsData = clients; - //System.out.println(clients); } public ArrayList getClientsData(){ @@ -129,9 +128,8 @@ public ArrayList getClientsData(){ @Override public String toString() { - return "Client [name=" + name + ", position_x=" + position_x + ", position_y=" + position_y + "]"; + return "Client [name=" + name + ", position_x=" + position_x + ", position_y=" + position_y + ", clientsData=" + + clientsData + ", socket=" + socket + ", outputStream=" + outputStream + ", objectOutputStream=" + + objectOutputStream + "]"; } - - - } diff --git a/src/client/ClientThread.java b/src/client/ClientThread.java index 2e788a8..de78474 100644 --- a/src/client/ClientThread.java +++ b/src/client/ClientThread.java @@ -11,12 +11,11 @@ import model.MessageValidator; /** - * ClientThread + * ClientThread is the thread that communicates with Server and Client. It receives Message from Client and sends Message to Client. * @author Corentin */ - public class ClientThread extends Thread{ - // Client Data (Name, posX, posY) saved in thread, not real Client instanced in ClientUI + // Client Data (Name, posX, posY) saved in thread, not real Client instanced in ClientUI. private Client clientData; private ObjectInputStream streamIn = null; private ObjectOutputStream streamOut = null; @@ -30,6 +29,7 @@ public ClientThread(Socket clientSocket, ArrayList threads) { } public synchronized void run() { + // Each ClientThread knows its ClientThread's pair. ArrayList threads = this.threads; try { @@ -37,14 +37,13 @@ public synchronized void run() { ObjectInputStream streamIn = new ObjectInputStream(is); streamOut = new ObjectOutputStream(clientSocket.getOutputStream()); + // Ask to Client it's username and save him into clientData streamOut.writeObject(new Message(Message._TEXT_,"Quel est votre nom ?", "BOT", 0, 0, getClients())); - - //streamOut.flush(); Message msgName = (Message)streamIn.readObject(); String clientName = msgName.text; this.clientData = new Client(clientName, 0, 0); - + for(ClientThread thread : threads){ if(thread != this){ streamOut.writeObject(new Message(Message._TEXT_," s'est connecté !", clientName, 0, 0, getClients())); @@ -52,9 +51,6 @@ public synchronized void run() { streamOut.writeObject(new Message( Message._NAME_, "Bonjour " + clientName + " et bienvenue dans le chat. Pour communiquer avec les utilisateurs, il est nécessaire de se positionner à leur portée", clientName, 0, 0, getClients())); } ArrayList clients = getClients(); - - - //streamOut.flush(); } /* Start the conversation. */ @@ -65,8 +61,6 @@ public synchronized void run() { if(msg != null){ switch(msg.type){ case Message._TEXT_: - /*Message clientsMsg = new Message(Message._CLIENTS_, "", "", 0, 0, getClients()); - messages.add(clientsMsg);*/ sendMessages(messages); break; @@ -89,12 +83,14 @@ public synchronized void run() { } } + /** This method allows threads to send Messages to every Client **/ public synchronized void sendMessages(ArrayList messages){ ArrayList clients = getClients(); for(ClientThread thread : threads){ for(Message message : messages){ try { + // All clients connected to server are saved into Message to allow Client to know them each other. message.clients = clients; /*MessageValidator val = new MessageValidator(this.clientData, thread.clientData); if(this == thread || val.isClientsNear() == true) @@ -116,6 +112,7 @@ public synchronized void sendMessage(Message message){ sendMessages(messages); } + /** Save Client new position **/ public synchronized void refreshClientData(String name, int posX, int posY){ this.clientData.setName(name); this.clientData.setX(posX); @@ -129,6 +126,7 @@ public synchronized Message createClientsMessage(){ return (new Message(Message._CLIENTS_, clients)); } + /** Return the list of Thread's Clients **/ public ArrayList getClients(){ ArrayList clients = new ArrayList<>(); for(ClientThread thread : threads){ diff --git a/src/client/ReceivingThread.java b/src/client/ReceivingThread.java index d5b0f7e..f5797eb 100644 --- a/src/client/ReceivingThread.java +++ b/src/client/ReceivingThread.java @@ -8,7 +8,7 @@ import model.Message; /** - * ReceivingThread + * ReceivingThread allows Client to listen ClientThread * @author Corentin */ public class ReceivingThread implements Runnable{ @@ -26,6 +26,7 @@ public void run() try { InputStream is = socket.getInputStream(); ObjectInputStream streamIn = new ObjectInputStream(is); + Message msg = null; while(true){ msg = (Message)streamIn.readObject(); @@ -33,7 +34,9 @@ public void run() this.client.setName(msg.clientName); msg.clientName = "BOT"; } + // Set Clients array to Client this.client.setClientsData(msg.clients); + // Notify all observers that ClientThread just send a Message this.client.notifyObservers("<"+msg.clientName+">"+msg.text); System.out.println("ReceivingThread.run Message :" + msg); } diff --git a/src/server/Server.java b/src/server/Server.java index 039d537..47c2220 100644 --- a/src/server/Server.java +++ b/src/server/Server.java @@ -8,7 +8,7 @@ import client.ClientThread; /** - * Server allow the communication between many clients. + * Server allow the communication between many ClientThread. * @author Corentin * */ From e89a8fe54f04976bda8f7ed90ebe3806ac808bdf Mon Sep 17 00:00:00 2001 From: Raphael-Erfani Date: Sun, 5 Mar 2017 21:03:41 +0100 Subject: [PATCH 09/10] Minor fix --- src/View/GridView.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/View/GridView.java b/src/View/GridView.java index ac38e8e..9f903db 100644 --- a/src/View/GridView.java +++ b/src/View/GridView.java @@ -31,14 +31,15 @@ public class GridView extends JPanel { private MapManager mapManager; /** Draw borders for the client scope */ + /* public void paintComponent(Graphics g){ super.paintComponent(g); - //g.drawOval(-200, -200, 400, 400); - g.drawRect(-200, -200, 400, 400); + g.drawOval(-200, -200, 400, 400); + //g.drawRect(-200, -200, 400, 400); //g.drawRoundRect(000, -200, 400, 400,100,100); // TODO :: La portée est affichée en brut ici, à changer dynamiquement ! - } + }*/ public GridView(MapManager mapManager, Client currentClient) { super(); From 4bd2d76338534a84ba6f4e7166cc4927735dd3f0 Mon Sep 17 00:00:00 2001 From: rhabban Date: Mon, 14 Aug 2017 12:09:55 +0200 Subject: [PATCH 10/10] Import from GitLab --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index cd5d004..ec3acf2 100644 --- a/README.md +++ b/README.md @@ -1 +1,2 @@ -devoir-java +## Chat Application - 2017/03 +This is my first Java project basing on Client-Server structure (with sockets), realised with 3 of my University colleagues. pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy