Select Git revision
dsa_database.cc
Client2.java 1.78 KiB
package client;
import remote.IChatController;
import remote.IClientController;
import client.ChatUpdate;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.concurrent.TimeUnit;
public class Client2
{
private String userName;
private Registry registryServer;
private IChatController chatController;
private IClientController clientController;
private ChatUpdate chatUpdate;
public Client2(String userName) throws RemoteException
{
this.userName = userName;
this.chatUpdate = new ChatUpdate();
}
public static void main(String[] args)
{
try
{
Client2 client1 = new Client2("Max");
client1.run();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public void run() throws RemoteException, NotBoundException
{
connect();
chatController.broadcast(userName, "Hello");
try
{
TimeUnit.MINUTES.sleep(5);
}
catch(Exception e)
{
e.printStackTrace();
}
}
public boolean connect() throws RemoteException, NotBoundException
{
registryServer = LocateRegistry.getRegistry("localhost");
chatController = (IChatController) registryServer.lookup("ChatController");
clientController = (IClientController) registryServer.lookup("ClientController");
System.out.println(userName + " fetched all controller from RMI registry");
if ( clientController.join(userName, this.chatUpdate ) )
{
System.out.println(userName + " registered at server");
return true;
}
return false;
}
}