-
Notifications
You must be signed in to change notification settings - Fork 15
/
service.go
136 lines (120 loc) · 3.52 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package service
/*
The service.go defines what to do for each API-call. This part of the service
runs on the node.
*/
import (
"time"
"errors"
"sync"
"github.com/dedis/cothority_template"
"github.com/dedis/cothority_template/protocol"
"go.dedis.ch/onet/v3"
"go.dedis.ch/onet/v3/log"
"go.dedis.ch/onet/v3/network"
)
// Used for tests
var templateID onet.ServiceID
func init() {
var err error
templateID, err = onet.RegisterNewService(template.ServiceName, newService)
log.ErrFatal(err)
network.RegisterMessage(&storage{})
}
// Service is our template-service
type Service struct {
// We need to embed the ServiceProcessor, so that incoming messages
// are correctly handled.
*onet.ServiceProcessor
storage *storage
}
// storageID reflects the data we're storing - we could store more
// than one structure.
var storageID = []byte("main")
// storage is used to save our data.
type storage struct {
Count int
sync.Mutex
}
// Clock starts a template-protocol and returns the run-time.
func (s *Service) Clock(req *template.Clock) (*template.ClockReply, error) {
s.storage.Lock()
s.storage.Count++
s.storage.Unlock()
s.save()
tree := req.Roster.GenerateNaryTreeWithRoot(2, s.ServerIdentity())
if tree == nil {
return nil, errors.New("couldn't create tree")
}
pi, err := s.CreateProtocol(protocol.Name, tree)
if err != nil {
return nil, err
}
start := time.Now()
pi.Start()
resp := &template.ClockReply{
Children: <-pi.(*protocol.TemplateProtocol).ChildCount,
}
resp.Time = time.Now().Sub(start).Seconds()
return resp, nil
}
// Count returns the number of instantiations of the protocol.
func (s *Service) Count(req *template.Count) (*template.CountReply, error) {
s.storage.Lock()
defer s.storage.Unlock()
return &template.CountReply{Count: s.storage.Count}, nil
}
// NewProtocol is called on all nodes of a Tree (except the root, since it is
// the one starting the protocol) so it's the Service that will be called to
// generate the PI on all others node.
// If you use CreateProtocolOnet, this will not be called, as the Onet will
// instantiate the protocol on its own. If you need more control at the
// instantiation of the protocol, use CreateProtocolService, and you can
// give some extra-configuration to your protocol in here.
func (s *Service) NewProtocol(tn *onet.TreeNodeInstance, conf *onet.GenericConfig) (onet.ProtocolInstance, error) {
log.Lvl3("Not templated yet")
return nil, nil
}
// saves all data.
func (s *Service) save() {
s.storage.Lock()
defer s.storage.Unlock()
err := s.Save(storageID, s.storage)
if err != nil {
log.Error("Couldn't save data:", err)
}
}
// Tries to load the configuration and updates the data in the service
// if it finds a valid config-file.
func (s *Service) tryLoad() error {
s.storage = &storage{}
msg, err := s.Load(storageID)
if err != nil {
return err
}
if msg == nil {
return nil
}
var ok bool
s.storage, ok = msg.(*storage)
if !ok {
return errors.New("Data of wrong type")
}
return nil
}
// newService receives the context that holds information about the node it's
// running on. Saving and loading can be done using the context. The data will
// be stored in memory for tests and simulations, and on disk for real deployments.
func newService(c *onet.Context) (onet.Service, error) {
s := &Service{
ServiceProcessor: onet.NewServiceProcessor(c),
}
if err := s.RegisterHandlers(s.Clock, s.Count); err != nil {
return nil, errors.New("Couldn't register messages")
}
if err := s.tryLoad(); err != nil {
log.Error(err)
return nil, err
}
return s, nil
}