forked from dinstein/fabric
/
client.go
160 lines (119 loc) · 3.98 KB
/
client.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
Copyright IBM Corp. 2016 All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package crypto
import (
"sync"
"github.com/abchain/fabric/core/crypto/utils"
)
// Private Variables
type clientEntry struct {
client Client
counter int64
}
var (
// Map of initialized clients
clients = make(map[string]clientEntry)
// Sync
clientMutex sync.Mutex
)
// Public Methods
// RegisterClient registers a client to the PKI infrastructure
func RegisterClient(name string, pwd []byte, enrollID, enrollPWD string) error {
clientMutex.Lock()
defer clientMutex.Unlock()
log.Infof("Registering client [%s] with name [%s]...", enrollID, name)
if _, ok := clients[name]; ok {
log.Infof("Registering client [%s] with name [%s]...done. Already initialized.", enrollID, name)
return nil
}
client := newClient()
if err := client.register(name, pwd, enrollID, enrollPWD); err != nil {
if err != utils.ErrAlreadyRegistered && err != utils.ErrAlreadyInitialized {
log.Errorf("Failed registering client [%s] with name [%s] [%s].", enrollID, name, err)
return err
}
log.Infof("Registering client [%s] with name [%s]...done. Already registered or initiliazed.", enrollID, name)
}
err := client.close()
if err != nil {
// It is not necessary to report this error to the caller
log.Warningf("Registering client [%s] with name [%s]. Failed closing [%s].", enrollID, name, err)
}
log.Infof("Registering client [%s] with name [%s]...done!", enrollID, name)
return nil
}
// InitClient initializes a client named name with password pwd
func InitClient(name string, pwd []byte) (Client, error) {
clientMutex.Lock()
defer clientMutex.Unlock()
log.Infof("Initializing client [%s]...", name)
if entry, ok := clients[name]; ok {
log.Infof("Client already initiliazied [%s]. Increasing counter from [%d]", name, clients[name].counter)
entry.counter++
clients[name] = entry
return clients[name].client, nil
}
client := newClient()
if err := client.init(name, pwd); err != nil {
log.Errorf("Failed client initialization [%s]: [%s].", name, err)
return nil, err
}
clients[name] = clientEntry{client, 1}
log.Infof("Initializing client [%s]...done!", name)
return client, nil
}
// CloseClient releases all the resources allocated by clients
func CloseClient(client Client) error {
clientMutex.Lock()
defer clientMutex.Unlock()
return closeClientInternal(client, false)
}
// CloseAllClients closes all the clients initialized so far
func CloseAllClients() (bool, []error) {
clientMutex.Lock()
defer clientMutex.Unlock()
log.Info("Closing all clients...")
errs := make([]error, len(clients))
for _, value := range clients {
err := closeClientInternal(value.client, true)
errs = append(errs, err)
}
log.Info("Closing all clients...done!")
return len(errs) != 0, errs
}
// Private Methods
func newClient() *clientImpl {
return &clientImpl{&nodeImpl{}, nil, nil, nil, nil}
}
func closeClientInternal(client Client, force bool) error {
if client == nil {
return utils.ErrNilArgument
}
name := client.GetName()
log.Infof("Closing client [%s]...", name)
entry, ok := clients[name]
if !ok {
return utils.ErrInvalidReference
}
if entry.counter == 1 || force {
defer delete(clients, name)
err := clients[name].client.(*clientImpl).close()
log.Debugf("Closing client [%s]...cleanup! [%s].", name, utils.ErrToString(err))
return err
}
// decrease counter
entry.counter--
clients[name] = entry
log.Debugf("Closing client [%s]...decreased counter at [%d].", name, clients[name].counter)
return nil
}