package ufrn.imd.controller.impl; import ufrn.imd.controller.Controller; import ufrn.imd.domain.Client; import ufrn.imd.service.BalanceService; import ufrn.imd.service.DepositService; import ufrn.imd.service.impl.BalanceServiceImpl; import ufrn.imd.utils.Log; import java.net.MalformedURLException; import java.rmi.AlreadyBoundException; import java.rmi.Naming; import java.rmi.RemoteException; import java.rmi.registry.LocateRegistry; import java.rmi.server.UnicastRemoteObject; import java.util.ArrayList; import java.util.List; import static ufrn.imd.utils.NameSpace.*; public class BalanceControllerImpl extends UnicastRemoteObject implements Controller { private volatile List<Client> clients = new ArrayList<>(); public BalanceControllerImpl(BalanceService service) throws RemoteException, MalformedURLException, AlreadyBoundException { super(); Log.info(BalanceServiceImpl.class, "Starting Deposit service!"); new Notify(service).start(); Log.info(BalanceServiceImpl.class, String.format("Initializing server in %s", HOST_BALANCE_SERVICE)); LocateRegistry.createRegistry(PORT_BALANCE_SERVICE); Naming.bind(HOST_BALANCE_SERVICE, this); } @Override public void registerClient(Client client) throws RemoteException { clients.add(client); Log.info(BalanceService.class, "new client registred"); Log.info(BalanceService.class, String.format("total users: %d", clients.size())); } private class Notify extends Thread{ private final BalanceService service; public Notify(BalanceService service) { super(); this.service = service; } public void run() { for(;;) { if(clients.size() > 0) { Log.info( BalanceService.class, "notyfing the clients!" ); int i = 0; for (Client client : clients) { try { this.service.balance(client.getAccount()); } catch (RemoteException e) { e.printStackTrace(); } } try { Thread.sleep(15 * 1000); } catch (InterruptedException e) { e.printStackTrace(); } } else { try { Thread.sleep(15 * 1000); } catch (InterruptedException e) { throw new RuntimeException(e); } Log.info(BalanceService.class, "No there's clients!"); } } } } }