diff --git a/src/GUI/ChatScreen.java b/src/GUI/ChatScreen.java new file mode 100644 index 0000000000000000000000000000000000000000..513546bd559cbf581e193c62096faeeb55dfde23 --- /dev/null +++ b/src/GUI/ChatScreen.java @@ -0,0 +1,118 @@ +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 new file mode 100644 index 0000000000000000000000000000000000000000..3edda911f8e581fa2eb7d576173e811ce6bb7abf --- /dev/null +++ b/src/client/Client.java @@ -0,0 +1,122 @@ +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 new file mode 100644 index 0000000000000000000000000000000000000000..5e7638f48e3e3307b6929a968b425e40722575a2 --- /dev/null +++ b/src/server/ClientController.java @@ -0,0 +1,122 @@ +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 b/src/server/User.java new file mode 100644 index 0000000000000000000000000000000000000000..543f26e6c9056a69b382ae8dc4819d4116e95559 --- /dev/null +++ b/src/server/User.java @@ -0,0 +1,47 @@ +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; } +}