/
state.go
60 lines (54 loc) · 1.23 KB
/
state.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
package host
import (
"errors"
"github.com/libp2p/go-libp2p-core/crypto"
"github.com/libp2p/go-libp2p-core/host"
"sync"
)
type HostState struct {
// only one routine can modify the host at a time
hLock sync.Mutex
p2pHost host.Host
}
// shortcut to check if there is a libp2p host available, and error-log if not available.
func (s *HostState) Host() (h host.Host, err error) {
h = s.p2pHost
if h == nil {
return nil, errors.New("Must first initialize Libp2p host. Try 'host start'")
}
return h, nil
}
func (s *HostState) SetHost(h host.Host) error {
s.hLock.Lock()
defer s.hLock.Unlock()
if s.p2pHost != nil {
return errors.New("existing host, cannot change host before closing old host")
}
s.p2pHost = h
return nil
}
func (s *HostState) CloseHost() error {
s.hLock.Lock()
defer s.hLock.Unlock()
if s.p2pHost == nil {
return errors.New("no host was open")
}
err := s.p2pHost.Close()
if err != nil {
return err
}
s.p2pHost = nil
return nil
}
func (s *HostState) GetHostPriv() *crypto.Secp256k1PrivateKey {
s.hLock.Lock()
defer s.hLock.Unlock()
if s.p2pHost == nil {
return nil
}
priv := s.p2pHost.Peerstore().PrivKey(s.p2pHost.ID())
if priv == nil {
return nil
}
return priv.(*crypto.Secp256k1PrivateKey)
}