-
Notifications
You must be signed in to change notification settings - Fork 106
/
simulation.go
71 lines (63 loc) · 1.67 KB
/
simulation.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package example_handlers
import (
"errors"
"strconv"
"github.com/BurntSushi/toml"
"gopkg.in/dedis/cothority.v0/lib/dbg"
"gopkg.in/dedis/cothority.v0/lib/monitor"
"gopkg.in/dedis/cothority.v0/lib/sda"
)
/*
This is a simple ExampleHandlers-protocol with two steps:
- announcement - which sends a message to all children
- reply - used for counting the number of children
*/
func init() {
sda.SimulationRegister("ExampleHandlers", NewSimulation)
}
// Simulation implements sda.Simulation.
type Simulation struct {
sda.SimulationBFTree
}
// NewSimulation is used internally to register the simulation (see the init()
// function above).
func NewSimulation(config string) (sda.Simulation, error) {
es := &Simulation{}
_, err := toml.Decode(config, es)
if err != nil {
return nil, err
}
return es, nil
}
// Setup implements sda.Simulation.
func (e *Simulation) Setup(dir string, hosts []string) (
*sda.SimulationConfig, error) {
sc := &sda.SimulationConfig{}
e.CreateEntityList(sc, hosts, 2000)
err := e.CreateTree(sc)
if err != nil {
return nil, err
}
return sc, nil
}
// Run implements sda.Simulation.
func (e *Simulation) Run(config *sda.SimulationConfig) error {
size := config.Tree.Size()
dbg.Lvl2("Size is:", size, "rounds:", e.Rounds)
for round := 0; round < e.Rounds; round++ {
dbg.Lvl1("Starting round", round)
round := monitor.NewTimeMeasure("round")
p, err := config.Overlay.CreateProtocol(config.Tree, "ExampleHandlers")
if err != nil {
return err
}
go p.Start()
children := <-p.(*ProtocolExampleHandlers).ChildCount
round.Record()
if children != size {
return errors.New("Didn't get " + strconv.Itoa(size) +
" children")
}
}
return nil
}