go.dedis.ch/onet/v3@v3.2.11-0.20210930124529-e36530bca7ef/simul/test_simul/simul.go (about)

     1  package main
     2  
     3  import (
     4  	"strconv"
     5  	"sync"
     6  	"time"
     7  
     8  	"github.com/BurntSushi/toml"
     9  	"go.dedis.ch/onet/v3"
    10  	"go.dedis.ch/onet/v3/log"
    11  	"go.dedis.ch/onet/v3/network"
    12  	"go.dedis.ch/onet/v3/simul"
    13  	"go.dedis.ch/onet/v3/simul/manage"
    14  	"go.dedis.ch/onet/v3/simul/monitor"
    15  	"golang.org/x/xerrors"
    16  )
    17  
    18  type testInit struct{}
    19  
    20  var testInitID network.MessageTypeID
    21  
    22  /*
    23  Defines the simulation for the count-protocol
    24  */
    25  
    26  func init() {
    27  	onet.SimulationRegister("CountTest", NewSimulation)
    28  
    29  	testInitID = network.RegisterMessage(&testInit{})
    30  }
    31  
    32  // Simulation only holds the BFTree simulation
    33  type simulation struct {
    34  	onet.SimulationBFTree
    35  	Other  string
    36  	Ration float64
    37  }
    38  
    39  // NewSimulation returns the new simulation, where all fields are
    40  // initialised using the config-file
    41  func NewSimulation(config string) (onet.Simulation, error) {
    42  	es := &simulation{}
    43  	_, err := toml.Decode(config, es)
    44  	if err != nil {
    45  		return nil, xerrors.Errorf("decoding toml: %v", err)
    46  	}
    47  	return es, nil
    48  }
    49  
    50  // Setup creates the tree used for that simulation
    51  func (e *simulation) Setup(dir string, hosts []string) (
    52  	*onet.SimulationConfig, error) {
    53  	sc := &onet.SimulationConfig{}
    54  	e.CreateRoster(sc, hosts, 2000)
    55  	err := e.CreateTree(sc)
    56  	if err != nil {
    57  		return nil, xerrors.Errorf("creating tree: %v", err)
    58  	}
    59  	return sc, nil
    60  }
    61  
    62  func (e *simulation) Node(config *onet.SimulationConfig) error {
    63  	wg := sync.WaitGroup{}
    64  	wg.Add(1)
    65  	config.Server.RegisterProcessorFunc(testInitID, func(*network.Envelope) error {
    66  		wg.Done()
    67  		return nil
    68  	})
    69  
    70  	if config.Server.ServerIdentity.Equal(config.Tree.Root.ServerIdentity) {
    71  		time.Sleep(1 * time.Second)
    72  		for _, tn := range config.Tree.List() {
    73  			config.Server.Send(tn.ServerIdentity, &testInit{})
    74  		}
    75  	}
    76  
    77  	wg.Wait()
    78  	return nil
    79  }
    80  
    81  // Run is used on the destination machines and runs a number of
    82  // rounds
    83  func (e *simulation) Run(config *onet.SimulationConfig) error {
    84  	size := config.Tree.Size()
    85  	log.Lvl2("Size is:", size, "rounds:", e.Rounds)
    86  	for round := 0; round < e.Rounds; round++ {
    87  		log.Lvl1("Starting round", round)
    88  		round := monitor.NewTimeMeasure("round")
    89  		p, err := config.Overlay.CreateProtocol("Count", config.Tree, onet.NilServiceID)
    90  		if err != nil {
    91  			return xerrors.Errorf("creating protocol: %v", err)
    92  		}
    93  		go p.Start()
    94  		children := <-p.(*manage.ProtocolCount).Count
    95  		round.Record()
    96  		if children != size {
    97  			return xerrors.New("Didn't get " + strconv.Itoa(size) +
    98  				" children")
    99  		}
   100  	}
   101  	return nil
   102  }
   103  
   104  func main() {
   105  	simul.Start()
   106  }