-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclientd.go
65 lines (57 loc) · 1.97 KB
/
clientd.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
package main
import(
"time"
"context"
libp2p "github.com/libp2p/go-libp2p"
ps "github.com/libp2p/go-libp2p-pubsub"
ma "github.com/multiformats/go-multiaddr"
p2pd "github.com/libp2p/go-libp2p-daemon"
c "github.com/libp2p/go-libp2p-daemon/p2pclient"
)
func createDaemon(daemonAddr ma.Multiaddr, opts []libp2p.Option) (*p2pd.Daemon, func(), error) {
ctx, cancelCtx := context.WithCancel(context.Background())
daemon, err := p2pd.NewDaemon(ctx, daemonAddr, "", opts...)
if err != nil {
return nil, nil, err
}
return daemon, cancelCtx, nil
}
func pubsub(daemon *p2pd.Daemon, pubsubRouter string, pubsubSign, pubsubSignStrict bool, gossipsubHeartbeatInterval, gossipsubHeartbeatInitialDelay time.Duration) error {
if gossipsubHeartbeatInterval > 0 {
ps.GossipSubHeartbeatInterval = gossipsubHeartbeatInterval
ps.GossipSubHeartbeatInitialDelay = gossipsubHeartbeatInitialDelay
}
err := daemon.EnablePubsub(pubsubRouter, pubsubSign, pubsubSignStrict)
if err != nil {
return err
}
return nil
}
func createClient(daemonAddr ma.Multiaddr, clientAddr ma.Multiaddr) (*c.Client, func(), error) {
client, err := c.NewClient(daemonAddr, clientAddr)
if err != nil {
return nil, nil, err
}
closer := func() {
client.Close()
}
return client, closer, nil
}
func createDaemonClientPair(opts []libp2p.Option, pubsubRouter string, pubsubSign, pubsubSignStrict bool, gossipsubHeartbeatInterval, gossipsubHeartbeatInitialDelay time.Duration) (*p2pd.Daemon, *c.Client, func(), error) {
dmaddr, cmaddr, dirCloser, err := getEndpointsMaker()()
daemon, closeDaemon, err := createDaemon(dmaddr, opts)
if err != nil {
return nil, nil, nil, err
}
pubsub(daemon, pubsubRouter, pubsubSign, pubsubSignStrict, gossipsubHeartbeatInterval, gossipsubHeartbeatInitialDelay)
client, closeClient, err := createClient(daemon.Listener().Multiaddr(), cmaddr)
if err != nil {
return nil, nil, nil, err
}
closer := func() {
closeDaemon()
closeClient()
dirCloser()
}
return daemon, client, closer, nil
}