Skip to content
Snippets Groups Projects
Select Git revision
  • ffbd40b1fbc998427d92dd6f3e28d356b0a07bb1
  • master default
  • releases/2.5-1
3 results

triangularsolve.hh

Blame
  • Forked from agnumpde / dune-matrix-vector
    Source project has a limited visibility.
    Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    StatisticsWalletConfiguration.java 3.39 KiB
    package fucoin.configurations;
    
    import akka.actor.ActorRef;
    import akka.pattern.Patterns;
    import fucoin.actions.control.ActionCreateSuperVisorSnapshot;
    import fucoin.actions.statistics.ActionInterchangeState;
    import fucoin.actions.transaction.ActionGetAmount;
    import fucoin.actions.transaction.ActionGetAmountAnswer;
    import fucoin.configurations.internal.ConfigurationName;
    import scala.concurrent.Await;
    import scala.concurrent.Future;
    
    import java.time.LocalDateTime;
    import java.util.Collections;
    import java.util.List;
    import java.util.concurrent.ThreadLocalRandom;
    
    /**
     * This configuration spawns 200 wallets to demonstrate the spawning of many headless wallets
     */
    @ConfigurationName("Statistics")
    public class StatisticsWalletConfiguration extends AbstractConfiguration {
        /**
         * Spawn the number of wallets.
         */
        private final static int sNumberOfWallets = 10;
    
        //private int numberOfInterchangeStatistic = 20;
    
        @Override
        public void run() {
            initSupervisor();
            try {
                spawnWallets(sNumberOfWallets, false);
                System.out.println("Wallet spawning done!");
            } catch (Exception e) {
                System.out.println("Wallet spawning timed out!");
            }
    
            //Wait until init money was send to wallets
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
    
            //Send InterchangeState messages between wallets
            new Thread(() -> {
                System.out.println("Randomly interchange message process started.");
                boolean toggle = true;
                while (true) {
                    try {
                        if (toggle) {
                            randomTransactions(wallets().size() / 4, 10);
                        } else {
                            interchangeStatistic(wallets().size() * 4);
                        }
                        toggle = !toggle;
                        Thread.sleep(ThreadLocalRandom.current().nextInt(10) * 1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }).run();
    
            //Transfer random many between wallets
    
        }
    
        private void interchangeStatistic(int numberOfInterchangeStatistic) {
            if (!(numberOfInterchangeStatistic > 0)) {
                //stop interchange statistics
                return;
            }
    
            List<ActorRef> wallets = wallets();
            Collections.shuffle(wallets);
    
            ActorRef sender = wallets.get(0);
            ActorRef recipient = wallets.get(1);
            try {
                Future<Object> future = Patterns.ask(sender, new ActionGetAmount(), timeout);
                ActionGetAmountAnswer answer = (ActionGetAmountAnswer) Await.result(future, timeout.duration());
                recipient.tell(new ActionInterchangeState(answer.amount), sender);
            } catch (Exception e) {
                e.printStackTrace();
            }
    
    
            numberOfInterchangeStatistic--;
            if (numberOfInterchangeStatistic == 0) {
                createSnapshot();
            } else {
                interchangeStatistic(numberOfInterchangeStatistic);
            }
        }
    
        /**
         * Create a Dump of the SuperVisor and all active Wallets.
         */
        private void createSnapshot() {
            superVisor.tell(new ActionCreateSuperVisorSnapshot(LocalDateTime.now()), self());
        }
    
        @Override
        public void onReceive(Object message) {
            super.onReceive(message);
        }
    
    
    }