Skip to content
Snippets Groups Projects
Select Git revision
  • 7854dccb3855ff558a9a1462d878ebc53a23fac2
  • master default protected
  • hai
  • isaac
  • CheHao
  • Eldar
  • mpriymak
  • master-before-merging-with-hai
  • master-before-merging-with-isaac
  • rmi-working-before-merging-with-isaac
  • all-code-merged-by-hai-v1
11 results

ClientController.java

  • ClientController.java 1.98 KiB
    package server;
    
    import java.io.Serializable;
    import java.net.InetAddress;
    import java.rmi.Remote;
    import java.rmi.RemoteException;
    import java.rmi.registry.LocateRegistry;
    import java.rmi.registry.Registry;
    import java.rmi.server.UnicastRemoteObject;
    import java.util.ArrayList;
    import java.util.Observable;
    import java.util.Observer;
    
    import client.UpdateController;
    import remote.IClientController;
    import remote.IUpdateController;
    
    /**
     * Server side implementation of the remote interface.
     * Must extend UnicastRemoteObject, to allow the JVM to create a
     * remote proxy/stub.
     *
     */
    public class ClientController extends UnicastRemoteObject implements IClientController, Serializable {
    
        /**
         * Added
         */
        //private static final long serialVersionUID = 1L;
    
        private ArrayList<User> userList;
    
        protected ClientController() throws RemoteException {
            userList = new ArrayList<>();
        }
    
        @Override
        public String joinServer(String userName, InetAddress ipAddress, String rmiName) throws RemoteException {
            try
            {
                Registry userRMI = LocateRegistry.getRegistry(ipAddress.getHostAddress());
                IUpdateController updateController = (IUpdateController) userRMI.lookup(rmiName);
    
                User user = new User(userName, ipAddress, rmiName);
                userList.add(user);
                for (User u : userList) {
                    if (!userName.equals(u.getUsername())) {
                        u.getUpdateController().updateClient(userName);
                    }
                }
                System.out.println("User " + userName + " added successfully");
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
            return "update successfully";
        }
    
        @Override
        public ArrayList<String> getUsernameList() throws RemoteException {
            ArrayList<String> usernameList = new ArrayList<>();
            for (User u : userList) {
                usernameList.add(u.getUsername());
            }
            return usernameList;
        }
    
    
    }