Skip to content
Snippets Groups Projects
Commit 3c75086b authored by mpriymak's avatar mpriymak
Browse files

Changed the join method to send over a reference of the ClientUpdate Interface

parent 63e4e614
Branches
Tags
1 merge request!25Master
......@@ -8,7 +8,7 @@
<properties>
<maximumSize width="-1" height="-1"/>
<minimumSize width="-1" height="-1"/>
<preferredSize width="1200" height="700"/>
<preferredSize width="1200" height="600"/>
</properties>
<border type="none"/>
<children>
......@@ -185,7 +185,7 @@
<text value="Send Message To:"/>
</properties>
</component>
<component id="cc35d" class="javax.swing.JComboBox" binding="sentMessageToComboBox">
<component id="cc35d" class="javax.swing.JComboBox" binding="sendMessageToComboBox">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
......
......@@ -15,7 +15,7 @@ public class ChatScreen {
private JButton sendButton;
private JPanel drawingPanel;
private JPanel othersPanel;
private JComboBox sentMessageToComboBox;
private JComboBox sendMessageToComboBox;
private JTextArea chatDisplayBox;
private JComboBox userSelectComboBox;
private JButton kickOutButton;
......@@ -50,6 +50,8 @@ public class ChatScreen {
frame.pack();
frame.setResizable(false);
frame.setVisible(true);
SwingUtilities.getRootPane(sendButton).setDefaultButton(sendButton);
}
private void createUIComponents() {
......@@ -60,8 +62,9 @@ public class ChatScreen {
return chatDisplayBox;
}
public JComboBox getSentMessageToComboBox() {
return sentMessageToComboBox;
public JComboBox getSendMessageToComboBox()
{
return sendMessageToComboBox;
}
public JPanel getDrawingPanel() {
......@@ -80,8 +83,18 @@ public class ChatScreen {
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();
......
......@@ -17,12 +17,23 @@ public class ChatUpdate extends UnicastRemoteObject implements IChatUpdate, Seri
}
@Override
public boolean notifyChat(String fromClient, String message) throws RemoteException
public boolean notifyChat(String fromClient, String message, boolean isPrivate) throws RemoteException
{
client.getChatScreen().getChatDisplayBox().append(fromClient + ": " + message + "\n");
String outputString;
if( isPrivate )
{
outputString = "PRIVATE (from " + fromClient + "): " + message + "\n";
}
else
{
outputString = fromClient + ": " + message + "\n";
}
client.getChatScreen().getChatDisplayBox().append(outputString);
//client.setReceivedMessage(message);
System.out.println(fromClient + ": " + message);
//System.out.println(fromClient + ": " + message);
return true;
}
......
......@@ -13,26 +13,32 @@ import java.rmi.registry.Registry;
public class Client
{
//test
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;
private PaintGUI paintGUI;
// public PaintGUI getPaintGUI() {
// return paintGUI;
// }
private String receivedMessage;
private String[] connectedUsers;
public PaintGUI getPaintGUI() {
return paintGUI;
}
public ChatScreen getChatScreen()
......@@ -40,10 +46,12 @@ public class Client
return chatScreen;
}
public void setConnectedUsers(String[] users)
{
this.connectedUsers = users;
}
private String receivedMessage;
public IChatController getChatController()
{
return chatController;
......@@ -79,7 +87,7 @@ public class Client
this.drawingUpdate = new DrawingUpdate(this);
this.startScreen = new StartScreen(this);
this.chatScreen = new ChatScreen(this);
// this.paintGUI = new PaintGUI(this);
this.paintGUI = new PaintGUI(this);
}
public static void main(String[] args)
......@@ -97,6 +105,22 @@ 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 boolean connect()
{
try
......@@ -107,7 +131,7 @@ public class Client
clientController = (IClientController) registryServer.lookup("ClientController");
drawingController = (IDrawingController) registryServer.lookup("DrawingController");
if (clientController.join(userName, this.chatUpdate, this.drawingUpdate))
if (clientController.join(userName, this.chatUpdate, this.clientUpdate, this.drawingUpdate))
{
System.out.println("Connected to server");
......@@ -121,4 +145,14 @@ public class Client
return false;
}
public String getReceivedMessage()
{
return receivedMessage;
}
public void setReceivedMessage(String receivedMessage)
{
this.receivedMessage = receivedMessage;
}
}
\ No newline at end of file
......@@ -2,7 +2,9 @@ package client;
import remote.IClientUpdate;
import javax.swing.*;
import java.io.Serializable;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
......@@ -19,7 +21,29 @@ public class ClientUpdate extends UnicastRemoteObject implements IClientUpdate,
@Override
public boolean notifyClient(String fromClient, String newUsername) throws RemoteException
{
client.getChatScreen().getSentMessageToComboBox().addItem(newUsername);
client.getChatScreen().getSendMessageToComboBox().addItem(newUsername);
return true;
}
@Override
public boolean updateUserList(String[] users) throws RemoteException
{
//client.setConnectedUsers(users);
JComboBox userBox = client.getChatScreen().getSendMessageToComboBox();
userBox.removeAllItems();
userBox.addItem("All");
for( String s : users )
{
if( !s.equals(client.getUserName()) )
{
userBox.addItem(s);
}
}
return true;
}
......
......@@ -8,4 +8,5 @@ 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;
boolean sendPrivateMessage(String fromClient, String toClient, String message) throws RemoteException;
}
......@@ -6,7 +6,7 @@ import java.rmi.RemoteException;
public interface IChatUpdate extends Remote, Serializable
{
boolean notifyChat(String fromClient, String message) throws RemoteException;
boolean notifyChat(String fromClient, String message, boolean isPrivate) throws RemoteException;
boolean notifyUserLogin(String fromClient) throws RemoteException;
boolean notifyUserLogout(String fromClient) throws RemoteException;
}
......@@ -5,7 +5,7 @@ import java.rmi.RemoteException;
public interface IClientController extends Remote
{
boolean join(String username, IChatUpdate clientChat, IDrawingUpdate clientDrawing) throws RemoteException;
boolean join(String username, IChatUpdate clientChat, IClientUpdate clientUpdate, IDrawingUpdate clientDrawing) throws RemoteException;
void quit(String username) throws RemoteException;
......
......@@ -7,4 +7,6 @@ import java.rmi.RemoteException;
public interface IClientUpdate extends Remote, Serializable {
boolean notifyClient(String fromClient, String newUsername) throws RemoteException;
boolean updateUserList(String[] users) throws RemoteException;
}
......@@ -26,7 +26,7 @@ public class ChatController extends UnicastRemoteObject implements IChatControll
for( User u : server.users )
{
client = u.getIChatUpdate();
client.notifyChat(fromClient, message);
client.notifyChat(fromClient, message, false);
}
System.out.print("...DONE\n");
......@@ -69,4 +69,22 @@ public class ChatController extends UnicastRemoteObject implements IChatControll
return true;
}
@Override
public boolean sendPrivateMessage(String fromClient, String toClient, String message) throws RemoteException
{
int toClientIndex = server.clientController.getUserIndex(toClient);
int fromClientIndex = server.clientController.getUserIndex(fromClient);
if( toClientIndex >= 0 && fromClientIndex >= 0 )
{
server.users.get(toClientIndex).getIChatUpdate().notifyChat(fromClient, message, true);
server.users.get(fromClientIndex).getIChatUpdate().notifyChat(fromClient, message, true);
return true;
}
return false;
}
}
......@@ -2,9 +2,11 @@ package server;
import remote.IChatUpdate;
import remote.IClientController;
import remote.IClientUpdate;
import remote.IDrawingUpdate;
import java.io.Serializable;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
......@@ -18,11 +20,11 @@ public class ClientController extends UnicastRemoteObject implements IClientCont
}
@Override
public boolean join(String username, IChatUpdate clientChat, IDrawingUpdate clientDrawing) throws RemoteException
public boolean join(String username, IChatUpdate clientChat, IClientUpdate clientUpdate, IDrawingUpdate clientDrawing) throws RemoteException
{
server.chatController.broadcastMessageUserLogin(username);
User newUser = new User(username, clientChat, clientDrawing);
User newUser = new User(username, clientChat, clientUpdate, clientDrawing);
server.users.add(newUser);
......@@ -33,6 +35,8 @@ public class ClientController extends UnicastRemoteObject implements IClientCont
System.out.println(username + " registered successfully");
broadcastUserList();
return true;
}
......@@ -46,6 +50,8 @@ public class ClientController extends UnicastRemoteObject implements IClientCont
server.users.remove(userIndex);
server.chatController.broadcastMessageUserLogout(username);
broadcastUserList();
}
}
......@@ -74,13 +80,16 @@ public class ClientController extends UnicastRemoteObject implements IClientCont
if ( adminIndex > 0 && userIndex > 0 && server.users.get(adminIndex).isAdmin() )
{
server.users.remove(userIndex);
broadcastUserList();
return true;
}
return false;
}
private int getUserIndex(String username)
public int getUserIndex(String username)
{
int index = -1;
......@@ -95,4 +104,19 @@ public class ClientController extends UnicastRemoteObject implements IClientCont
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);
}
}
}
......@@ -5,13 +5,15 @@ public class User
private String username;
private remote.IChatUpdate IChatUpdate;
private remote.IDrawingUpdate IDrawingUpdate;
private remote.IClientUpdate IClientUpdate;
private boolean isAdmin;
public User(String username, remote.IChatUpdate IChatUpdate, remote.IDrawingUpdate IDrawingUpdate)
public User(String username, remote.IChatUpdate IChatUpdate, remote.IClientUpdate IClientUpdate, remote.IDrawingUpdate IDrawingUpdate)
{
this.username = username;
this.IChatUpdate = IChatUpdate;
this.IDrawingUpdate = IDrawingUpdate;
this.IClientUpdate = IClientUpdate;
this.isAdmin = false;
}
......@@ -35,5 +37,10 @@ public class User
return this.IChatUpdate;
}
public remote.IClientUpdate getIClientUpdate()
{
return this.IClientUpdate;
}
public remote.IDrawingUpdate getIDrawingUpdate() { return this.IDrawingUpdate; }
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment