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

Implemented multiple automated random commits

parent 555a1eb0
No related branches found
No related tags found
1 merge request!5Configuration system
...@@ -56,7 +56,6 @@ public class ActionCommitDistributedCommittedTransfer extends ClientAction { ...@@ -56,7 +56,6 @@ public class ActionCommitDistributedCommittedTransfer extends ClientAction {
if (observer != null) { if (observer != null) {
observer.tell(new ActionNotifyObserver(source, target, amount, granted, timestamp, id), self); observer.tell(new ActionNotifyObserver(source, target, amount, granted, timestamp, id), self);
} }
} }
} else { } else {
......
...@@ -38,7 +38,6 @@ public class ActionPrepareDistributedCommittedTransfer extends Transaction { ...@@ -38,7 +38,6 @@ public class ActionPrepareDistributedCommittedTransfer extends Transaction {
wallet.amounts.put(source, sourceAmount - amount); wallet.amounts.put(source, sourceAmount - amount);
wallet.amounts.put(target, targetAmount + amount); wallet.amounts.put(target, targetAmount + amount);
sender.tell(new ActionPrepareDistributedCommittedTransferAnswer(source, target, amount, timestamp, granted, id), self); sender.tell(new ActionPrepareDistributedCommittedTransferAnswer(source, target, amount, timestamp, granted, id), self);
} }
......
...@@ -6,7 +6,11 @@ import akka.pattern.Patterns; ...@@ -6,7 +6,11 @@ import akka.pattern.Patterns;
import akka.util.Timeout; import akka.util.Timeout;
import fucoin.AbstractNode; import fucoin.AbstractNode;
import fucoin.actions.control.ActionAnnounceWalletCreation; import fucoin.actions.control.ActionAnnounceWalletCreation;
import fucoin.actions.control.ActionWalletSendMoney;
import fucoin.actions.join.ActionTellSupervisor; import fucoin.actions.join.ActionTellSupervisor;
import fucoin.actions.transaction.ActionGetAmount;
import fucoin.actions.transaction.ActionGetAmountAnswer;
import fucoin.actions.transaction.ActionNotifyObserver;
import fucoin.configurations.internal.ConfigurationCreator; import fucoin.configurations.internal.ConfigurationCreator;
import fucoin.supervisor.SuperVisorImpl; import fucoin.supervisor.SuperVisorImpl;
import fucoin.wallet.WalletImpl; import fucoin.wallet.WalletImpl;
...@@ -15,6 +19,7 @@ import scala.concurrent.Future; ...@@ -15,6 +19,7 @@ import scala.concurrent.Future;
import scala.concurrent.duration.Duration; import scala.concurrent.duration.Duration;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.ThreadLocalRandom;
...@@ -29,6 +34,8 @@ public abstract class AbstractConfiguration extends AbstractNode { ...@@ -29,6 +34,8 @@ public abstract class AbstractConfiguration extends AbstractNode {
private Timeout timeout = new Timeout(Duration.create(10, "seconds")); private Timeout timeout = new Timeout(Duration.create(10, "seconds"));
private int remainingTransactions;
public static Props props(Class configurationClass) { public static Props props(Class configurationClass) {
return Props.create(new ConfigurationCreator(configurationClass)); return Props.create(new ConfigurationCreator(configurationClass));
...@@ -36,6 +43,7 @@ public abstract class AbstractConfiguration extends AbstractNode { ...@@ -36,6 +43,7 @@ public abstract class AbstractConfiguration extends AbstractNode {
/** /**
* Spawns a new wallet and blocks until it has received its initial money * Spawns a new wallet and blocks until it has received its initial money
*
* @throws Exception on timeout * @throws Exception on timeout
*/ */
ActorRef spawnWallet(String name, boolean createGUI) throws Exception { ActorRef spawnWallet(String name, boolean createGUI) throws Exception {
...@@ -70,6 +78,7 @@ public abstract class AbstractConfiguration extends AbstractNode { ...@@ -70,6 +78,7 @@ public abstract class AbstractConfiguration extends AbstractNode {
/** /**
* Spawn multiple wallets and wait until they all have their initial FUC * Spawn multiple wallets and wait until they all have their initial FUC
*
* @throws Exception on timeout * @throws Exception on timeout
*/ */
public void spawnWallets(int n, boolean createGUI) throws Exception { public void spawnWallets(int n, boolean createGUI) throws Exception {
...@@ -92,6 +101,43 @@ public abstract class AbstractConfiguration extends AbstractNode { ...@@ -92,6 +101,43 @@ public abstract class AbstractConfiguration extends AbstractNode {
return this.activeActors; return this.activeActors;
} }
protected void randomTransactions(int number, int maxTransactionsAtTheSameTime) {
remainingTransactions = number;
for (int i = 0; i < Math.min(number, maxTransactionsAtTheSameTime); i++) {
nextRandomTransaction();
}
}
private void nextRandomTransaction() {
remainingTransactions--;
try {
randomTransaction();
} catch (Exception e) {
System.err.println("Error while trying to perform a random transaction: "+e.getMessage());
remainingTransactions = 0;
}
}
private void randomTransaction() throws Exception {
List<ActorRef> wallets = wallets();
Collections.shuffle(wallets);
ActorRef sender = wallets.get(0);
ActorRef recipient = wallets.get(1);
Future<Object> future = Patterns.ask(sender, new ActionGetAmount(), timeout);
ActionGetAmountAnswer answer = (ActionGetAmountAnswer) Await.result(future, timeout.duration());
int transferAmount = 1 + ThreadLocalRandom.current().nextInt(answer.amount);
sender.tell(new ActionWalletSendMoney(recipient.path().name(), transferAmount, self()), self());
}
/** /**
* Create the supervisor node * Create the supervisor node
*/ */
...@@ -107,6 +153,26 @@ public abstract class AbstractConfiguration extends AbstractNode { ...@@ -107,6 +153,26 @@ public abstract class AbstractConfiguration extends AbstractNode {
return superVisor; return superVisor;
} }
@Override
public void onReceive(Object message) {
if (message instanceof ActionNotifyObserver) {
ActionNotifyObserver notification = (ActionNotifyObserver) message;
String status = "successful";
if (!notification.granted) {
status = "failed";
}
System.out.println("Observed a " + status + " transaction of " + notification.amount + " FUCs from " +
notification.source.path().name() + " to " + notification.target.path().name());
if (remainingTransactions > 0) {
nextRandomTransaction();
}
}
}
@Override @Override
public void preStart() throws Exception { public void preStart() throws Exception {
super.preStart(); super.preStart();
......
...@@ -14,7 +14,7 @@ import scala.concurrent.duration.Duration; ...@@ -14,7 +14,7 @@ import scala.concurrent.duration.Duration;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Random; import java.util.concurrent.ThreadLocalRandom;
/** /**
* This configuration is the previous default of 2 wallets with GUI and a supervisor. * This configuration is the previous default of 2 wallets with GUI and a supervisor.
...@@ -22,10 +22,11 @@ import java.util.Random; ...@@ -22,10 +22,11 @@ import java.util.Random;
@ConfigurationName("Default Configuration") @ConfigurationName("Default Configuration")
public class DefaultConfiguration extends AbstractConfiguration { public class DefaultConfiguration extends AbstractConfiguration {
private Random rand; private ThreadLocalRandom rand;
private Timeout timeout = new Timeout(Duration.create(10, "seconds"));
public DefaultConfiguration(){ public DefaultConfiguration(){
rand = new Random(); rand = ThreadLocalRandom.current();
} }
@Override @Override
...@@ -44,47 +45,16 @@ public class DefaultConfiguration extends AbstractConfiguration { ...@@ -44,47 +45,16 @@ public class DefaultConfiguration extends AbstractConfiguration {
} catch (Exception e) { } catch (Exception e) {
System.out.println("Wallet1 spawning timed out"); System.out.println("Wallet1 spawning timed out");
} }
/*
if (wallet1 != null && wallet2 != null) { if (wallet1 != null && wallet2 != null) {
wallet1.tell(new ActionWalletSendMoney(wallet2.path().name(), 50, getSelf()), wallet1); 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 @Override
public void onReceive(Object message) { public void onReceive(Object message) {
if (message instanceof ActionNotifyObserver){ super.onReceive(message);
ActionNotifyObserver notification = (ActionNotifyObserver) message;
String status = "successful";
if(! notification.granted){
status = "failed";
}
System.out.println("Observed a " + status + " transaction of " + notification.amount+" FUCs from " +
notification.source.path().name() + " to " + notification.target.path().name());
}
} }
} }
...@@ -16,10 +16,12 @@ public class MassWalletConfiguration extends AbstractConfiguration { ...@@ -16,10 +16,12 @@ public class MassWalletConfiguration extends AbstractConfiguration {
} catch (Exception e) { } catch (Exception e) {
System.out.println("Wallet spawning timed out!"); System.out.println("Wallet spawning timed out!");
} }
randomTransactions(100, 10);
} }
@Override @Override
public void onReceive(Object message) { public void onReceive(Object message) {
super.onReceive(message);
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment