diff --git a/.idea/misc.xml b/.idea/misc.xml index e208459b8afde5f7980720efd6bbb97f7ae24541..fe18ee620100e2d75aa025f3853d282724948ec7 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,10 @@ <?xml version="1.0" encoding="UTF-8"?> <project version="4"> +<<<<<<< HEAD <component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK"> +======= + <component name="ProjectRootManager" version="2" languageLevel="JDK_12" default="false" project-jdk-name="12" project-jdk-type="JavaSDK"> +>>>>>>> 319c99e690794f9fba4fc91d12c81e0115df012a <output url="file://$PROJECT_DIR$/out" /> </component> </project> \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml index bd680ec3dd6a54e5a7789c6a688cedf3147ed03e..326e2be2911aaef96184c3049e4db19d8ccbb51c 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -5,7 +5,11 @@ <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/src/GUI/ApplicationMain.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/GUI/ApplicationMain.java" afterDir="false" /> <change beforePath="$PROJECT_DIR$/src/GUI/ChatScreen.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/GUI/ChatScreen.java" afterDir="false" /> + <change beforePath="$PROJECT_DIR$/src/GUI/StartScreen.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/GUI/StartScreen.java" afterDir="false" /> <change beforePath="$PROJECT_DIR$/src/client/Client.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/client/Client.java" afterDir="false" /> + <change beforePath="$PROJECT_DIR$/src/client/ClientUpdate.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/client/ClientUpdate.java" afterDir="false" /> + <change beforePath="$PROJECT_DIR$/src/server/ChatController.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/server/ChatController.java" afterDir="false" /> + <change beforePath="$PROJECT_DIR$/src/server/ClientController.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/server/ClientController.java" afterDir="false" /> </list> <option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" /> <option name="SHOW_DIALOG" value="false" /> @@ -122,7 +126,7 @@ <option name="presentableId" value="Default" /> <updated>1571631720382</updated> <workItem from="1571631723795" duration="17601000" /> - <workItem from="1571654347656" duration="23038000" /> + <workItem from="1571654347656" duration="25114000" /> </task> <task id="LOCAL-00001" summary="Changed the join method to send over a reference of the ClientUpdate Interface"> <created>1571646862883</created> diff --git a/src/GUI/ApplicationMain.java b/src/GUI/ApplicationMain.java index c7b86f80b9bdd8c08c9ab1646f435893ef9bd680..f58583c786fdc49d256d02afc788c81f773f9f06 100644 --- a/src/GUI/ApplicationMain.java +++ b/src/GUI/ApplicationMain.java @@ -28,36 +28,45 @@ public class ApplicationMain extends JPanel { this.paintGUI = new PaintGUI(client); } - public void createAndShowGUI() { + 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); content.add(chatScreen.panel2, BorderLayout.EAST); + chatScreen.setUserName(client.getUserName()); + SwingUtilities.getRootPane(chatScreen.getSendButton()).setDefaultButton(chatScreen.getSendButton()); frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent arg0) { - int answer = JOptionPane.showConfirmDialog(null, + int reply = JOptionPane.showConfirmDialog(null, "Are you sure you want to quit the session?", "Shut down session", JOptionPane.YES_NO_OPTION); - System.out.println(answer); - if (answer == 0) + + if( reply == JOptionPane.YES_OPTION ) { - System.out.println("Quitting session"); try { client.getClientController().quit(client.getUserName()); } catch (RemoteException e) { - e.printStackTrace(); + StartScreen.showErrorMessage("Error in quitting the server"); + //e.printStackTrace(); } + System.exit(0); } + + if( reply == JOptionPane.NO_OPTION ) + { + //do nothing + } } }); @@ -68,5 +77,4 @@ public class ApplicationMain extends JPanel { frame.pack(); frame.setVisible(true); } - } diff --git a/src/GUI/ChatScreen.java b/src/GUI/ChatScreen.java index 3503299316d79f75d32f69028489d5ddc71e40db..b36a1a53b732ce6027fb4fc3de3dec9c97b63ee4 100644 --- a/src/GUI/ChatScreen.java +++ b/src/GUI/ChatScreen.java @@ -39,18 +39,21 @@ public class ChatScreen { public Client client; - public ChatScreen(Client client) { this.client = client; yourNameDisplay.setText(client.getUserName()); + exitThisRoomButton.addActionListener(actionListener); quitButton.addActionListener(actionListener); sendButton.addActionListener(actionListener); kickOutButton.addActionListener(actionListener); promoteToManagerButton.addActionListener(actionListener); - } + public void setUserName(String userName) + { + this.yourNameDisplay.setText(userName); + } public JButton getSendButton() { return sendButton; diff --git a/src/GUI/StartScreen.java b/src/GUI/StartScreen.java index 77556623ec82c8efc4ec20a6da9f98566d176490..10ae2adbd8c2b4b5a47b8974c2eec8ab072b57be 100644 --- a/src/GUI/StartScreen.java +++ b/src/GUI/StartScreen.java @@ -33,32 +33,42 @@ public class StartScreen { { if (e.getSource() == joinButton) { - client.setServerAddress(textField2.getText()); - client.setUsername(textField1.getText()); + String serverAddress = textField2.getText(); + String userName = textField1.getText(); - if( client.connect() ) + int connectionStatus = client.connect(userName, serverAddress); + + if( connectionStatus == 1 ) { frame.setVisible(false); frame.dispose(); - //client.doSomething(); - new ChatScreen(client); + client.startApplication(); + } + else if( connectionStatus == 2 ) + { + showErrorMessage("Duplicate username: Please enter a new username"); + } + else if( connectionStatus == 3 ) + { + showErrorMessage("Cannot connect to server: Please check the server address"); } else { - showErrorMessage("Could not connect to server..."); + showErrorMessage("Unknown Connection Status"); } } } }; - private void showErrorMessage(String message) + public static void showErrorMessage(String message) { JOptionPane.showMessageDialog(null, - message, "Error", JOptionPane.PLAIN_MESSAGE); + message, "Error", JOptionPane.ERROR_MESSAGE); } - public void go(){ + public void go() + { joinButton.addActionListener(actionListener); frame = new JFrame("StartScreen"); frame.setContentPane(panel1); diff --git a/src/client/Client.java b/src/client/Client.java index c8b7f1752cee1559ed12f0219172e016f3733e1b..5eed15f56e0382d5755970a297a77061654b344c 100644 --- a/src/client/Client.java +++ b/src/client/Client.java @@ -14,6 +14,9 @@ import java.rmi.registry.Registry; public class Client { + private final String DEFAULT_USERNAME = "Anonymous"; + private final String DEFAULT_SERVER_ADDRESS = "localhost"; + private String userName; private String serverAddress; @@ -28,9 +31,30 @@ public class Client private StartScreen startScreen; private PaintGUI paintGUI; - private ApplicationMain applicationMain; + public String getUserName() + { + return this.userName; + } + + public void setUserName(String userName) + { + this.userName = userName; + } + + public String getServerAddress() + { + return serverAddress; + } + + public void setServerAddress(String serverAddress) + { + this.serverAddress = serverAddress; + } + + private ApplicationMain applicationMain; + public ApplicationMain getApplicationMain() { return applicationMain; } public PaintGUI getPaintGUI() { @@ -53,21 +77,6 @@ public class Client public IDrawingController getDrawingController() { return drawingController; } - public void setUsername(String userName) - { - this.userName = userName; - } - - public String getUserName() - { - return this.userName; - } - - public void setServerAddress(String serverAddress) - { - this.serverAddress = serverAddress; - } - public Client(String username) throws RemoteException { @@ -75,6 +84,7 @@ public class Client this.clientUpdate = new ClientUpdate(this); this.chatUpdate = new ChatUpdate(this); this.drawingUpdate = new DrawingUpdate(this); + this.startScreen = new StartScreen(this); this.applicationMain = new ApplicationMain(this); } @@ -83,40 +93,72 @@ public class Client try { Client client = new Client(args[0]); - client.connect(); - client.getApplicationMain().createAndShowGUI(); - client.getChatScreen().setManagerToolsVisibility(); - + client.showStartScreen(); } catch (Exception e) { - e.printStackTrace(); + StartScreen.showErrorMessage("Error starting up client"); } } - public boolean connect() + + public void showStartScreen() + { + startScreen.go(); + } + + + public void startApplication() { + applicationMain.createAndShowGUI(); + applicationMain.getChatScreen().setManagerToolsVisibility(); + } + + // return = 1 -> connected successfully + // return = 2 -> duplicate username + // return = 3 -> error in locating the server + public int connect(String userName, String serverAddress) + { + if( !userName.trim().isEmpty() ) + { + setUserName(userName); + } + userName = getUserName(); + + if( serverAddress.trim().isEmpty() ) + { + serverAddress = DEFAULT_SERVER_ADDRESS; + setServerAddress(serverAddress); + } + setServerAddress(serverAddress); + try { + 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"); - if (clientController.join(userName, this.chatUpdate, this.clientUpdate, this.drawingUpdate)) + System.out.println("User name:" + userName); + if( clientController.join(userName, this.chatUpdate, this.clientUpdate, this.drawingUpdate) ) { System.out.println("Connected to server"); - return true; + return 1; + } + else + { + return 2; } } catch (Exception e) { - e.printStackTrace(); - } + //e.printStackTrace(); - return false; + return 3; + } } } \ No newline at end of file diff --git a/src/client/ClientUpdate.java b/src/client/ClientUpdate.java index 6f5e3805f3d535ced928f263847ae025461d6794..9fdef79bf4438c8af406c61911df11a72583d383 100644 --- a/src/client/ClientUpdate.java +++ b/src/client/ClientUpdate.java @@ -18,19 +18,41 @@ public class ClientUpdate extends UnicastRemoteObject implements IClientUpdate, this.client = client; } + public boolean notifyClient(String fromClient, String newUsername) throws RemoteException + { + client.getApplicationMain().getChatScreen().getSendMessageToComboBox().addItem(newUsername); + + return true; + } + + // for debuggins purposes + private void printUserList(String[] users) + { + System.out.print("Currently connected users: "); + for( String s : users ) + { + System.out.print(s + " "); + } + System.out.println(); + } + @Override public boolean updateUserList(String[] users) throws RemoteException { - //client.setConnectedUsers(users); + printUserList(users); - JComboBox userBox = client.getChatScreen().getSendMessageToComboBox(); + System.out.println("TEST1"); + JComboBox userBox = client.getApplicationMain().getChatScreen().getSendMessageToComboBox(); JComboBox kickUserBox = client.getChatScreen().getKickUserComboBox(); + System.out.println("TEST2"); userBox.removeAllItems(); kickUserBox.removeAllItems(); + System.out.println("TEST3"); userBox.addItem("All"); + System.out.println("TEST4"); for( String s : users ) { if( !s.equals(client.getUserName()) ) diff --git a/src/server/ChatController.java b/src/server/ChatController.java index 8fbff1e4457c8c633893caad33f1ceeff7b6b49c..a2a38a03e75bb952ed298a7f8050029cb38b0324 100644 --- a/src/server/ChatController.java +++ b/src/server/ChatController.java @@ -21,12 +21,9 @@ public class ChatController extends UnicastRemoteObject implements IChatControll { System.out.print("Broadcasting message to everyone..."); - IChatUpdate client; - for( User u : server.users ) { - client = u.getIChatUpdate(); - client.notifyChat(fromClient, message, false); + u.getIChatUpdate().notifyChat(fromClient, message, false); } System.out.print("...DONE\n"); diff --git a/src/server/ClientController.java b/src/server/ClientController.java index 34967f9fd3f153c12500b6e4c19f0135861d4851..dfdb350ab7698554b76cf1eed74f27fc168d0107 100644 --- a/src/server/ClientController.java +++ b/src/server/ClientController.java @@ -22,22 +22,30 @@ public class ClientController extends UnicastRemoteObject implements IClientCont @Override public boolean join(String username, IChatUpdate clientChat, IClientUpdate clientUpdate, IDrawingUpdate clientDrawing) throws RemoteException { - server.chatController.broadcastMessageUserLogin(username); + if( getUserIndex(username) < 0 ) + { + // user with same username is not connected + server.chatController.broadcastMessageUserLogin(username); - User newUser = new User(username, clientChat, clientUpdate, clientDrawing); + User newUser = new User(username, clientChat, clientUpdate, clientDrawing); - server.users.add(newUser); + server.users.add(newUser); - if(server.users.size() == 1) - { - server.users.get(0).setAdmin(true); - } + if(server.users.size() == 1) + { + newUser.setAdmin(true); + } - System.out.println(username + " registered successfully"); + System.out.println(username + " registered successfully"); - broadcastUserList(); + broadcastUserList(); - return true; + return true; + } + else + { + return false; + } } @Override @@ -53,6 +61,19 @@ public class ClientController extends UnicastRemoteObject implements IClientCont broadcastUserList(); } + + printUserList(); + } + + // for debuggins purposes + private void printUserList() + { + System.out.print("Currently connected users: "); + for( User u : server.users ) + { + System.out.print(u.getUserName()); + } + System.out.println(); } @Override @@ -119,16 +140,20 @@ public class ClientController extends UnicastRemoteObject implements IClientCont public void broadcastUserList() throws RemoteException { - String[] connectedUsers = new String[server.users.size()]; + String[] connectedUsers = new String[server.users.size()]; for( int i = 0; i<server.users.size(); i++ ) { connectedUsers[i] = server.users.get(i).getUserName(); } + printUserList(); + for( User u : server.users ) { + System.out.print(u.getUserName() + " being notified"); u.getIClientUpdate().updateUserList(connectedUsers); + System.out.println("...DONE"); } }