/
service.go
77 lines (69 loc) · 2.03 KB
/
service.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
72
73
74
75
76
77
package main
import (
"github.com/BurntSushi/toml"
"github.com/dedis/cothority_template"
"go.dedis.ch/onet/v3"
"go.dedis.ch/onet/v3/log"
"go.dedis.ch/onet/v3/simul/monitor"
)
/*
* Defines the simulation for the service-template
*/
func init() {
onet.SimulationRegister("TemplateService", NewSimulationService)
}
// SimulationService only holds the BFTree simulation
type SimulationService struct {
onet.SimulationBFTree
}
// NewSimulationService returns the new simulation, where all fields are
// initialised using the config-file
func NewSimulationService(config string) (onet.Simulation, error) {
es := &SimulationService{}
_, err := toml.Decode(config, es)
if err != nil {
return nil, err
}
return es, nil
}
// Setup creates the tree used for that simulation
func (s *SimulationService) Setup(dir string, hosts []string) (
*onet.SimulationConfig, error) {
sc := &onet.SimulationConfig{}
s.CreateRoster(sc, hosts, 2000)
err := s.CreateTree(sc)
if err != nil {
return nil, err
}
return sc, nil
}
// Node can be used to initialize each node before it will be run
// by the server. Here we call the 'Node'-method of the
// SimulationBFTree structure which will load the roster- and the
// tree-structure to speed up the first round.
func (s *SimulationService) Node(config *onet.SimulationConfig) error {
index, _ := config.Roster.Search(config.Server.ServerIdentity.ID)
if index < 0 {
log.Fatal("Didn't find this node in roster")
}
log.Lvl3("Initializing node-index", index)
return s.SimulationBFTree.Node(config)
}
// Run is used on the destination machines and runs a number of
// rounds
func (s *SimulationService) Run(config *onet.SimulationConfig) error {
size := config.Tree.Size()
log.Lvl2("Size is:", size, "rounds:", s.Rounds)
c := template.NewClient()
for round := 0; round < s.Rounds; round++ {
log.Lvl1("Starting round", round)
round := monitor.NewTimeMeasure("round")
resp, err := c.Clock(config.Roster)
log.ErrFatal(err)
if resp.Time <= 0 {
log.Fatal("0 time elapsed")
}
round.Record()
}
return nil
}