-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathnode.go
106 lines (86 loc) · 2.62 KB
/
node.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
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package node
import (
"os"
node2 "github.com/hyperledger-labs/fabric-smart-client/node/node"
"github.com/hyperledger-labs/fabric-smart-client/node/version"
"github.com/hyperledger-labs/fabric-smart-client/pkg/api"
node3 "github.com/hyperledger-labs/fabric-smart-client/pkg/node"
"github.com/hyperledger-labs/fabric-smart-client/platform/common/services/logging"
sdk "github.com/hyperledger-labs/fabric-smart-client/platform/view/sdk/dig"
"github.com/hyperledger-labs/fabric-smart-client/platform/view/view"
"github.com/spf13/cobra"
)
var logger = logging.MustGetLogger("fsc")
type ExecuteCallbackFunc = func() error
type FabricSmartClient interface {
Start() error
Stop()
InstallSDK(p api.SDK) error
ConfigService() node3.ConfigService
GetService(v interface{}) (interface{}, error)
RegisterService(service interface{}) error
RegisterFactory(id string, factory api.Factory) error
RegisterResponder(responder view.View, initiatedBy interface{}) error
RegisterResponderWithIdentity(responder view.View, id view.Identity, initiatedBy view.View) error
// RegisterViewManager injects the ViewManager dependency
RegisterViewManager(manager node3.ViewManager)
// RegisterViewRegistry injects the ViewRegistry dependency
RegisterViewRegistry(registry node3.ViewRegistry)
}
type node struct {
FabricSmartClient
mainCmd *cobra.Command
callbackChannel chan error
executeCallbackFunc ExecuteCallbackFunc
}
func New() *node {
return NewFromConfPath("")
}
func NewFromConfPath(confPath string) *node {
n := node3.NewEmpty(confPath)
n.AddSDK(sdk.NewSDK(n))
return newFromFsc(n)
}
func newFromFsc(fscNode FabricSmartClient) *node {
mainCmd := &cobra.Command{Use: "peer"}
node := &node{
FabricSmartClient: fscNode,
mainCmd: mainCmd,
callbackChannel: make(chan error, 1),
}
mainCmd.AddCommand(version.Cmd())
mainCmd.AddCommand(node2.Cmd(node))
return node
}
func NewEmpty(confPath string) *node {
return newFromFsc(node3.NewEmpty(confPath))
}
func (n *node) Callback() chan<- error {
return n.callbackChannel
}
func (n *node) Execute(executeCallbackFunc ExecuteCallbackFunc) {
n.executeCallbackFunc = executeCallbackFunc
go n.listen()
if n.mainCmd.Execute() != nil {
os.Exit(1)
}
}
func (n *node) listen() {
logger.Debugf("Wait for callback signal")
err := <-n.callbackChannel
logger.Debugf("Callback signal came with err [%s]", err)
if err != nil {
panic(err)
}
if n.executeCallbackFunc != nil {
logger.Debugf("Calling callback...")
err = n.executeCallbackFunc()
if err != nil {
panic(err)
}
}
}