feat(gcr/c) add support for multiple servers
Brian Tiger Chow committed
Jan 28, 2015 at 05:20 UTC
d80e9aba7a7502e4ebb6a64492ad267ceddca955
2 files changed
+39
-12
core/corerouting/core.go
+5
-1
@@ -70,7 +70,11 @@ func GrandCentralClient(remotes ...peer.PeerInfo) core.RoutingOption {
70
// TODO right now, I think this has a hidden dependency on the
71
// bootstrap peers provided to the core.Node. Careful...
72
73
- proxy := gcproxy.Standard(node.PeerHost, remotes[0].ID) // TODO support more than one
73
+ var ids []peer.ID
74
+ for _, info := range remotes {
75
+ ids = append(ids, info.ID)
76
+ }
77
+ proxy := gcproxy.Standard(node.PeerHost, ids)
78
return grandcentral.NewClient(proxy, node.Peerstore, node.Identity)
79
}
80
}
routing/grandcentral/proxy/standard.go
+34
-11
@@ -2,13 +2,13 @@ package proxy
2
3
import (
4
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
5
- host "github.com/jbenet/go-ipfs/p2p/host"
5
ggio "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/gogoprotobuf/io"
6
+ host "github.com/jbenet/go-ipfs/p2p/host"
7
inet "github.com/jbenet/go-ipfs/p2p/net"
8
peer "github.com/jbenet/go-ipfs/p2p/peer"
9
dhtpb "github.com/jbenet/go-ipfs/routing/dht/pb"
10
- errors "github.com/jbenet/go-ipfs/util/debugerror"
10
eventlog "github.com/jbenet/go-ipfs/thirdparty/eventlog"
11
+ errors "github.com/jbenet/go-ipfs/util/debugerror"
12
)
13
14
var log = eventlog.Logger("proxy")
@@ -19,21 +19,32 @@ type Proxy interface {
19
}
20
21
type standard struct {
22
- Host host.Host
23
- Remote peer.ID
22
+ Host host.Host
23
+ Remotes []peer.ID
24
}
25
26
-func Standard(h host.Host, remote peer.ID) Proxy {
27
- return &standard{h, remote}
26
+func Standard(h host.Host, remotes []peer.ID) Proxy {
27
+ return &standard{h, remotes}
28
}
29
30
const ProtocolGCR = "/ipfs/grandcentral"
31
32
func (px *standard) SendMessage(ctx context.Context, m *dhtpb.Message) error {
33
- if err := px.Host.Connect(ctx, peer.PeerInfo{ID: px.Remote}); err != nil {
33
+ var err error
34
+ for _, remote := range px.Remotes {
35
+ if err = px.sendMessage(ctx, m, remote); err != nil { // careful don't re-declare err!
36
+ continue
37
+ }
38
+ return nil // success
39
+ }
40
+ return err // NB: returns the last error
41
+}
42
+
43
+func (px *standard) sendMessage(ctx context.Context, m *dhtpb.Message, remote peer.ID) error {
44
+ if err := px.Host.Connect(ctx, peer.PeerInfo{ID: remote}); err != nil {
45
return err
46
}
36
- s, err := px.Host.NewStream(ProtocolGCR, px.Remote)
47
+ s, err := px.Host.NewStream(ProtocolGCR, remote)
48
if err != nil {
49
return err
50
}
@@ -46,10 +57,23 @@ func (px *standard) SendMessage(ctx context.Context, m *dhtpb.Message) error {
57
}
58
59
func (px *standard) SendRequest(ctx context.Context, m *dhtpb.Message) (*dhtpb.Message, error) {
49
- if err := px.Host.Connect(ctx, peer.PeerInfo{ID: px.Remote}); err != nil {
60
+ var err error
61
+ for _, remote := range px.Remotes {
62
+ var reply *dhtpb.Message
63
+ reply, err = px.sendRequest(ctx, m, remote) // careful don't redeclare err!
64
+ if err != nil {
65
+ continue
66
+ }
67
+ return reply, nil // success
68
+ }
69
+ return nil, err // NB: returns the last error
70
+}
71
+
72
+func (px *standard) sendRequest(ctx context.Context, m *dhtpb.Message, remote peer.ID) (*dhtpb.Message, error) {
73
+ if err := px.Host.Connect(ctx, peer.PeerInfo{ID: remote}); err != nil {
74
return nil, err
75
}
52
- s, err := px.Host.NewStream(ProtocolGCR, px.Remote)
76
+ s, err := px.Host.NewStream(ProtocolGCR, remote)
77
if err != nil {
78
return nil, err
79
}
@@ -70,4 +94,3 @@ func (px *standard) SendRequest(ctx context.Context, m *dhtpb.Message) (*dhtpb.M
94
}
95
return &reply, nil
96
}
73
-