-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathapp.go
95 lines (87 loc) · 2.25 KB
/
app.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
* This is a template for creating an app. It only has one command which
* prints out the name of the app.
*/
package main
import (
"errors"
"os"
template "github.com/dedis/cothority_template"
"go.dedis.ch/onet/v3/app"
"go.dedis.ch/onet/v3/log"
"gopkg.in/urfave/cli.v1"
)
func main() {
cliApp := cli.NewApp()
cliApp.Name = "Template"
cliApp.Usage = "Used for building other apps."
cliApp.Version = "0.1"
groupsDef := "the group-definition-file"
cliApp.Commands = []cli.Command{
{
Name: "time",
Usage: "measure the time to contact all nodes",
Aliases: []string{"t"},
ArgsUsage: groupsDef,
Action: cmdTime,
},
{
Name: "counter",
Usage: "return the counter",
Aliases: []string{"t"},
ArgsUsage: groupsDef,
Action: cmdCounter,
},
}
cliApp.Flags = []cli.Flag{
cli.IntFlag{
Name: "debug, d",
Value: 0,
Usage: "debug-level: 1 for terse, 5 for maximal",
},
}
cliApp.Before = func(c *cli.Context) error {
log.SetDebugVisible(c.Int("debug"))
return nil
}
log.ErrFatal(cliApp.Run(os.Args))
}
// Returns the time needed to contact all nodes.
func cmdTime(c *cli.Context) error {
log.Info("Time command")
group := readGroup(c)
client := template.NewClient()
resp, err := client.Clock(group.Roster)
if err != nil {
return errors.New("When asking the time: " + err.Error())
}
log.Infof("Children: %d - Time spent: %f", resp.Children, resp.Time)
return nil
}
// Returns the number of calls.
func cmdCounter(c *cli.Context) error {
log.Info("Counter command")
group := readGroup(c)
client := template.NewClient()
counter, err := client.Count(group.Roster.RandomServerIdentity())
if err != nil {
return errors.New("When asking for counter: " + err.Error())
}
log.Info("Number of requests:", counter)
return nil
}
func readGroup(c *cli.Context) *app.Group {
if c.NArg() != 1 {
log.Fatal("Please give the group-file as argument")
}
name := c.Args().First()
f, err := os.Open(name)
log.ErrFatal(err, "Couldn't open group definition file")
group, err := app.ReadGroupDescToml(f)
log.ErrFatal(err, "Error while reading group definition file", err)
if len(group.Roster.List) == 0 {
log.ErrFatalf(err, "Empty entity or invalid group defintion in: %s",
name)
}
return group
}