Skip to content
Snippets Groups Projects
DepositControllerImpl.java 1.8 KiB
Newer Older
package ufrn.imd.controller.impl;

import ufrn.imd.controller.Controller;
import ufrn.imd.log.Log;
import ufrn.imd.message.Client;
import ufrn.imd.service.DepositService;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import java.util.List;

public class DepositControllerImpl extends UnicastRemoteObject implements Controller {

    private volatile List<Client> clients = new ArrayList<>();

    public DepositControllerImpl(DepositService service) throws RemoteException{
        super();
        Log.info(DepositControllerImpl.class, "Starting Deposit service!");
        new Notify(service).start();
    }
    public void registerClient(Client client) throws RemoteException {

        clients.add(client);
        System.out.println("Novo cliente registrado com sucesso! Total: "+clients.size());
    }


    private class Notify extends Thread{

        private final DepositService service;

        public Notify(DepositService service) {
            super();
            this.service = service;
        }
        public void run() {

            for(;;) {

                if(clients.size() > 0) {

                    System.out.println("Notificando clientes");

                    int i = 0;
                    for (Client client : clients) {

                        try {
                            this.service.deposit(client.getBalance(), client.getAccount());
                        } catch (RemoteException e) {
                            e.printStackTrace();
                        }
                    }


                    try {
                        Thread.sleep(15 * 1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            }

        }
    }
}