Skip to content
Snippets Groups Projects
Commit 04615562 authored by Fernando Ferreira de Lima Filho's avatar Fernando Ferreira de Lima Filho
Browse files

Balance impl

parent fd62886a
No related branches found
No related tags found
No related merge requests found
Showing with 155 additions and 21 deletions
package ufrn.imd;
import ufrn.imd.controller.Controller;
import ufrn.imd.controller.impl.BalanceControllerImpl;
import ufrn.imd.controller.impl.DepositControllerImpl;
import ufrn.imd.service.BalanceService;
import ufrn.imd.service.DepositService;
import ufrn.imd.service.impl.BalanceServiceImpl;
import ufrn.imd.service.impl.DepositServiceImpl;
import java.net.MalformedURLException;
......@@ -13,7 +16,10 @@ public class BankingServerApp {
public static void main(String[] args) throws RemoteException, MalformedURLException, AlreadyBoundException {
DepositService depositService = new DepositServiceImpl();
BalanceService balanceService = new BalanceServiceImpl();
Controller depositController = new DepositControllerImpl(depositService);
Controller balanceController = new BalanceControllerImpl(balanceService);
}
......
package ufrn.imd.controller;
import ufrn.imd.message.Client;
import ufrn.imd.domain.Client;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.List;
public interface Controller extends Remote {
public void registerClient(Client client) throws RemoteException;
}
package ufrn.imd.controller.impl;public class BalanceControllerImpl {
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!");
}
}
}
}
}
package ufrn.imd.controller.impl;
import ufrn.imd.controller.Controller;
import ufrn.imd.log.Log;
import ufrn.imd.message.Client;
import ufrn.imd.utils.Log;
import ufrn.imd.domain.Client;
import ufrn.imd.service.DepositService;
import java.net.MalformedURLException;
......@@ -14,21 +14,22 @@ import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import java.util.List;
import static ufrn.imd.nameSpace.NameSpace.HOST_DEPOSIT_SERVICE;
import static ufrn.imd.nameSpace.NameSpace.PORT_DEPOSIT_SERVICE;
import static ufrn.imd.utils.NameSpace.HOST_DEPOSIT_SERVICE;
import static ufrn.imd.utils.NameSpace.PORT_DEPOSIT_SERVICE;
public class DepositControllerImpl extends UnicastRemoteObject implements Controller {
private volatile List<Client> clients = new ArrayList<>();
public DepositControllerImpl(DepositService service) throws RemoteException, MalformedURLException, AlreadyBoundException {
super();
Log.info(DepositControllerImpl.class, "Starting Deposit service!");
new Notify(service).start();
new Notify(service).start();
Log.info(DepositControllerImpl.class,
String.format("Initializing server in %s", HOST_DEPOSIT_SERVICE));
LocateRegistry.createRegistry(PORT_DEPOSIT_SERVICE);
Naming.bind(HOST_DEPOSIT_SERVICE, this);
}
......@@ -40,6 +41,8 @@ public class DepositControllerImpl extends UnicastRemoteObject implements Contro
}
private class Notify extends Thread{
private final DepositService service;
......@@ -63,17 +66,25 @@ public class DepositControllerImpl extends UnicastRemoteObject implements Contro
try {
this.service.deposit(client.getBalance(), 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(DepositControllerImpl.class, "No there's clients!");
}
}
......
package ufrn.imd.message;
package ufrn.imd.domain;
import ufrn.imd.domain.Account;
......@@ -7,7 +7,6 @@ import java.util.Optional;
public class Client implements Serializable {
private static final long serialVersionUID = 1L;
private Account account;
public Double getBalance() {
return account.getBalance();
......
......@@ -4,8 +4,9 @@ import ufrn.imd.domain.Account;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.Optional;
public interface BalanceService extends Remote {
public void balance(Account account) throws RemoteException;
public void balance(Optional<Account> account) throws RemoteException;
}
......@@ -8,6 +8,6 @@ import java.util.Optional;
public interface DepositService extends Remote {
public void deposit(Double value, Optional<Account> account) throws RemoteException;
public void deposit(Double value, Optional<Account> account) throws RemoteException, RuntimeException;
}
package ufrn.imd.service.impl;public class BalanceService {
package ufrn.imd.service.impl;
import ufrn.imd.domain.Account;
import java.rmi.RemoteException;
import java.util.Optional;
import ufrn.imd.service.*;
import ufrn.imd.utils.Log;
public class BalanceServiceImpl implements BalanceService {
@Override
public void balance(Optional<Account> accOP) throws RemoteException {
Account account = accOP.orElseThrow(() -> new RuntimeException("Invalid account"));
Log.info(
BalanceServiceImpl.class,
String.format("Account Balance: R$ %f", account.getBalance())
);
}
}
package ufrn.imd.service.impl;
import ufrn.imd.domain.Account;
import ufrn.imd.log.Log;
import ufrn.imd.utils.Log;
import ufrn.imd.service.DepositService;
import java.rmi.RemoteException;
......@@ -15,11 +15,9 @@ public class DepositServiceImpl implements DepositService {
public void deposit(Double value, Optional<Account> acOp) throws RemoteException, RuntimeException {
Account account = acOp.orElseThrow(() -> new RuntimeException("Null User!"));
if(account.getBalance() < value)
throw new RuntimeException("There's no money!");
if(account.getBalance() < value) throw new RuntimeException("There's no money!");
account.setBalance(account.getBalance() - value);
Log.info(DepositServiceImpl.class, "deposit");
}
}
package ufrn.imd.utils.log;
package ufrn.imd.utils;
public class Log {
......
package ufrn.imd.utils.nameSpace;
package ufrn.imd.utils;
public class NameSpace {
public final static Integer PORT_BALANCE_SERVICE = 1908;
public final static Integer PORT_DEPOSIT_SERVICE = 1907;
public final static String HOST_DEPOSIT_SERVICE =
String.format("rmi://127.0.0.1:%d/%s", PORT_DEPOSIT_SERVICE, "deposit");
public final static String HOST_BALANCE_SERVICE =
String.format("rmi://127.0.0.1:%d/%s", PORT_BALANCE_SERVICE, "balance");
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment