Skip to content
Snippets Groups Projects
Unverified Commit 555a1eb0 authored by Luca Keidel's avatar Luca Keidel
Browse files

Implemented a basic version of a random transaction

parent 8da179f4
No related branches found
No related tags found
1 merge request!5Configuration system
package fucoin.configurations;
import akka.actor.ActorRef;
import akka.pattern.Patterns;
import akka.util.Timeout;
import fucoin.actions.control.ActionWalletSendMoney;
import fucoin.actions.transaction.ActionGetAmount;
import fucoin.actions.transaction.ActionGetAmountAnswer;
import fucoin.actions.transaction.ActionNotifyObserver;
import fucoin.configurations.internal.ConfigurationName;
import scala.concurrent.Await;
import scala.concurrent.Future;
import scala.concurrent.duration.Duration;
import java.util.Collections;
import java.util.List;
import java.util.Random;
/**
* This configuration is the previous default of 2 wallets with GUI and a supervisor.
......@@ -11,6 +22,12 @@ import fucoin.configurations.internal.ConfigurationName;
@ConfigurationName("Default Configuration")
public class DefaultConfiguration extends AbstractConfiguration {
private Random rand;
public DefaultConfiguration(){
rand = new Random();
}
@Override
public void run() {
initSupervisor();
......@@ -18,19 +35,41 @@ public class DefaultConfiguration extends AbstractConfiguration {
ActorRef wallet1 = null;
ActorRef wallet2 = null;
try {
wallet1 = spawnWallet("Wallet0", false);
wallet1 = spawnWallet("Wallet0", true);
} catch (Exception e) {
System.out.println("Wallet0 spawning timed out");
}
try {
wallet2 = spawnWallet("Wallet1", false);
wallet2 = spawnWallet("Wallet1", true);
} catch (Exception e) {
System.out.println("Wallet1 spawning timed out");
}
/*
if (wallet1 != null && wallet2 != null) {
wallet1.tell(new ActionWalletSendMoney(wallet2.path().name(), 50, getSelf()), wallet1);
}
*/
try {
randomTransaction();
} catch (Exception e) {
e.printStackTrace();
}
}
private void randomTransaction() throws Exception{
List<ActorRef> wallets = wallets();
Collections.shuffle(wallets);
ActorRef sender = wallets.get(0);
ActorRef recipient = wallets.get(1);
Timeout timeout = new Timeout(Duration.create(10, "seconds"));
Future<Object> future = Patterns.ask(sender, new ActionGetAmount(), timeout);
ActionGetAmountAnswer answer = (ActionGetAmountAnswer) Await.result(future, timeout.duration());
int transferAmount = rand.nextInt(answer.amount);
sender.tell(new ActionWalletSendMoney(recipient.path().name(), transferAmount, self()), self());
}
@Override
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment