go.dedis.ch/onet/v4@v4.0.0-pre1/simul/test_simul/simul.go (about)

     1  package main
     2  
     3  import (
     4  	"strconv"
     5  
     6  	"github.com/BurntSushi/toml"
     7  	"go.dedis.ch/onet/v4"
     8  	"go.dedis.ch/onet/v4/log"
     9  	"go.dedis.ch/onet/v4/simul"
    10  	"go.dedis.ch/onet/v4/simul/manage"
    11  	"go.dedis.ch/onet/v4/simul/monitor"
    12  	"golang.org/x/xerrors"
    13  )
    14  
    15  /*
    16  Defines the simulation for the count-protocol
    17  */
    18  
    19  func init() {
    20  	onet.SimulationRegister("CountTest", NewSimulation)
    21  }
    22  
    23  // Simulation only holds the BFTree simulation
    24  type simulation struct {
    25  	onet.SimulationBFTree
    26  	Other  string
    27  	Ration float64
    28  }
    29  
    30  // NewSimulation returns the new simulation, where all fields are
    31  // initialised using the config-file
    32  func NewSimulation(config string) (onet.Simulation, error) {
    33  	es := &simulation{}
    34  	_, err := toml.Decode(config, es)
    35  	if err != nil {
    36  		return nil, xerrors.Errorf("decoding toml: %v", err)
    37  	}
    38  	return es, nil
    39  }
    40  
    41  // Setup creates the tree used for that simulation
    42  func (e *simulation) Setup(dir string, hosts []string) (
    43  	*onet.SimulationConfig, error) {
    44  	sc := &onet.SimulationConfig{}
    45  	e.CreateRoster(sc, hosts, 2000)
    46  	err := e.CreateTree(sc)
    47  	if err != nil {
    48  		return nil, xerrors.Errorf("creating tree: %v", err)
    49  	}
    50  	return sc, nil
    51  }
    52  
    53  // Run is used on the destination machines and runs a number of
    54  // rounds
    55  func (e *simulation) Run(config *onet.SimulationConfig) error {
    56  	size := config.Tree.Size()
    57  	log.Lvl2("Size is:", size, "rounds:", e.Rounds)
    58  	for round := 0; round < e.Rounds; round++ {
    59  		log.Lvl1("Starting round", round)
    60  		round := monitor.NewTimeMeasure("round")
    61  		p, err := config.Overlay.CreateProtocol("Count", config.Tree, onet.NilServiceID)
    62  		if err != nil {
    63  			return xerrors.Errorf("creating protocol: %v", err)
    64  		}
    65  		go p.Start()
    66  		children := <-p.(*manage.ProtocolCount).Count
    67  		round.Record()
    68  		if children != size {
    69  			return xerrors.New("Didn't get " + strconv.Itoa(size) +
    70  				" children")
    71  		}
    72  	}
    73  	return nil
    74  }
    75  
    76  func main() {
    77  	simul.Start()
    78  }