diff --git a/src/GUI/ChatScreen.java b/src/GUI/ChatScreen.java index 27f9402f9cdc23f1342d0b771eda52d1921b68f0..d2421903134b59d24171516bae0a190556e04150 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 9d2183bb0322e0e93cb80ec9f31e8f51da0b964a..b2349f28ff776cae3cb9e1f9b0b8ca6d284c0150 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 74d68ce593ed415444844dbbf67269dc2b951cd1..90641c9ef906983ec5bb2bb80f8721fadbd174fc 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 6fdb1bdad02e8540b44c4cafefbd81f36830a104..77556623ec82c8efc4ec20a6da9f98566d176490 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 76dc1309b8432b1b0d13998a3fbe12998f319602..04087e80db705c26881b58bcde46bfead77687d7 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 72682a9249b2ceae23b1a156d620252df2fdd778..e12ac0337543a525a050c67d51de4dbb98f13b3c 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 6a1845d0631815a7f46c925bda139d1f841f03a8..5d11a48b9d1d3d3b7a15bd249ced4f14e6370f1a 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 b825a8a4bcd3a906e5786e02193f89e0742bfdd4..0000000000000000000000000000000000000000 --- 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 eadd3e92d212ee264f39f6a478ad14f67dc1bca5..7716e995e7ace7276607d281cb548b6c61dd76c6 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 a9391bf3c4e8425871c5d2740c95de39ec482868..64e681194d8783b3a45feb132b84d11140fa78d0 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 9921ed5618d0f326b3feeedceff7a4160b5da540..55b2e62445fe0e9d455518d4cbe188adfcb89a9e 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 0000000000000000000000000000000000000000..9eb5bc90e58e864fdf182b29e871c410adf7f4fb --- /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 0000000000000000000000000000000000000000..caad27c6264f84f0e9890a2e954f58d6510b7e7d --- /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 8a5cc692b93722bfeec798279e7f9a58b4db0a16..79c344a9a50743c6dbeede8238f75b2ffb7ba579 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 8b7993d43263c03f0c4a6bb5ce4b5283c69ef5b0..f3753fa6f0ad8660d87cb0cb91163a699329d17b 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 a1c07ccf33b9c2ddec6bb6c62f773c05aa56ba8f..1471e8a38a33c3fc1d496f36ffa306ad0f0a1888 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 1223ef4887e84f330b3f67267d6febf19db71df8..0971b974feaf49386eb2ea18664c0dbc6bf91c7b 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 25618059d8fd88d27434baab9a923b8a8c3e028c..51047ae656f86c128b532782af263dbd91dceb7a 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; } }