Skip to content
Snippets Groups Projects
DepositControllerImpl.java 2.51 KiB
Newer Older
unknown's avatar
unknown committed
package ufrn.imd.controller.impl;

unknown's avatar
unknown committed
import lombok.extern.java.Log;
unknown's avatar
unknown committed
import ufrn.imd.controller.Controller;
import ufrn.imd.domain.Client;
import ufrn.imd.service.impl.DepositServiceImpl;
unknown's avatar
unknown committed

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.*;
unknown's avatar
unknown committed

import static ufrn.imd.utils.NameSpace.HOST_DEPOSIT_SERVICE;
import static ufrn.imd.utils.NameSpace.PORT_DEPOSIT_SERVICE;

unknown's avatar
unknown committed
@Log
unknown's avatar
unknown committed
public class DepositControllerImpl extends UnicastRemoteObject implements Controller {

    private volatile Queue<Client> clients = new ArrayDeque<>();
    public DepositControllerImpl(DepositServiceImpl service) throws RemoteException, MalformedURLException, AlreadyBoundException {
unknown's avatar
unknown committed
        super();
unknown's avatar
unknown committed
        log.info("Starting Deposit service!");
unknown's avatar
unknown committed

        new Notify(service).start();
unknown's avatar
unknown committed
        log.info(String.format("Initializing server in %s", HOST_DEPOSIT_SERVICE));
unknown's avatar
unknown committed

        LocateRegistry.createRegistry(PORT_DEPOSIT_SERVICE);

        Naming.bind(HOST_DEPOSIT_SERVICE, this);
    }

    @Override
    public void registerClient(Client client) throws RemoteException {
        clients.add(client);
unknown's avatar
unknown committed
        log.info("new client registred");
        log.info(String.format("total users: %d", clients.size()));
unknown's avatar
unknown committed
    }




    private class Notify extends Thread{

        private final DepositServiceImpl service;
unknown's avatar
unknown committed

        public Notify(DepositServiceImpl service) {
unknown's avatar
unknown committed
            super();
            this.service = service;
        }
        public void run() {

            for(;;) {

                if(clients.size() > 0) {

unknown's avatar
unknown committed
                    log.info("notyfing the clients!");
unknown's avatar
unknown committed
                    int i = 0;
                    clients.stream().forEach(client -> {
unknown's avatar
unknown committed
                        try {
                            log.info(String.format("Clients waiting %d", clients.size()));
                            this.service.deposit(200.00, Optional.of(client));
                            clients.remove();
                            log.info(String.format("Clients waiting %d", clients.size()));
unknown's avatar
unknown committed

                        } catch (RemoteException e) {
                            e.printStackTrace();
                        }
unknown's avatar
unknown committed
                    try {
                        Thread.sleep(15 * 1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }

        }
    }
}