-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
113 lines (92 loc) · 2.51 KB
/
main.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
package main
import (
"fmt"
"log"
"time"
ipfslite "github.com/hsanjuan/ipfs-lite"
host "github.com/libp2p/go-libp2p-host"
multiaddr "github.com/multiformats/go-multiaddr"
"github.com/open-services/bolivar/cli"
http "github.com/open-services/bolivar/http"
"github.com/open-services/bolivar/p2p"
"github.com/open-services/bolivar/update"
)
// split to packages
var appConfig cli.Config
// What the version of the application is
var appVersion = "0.1.0"
// What the author of the application is
var appAuthor = "Open-Registry"
// libp2p peer to use for transfer
var libp2pNode *ipfslite.Peer
func main() {
appConfig = cli.Init(appVersion, appAuthor)
if appConfig.Verbose {
cli.PrintConfig(appConfig)
}
updater := update.NewUpdater(appConfig)
go func() {
for {
time.Sleep(1 * time.Second)
http.SetRootHash(updater.Update())
}
}()
http.SetRootHash(updater.Update())
libp2p, h := p2p.StartLibp2p(appConfig)
libp2pNode = libp2p
addrs := h.Addrs()
fmt.Println("libp2p listening on the following addresses:")
for _, addr := range addrs {
fmt.Println(addr)
}
connectAndReconnect(libp2pNode, h, appConfig.FederateAddr)
// TODO should start immediately and hang requests until we have the libp2p
// node ready and connected
log.Fatal(http.StartServer(appConfig, libp2pNode))
}
// Ensures we stay connected to this peer.
// In case where peer got disconnected, try to reconnect
func connectAndReconnect(libp2pNode *ipfslite.Peer, host host.Host, ma string) {
err := p2p.ConnectToPeer(libp2pNode, host, ma)
if err != nil {
panic(err)
}
// make sure we're connected to federate addr always
go func() {
fedID, err := getPeerIDFromMultiaddr(ma)
if err != nil {
panic(err)
}
for {
time.Sleep(5 * time.Second)
conns := host.Network().Conns()
foundOR := false
for _, conn := range conns {
if conn.RemotePeer().String() == fedID {
foundOR = true
}
}
if !foundOR {
log.Println("Lost connection to OR for some reason")
log.Println("reconnecting")
err := p2p.ConnectToPeer(libp2pNode, host, appConfig.FederateAddr)
if err != nil {
panic(err)
}
log.Println("reconnected")
}
}
}()
}
func getPeerIDFromMultiaddr(maStr string) (string, error) {
ma, err := multiaddr.NewMultiaddr(maStr)
if err != nil {
return "", err
}
// Protocol ID from https://github.com/multiformats/go-multiaddr/blob/e1825f7b50d1dcebdaa28bc31a310fa2be4c00ee/protocols.go#L17
fedID, err := ma.ValueForProtocol(0x01A5)
if err != nil {
return "", err
}
return fedID, nil
}