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

initial commit

parent 44ed2321
No related branches found
No related tags found
1 merge request!1Client state machine
Showing
with 643 additions and 159 deletions
/banking-server/target/ banking-server/target/
banking-client/target/
banking-client/.idea/
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>banking-client</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package ufrn.imd; package ufrn.imd;
import lombok.extern.java.Log;
import ufrn.imd.controller.Controller; import ufrn.imd.controller.Controller;
import ufrn.imd.domain.Account; import ufrn.imd.domain.Account;
import ufrn.imd.log.Log; import ufrn.imd.domain.Client;
import ufrn.imd.message.Client; import ufrn.imd.utils.MachineState;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.rmi.Naming; import java.rmi.Naming;
import java.rmi.NotBoundException; import java.rmi.NotBoundException;
import java.rmi.RemoteException; import java.rmi.RemoteException;
import static ufrn.imd.nameSpace.NameSpace.HOST_DEPOSIT_SERVICE; import static ufrn.imd.utils.MachineState.*;
import static ufrn.imd.utils.NameSpace.HOST_DEPOSIT_SERVICE;
@Log
public class BankingClientApp { public class BankingClientApp {
public static void main(String[] args) throws MalformedURLException, NotBoundException, RemoteException { public static void main(String[] args) throws MalformedURLException, NotBoundException, RemoteException {
final Integer PORT = 1097;
Log.info(BankingClientApp.class,
String.format("connecting with server in host %s", HOST_DEPOSIT_SERVICE)
);
Controller server = (Controller) Naming.lookup(HOST_DEPOSIT_SERVICE);
Client client = new Client(new Account(1, "222", 10.00)); MachineState mS = new MachineState();
Log.info(BankingClientApp.class, "registring user in the server");
server.registerClient(client); while(mS.quit()) {
mS.render();
mS.processEvents();
mS.update();
}
} }
} }
package ufrn.imd.controller; package ufrn.imd.controller;
import ufrn.imd.message.Client; import ufrn.imd.domain.Client;
import java.rmi.Remote; import java.rmi.Remote;
import java.rmi.RemoteException; import java.rmi.RemoteException;
......
package ufrn.imd.domain; package ufrn.imd.domain;
import lombok.AllArgsConstructor;
import lombok.Data;
import ufrn.imd.domain.Account; import ufrn.imd.domain.Account;
import java.io.Serializable; import java.io.Serializable;
import java.util.Optional; import java.util.Optional;
@Data
@AllArgsConstructor
public class Client implements Serializable { public class Client implements Serializable {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private Account account; private Integer id;
public Client(Account account) {
this.account = account;
}
public Double getBalance() {
return account.getBalance();
}
public Optional<Account> getAccount() {
return Optional.of(account);
}
} }
package ufrn.imd.utils;
import lombok.Data;
import ufrn.imd.controller.Controller;
import ufrn.imd.domain.Client;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.Scanner;
import static ufrn.imd.utils.NameSpace.HOST_DEPOSIT_SERVICE;
@Data
public class MachineState {
public static final String INITIAL= "INITIAL";
public static final String RENDER = "RENDER";
public static final String UPDATE_STATE = "UPDATE_STATE";
public static final String READ = "READ";
public static final String DEPOSIT = "DEPOSIT";
public static final String BALANCE = "BALANCE";
public static final String HELP = "HELP";
public static final String INVALID_INPUT = "INVALID_INPUT";
public static final String QUIT = "QUIT";
private String state;
private Scanner sc;
private String inputValue;
public MachineState() {
state = INITIAL;
inputValue = new String();
sc = new Scanner(System.in);
}
public Boolean quit() {
return !state.equals(QUIT);
}
public void update() {
if(state.equals(INITIAL)) {
state = HELP;
} else if(state.equals(HELP)) {
state = READ;
} else if(state.equals(READ)) {
state = inputValue;
} else if(state.equals(INVALID_INPUT)) {
state = HELP;
} else if(state.equals(DEPOSIT)) {
state = HELP;
}
}
public void processEvents() throws MalformedURLException, NotBoundException, RemoteException {
if(state.equals(READ)) {
inputValue = new String();
inputValue = sc.nextLine().trim().toUpperCase();
System.out.println(String.format(">>> input value: %s", inputValue));
if(!inputValue.equals(DEPOSIT) && !inputValue.equals(DEPOSIT) && !inputValue.equals(BALANCE)){
inputValue = INVALID_INPUT;
}
} else if(state.equals(DEPOSIT)) {
double v = sc.nextDouble();
System.out.println(String.format(">>> input value: %.2f", v));
Client client = new Client(1);
Controller server = (Controller) Naming.lookup(HOST_DEPOSIT_SERVICE);
server.registerClient(client);
}
}
public void render() {
try {
if(state.equals(INITIAL)) {
System.out.println(">>> Initializing...");
Thread.sleep(1000);
} else if(state.equals(HELP)) {
System.out.println("These are the avaliable operations");
System.out.println("---------------------------------");
System.out.println(" - deposit \t - balance");
System.out.println(" - quit");
System.out.println("---------------------------------");
Thread.sleep(1000);
} else if(state.equals(READ)) {
System.out.println(">>> Please enter with wich operation do you want");
} else if(state.equals(INVALID_INPUT)) {
System.out.println(">>> Please enter with a VALID operation");
Thread.sleep(3000);
} else if(state.equals(DEPOSIT)) {
System.out.println(">>> Starting deposit operation");
System.out.println(">>> Please enter with the value that you want deposit");
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
package ufrn.imd.nameSpace; package ufrn.imd.utils;
public class NameSpace { public class NameSpace {
public static final Integer PORT_DEPOSIT_SERVICE = 1907; public static final Integer PORT_DEPOSIT_SERVICE = 1907;
......
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();
}
}
}
}
}
}
package ufrn.imd.log;
public class Log {
public static void info(Class path, String text) {
System.out.println(
String.format("[LOG] %s - %s", path , text)
);
}
}
package ufrn.imd.service;
import ufrn.imd.domain.Account;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface BalanceService extends Remote {
public void balance(Account account) throws RemoteException;
}
package ufrn.imd.service;
import ufrn.imd.domain.Account;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.Optional;
public interface DepositService extends Remote {
public void deposit(Double value, Optional<Account> account) throws RemoteException;
}
package ufrn.imd.service;
import ufrn.imd.domain.Account;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface WidthawService extends Remote {
public void widthaw(Double value, Account account) throws RemoteException;
}
package ufrn.imd.service.impl;
import ufrn.imd.domain.Account;
import ufrn.imd.log.Log;
import ufrn.imd.service.DepositService;
import java.rmi.RemoteException;
import java.util.Optional;
public class DepositServiceImpl implements DepositService {
public DepositServiceImpl() {
super();
}
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!");
account.setBalance(account.getBalance() - value);
Log.info(DepositServiceImpl.class, "deposit");
}
}
This diff is collapsed.
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
</list> </list>
</option> </option>
</component> </component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="17" project-jdk-type="JavaSDK"> <component name="ProjectRootManager" version="2" languageLevel="JDK_11" default="true" project-jdk-name="11" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" /> <output url="file://$PROJECT_DIR$/out" />
</component> </component>
</project> </project>
\ No newline at end of file
...@@ -9,8 +9,8 @@ ...@@ -9,8 +9,8 @@
<version>1.0-SNAPSHOT</version> <version>1.0-SNAPSHOT</version>
<properties> <properties>
<maven.compiler.source>17</maven.compiler.source> <maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target> <maven.compiler.target>11</maven.compiler.target>
</properties> </properties>
<dependencies> <dependencies>
......
...@@ -3,8 +3,7 @@ package ufrn.imd; ...@@ -3,8 +3,7 @@ package ufrn.imd;
import ufrn.imd.controller.Controller; import ufrn.imd.controller.Controller;
import ufrn.imd.controller.impl.BalanceControllerImpl; import ufrn.imd.controller.impl.BalanceControllerImpl;
import ufrn.imd.controller.impl.DepositControllerImpl; import ufrn.imd.controller.impl.DepositControllerImpl;
import ufrn.imd.service.BalanceService; import ufrn.imd.repository.impl.ClientRepository;
import ufrn.imd.service.DepositService;
import ufrn.imd.service.impl.BalanceServiceImpl; import ufrn.imd.service.impl.BalanceServiceImpl;
import ufrn.imd.service.impl.DepositServiceImpl; import ufrn.imd.service.impl.DepositServiceImpl;
...@@ -15,8 +14,8 @@ import java.rmi.RemoteException; ...@@ -15,8 +14,8 @@ import java.rmi.RemoteException;
public class BankingServerApp { public class BankingServerApp {
public static void main(String[] args) throws RemoteException, MalformedURLException, AlreadyBoundException { public static void main(String[] args) throws RemoteException, MalformedURLException, AlreadyBoundException {
DepositService depositService = new DepositServiceImpl(); DepositServiceImpl depositService = new DepositServiceImpl(new ClientRepository());
BalanceService balanceService = new BalanceServiceImpl(); BalanceServiceImpl balanceService = new BalanceServiceImpl(new ClientRepository());
Controller depositController = new DepositControllerImpl(depositService); Controller depositController = new DepositControllerImpl(depositService);
Controller balanceController = new BalanceControllerImpl(balanceService); Controller balanceController = new BalanceControllerImpl(balanceService);
......
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