diff --git a/src/GUI/ApplicationMain.java b/src/GUI/ApplicationMain.java index 2e6823a7ddf65b173a760f480840065b8152a812..60dc6aab08c0ef6186c2bc03b89b158531e2c2b3 100644 --- a/src/GUI/ApplicationMain.java +++ b/src/GUI/ApplicationMain.java @@ -15,7 +15,7 @@ public class ApplicationMain extends JPanel { private Client client; private ChatScreen chatScreen; private PaintGUI paintGUI; - private JFrame frame; + JFrame frame; public ChatScreen getChatScreen() { return chatScreen; } @@ -39,8 +39,7 @@ public class ApplicationMain extends JPanel { return answer; } - public String showAssignManagerMessage() { - int numUsers = client.getChatScreen().getKickUserComboBox().getItemCount(); + public String showAssignManagerMessage(int numUsers) { String[] userOptions = new String[numUsers]; for (int i = 0; i < numUsers; i++) { userOptions[i] = client.getChatScreen().getKickUserComboBox().getItemAt(i).toString(); @@ -60,7 +59,6 @@ public class ApplicationMain extends JPanel { public void createAndShowGUI() { frame = new JFrame("Application Main"); - JFrame.setDefaultLookAndFeelDecorated(true); Container content = frame.getContentPane(); content.setLayout(new BorderLayout()); content.add(paintGUI.getGlobal(), BorderLayout.WEST); @@ -99,8 +97,11 @@ public class ApplicationMain extends JPanel { try { client.getDrawingController().getImage(client.getUserName()); } - catch (RemoteException err) { - err.printStackTrace(); + catch (RemoteException err) + { + StartScreen.showErrorMessage("Connection with server lost"); + System.exit(0); + //err.printStackTrace(); } @@ -110,52 +111,7 @@ public class ApplicationMain extends JPanel { @Override public void windowClosing(WindowEvent arg0) { - - try { - if (client.getUserName().equals(client.getClientController().getAdmin())) { - - int terminateAppAnswer = showManagerQuitMessage(); - // If the manager terminates the application - if (terminateAppAnswer == 0) { - client.getClientController().kickAll(client.getUserName()); - exitApplication(); - } - else if (terminateAppAnswer == 1) { - int answer = showNextManagerMessage(); - // If the manager wants to assign the next manager manually - if (answer == 0) { - String newManager = showAssignManagerMessage(); - client.getClientController().assignAdmin(client.getUserName(), newManager); - client.getClientController().quit(client.getUserName()); - exitApplication(); - } - // If the manager wants to assign the next manager by random choice - if (answer == 1) { - int numUsers = client.getChatScreen().getKickUserComboBox().getItemCount(); - Random random = new Random(); - int randomUserIndex = random.nextInt(numUsers); - String newManager = client.getChatScreen().getKickUserComboBox().getItemAt(randomUserIndex).toString(); - client.getClientController().assignAdmin(client.getUserName(), newManager); - client.getClientController().quit(client.getUserName()); - exitApplication(); - } - } - - } - else { - int reply = JOptionPane.showConfirmDialog(null, - "Are you sure you want to quit the session?", - "Shut down session", JOptionPane.YES_NO_OPTION); - if( reply == 0 ) - { - client.getClientController().quit(client.getUserName()); - exitApplication(); - } - } - } catch (RemoteException e) { - e.printStackTrace(); - } - + closeWindow(); } }); @@ -169,9 +125,7 @@ public class ApplicationMain extends JPanel { } public void exitApplication(){ - System.out.println("I am in exit application"); frame.setVisible(false); - frame.dispose(); client.setVisibleStartScreen(); } @@ -179,4 +133,52 @@ public class ApplicationMain extends JPanel { frame.setVisible(true); chatScreen.setUserName(client.getUserName()); } + + public void closeWindow() { + try { + int numUsers = client.getChatScreen().getKickUserComboBox().getItemCount(); + if (client.getUserName().equals(client.getClientController().getAdmin()) && numUsers > 0) { + int terminateAppAnswer = showManagerQuitMessage(); + // If the manager terminates the application + if (terminateAppAnswer == 0) { + client.getClientController().kickAll(client.getUserName()); + exitApplication(); + } + else if (terminateAppAnswer == 1) { + int answer = showNextManagerMessage(); + // If the manager wants to assign the next manager manually + if (answer == 0) { + String newManager = showAssignManagerMessage(numUsers); + client.getClientController().assignAdmin(client.getUserName(), newManager); + client.getClientController().quit(client.getUserName()); + exitApplication(); + } + // If the manager wants to assign the next manager by random choice + if (answer == 1) { + Random random = new Random(); + int randomUserIndex = random.nextInt(numUsers); + String newManager = client.getChatScreen().getKickUserComboBox().getItemAt(randomUserIndex).toString(); + client.getClientController().assignAdmin(client.getUserName(), newManager); + client.getClientController().quit(client.getUserName()); + exitApplication(); + } + } + } + else { + int reply = JOptionPane.showConfirmDialog(null, + "Are you sure you want to quit the session?", + "Shut down session", JOptionPane.YES_NO_OPTION); + if (reply == 0) { + client.getClientController().quit(client.getUserName()); + exitApplication(); + } + } + } catch (RemoteException e) + { + StartScreen.showErrorMessage("Connection with server lost"); + System.exit(0); + //e.printStackTrace(); + } + } + } diff --git a/src/GUI/ChatScreen.java b/src/GUI/ChatScreen.java index 1ffd49fbd11023d27ec0e09eaf2d679e1c1310fd..c8a432fc142955bff2a696a1cd2c55525610bb8e 100644 --- a/src/GUI/ChatScreen.java +++ b/src/GUI/ChatScreen.java @@ -106,13 +106,24 @@ public class ChatScreen { public void setManagerToolsVisibility() { try { if (client.getClientController().getAdmin().equals(client.getUserName())){ - managersPanel.setVisible(true); + managersPanel.setEnabled(true); + kickUserComboBox.setEnabled(true); + kickOutButton.setEnabled(true); + promoteToManagerButton.setEnabled(true); + client.getApplicationMain().getPaintGUI().enableFileControl(); } else { - managersPanel.setVisible(false); + managersPanel.setEnabled(false); + kickUserComboBox.setEnabled(false); + kickOutButton.setEnabled(false); + promoteToManagerButton.setEnabled(false); + client.getApplicationMain().getPaintGUI().disableFileControl(); } - } catch (RemoteException e) { - e.printStackTrace(); + } catch (RemoteException e) + { + StartScreen.showErrorMessage("Connection with server lost"); + System.exit(0); + //e.printStackTrace(); } } @@ -146,26 +157,18 @@ public class ChatScreen { } catch (RemoteException ex) { - ex.printStackTrace(); + StartScreen.showErrorMessage("Connection with server lost"); + System.exit(0); + //ex.printStackTrace(); } } else if (e.getSource() == exitThisRoomButton) { - IClientController clientController = client.getClientController(); - try - { - System.out.println("Exit room button pressed"); - clientController.quit(client.getUserName()); - exitChatScreen(); - } - catch (RemoteException ex) - { - ex.printStackTrace(); - } + System.out.println("Exit room button pressed by: " + client.getUserName()); + client.getApplicationMain().closeWindow(); } else if (e.getSource() == kickOutButton) { - IClientController clientController = client.getClientController(); String toUser = kickUserComboBox.getSelectedItem().toString(); @@ -174,8 +177,9 @@ public class ChatScreen { clientController.kickUser(client.getUserName(), toUser); } catch (RemoteException ex) { - - ex.printStackTrace(); + StartScreen.showErrorMessage("Connection with server lost"); + System.exit(0); + //ex.printStackTrace(); } } @@ -190,7 +194,9 @@ public class ChatScreen { clientController.assignAdmin(client.getUserName(), toUser); } catch (RemoteException ex) { - ex.printStackTrace(); + StartScreen.showErrorMessage("Connection with server lost"); + System.exit(0); + //ex.printStackTrace(); } } diff --git a/src/GUI/DrawingArea.java b/src/GUI/DrawingArea.java index 98246f24433a09866c45df28c54893be39bb3c2b..cc4d387d4cb8e4b33d700062969746208520d97e 100644 --- a/src/GUI/DrawingArea.java +++ b/src/GUI/DrawingArea.java @@ -126,7 +126,8 @@ public class DrawingArea extends JPanel implements MouseMotionListener, MouseLis try { ImageIO.write(image, "PNG", file); } catch (IOException e) { - e.printStackTrace(); + StartScreen.showErrorMessage("Error in saving PNG"); + //e.printStackTrace(); } } @@ -137,7 +138,8 @@ public class DrawingArea extends JPanel implements MouseMotionListener, MouseLis try { ImageIO.write(imageJPG, "JPG", file); } catch (IOException e) { - e.printStackTrace(); + StartScreen.showErrorMessage("Error in saving JPG"); + //e.printStackTrace(); } } @@ -145,7 +147,8 @@ public class DrawingArea extends JPanel implements MouseMotionListener, MouseLis try { image = ImageIO.read(file); } catch (IOException e) { - e.printStackTrace(); + StartScreen.showErrorMessage("Error in opening file"); + //e.printStackTrace(); } repaint(); } @@ -213,7 +216,9 @@ public class DrawingArea extends JPanel implements MouseMotionListener, MouseLis try { client.getDrawingController().broadcastText(client.getUserName(), textString, g2.getFont(), shapeColor, startPoint); } catch (RemoteException ex) { - ex.printStackTrace(); + StartScreen.showErrorMessage("Connection with server lost"); + System.exit(0); + //ex.printStackTrace(); } repaint(); break; @@ -296,6 +301,7 @@ public class DrawingArea extends JPanel implements MouseMotionListener, MouseLis try { drawingController.broadcastDrawing(client.getUserName(), drawing, currentMode.toString(), shapeColor, strokeSize); + // if (currentMode == Mode.TEXT){ // notifyUsingTimer(); // } @@ -303,13 +309,18 @@ public class DrawingArea extends JPanel implements MouseMotionListener, MouseLis // drawingController.broadcastDrawingUserStopped(client.getUserName()); // } - } catch (RemoteException ex) { - ex.printStackTrace(); + + } catch (RemoteException ex) + { + StartScreen.showErrorMessage("Connection with server lost"); + System.exit(0); + //ex.printStackTrace(); } drawing = null; } + // private void notifyUsingTimer() throws RemoteException { // // Timer timer = new Timer(4000, new ActionListener() @@ -335,6 +346,7 @@ public class DrawingArea extends JPanel implements MouseMotionListener, MouseLis // timer.start(); // } + @Override public void mouseEntered(MouseEvent e) { @@ -410,11 +422,15 @@ public class DrawingArea extends JPanel implements MouseMotionListener, MouseLis try { if (currentMode != Mode.TEXT){ drawingController.broadcastDraggingDrawing(client.getUserName(), drawing, currentMode.toString(), shapeColor, strokeSize); + // drawingController.broadcastDrawingUser(client.getUserName()); + } } catch (RemoteException ex) { - ex.printStackTrace(); + StartScreen.showErrorMessage("Connection with server lost"); + System.exit(0); + //ex.printStackTrace(); } } diff --git a/src/GUI/PaintGUI.java b/src/GUI/PaintGUI.java index bf6c13b8510f83203590b7b48f3ee1696b5c4447..7d06d8a9a237a7a514807f4ee5fa5119e1314352 100644 --- a/src/GUI/PaintGUI.java +++ b/src/GUI/PaintGUI.java @@ -84,7 +84,8 @@ public class PaintGUI extends JPanel { textBtn = new JButton(new ImageIcon(textIcon)); textBtn.addActionListener(actionListener); } catch (Exception e) { - e.printStackTrace(); + StartScreen.showErrorMessage("Error setting up canvas buttons"); + //e.printStackTrace(); } strokeOptions = new JComboBox(strokes); @@ -236,7 +237,9 @@ public class PaintGUI extends JPanel { } catch (RemoteException err) { - err.printStackTrace(); + StartScreen.showErrorMessage("Connection with server lost"); + System.exit(0); + //err.printStackTrace(); } ByteArrayOutputStream baos = new ByteArrayOutputStream(); @@ -246,7 +249,8 @@ public class PaintGUI extends JPanel { baos.close(); } catch (IOException err) { - err.printStackTrace(); + StartScreen.showErrorMessage("Error writing file"); + //err.printStackTrace(); } try { @@ -254,7 +258,9 @@ public class PaintGUI extends JPanel { client.getDrawingController().broadcastUpdateImage(client.getUserName()); } } catch (RemoteException ex) { - ex.printStackTrace(); + StartScreen.showErrorMessage("Connection with server lost"); + System.exit(0); + //ex.printStackTrace(); } } @@ -410,4 +416,20 @@ public class PaintGUI extends JPanel { drawingArea.setModeText(textString, size); } + public void disableFileControl() { + newBtn.setEnabled(false); + openBtn.setEnabled(false); + saveBtn.setEnabled(false); + saveAsBtn.setEnabled(false); + clearBtn.setEnabled(false); + } + + public void enableFileControl() { + newBtn.setEnabled(true); + openBtn.setEnabled(true); + saveBtn.setEnabled(true); + saveAsBtn.setEnabled(true); + clearBtn.setEnabled(true); + } + } \ No newline at end of file diff --git a/src/GUI/StartScreen.form b/src/GUI/StartScreen.form index f348817e1243275dfd3dd145b7bc1983957c0624..c6bb9dc4eec1db5e8c1867f31a1ed3d089b864e1 100644 --- a/src/GUI/StartScreen.form +++ b/src/GUI/StartScreen.form @@ -1,14 +1,14 @@ <?xml version="1.0" encoding="UTF-8"?> <form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="GUI.StartScreen"> - <grid id="27dc6" binding="panel1" layout-manager="GridLayoutManager" row-count="3" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1"> + <grid id="27dc6" binding="mainPanel" layout-manager="GridLayoutManager" row-count="4" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1"> <margin top="0" left="0" bottom="0" right="0"/> <constraints> - <xy x="20" y="20" width="833" height="400"/> + <xy x="20" y="20" width="833" height="417"/> </constraints> <properties/> <border type="none"/> <children> - <grid id="abaca" layout-manager="GridLayoutManager" row-count="3" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1"> + <grid id="abaca" binding="titlePanel" layout-manager="GridLayoutManager" row-count="3" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1"> <margin top="0" left="0" bottom="0" right="0"/> <constraints> <grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false"> @@ -29,33 +29,33 @@ </constraints> <properties> <font name="Apple SD Gothic Neo" size="20"/> - <text value="Distributed Whiteboard App"/> + <text value="Shared Whiteboard App"/> </properties> </component> <component id="e3c91" class="javax.swing.JTextPane" binding="information"> <constraints> - <grid row="2" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="6" anchor="0" fill="3" indent="0" use-parent-layout="false"> + <grid row="2" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"> <preferred-size width="150" height="70"/> </grid> </constraints> <properties> <editable value="false"/> - <text value="Please provide your username and password, as well as server's IP to start."/> + <text value="Please provide your username and password, as well as server's IP to join."/> </properties> </component> </children> </grid> - <grid id="790e1" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1"> + <grid id="790e1" binding="joinButtonPanel" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1"> <margin top="0" left="0" bottom="0" right="0"/> <constraints> - <grid row="2" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false"/> + <grid row="3" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false"/> </constraints> <properties/> <border type="none"/> <children> <component id="ea0c1" class="javax.swing.JButton" binding="joinButton"> <constraints> - <grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="7" anchor="0" fill="1" indent="0" use-parent-layout="false"/> + <grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="7" anchor="0" fill="0" indent="0" use-parent-layout="false"/> </constraints> <properties> <text value="JOIN WHITEBOARD"/> @@ -63,10 +63,10 @@ </component> </children> </grid> - <grid id="6ef31" layout-manager="GridLayoutManager" row-count="4" column-count="4" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1"> + <grid id="6ef31" binding="logInPanel" layout-manager="GridLayoutManager" row-count="4" column-count="4" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1"> <margin top="0" left="0" bottom="0" right="0"/> <constraints> - <grid row="1" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false"/> + <grid row="2" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false"/> </constraints> <properties> <name value=""/> @@ -133,6 +133,35 @@ </component> </children> </grid> + <grid id="a1249" binding="waitingPanel" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1"> + <margin top="0" left="0" bottom="0" right="0"/> + <constraints> + <grid row="1" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/> + </constraints> + <properties> + <visible value="false"/> + </properties> + <border type="none"/> + <children> + <vspacer id="b85ee"> + <constraints> + <grid row="0" column="1" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/> + </constraints> + </vspacer> + <component id="3a3ef" class="javax.swing.JTextPane"> + <constraints> + <grid row="0" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"> + <preferred-size width="150" height="70"/> + </grid> + </constraints> + <properties> + <editable value="false"/> + <text value="Waiting for manager's approval ..."/> + <visible value="true"/> + </properties> + </component> + </children> + </grid> </children> </grid> </form> diff --git a/src/GUI/StartScreen.java b/src/GUI/StartScreen.java index 6b030e1fada0d4a4b0576dbc088fd53aa94b93e8..acd121f5828f6ff5a7f545f5d0fa9f5254fd1a10 100644 --- a/src/GUI/StartScreen.java +++ b/src/GUI/StartScreen.java @@ -2,20 +2,18 @@ package GUI; import client.Client; -import javax.imageio.ImageIO; import javax.swing.*; +import javax.swing.event.ChangeEvent; +import javax.swing.event.ChangeListener; import java.awt.event.*; -import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; -import java.io.File; -import java.io.IOException; public class StartScreen { private JTextPane information; - private JPanel panel1; + private JPanel mainPanel; private JTextField usernameField; private JButton joinButton; @@ -26,6 +24,11 @@ public class StartScreen { private JTextField serverField; private JTextField textField3; private JPasswordField passwordField; + private JPanel logInPanel; + private JPanel joinButtonPanel; + private JPanel titlePanel; + private JPanel waitingPanel; + private ButtonModel joinButtonModel = joinButton.getModel(); JFrame frame; @@ -36,6 +39,20 @@ public class StartScreen { this.client = client; } + ChangeListener changeListener = new ChangeListener() { + @Override + public void stateChanged(ChangeEvent e) { + if (joinButtonModel.isPressed()){ + waitingPanel.setVisible(true); + logInPanel.setVisible(false); + } + else { + waitingPanel.setVisible(false); + logInPanel.setVisible(true); + } + } + }; + ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent e) @@ -48,23 +65,20 @@ public class StartScreen { if (userName.length() <= 15) { int connectionStatus = client.connect(userName, serverAddress, password); - if( connectionStatus == 1 ) + + if( connectionStatus == 0 ) { frame.setVisible(false); frame.dispose(); - if (client.getApplicationMain().getFrame() == null) { - client.startApplication(); - } - else { - client.clearChat(); - client.clearDrawingArea(); - client.setVisibleApplication(); - } - + client.startApplication(); } - else if( connectionStatus == 2 || connectionStatus == 6 ) + else if( connectionStatus == 1) { - showErrorMessage("Duplicate usernameField: Please enter a new usernameField"); + showErrorMessage("The manager rejected your join request"); + } + else if( connectionStatus == 2) + { + showErrorMessage("Duplicate username: Please enter a different username"); } else if( connectionStatus == 3 ) { @@ -74,12 +88,6 @@ public class StartScreen { { showErrorMessage("Incorrect Password"); } - else if( connectionStatus == 5 ) - { - frame.setVisible(false); - frame.dispose(); - client.setVisibleApplication(); - } else { showErrorMessage("Unknown Connection Status"); @@ -94,18 +102,18 @@ public class StartScreen { }; - public static void showErrorMessage(String message) { JOptionPane.showMessageDialog(null, - message, "Error", JOptionPane.ERROR_MESSAGE); + message, "Error", JOptionPane.ERROR_MESSAGE); } public void go() { joinButton.addActionListener(actionListener); + joinButtonModel.addChangeListener(changeListener); frame = new JFrame("StartScreen"); - frame.setContentPane(panel1); + frame.setContentPane(mainPanel); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); @@ -115,13 +123,16 @@ public class StartScreen { //frame.getContentPane().add(new JPanelWithBackground("sample.jpeg")); frame.setVisible(true); - frame.addWindowListener(new WindowAdapter() { - @Override - public void windowOpened(WindowEvent e) { + frame.addComponentListener(new ComponentAdapter() { + public void componentShown(ComponentEvent e) { if (isAppTerminated()){ showKickAllMessage(); setAppTerminated(false); } + if (isKickedOut()){ + showKickOutMessage(); + setKickedOut(false); + } } }); } @@ -152,6 +163,12 @@ public class StartScreen { JOptionPane.ERROR_MESSAGE); } + public void showKickOutMessage() { + JOptionPane.showMessageDialog(null, + "The manager kicked you out of the whiteboard", "Kicked out", + JOptionPane.ERROR_MESSAGE); + } + // public class JPanelWithBackground extends JPanel // { // diff --git a/src/client/Client.java b/src/client/Client.java index acfe956e63539b52110b1831e0570e2d2a1f87c5..b2033f41c3ae2bda6175146b59423132758cfe9d 100644 --- a/src/client/Client.java +++ b/src/client/Client.java @@ -155,69 +155,36 @@ public class Client if (serverAddress.trim().isEmpty()) { serverAddress = DEFAULT_SERVER_ADDRESS; } - // If the client wants to reconnect to the same server using the same username - if (registryServer != null && getUserName().equals(userName) - && getServerAddress().equals(serverAddress) - && getApplicationMain().getFrame() != null) { - - System.out.println("Server address:" + getServerAddress()); - System.out.println("User name:" + getUserName()); - System.out.println("Password: " + password); - - if (clientController.setSharedKey(this.encryptionUpdate)) { - SealedObject sealedPassword = EncryptDecrypt.encryptString(password, this.encryptionUpdate.getSharedSecretKey()); - - if (clientController.checkPassword(sealedPassword)) { - if (clientController.join(getUserName(), this.chatUpdate, this.clientUpdate, this.drawingUpdate)) { - System.out.println("Connected to server"); - return 5; - } else { - return 6; - } - } - else { - return 4; - } - } - else - { - return 4; - } - } // New connection - else { - setUserName(userName); - setServerAddress(serverAddress); - System.out.println("Server address:" + serverAddress); - - registryServer = LocateRegistry.getRegistry(serverAddress); - chatController = (IChatController) registryServer.lookup("ChatController"); - clientController = (IClientController) registryServer.lookup("ClientController"); - drawingController = (IDrawingController) registryServer.lookup("DrawingController"); - - System.out.println("User name:" + getUserName()); - System.out.println("Password: " + password); - if (clientController.setSharedKey(this.encryptionUpdate)) { - SealedObject sealedPassword = EncryptDecrypt.encryptString(password, this.encryptionUpdate.getSharedSecretKey()); - - if (clientController.checkPassword(sealedPassword)) { - if (clientController.join(getUserName(), this.chatUpdate, this.clientUpdate, this.drawingUpdate)) { - System.out.println("Connected to server"); - return 1; - } else { - return 2; - } - } - else { - return 4; - } + setUserName(userName); + setServerAddress(serverAddress); + System.out.println("Server address:" + serverAddress); + + registryServer = LocateRegistry.getRegistry(serverAddress); + chatController = (IChatController) registryServer.lookup("ChatController"); + clientController = (IClientController) registryServer.lookup("ClientController"); + drawingController = (IDrawingController) registryServer.lookup("DrawingController"); + + System.out.println("User name:" + getUserName()); + System.out.println("Password: " + password); + + if (clientController.setSharedKey(this.encryptionUpdate)) { + SealedObject sealedPassword = EncryptDecrypt.encryptString(password, this.encryptionUpdate.getSharedSecretKey()); + + if (clientController.checkPassword(sealedPassword)) { + int joinStatus = clientController.join(getUserName(), this.chatUpdate, this.clientUpdate, this.drawingUpdate); + return joinStatus; } else { return 4; } - } + else + { + return 4; + } + } catch (Exception e) { diff --git a/src/client/ClientUpdate.java b/src/client/ClientUpdate.java index 860a34b829d1685b3150a8394c9137e14b61eeb5..a45130052bdc3db3fcc0662f42ca4a854fdb92f8 100644 --- a/src/client/ClientUpdate.java +++ b/src/client/ClientUpdate.java @@ -80,17 +80,44 @@ public class ClientUpdate extends UnicastRemoteObject implements IClientUpdate, } @Override - public void notifyManagerActions(String toClient, Action action) throws RemoteException { + public int notifyManagerActions(String toClient, Action action) throws RemoteException { + int answer = -1; switch (action) { case KICKOUT: - client.getChatScreen().getChatDisplayBox().append(toClient + " has been kicked out by the manager.\n"); + if (client.getUserName().equals(toClient)) { + client.getStartScreen().setKickedOut(true); + client.getApplicationMain().exitApplication(); + client.getChatScreen().getChatDisplayBox().append(" You were kicked out by the manager.\n"); + } + else { + client.getChatScreen().getChatDisplayBox().append(toClient + " has been kicked out by the manager.\n"); + } break; case ASSIGNADMIN: - client.getChatScreen().getChatDisplayBox().append(toClient + " is the new manager.\n"); + if (client.getUserName().equals(toClient)) { + JOptionPane.showMessageDialog(null, + "Congratulations! the manager promoted you, now you are the new manager.", + "Promoted", + JOptionPane.INFORMATION_MESSAGE); + client.getChatScreen().getChatDisplayBox().append("You are the new manager.\n"); + } + else { + client.getChatScreen().getChatDisplayBox().append(toClient + " is the new manager.\n"); + } break; case KICKALL: client.getStartScreen().setAppTerminated(true); client.getApplicationMain().exitApplication(); + break; + case JOINAPPROVAL: + if (client.getUserName().equals(client.getClientController().getAdmin())) { + answer = JOptionPane.showConfirmDialog(null, + "The user: " + toClient + " wants to join to your whiteboard, do you want to accept?", + "Join request", JOptionPane.YES_NO_OPTION); + } + break; } + return answer; } + } diff --git a/src/client/DrawingUpdate.java b/src/client/DrawingUpdate.java index bb920065cd8235615d3fb9ad60f49bcf0b84e202..e7f4efccc1662ab0401a1a128f2b32618a0c8e9c 100644 --- a/src/client/DrawingUpdate.java +++ b/src/client/DrawingUpdate.java @@ -1,5 +1,6 @@ package client; +import GUI.StartScreen; import remote.IDrawingUpdate; import javax.swing.*; @@ -155,8 +156,10 @@ public class DrawingUpdate extends UnicastRemoteObject implements IDrawingUpdate // do whatever you wish with the image } - catch (IOException err) { - err.printStackTrace(); + catch (IOException err) + { + StartScreen.showErrorMessage("Error in receiving image"); + //err.printStackTrace(); } return true; } @@ -185,8 +188,10 @@ class removeClientTimer extends Thread { elementIndex = temp.indexOf(fromClient); temp.remove(elementIndex); } - } catch (InterruptedException e) { - e.printStackTrace(); + } catch (InterruptedException e) + { + StartScreen.showErrorMessage("Interrupted Exception"); + //e.printStackTrace(); } } diff --git a/src/client/EncryptionUpdate.java b/src/client/EncryptionUpdate.java index 52d1fa52d2f6ee065599c615ec936c707b733e24..33500cb452a6c77a573df847004eacdb0b646b32 100644 --- a/src/client/EncryptionUpdate.java +++ b/src/client/EncryptionUpdate.java @@ -1,5 +1,6 @@ package client; +import GUI.StartScreen; import remote.IDrawingUpdate; import remote.IEncryptionUpdate; @@ -35,15 +36,20 @@ public class EncryptionUpdate extends UnicastRemoteObject implements IEncryption } catch (NoSuchAlgorithmException e) { - e.printStackTrace(); + StartScreen.showErrorMessage("Shared key error"); + //e.printStackTrace(); } catch (NoSuchProviderException e) { - e.printStackTrace(); + StartScreen.showErrorMessage("Shared key error"); + //e.printStackTrace(); } catch (NoSuchPaddingException | InvalidKeyException e) { - e.printStackTrace(); + StartScreen.showErrorMessage("Shared key error"); + //e.printStackTrace(); } catch (BadPaddingException e) { - e.printStackTrace(); + StartScreen.showErrorMessage("Shared key error"); + //e.printStackTrace(); } catch (IllegalBlockSizeException e) { - e.printStackTrace(); + StartScreen.showErrorMessage("Shared key error"); + //e.printStackTrace(); } @@ -59,9 +65,8 @@ public class EncryptionUpdate extends UnicastRemoteObject implements IEncryption } catch (Exception e){ - e.printStackTrace(); + StartScreen.showErrorMessage("Encryption Update error"); + //e.printStackTrace(); } - } - } diff --git a/src/remote/IClientController.java b/src/remote/IClientController.java index 67b319d8d260b6dacec7edb0d87d0fe1a4a5122a..0233a5841bf26f3909b1c5bb90524517872691f6 100644 --- a/src/remote/IClientController.java +++ b/src/remote/IClientController.java @@ -6,9 +6,9 @@ import java.rmi.RemoteException; public interface IClientController extends Remote { - enum Action {KICKOUT, ASSIGNADMIN, KICKALL}; + enum Action {KICKOUT, ASSIGNADMIN}; - boolean join(String username, IChatUpdate clientChat, IClientUpdate clientUpdate, IDrawingUpdate clientDrawing) throws RemoteException; + int join(String username, IChatUpdate clientChat, IClientUpdate clientUpdate, IDrawingUpdate clientDrawing) throws RemoteException; void quit(String username) throws RemoteException; @@ -23,5 +23,7 @@ public interface IClientController extends Remote String getAdmin() throws RemoteException; boolean checkPassword(SealedObject sealedPassword) throws RemoteException; - public boolean setSharedKey(IEncryptionUpdate encryptionUpdate) throws RemoteException; + + boolean setSharedKey(IEncryptionUpdate encryptionUpdate) throws RemoteException; + } diff --git a/src/remote/IClientUpdate.java b/src/remote/IClientUpdate.java index 3788bd2810f5cf17dd822be3dc4a45f2b308ca4e..0f818fb80ae5664b6b0312ad30a83dd18f515c58 100644 --- a/src/remote/IClientUpdate.java +++ b/src/remote/IClientUpdate.java @@ -6,10 +6,10 @@ import java.rmi.RemoteException; public interface IClientUpdate extends Remote, Serializable { - enum Action {KICKOUT, ASSIGNADMIN, KICKALL}; + enum Action {KICKOUT, ASSIGNADMIN, KICKALL, JOINAPPROVAL}; boolean updateUserList(String[] users) throws RemoteException; void terminateChat() throws RemoteException; - void notifyManagerActions(String toClient, Action action) throws RemoteException; + int notifyManagerActions(String toClient, Action action) throws RemoteException; void setVisibility() throws RemoteException; } diff --git a/src/server/ClientController.java b/src/server/ClientController.java index d8ced8a05b6310956f922349b69e0bf42a1a63c4..60b181f54725290ba80c93e9e09b78f96a48e3cf 100644 --- a/src/server/ClientController.java +++ b/src/server/ClientController.java @@ -19,33 +19,42 @@ public class ClientController extends UnicastRemoteObject implements IClientCont private SecretKey sharedSecretKey; @Override - public boolean join(String username, IChatUpdate clientChat, IClientUpdate clientUpdate, IDrawingUpdate clientDrawing) throws RemoteException + public int join(String username, IChatUpdate clientChat, IClientUpdate clientUpdate, IDrawingUpdate clientDrawing) throws RemoteException { - System.out.println("I am in join"); - if( getUserIndex(username) < 0 ) - { - // user with same username is not connected - server.chatController.broadcastMessageUserLogin(username); - + // If there is no user in the list then let the user be the manager + if(server.users.size() == 0) { User newUser = new User(username, clientChat, clientUpdate, clientDrawing, sharedSecretKey); - server.users.add(newUser); - - if(server.users.size() == 1) - { - newUser.setAdmin(true); - } - - System.out.println(username + " registered successfully"); - + newUser.setAdmin(true); broadcastUserList(); - - return true; + clientUpdate.setVisibility(); + return 0; + } + // If there are users in the user list, then check if the username is used + else if (getUserIndex(username) < 0 ) { + // Ask the manager for the join approval + int managerIndex = getUserIndex(getAdmin()); + User manager = server.users.get(managerIndex); + IClientUpdate client; + client = manager.getIClientUpdate(); + int answer = client.notifyManagerActions(username, IClientUpdate.Action.JOINAPPROVAL); + + // If the manager accepts the new user to enter, then add the user to the list + if (answer == 0) { + server.chatController.broadcastMessageUserLogin(username); + User newUser = new User(username, clientChat, clientUpdate, clientDrawing, sharedSecretKey); + server.users.add(newUser); + broadcastUserList(); + return 0; + } + else { + return 1; + } } - else - { - return false; + else { + return 2; } + } @Override @@ -102,17 +111,9 @@ public class ClientController extends UnicastRemoteObject implements IClientCont if ( adminIndex >= 0 && userIndex >= 0 && server.users.get(adminIndex).isAdmin() ) { - - try - { - server.users.get(userIndex).getIClientUpdate().terminateChat(); - } - finally { - server.users.remove(userIndex); - broadcastUserList(); - broadcastManagerMessage(kickedUser, Action.KICKOUT); - } - + broadcastManagerMessage(kickedUser, Action.KICKOUT); + server.users.remove(userIndex); + broadcastUserList(); return true; } diff --git a/src/server/Server.java b/src/server/Server.java index b65b320e1acd0861b99fa9eeb41d0cb345a9c8cc..da4be37fa3a1dab5dd7921f709e011e1c414fe49 100644 --- a/src/server/Server.java +++ b/src/server/Server.java @@ -18,6 +18,7 @@ import java.util.ArrayList; public class Server { protected ArrayList<User> users; + protected ArrayList<User> waitingList; protected ClientController clientController; protected ChatController chatController; @@ -29,6 +30,7 @@ public class Server { password = null; users = new ArrayList<User>(); + waitingList = new ArrayList<User>(); clientController = new ClientController(this); chatController = new ChatController(this); drawingController = new DrawingController(this); @@ -110,6 +112,6 @@ public class Server } System.out.println("IP Address:- " + inetAddress.getHostAddress()); - System.out.println("Host Name:- " + inetAddress.getHostName()); + //System.out.println("Host Name:- " + inetAddress.getHostName()); } }