From 7f10c942bfc6cb73c2b36584d63dfb0b40fc6f24 Mon Sep 17 00:00:00 2001
From: Hai HoDac <hhodac@student.unimelb.edu.au>
Date: Mon, 21 Oct 2019 13:56:42 +1100
Subject: [PATCH] Chat function worked!
---
src/GUI/ChatScreen.java | 33 +--
src/{client => GUI}/DrawingArea.java | 339 +++++++++++++++------------
src/GUI/PaintGUI.java | 25 +-
src/GUI/StartScreen.java | 2 +-
src/GUI/ToolBar.java | 2 -
src/client/ChatUpdate.java | 15 +-
src/client/Client.java | 71 +++---
src/client/ClientCanvas.java | 43 ----
src/client/DrawingUpdate.java | 8 +-
src/remote/IChatController.java | 2 +
src/remote/IChatUpdate.java | 2 +
src/remote/IClientUpdate.java | 10 +
src/remote/IUpdateController.java | 17 ++
src/server/ChatController.java | 36 +++
src/server/ClientController.java | 6 +-
src/server/DrawingController.java | 10 +-
src/server/Server.java | 6 +
src/server/User.java | 21 +-
18 files changed, 379 insertions(+), 269 deletions(-)
rename src/{client => GUI}/DrawingArea.java (52%)
delete mode 100644 src/client/ClientCanvas.java
create mode 100644 src/remote/IClientUpdate.java
create mode 100644 src/remote/IUpdateController.java
diff --git a/src/GUI/ChatScreen.java b/src/GUI/ChatScreen.java
index 27f9402..d242190 100644
--- a/src/GUI/ChatScreen.java
+++ b/src/GUI/ChatScreen.java
@@ -22,6 +22,11 @@ public class ChatScreen {
private JTextArea chatOutputArea;
private JTextArea chatInputArea;
+
+ public JTextArea getUserListArea() {
+ return userListArea;
+ }
+
private JTextArea userListArea;
private JPanel ryanPanel;
private JPanel x;
@@ -77,31 +82,31 @@ public class ChatScreen {
this.client = client;
quitButton.addActionListener(actionListener);
send.addActionListener(actionListener);
- frame = new JFrame("App");
+ frame = new JFrame("ChatScreen");
frame.setContentPane(panel2);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
- createUIComponents();
+// createUIComponents();
}
- private void createUIComponents() {
- ryanPanel = new PaintGUI().getGlobal();
-
- }
+// private void createUIComponents() {
+// ryanPanel = new PaintGUI().getGlobal();
+//
+// }
// public static void main (String[] args) {
// ChatScreen chatScreen = new ChatScreen();
// chatScreen.go();
// }
- public void go(){
- //joinButton.addActionListener(actionListener);
- frame = new JFrame("App");
- frame.setContentPane(panel2);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.pack();
- frame.setVisible(true);
- }
+// public void go(){
+// //joinButton.addActionListener(actionListener);
+// frame = new JFrame("App");
+// frame.setContentPane(panel2);
+// frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+// frame.pack();
+// frame.setVisible(true);
+// }
}
diff --git a/src/client/DrawingArea.java b/src/GUI/DrawingArea.java
similarity index 52%
rename from src/client/DrawingArea.java
rename to src/GUI/DrawingArea.java
index 9d2183b..b2349f2 100644
--- a/src/client/DrawingArea.java
+++ b/src/GUI/DrawingArea.java
@@ -1,11 +1,14 @@
-package client;
+package GUI;
+
+import client.Client;
+import remote.IDrawingController;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
-import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
-import java.awt.event.MouseMotionAdapter;
+import java.awt.event.MouseListener;
+import java.awt.event.MouseMotionListener;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
@@ -13,178 +16,64 @@ import java.awt.geom.RectangularShape;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
+import java.rmi.RemoteException;
+
+public class DrawingArea extends JPanel implements MouseMotionListener, MouseListener {
+
-public class DrawingArea extends JPanel {
-/// enum for all different mode ///
+ /// enum for all different mode ///
enum Mode { FREEHAND, LINE, CIRCLE, RECTANGLE, OVAL, ERASE }
-/// Canvas size parameter ///
+ /// Canvas size parameter ///
private final static int AREA_WIDTH = 600;
private final static int AREA_HEIGHT = 500;
-// private int initialX, initialY, currentX, currentY, finalX, finalY;
+ /// Shape to be drawn on the canvas ///
+ private Client client;
+
+ private Shape drawing;
private Point startPoint;
private Point previousPoint;
- private Point currentPoint;
-
-/// Create a empty canvas ///
- private BufferedImage image = new BufferedImage(AREA_WIDTH, AREA_HEIGHT, BufferedImage.TYPE_INT_ARGB);
-
-/// Default mode and color ///
- private Color shapeColor = new Color(0, 0, 0);
- private Mode currentMode = Mode.FREEHAND;
-
-/// Shape to be drawn on the canvas ///
- private Shape drawing;
-/// Drawing tool
- private Graphics2D g2 = (Graphics2D) image.getGraphics();
+ private Point currentPoint;
+ /// Default mode and color ///
+ private Color shapeColor;// = new Color(0, 0, 0);
+ private Mode currentMode;// = Mode.FREEHAND;
+ /// Create a empty canvas ///
+ private BufferedImage image;// = new BufferedImage(AREA_WIDTH, AREA_HEIGHT, BufferedImage.TYPE_INT_ARGB);
+ /// Drawing tool
+ private Graphics2D g2;// = (Graphics2D) image.getGraphics();
- public DrawingArea() {
-/// Antialiasing the graphic for smoothness ///
+ public DrawingArea(Client client) {
+ this.client = client;
+ setBackground(Color.WHITE); // Set Background color
+ setDoubleBuffered(false); // Non-buffered drawing
+ image = new BufferedImage(AREA_WIDTH, AREA_HEIGHT, BufferedImage.TYPE_INT_ARGB);
+ g2 = (Graphics2D) image.getGraphics();
+ /// Antialiasing the graphic for smoothness ///
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
+ shapeColor = new Color(0, 0, 0);
+ currentMode = Mode.FREEHAND;
+ drawing = null;
+ addMouseListener(this);
+ addMouseMotionListener(this);
+ }
-/// Set Background color ///
- setBackground(Color.WHITE);
-
-/// Non-buffered drawing ///
- setDoubleBuffered(false);
-
-/// Mouse listeners for pressing///
- addMouseListener(new MouseAdapter() {
- public void mousePressed(MouseEvent e) {
-
- startPoint = previousPoint = e.getPoint();
-
-/// Instantiate object based on current mode ///
- switch (currentMode) {
-
- case FREEHAND:
- case LINE:
-
- drawing = new Line2D.Double();
- break;
-
- case ERASE:
- case CIRCLE:
- case OVAL:
-
- drawing = new Ellipse2D.Double();
- break;
-
- case RECTANGLE:
-
- drawing = new Rectangle2D.Double();
- break;
- }
- }
- });
-
-/// Mouse motion listeners for dragging ///
- addMouseMotionListener(new MouseMotionAdapter() {
- public void mouseDragged(MouseEvent e) {
-
- currentPoint = e.getPoint();
-
- int x = Math.min(startPoint.x, e.getX());
- int y = Math.min(startPoint.y, e.getY());
- int width = Math.abs(startPoint.x - e.getX());
- int height = Math.abs(startPoint.y - e.getY());
-
- switch (currentMode) {
-
-/// Freehand drawing is continuously drawing line from current point to previous point ///
- case FREEHAND:
-
- ((Line2D) drawing).setLine(currentPoint, previousPoint);
- g2.setColor(shapeColor);
- g2.draw(drawing);
- previousPoint = currentPoint;
- break;
-
-/// Single line ///
- case LINE:
-
- ((Line2D) drawing).setLine(startPoint, currentPoint);
- break;
-
-/// Eraser is continuously drawing "small white circle" from current point to previous point ///
- case ERASE:
-
- ((Ellipse2D) drawing).setFrame(currentPoint.getX(), currentPoint.getY(), 10, 10);
- g2.setColor(Color.WHITE);
- g2.fill(drawing);
- g2.draw(drawing);
- break;
-
-/// Single circle (How to draw more intuitively?)///
- case CIRCLE:
-
- double radius = Math.sqrt(width * width + height * height);
-// ((Ellipse2D) drawing).setFrame(x, y, radius, radius);
- ((Ellipse2D) drawing).setFrame(startPoint.getX() - radius, startPoint.getY() - radius, 2 * radius, 2 * radius);
- break;
-
-/// Single oval ///
- case OVAL:
-
- ((Ellipse2D) drawing).setFrame(x, y, width, height);
- break;
-
-/// Single rectangle ///
- case RECTANGLE:
-
- ((Rectangle2D) drawing).setFrame(x, y, width, height);
- break;
- }
-
- repaint();
-
-
- }
- });
-
-/// Mouse listener for releasing ///
- addMouseListener(new MouseAdapter() {
- public void mouseReleased(MouseEvent e) {
-
-/// Draw the final drawing to the buffered image ///
- switch (currentMode) {
- case OVAL:
- case RECTANGLE:
- case CIRCLE:
-
-/// Abort drawing if 2D has no width or height ///
- if (((RectangularShape) drawing).getWidth() == 0 || ((RectangularShape) drawing).getHeight() == 0) {
- break;
- }
- case FREEHAND:
- case LINE:
-// Graphics2D g2 = (Graphics2D) image.getGraphics();
- g2.setColor(shapeColor);
-
-/// Uncomment the line below to fill the shapes with color ///
-// g2.fill(drawing);
- g2.draw(drawing);
- }
- g2 = (Graphics2D) image.getGraphics();
- g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
- g2.setColor(shapeColor);
-
-/// This repaint is needed if we want to fill the drawing shape with color
- repaint();
+ public Shape getDrawing() {
+ return drawing;
+ }
- drawing = null;
- }
- });
+ public void setDrawing(Shape drawing) {
+ this.drawing = drawing;
}
+
@Override
public Dimension getPreferredSize()
{
@@ -219,6 +108,12 @@ public class DrawingArea extends JPanel {
Color borderColor = currentMode != Mode.ERASE ? shapeColor : Color.WHITE;
g2.setColor(borderColor);
g2.draw(drawing);
+ IDrawingController drawingController = client.getDrawingController();
+ try {
+ drawingController.broadcastDrawing(client.getUserName(), drawing);
+ } catch (RemoteException ex) {
+ ex.printStackTrace();
+ }
}
}
@@ -340,4 +235,140 @@ public class DrawingArea extends JPanel {
shapeColor = new Color(255, 255, 0);
}
+ @Override
+ public void mouseClicked(MouseEvent e) {
+
+ }
+
+ @Override
+ public void mousePressed(MouseEvent e) {
+ startPoint = previousPoint = e.getPoint();
+
+/// Instantiate object based on current mode ///
+ switch (currentMode) {
+
+ case FREEHAND:
+ case LINE:
+
+ drawing = new Line2D.Double();
+ break;
+
+ case ERASE:
+ case CIRCLE:
+ case OVAL:
+
+ drawing = new Ellipse2D.Double();
+ break;
+
+ case RECTANGLE:
+
+ drawing = new Rectangle2D.Double();
+ break;
+ }
+ }
+
+ @Override
+ public void mouseReleased(MouseEvent e) {
+ switch (currentMode) {
+ case OVAL:
+ case RECTANGLE:
+ case CIRCLE:
+
+/// Abort drawing if 2D has no width or height ///
+ if (((RectangularShape) drawing).getWidth() == 0 || ((RectangularShape) drawing).getHeight() == 0) {
+ break;
+ }
+ case FREEHAND:
+ case LINE:
+// Graphics2D g2 = (Graphics2D) image.getGraphics();
+ g2.setColor(shapeColor);
+
+/// Uncomment the line below to fill the shapes with color ///
+// g2.fill(drawing);
+ g2.draw(drawing);
+ }
+
+ g2 = (Graphics2D) image.getGraphics();
+ g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
+ g2.setColor(shapeColor);
+
+/// This repaint is needed if we want to fill the drawing shape with color
+ repaint();
+
+ drawing = null;
+ }
+
+ @Override
+ public void mouseEntered(MouseEvent e) {
+
+ }
+
+ @Override
+ public void mouseExited(MouseEvent e) {
+
+ }
+
+ @Override
+ public void mouseDragged(MouseEvent e) {
+ currentPoint = e.getPoint();
+
+ int x = Math.min(startPoint.x, e.getX());
+ int y = Math.min(startPoint.y, e.getY());
+ int width = Math.abs(startPoint.x - e.getX());
+ int height = Math.abs(startPoint.y - e.getY());
+
+ switch (currentMode) {
+
+/// Freehand drawing is continuously drawing line from current point to previous point ///
+ case FREEHAND:
+
+ ((Line2D) drawing).setLine(currentPoint, previousPoint);
+ g2.setColor(shapeColor);
+ g2.draw(drawing);
+ previousPoint = currentPoint;
+ break;
+
+/// Single line ///
+ case LINE:
+
+ ((Line2D) drawing).setLine(startPoint, currentPoint);
+ break;
+
+/// Eraser is continuously drawing "small white circle" from current point to previous point ///
+ case ERASE:
+
+ ((Ellipse2D) drawing).setFrame(currentPoint.getX(), currentPoint.getY(), 10, 10);
+ g2.setColor(Color.WHITE);
+ g2.fill(drawing);
+ g2.draw(drawing);
+ break;
+
+/// Single circle (How to draw more intuitively?)///
+ case CIRCLE:
+
+ double radius = Math.sqrt(width * width + height * height);
+// ((Ellipse2D) drawing).setFrame(x, y, radius, radius);
+ ((Ellipse2D) drawing).setFrame(startPoint.getX() - radius, startPoint.getY() - radius, 2 * radius, 2 * radius);
+ break;
+
+/// Single oval ///
+ case OVAL:
+
+ ((Ellipse2D) drawing).setFrame(x, y, width, height);
+ break;
+
+/// Single rectangle ///
+ case RECTANGLE:
+
+ ((Rectangle2D) drawing).setFrame(x, y, width, height);
+ break;
+ }
+
+ repaint();
+ }
+
+ @Override
+ public void mouseMoved(MouseEvent e) {
+
+ }
}
\ No newline at end of file
diff --git a/src/GUI/PaintGUI.java b/src/GUI/PaintGUI.java
index 74d68ce..90641c9 100644
--- a/src/GUI/PaintGUI.java
+++ b/src/GUI/PaintGUI.java
@@ -1,6 +1,6 @@
package GUI;
-import client.DrawingArea;
+import client.Client;
import javax.swing.*;
import java.awt.*;
@@ -19,7 +19,12 @@ public class PaintGUI extends JPanel {
JComboBox colorOptions;
JComboBox shapeOptions;
+ public DrawingArea getDrawingArea() {
+ return drawingArea;
+ }
+
DrawingArea drawingArea;
+ Client client;
public JPanel getGlobal() {
return global;
@@ -165,9 +170,11 @@ public class PaintGUI extends JPanel {
/// GUI setup ///
- public PaintGUI() {
+ public PaintGUI(Client client) {
+ this.client = client;
+
/// Main drawing area ///
- drawingArea = new DrawingArea();
+ drawingArea = new DrawingArea(client);
/// Set up main frame and container ///
global.setLayout(new BorderLayout());
@@ -209,4 +216,16 @@ public class PaintGUI extends JPanel {
global.add(toolbox, BorderLayout.SOUTH);
}
+ public void showGUI() {
+ frame = new JFrame("Shared Whiteboard System");
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ frame.setContentPane(global);
+
+ frame.setSize(800, 600);
+ frame.setLocationRelativeTo( null );
+ frame.setResizable(false);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+ frame.setVisible(true);
+ }
+
}
\ No newline at end of file
diff --git a/src/GUI/StartScreen.java b/src/GUI/StartScreen.java
index 6fdb1bd..7755662 100644
--- a/src/GUI/StartScreen.java
+++ b/src/GUI/StartScreen.java
@@ -60,7 +60,7 @@ public class StartScreen {
public void go(){
joinButton.addActionListener(actionListener);
- frame = new JFrame("App");
+ frame = new JFrame("StartScreen");
frame.setContentPane(panel1);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
diff --git a/src/GUI/ToolBar.java b/src/GUI/ToolBar.java
index 76dc130..04087e8 100644
--- a/src/GUI/ToolBar.java
+++ b/src/GUI/ToolBar.java
@@ -1,7 +1,5 @@
package GUI;
-import client.DrawingArea;
-
import javax.swing.*;
import java.awt.*;
diff --git a/src/client/ChatUpdate.java b/src/client/ChatUpdate.java
index 72682a9..e12ac03 100644
--- a/src/client/ChatUpdate.java
+++ b/src/client/ChatUpdate.java
@@ -19,12 +19,23 @@ public class ChatUpdate extends UnicastRemoteObject implements IChatUpdate, Seri
@Override
public boolean notifyChat(String fromClient, String message) throws RemoteException
{
-// client.getChatScreen().setChatOutput(message);
- client.getChatScreen().getChatOutputArea().setText(message);
+ client.getChatScreen().getChatOutputArea().append(fromClient + ": " + message + "\n");
//client.setReceivedMessage(message);
System.out.println(fromClient + ": " + message);
return true;
}
+ @Override
+ public boolean notifyUserLogin(String fromClient) throws RemoteException {
+ client.getChatScreen().getChatOutputArea().append(fromClient + " has joined the room.\n");
+ return true;
+ }
+
+ @Override
+ public boolean notifyUserLogout(String fromClient) throws RemoteException {
+ client.getChatScreen().getChatOutputArea().append(fromClient + " has left the room.\n");
+ return true;
+ }
+
}
diff --git a/src/client/Client.java b/src/client/Client.java
index 6a1845d..5d11a48 100644
--- a/src/client/Client.java
+++ b/src/client/Client.java
@@ -1,16 +1,15 @@
package client;
import GUI.ChatScreen;
+import GUI.PaintGUI;
+import GUI.StartScreen;
import remote.IChatController;
import remote.IClientController;
-import client.ChatUpdate;
-import GUI.StartScreen;
+import remote.IDrawingController;
-import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
-import java.util.concurrent.TimeUnit;
public class Client
{
@@ -21,18 +20,28 @@ public class Client
private Registry registryServer;
private IChatController chatController;
private IClientController clientController;
+
+ private IDrawingController drawingController;
+
+ private ClientUpdate clientUpdate;
+
private ChatUpdate chatUpdate;
private DrawingUpdate drawingUpdate;
-
private StartScreen startScreen;
+ private ChatScreen chatScreen;
+ private PaintGUI paintGUI;
+
+ public PaintGUI getPaintGUI() {
+ return paintGUI;
+ }
+
+
public ChatScreen getChatScreen()
{
return chatScreen;
}
- private ChatScreen chatScreen;
-
private String receivedMessage;
@@ -47,6 +56,8 @@ public class Client
return clientController;
}
+ public IDrawingController getDrawingController() { return drawingController; }
+
public void setUsername(String userName)
{
this.userName = userName;
@@ -62,20 +73,25 @@ public class Client
this.serverAddress = serverAddress;
}
- public Client() throws RemoteException
+ public Client(String username) throws RemoteException
{
- this.userName = "DefaultUser";
+ this.userName = username;
+ this.clientUpdate = new ClientUpdate(this);
this.chatUpdate = new ChatUpdate(this);
+ this.drawingUpdate = new DrawingUpdate(this);
this.startScreen = new StartScreen(this);
- this.drawingUpdate = new DrawingUpdate();
+ this.chatScreen = new ChatScreen(this);
+ this.paintGUI = new PaintGUI(this);
}
public static void main(String[] args)
{
try
{
- Client client = new Client();
- client.startScreen.go();
+ Client client = new Client(args[0]);
+ client.connect();
+// client.startScreen.go();
+ client.getPaintGUI().showGUI();
}
catch (Exception e)
{
@@ -83,21 +99,21 @@ public class Client
}
}
- public void doSomething()
- {
-// new ChatScreen();
-
- try
- {
- new ChatScreen(this);
- System.out.println("Sleeping...");
- TimeUnit.MINUTES.sleep(5);
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- }
+// public void doSomething()
+// {
+//// new ChatScreen();
+//
+// try
+// {
+// new ChatScreen(this);
+// System.out.println("Sleeping...");
+// TimeUnit.MINUTES.sleep(5);
+// }
+// catch(Exception e)
+// {
+// e.printStackTrace();
+// }
+// }
public boolean connect()
{
@@ -107,6 +123,7 @@ public class Client
chatController = (IChatController) registryServer.lookup("ChatController");
clientController = (IClientController) registryServer.lookup("ClientController");
+ drawingController = (IDrawingController) registryServer.lookup("DrawingController");
if (clientController.join(userName, this.chatUpdate, this.drawingUpdate))
{
diff --git a/src/client/ClientCanvas.java b/src/client/ClientCanvas.java
deleted file mode 100644
index b825a8a..0000000
--- a/src/client/ClientCanvas.java
+++ /dev/null
@@ -1,43 +0,0 @@
-package client;
-
-import remote.IClientController;
-import remote.IDrawingController;
-import server.User;
-
-import java.rmi.registry.LocateRegistry;
-import java.rmi.registry.Registry;
-import java.util.ArrayList;
-import java.util.Observable;
-
-public class ClientCanvas extends Observable {
-
- private static int id=1;
- private IClientController clientController;
- private IDrawingController drawingController;
- private ArrayList<User> users;
- private DrawingArea drawing;
-
- public ClientCanvas() {
- users = new ArrayList<>();
-
- try {
- //Connect to the rmiregistry that is running on localhost
- Registry registry = LocateRegistry.getRegistry("localhost");
-
- //Retrieve the stub/proxy for the remote math object from the registry
- this.drawingController = (IDrawingController) registry.lookup("DrawingController");
- this.clientController = (IClientController) registry.lookup("ClientController");
-
- //Call methods on the remote object as if it was a local object
- System.out.println("Client: calling remote methods");
-
-// clientController.addClient(id);
-
- }catch(Exception e) {
- e.printStackTrace();
- }
- }
-
-
-
-}
\ No newline at end of file
diff --git a/src/client/DrawingUpdate.java b/src/client/DrawingUpdate.java
index eadd3e9..7716e99 100644
--- a/src/client/DrawingUpdate.java
+++ b/src/client/DrawingUpdate.java
@@ -9,13 +9,17 @@ import java.rmi.server.UnicastRemoteObject;
public class DrawingUpdate extends UnicastRemoteObject implements IDrawingUpdate, Serializable {
- public DrawingUpdate() throws RemoteException {
+ private Client client;
+
+ public DrawingUpdate(Client client) throws RemoteException {
super();
+ this.client = client;
}
@Override
public boolean notifyDrawing(String fromClient, Shape drawing) throws RemoteException {
-
+ client.getPaintGUI().getDrawingArea().setDrawing(drawing);
+ client.getPaintGUI().getDrawingArea().repaint();
return false;
}
}
diff --git a/src/remote/IChatController.java b/src/remote/IChatController.java
index a9391bf..64e6811 100644
--- a/src/remote/IChatController.java
+++ b/src/remote/IChatController.java
@@ -6,4 +6,6 @@ import java.rmi.RemoteException;
public interface IChatController extends Remote
{
boolean broadcastMessage(String fromClient, String message) throws RemoteException;
+ boolean broadcastMessageUserLogin(String fromClient) throws RemoteException;
+ boolean broadcastMessageUserLogout(String fromClient) throws RemoteException;
}
diff --git a/src/remote/IChatUpdate.java b/src/remote/IChatUpdate.java
index 9921ed5..55b2e62 100644
--- a/src/remote/IChatUpdate.java
+++ b/src/remote/IChatUpdate.java
@@ -7,4 +7,6 @@ import java.rmi.RemoteException;
public interface IChatUpdate extends Remote, Serializable
{
boolean notifyChat(String fromClient, String message) throws RemoteException;
+ boolean notifyUserLogin(String fromClient) throws RemoteException;
+ boolean notifyUserLogout(String fromClient) throws RemoteException;
}
diff --git a/src/remote/IClientUpdate.java b/src/remote/IClientUpdate.java
new file mode 100644
index 0000000..9eb5bc9
--- /dev/null
+++ b/src/remote/IClientUpdate.java
@@ -0,0 +1,10 @@
+package remote;
+
+import java.io.Serializable;
+import java.rmi.Remote;
+import java.rmi.RemoteException;
+
+public interface IClientUpdate extends Remote, Serializable {
+
+ boolean notifyClient(String fromClient, String newUsername) throws RemoteException;
+}
diff --git a/src/remote/IUpdateController.java b/src/remote/IUpdateController.java
new file mode 100644
index 0000000..caad27c
--- /dev/null
+++ b/src/remote/IUpdateController.java
@@ -0,0 +1,17 @@
+package remote;
+
+import java.rmi.Remote;
+import java.rmi.RemoteException;
+import java.util.ArrayList;
+
+/**
+ * RMI Remote interface - IRemoteUpdate.
+ * Allows Server to command Client to update: canvas, chat, other.
+ */
+
+public interface IUpdateController extends Remote {
+
+ public void updateClient(String text) throws RemoteException;
+ public ArrayList<String> getUserList() throws RemoteException;
+ public void updateUserList(ArrayList userList) throws RemoteException;
+}
diff --git a/src/server/ChatController.java b/src/server/ChatController.java
index 8a5cc69..79c344a 100644
--- a/src/server/ChatController.java
+++ b/src/server/ChatController.java
@@ -33,4 +33,40 @@ public class ChatController extends UnicastRemoteObject implements IChatControll
return true;
}
+
+ @Override
+ public boolean broadcastMessageUserLogin(String fromClient) throws RemoteException {
+ System.out.print("Broadcasting message to everyone...");
+
+ IChatUpdate client;
+
+ for( User u : server.users )
+ {
+ client = u.getIChatUpdate();
+ client.notifyUserLogin(fromClient);
+ }
+
+ System.out.print("...DONE\n");
+ System.out.println(fromClient + " has joined the room.");
+
+ return true;
+ }
+
+ @Override
+ public boolean broadcastMessageUserLogout(String fromClient) throws RemoteException {
+ System.out.print("Broadcasting message to everyone...");
+
+ IChatUpdate client;
+
+ for( User u : server.users )
+ {
+ client = u.getIChatUpdate();
+ client.notifyUserLogout(fromClient);
+ }
+
+ System.out.print("...DONE\n");
+ System.out.println(fromClient + " has left the room.");
+
+ return true;
+ }
}
diff --git a/src/server/ClientController.java b/src/server/ClientController.java
index 8b7993d..f3753fa 100644
--- a/src/server/ClientController.java
+++ b/src/server/ClientController.java
@@ -1,8 +1,8 @@
package server;
import remote.IChatUpdate;
-import remote.IDrawingUpdate;
import remote.IClientController;
+import remote.IDrawingUpdate;
import java.io.Serializable;
import java.rmi.RemoteException;
@@ -20,7 +20,7 @@ public class ClientController extends UnicastRemoteObject implements IClientCont
@Override
public boolean join(String username, IChatUpdate clientChat, IDrawingUpdate clientDrawing) throws RemoteException
{
- server.chatController.broadcastMessage(username, " joined the room");
+ server.chatController.broadcastMessageUserLogin(username);
User newUser = new User(username, clientChat, clientDrawing);
@@ -45,7 +45,7 @@ public class ClientController extends UnicastRemoteObject implements IClientCont
{
server.users.remove(userIndex);
- server.chatController.broadcastMessage(username, username + " has left the server");
+ server.chatController.broadcastMessageUserLogout(username);
}
}
diff --git a/src/server/DrawingController.java b/src/server/DrawingController.java
index a1c07cc..1471e8a 100644
--- a/src/server/DrawingController.java
+++ b/src/server/DrawingController.java
@@ -1,12 +1,12 @@
package server;
-import java.awt.*;
-import java.rmi.RemoteException;
- import java.rmi.server.UnicastRemoteObject;
-
import remote.IDrawingController;
import remote.IDrawingUpdate;
+import java.awt.*;
+import java.rmi.RemoteException;
+import java.rmi.server.UnicastRemoteObject;
+
public class DrawingController extends UnicastRemoteObject implements IDrawingController {
private Server server;
@@ -17,7 +17,7 @@ public class DrawingController extends UnicastRemoteObject implements IDrawingCo
@Override
public boolean broadcastDrawing(String fromClient, Shape drawing) throws RemoteException {
- System.out.print("Broadcasting message to everyone...");
+ System.out.print("Broadcasting drawing to everyone...");
IDrawingUpdate client;
diff --git a/src/server/Server.java b/src/server/Server.java
index 1223ef4..0971b97 100644
--- a/src/server/Server.java
+++ b/src/server/Server.java
@@ -2,6 +2,7 @@ package server;
import remote.IChatController;
import remote.IClientController;
+import remote.IDrawingController;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
@@ -38,16 +39,21 @@ public class Server
public void run() throws RemoteException
{
+
+ LocateRegistry.createRegistry(1099);
Registry registry = LocateRegistry.getRegistry();
String clientControllerName = "ClientController";
String chatControllerName = "ChatController";
+ String drawingControllerName = "DrawingController";
IClientController clientController = new ClientController(this);
IChatController chatController = new ChatController(this);
+ IDrawingController drawingController = new DrawingController(this);
registry.rebind(clientControllerName, clientController);
registry.rebind(chatControllerName, chatController);
+ registry.rebind(drawingControllerName, drawingController);
System.out.println("Server is ready");
}
diff --git a/src/server/User.java b/src/server/User.java
index 2561805..51047ae 100644
--- a/src/server/User.java
+++ b/src/server/User.java
@@ -1,16 +1,13 @@
package server;
-import remote.IChatUpdate;
-import remote.IDrawingUpdate;
-
public class User
{
private String username;
- private IChatUpdate IChatUpdate;
- private IDrawingUpdate IDrawingUpdate;
+ private remote.IChatUpdate IChatUpdate;
+ private remote.IDrawingUpdate IDrawingUpdate;
private boolean isAdmin;
- public User(String username, IChatUpdate IChatUpdate, IDrawingUpdate IDrawingUpdate)
+ public User(String username, remote.IChatUpdate IChatUpdate, remote.IDrawingUpdate IDrawingUpdate)
{
this.username = username;
this.IChatUpdate = IChatUpdate;
@@ -23,11 +20,6 @@ public class User
return this.username;
}
- public IChatUpdate getIChatUpdate()
- {
- return this.IChatUpdate;
- }
-
public void setAdmin(boolean admin)
{
isAdmin = admin;
@@ -38,7 +30,10 @@ public class User
return isAdmin;
}
- public IDrawingUpdate getIDrawingUpdate() {
- return this.IDrawingUpdate;
+ public remote.IChatUpdate getIChatUpdate()
+ {
+ return this.IChatUpdate;
}
+
+ public remote.IDrawingUpdate getIDrawingUpdate() { return this.IDrawingUpdate; }
}
--
GitLab