diff --git a/src/GUI/ChatScreen.java b/src/GUI/ChatScreen.java index 513546bd559cbf581e193c62096faeeb55dfde23..cb1f200306e11ecc3b2f2fd766de62050a60eedd 100644 --- a/src/GUI/ChatScreen.java +++ b/src/GUI/ChatScreen.java @@ -43,11 +43,17 @@ public class ChatScreen { public ChatScreen(Client client) { this.client = client; - yourNameDisplay.setText(client.getUserName()); + exitThisRoomButton.addActionListener(actionListener); + sendButton.addActionListener(actionListener); +// yourNameDisplay.setText(client.getUserName()); // drawingPanel = new PaintGUI(client); // SwingUtilities.getRootPane(sendButton).setDefaultButton(sendButton); } + public void setUserName(String userName) + { + this.yourNameDisplay.setText(userName); + } public JButton getSendButton() { return sendButton; @@ -68,7 +74,6 @@ public class ChatScreen { // } - ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent e) diff --git a/src/GUI/ChatScreen.java~origin_master b/src/GUI/ChatScreen.java~origin_master deleted file mode 100644 index 513546bd559cbf581e193c62096faeeb55dfde23..0000000000000000000000000000000000000000 --- a/src/GUI/ChatScreen.java~origin_master +++ /dev/null @@ -1,118 +0,0 @@ -package GUI; - -import client.Client; -import remote.IChatController; -import remote.IClientController; - -import javax.swing.*; -import java.awt.event.*; -import java.rmi.RemoteException; - -public class ChatScreen { - - public JPanel panel2; - - private JButton sendButton; - - // private JPanel drawingPanel; - private JPanel othersPanel; - private JComboBox sendMessageToComboBox; - private JTextArea chatDisplayBox; - private JComboBox userSelectComboBox; - private JButton kickOutButton; - private JButton promoteToManagerButton; - private JTextField chatInputBox; - private JLabel sendMessageToLabel; - private JLabel managersNameLabel; - private JLabel yourNameLabel; - private JLabel yourNameDisplay; - private JLabel managersNameDisplay; - private JPanel myAreaPanel; - private JPanel managersPanel; - private JPanel chatPanel; - private JButton exitThisRoomButton; - private JButton quitButton; - private JFrame frame; - public Client getClient() { - return client; - } - - public Client client; - - - public ChatScreen(Client client) - { - this.client = client; - yourNameDisplay.setText(client.getUserName()); -// drawingPanel = new PaintGUI(client); -// SwingUtilities.getRootPane(sendButton).setDefaultButton(sendButton); - } - - - public JButton getSendButton() { - return sendButton; - } - - - public JTextArea getChatDisplayBox() { - return chatDisplayBox; - } - - public JComboBox getSendMessageToComboBox() - { - return sendMessageToComboBox; - } - -// public JPanel getDrawingPanel() { -// return drawingPanel; -// } - - - - ActionListener actionListener = new ActionListener() - { - public void actionPerformed(ActionEvent e) - { - if (e.getSource() == sendButton) - { - String message = chatInputBox.getText(); - chatInputBox.setText(""); - IChatController chatController = client.getChatController(); - try - { - System.out.println("Send button pressed"); - - String toUser = sendMessageToComboBox.getSelectedItem().toString(); - - if( toUser.equals("All") ) - { - chatController.broadcastMessage(client.getUserName(), message); - } - else - { - chatController.sendPrivateMessage(client.getUserName(), toUser, message); - } - } - catch (RemoteException ex) - { - ex.printStackTrace(); - } - } - else if (e.getSource() == exitThisRoomButton) - { - IClientController clientController = client.getClientController(); - try - { - System.out.println("Exit room button pressed"); - clientController.quit(client.getUserName()); - System.exit(0); - } - catch (RemoteException ex) - { - ex.printStackTrace(); - } - } - } - - }; -} diff --git a/src/client/Client.java b/src/client/Client.java index 3edda911f8e581fa2eb7d576173e811ce6bb7abf..a068096862e91c6acfb8f7a8a4c554e7dc83c532 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; @@ -30,6 +33,28 @@ public class Client private ChatScreen chatScreen; private PaintGUI paintGUI; + + 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; + } + + public ApplicationMain getApplicationMain() { return applicationMain; } private ApplicationMain applicationMain; @@ -51,20 +76,9 @@ 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 { @@ -72,11 +86,8 @@ public class Client this.clientUpdate = new ClientUpdate(this); this.chatUpdate = new ChatUpdate(this); this.drawingUpdate = new DrawingUpdate(this); -// this.startScreen = new StartScreen(this); -// this.chatScreen = new ChatScreen(this); -// this.paintGUI = new PaintGUI(this); + this.startScreen = new StartScreen(this); this.applicationMain = new ApplicationMain(this); - } public static void main(String[] args) @@ -84,39 +95,72 @@ public class Client try { Client client = new Client(args[0]); - client.connect(); -// client.getChatScreen().showGUI(); -// client.getPaintGUI().showGUI(); - client.getApplicationMain().createAndShowGUI(); + + client.showStartScreen(); } catch (Exception e) { - e.printStackTrace(); + StartScreen.showErrorMessage("Error starting up client"); + //e.printStackTrace(); } } - public boolean connect() + + public void showStartScreen() + { + startScreen.go(); + } + + + public void startApplication() + { + applicationMain.createAndShowGUI(); + } + + // 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().isBlank() ) + { + setUserName(userName); + } + userName = getUserName(); + + if( serverAddress.trim().isBlank() ) + { + 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/Client.java~origin_master b/src/client/Client.java~origin_master deleted file mode 100644 index 3edda911f8e581fa2eb7d576173e811ce6bb7abf..0000000000000000000000000000000000000000 --- a/src/client/Client.java~origin_master +++ /dev/null @@ -1,122 +0,0 @@ -package client; - -import GUI.ApplicationMain; -import GUI.ChatScreen; -import GUI.PaintGUI; -import GUI.StartScreen; -import remote.IChatController; -import remote.IClientController; -import remote.IDrawingController; - -import java.rmi.RemoteException; -import java.rmi.registry.LocateRegistry; -import java.rmi.registry.Registry; - -public class Client -{ - private String userName; - private String serverAddress; - - 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 ApplicationMain getApplicationMain() { return applicationMain; } - - private ApplicationMain applicationMain; - - public PaintGUI getPaintGUI() { return paintGUI; } - - - public ChatScreen getChatScreen() { return chatScreen; } - - public IChatController getChatController() - { - return chatController; - } - - public IClientController getClientController() - { - return clientController; - } - - 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 - { - this.userName = username; - this.clientUpdate = new ClientUpdate(this); - this.chatUpdate = new ChatUpdate(this); - this.drawingUpdate = new DrawingUpdate(this); -// this.startScreen = new StartScreen(this); -// this.chatScreen = new ChatScreen(this); -// this.paintGUI = new PaintGUI(this); - this.applicationMain = new ApplicationMain(this); - - } - - public static void main(String[] args) - { - try - { - Client client = new Client(args[0]); - client.connect(); -// client.getChatScreen().showGUI(); -// client.getPaintGUI().showGUI(); - client.getApplicationMain().createAndShowGUI(); - } - catch (Exception e) - { - e.printStackTrace(); - } - } - - public boolean connect() - { - try - { - 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("Connected to server"); - - return true; - } - } - catch (Exception e) - { - e.printStackTrace(); - } - - return false; - } -} \ No newline at end of file diff --git a/src/server/ClientController.java b/src/server/ClientController.java index 5e7638f48e3e3307b6929a968b425e40722575a2..bf99749af346687afeaf96f106b4a44e86586728 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) + { + assignAdmin(username); + } - System.out.println(username + " registered successfully"); + System.out.println(username + " registered successfully"); - broadcastUserList(); + broadcastUserList(); - return true; + return true; + } + else + { + return false; + } } @Override @@ -45,6 +53,8 @@ public class ClientController extends UnicastRemoteObject implements IClientCont { int userIndex = getUserIndex(username); + //System.out.println("Quitting user has index: " + userIndex); + if( userIndex >= 0 ) { server.users.remove(userIndex); @@ -53,6 +63,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 @@ -64,6 +87,8 @@ public class ClientController extends UnicastRemoteObject implements IClientCont { server.users.get(adminIndex).setAdmin(true); + // do something here to update the state of client canvas + return true; } @@ -102,21 +127,26 @@ public class ClientController extends UnicastRemoteObject implements IClientCont } } + //System.out.println("User located at index: " + index); return index; } private 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"); } } } diff --git a/src/server/ClientController.java~origin_master b/src/server/ClientController.java~origin_master deleted file mode 100644 index 5e7638f48e3e3307b6929a968b425e40722575a2..0000000000000000000000000000000000000000 --- a/src/server/ClientController.java~origin_master +++ /dev/null @@ -1,122 +0,0 @@ -package server; - -import remote.IChatUpdate; -import remote.IClientUpdate; -import remote.IDrawingUpdate; -import remote.IClientController; - -import java.io.Serializable; -import java.rmi.Remote; -import java.rmi.RemoteException; -import java.rmi.server.UnicastRemoteObject; - -public class ClientController extends UnicastRemoteObject implements IClientController, Serializable -{ - private Server server; - - protected ClientController(Server server) throws RemoteException - { - this.server = server; - } - - @Override - public boolean join(String username, IChatUpdate clientChat, IClientUpdate clientUpdate, IDrawingUpdate clientDrawing) throws RemoteException - { - server.chatController.broadcastMessageUserLogin(username); - - User newUser = new User(username, clientChat, clientUpdate, clientDrawing); - - server.users.add(newUser); - - if(server.users.size() == 1) - { - server.users.get(0).setAdmin(true); - } - - System.out.println(username + " registered successfully"); - - broadcastUserList(); - - return true; - } - - @Override - public void quit(String username) throws RemoteException - { - int userIndex = getUserIndex(username); - - if( userIndex >= 0 ) - { - server.users.remove(userIndex); - - server.chatController.broadcastMessageUserLogout(username); - - broadcastUserList(); - } - } - - @Override - public boolean assignAdmin(String username) throws RemoteException - { - int adminIndex = getUserIndex(username); - - if( adminIndex >= 0 ) - { - server.users.get(adminIndex).setAdmin(true); - - return true; - } - - return false; - } - - @Override - public boolean kickUser(String username, String who) throws RemoteException - { - int userIndex = getUserIndex(who); - - int adminIndex = getUserIndex(username); - - if ( adminIndex > 0 && userIndex > 0 && server.users.get(adminIndex).isAdmin() ) - { - server.users.remove(userIndex); - - broadcastUserList(); - - return true; - } - - return false; - } - - public int getUserIndex(String username) - { - int index = -1; - - for( int i = 0; i < server.users.size(); i++ ) - { - if( server.users.get(i).getUserName().equals(username) ) - { - index = i; - break; - } - } - - return index; - } - - private void broadcastUserList() throws RemoteException - { - String[] connectedUsers = new String[server.users.size()]; - - for( int i = 0; i<server.users.size(); i++ ) - { - connectedUsers[i] = server.users.get(i).getUserName(); - } - - for( User u : server.users ) - { - u.getIClientUpdate().updateUserList(connectedUsers); - } - } -} diff --git a/src/server/User.java~origin_master b/src/server/User.java~origin_master deleted file mode 100644 index 543f26e6c9056a69b382ae8dc4819d4116e95559..0000000000000000000000000000000000000000 --- a/src/server/User.java~origin_master +++ /dev/null @@ -1,47 +0,0 @@ -package server; - -import remote.IChatUpdate; -import remote.IClientUpdate; -import remote.IDrawingUpdate; - -public class User -{ - private String username; - private IChatUpdate IChatUpdate; - private IDrawingUpdate IDrawingUpdate; - private IClientUpdate IClientUpdate; - private boolean isAdmin; - - public User(String username, IChatUpdate IChatUpdate, IClientUpdate IClientUpdate, IDrawingUpdate IDrawingUpdate) - { - this.username = username; - this.IChatUpdate = IChatUpdate; - this.IDrawingUpdate = IDrawingUpdate; - this.IClientUpdate = IClientUpdate; - this.isAdmin = false; - } - - public String getUserName() - { - return this.username; - } - - public void setAdmin(boolean admin) - { - isAdmin = admin; - } - - public boolean isAdmin() - { - return isAdmin; - } - - public IChatUpdate getIChatUpdate() - { - return this.IChatUpdate; - } - - public IDrawingUpdate getIDrawingUpdate() { return this.IDrawingUpdate; } - - public IClientUpdate getIClientUpdate() { return this.IClientUpdate; } -}