-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathnode.go
More file actions
executable file
·144 lines (120 loc) · 3.22 KB
/
node.go
File metadata and controls
executable file
·144 lines (120 loc) · 3.22 KB
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
137
138
139
140
141
142
143
144
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package node
import (
"context"
"log"
"runtime/debug"
"github.com/hyperledger-labs/fabric-smart-client/pkg/utils/errors"
"github.com/hyperledger-labs/fabric-smart-client/platform/common/services/logging"
utils "github.com/hyperledger-labs/fabric-smart-client/platform/view/services"
"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/config"
"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/view"
)
var logger = logging.MustGetLogger()
type ExecuteCallbackFunc = func() error
// PostStart enables a platform to execute additional tasks after all platforms have started
type PostStart interface {
PostStart(context.Context) error
}
// Node is a struct that allows the developer to compose sequentially multiple SDKs.
// The struct offers also a service locator that the SDKs can use to register services.
type Node struct {
registry utils.Registry
id string
sdks []SDK
ctx context.Context
cancel context.CancelFunc
running bool
}
func NewFromConfPath(confPath string) *Node {
node, err := NewFromConfPathE(confPath)
if err != nil {
panic(err)
}
return node
}
func NewFromConfPathE(confPath string) (*Node, error) {
configService, err := config.NewProvider(confPath)
if err != nil {
return nil, err
}
registry := view.NewServiceProvider()
if err := registry.RegisterService(configService); err != nil {
return nil, err
}
return &Node{
sdks: []SDK{},
registry: registry,
id: configService.ID(),
}, nil
}
func (n *Node) AddSDK(sdk SDK) {
n.sdks = append(n.sdks, sdk)
}
func (n *Node) ID() string {
return n.id
}
func (n *Node) Start() (err error) {
defer func() {
if r := recover(); r != nil {
log.Printf("start triggered panic: %s\n%s\n", r, debug.Stack())
err = errors.Errorf("start triggered panic: %s", r)
n.Stop()
}
}()
// Install
logger.Infof("installing sdks...")
for _, p := range n.sdks {
if err := p.Install(); err != nil {
logger.Errorf("failed installing platform [%s]", err.Error())
return err
}
}
logger.Infof("installing sdks...done")
n.ctx, n.cancel = context.WithCancel(context.Background())
// Start
logger.Info("starting sdks...")
for _, p := range n.sdks {
if err := p.Start(n.ctx); err != nil {
logger.Errorf("failed starting platform [%s]", err.Error())
return err
}
}
logger.Infof("starting sdks...done")
// PostStart
logger.Info("post-starting sdks...")
for _, p := range n.sdks {
ps, ok := p.(PostStart)
if ok {
if err := ps.PostStart(n.ctx); err != nil {
logger.Errorf("failed post-starting platform [%s]", err.Error())
return err
}
}
}
logger.Infof("Post-starting sdks...done")
n.running = true
return nil
}
func (n *Node) Stop() {
n.running = false
if n.cancel != nil {
n.cancel()
}
}
func (n *Node) InstallSDK(p SDK) error {
if n.running {
return errors.New("failed installing platform, the system is already running")
}
n.sdks = append(n.sdks, p)
return nil
}
func (n *Node) RegisterService(service interface{}) error {
return n.registry.RegisterService(service)
}
func (n *Node) GetService(v interface{}) (interface{}, error) {
return n.registry.GetService(v)
}