@cryptotaxi247 / kubo / commits / 1dfcce9f5

fix(grc) move Bootstrap method onto routing interface

Brian Tiger Chow committed Feb 2, 2015 at 07:31 UTC 1dfcce9f5a8c46673e8a3e4006d2ced6005055fb
5 files changed +28 -16
core/corerouting/core.go
+1 -12
@@ -66,18 +66,7 @@ func SupernodeClient(remotes ...peer.PeerInfo) core.RoutingOption {
66 return nil, errors.New("need peerstore")
67 }
68
69 - // TODO move to bootstrap method
70 - for _, info := range remotes {
71 - if err := node.PeerHost.Connect(ctx, info); err != nil {
72 - return nil, err // TODO
73 - }
74 - }
75 -
76 - var ids []peer.ID
77 - for _, info := range remotes {
78 - ids = append(ids, info.ID)
79 - }
80 - proxy := gcproxy.Standard(node.PeerHost, ids)
69 + proxy := gcproxy.Standard(node.PeerHost, remotes)
70 node.PeerHost.SetStreamHandler(gcproxy.ProtocolSNR, proxy.HandleStream)
71 return supernode.NewClient(proxy, node.PeerHost, node.Peerstore, node.Identity)
72 }
routing/supernode/client.go
+4
@@ -131,4 +131,8 @@ func (c *Client) Ping(ctx context.Context, id peer.ID) (time.Duration, error) {
131 return time.Nanosecond, errors.New("supernode routing does not support the ping method")
132 }
133
134 +func (c *Client) Bootstrap(ctx context.Context) error {
135 + return c.proxy.Bootstrap(ctx)
136 +}
137 +
138 var _ routing.IpfsRouting = &Client{}
routing/supernode/proxy/loopback.go
+5
@@ -20,6 +20,11 @@ type Loopback struct {
20 Local peer.ID
21 }
22
23 +func (_ *Loopback) Bootstrap(ctx context.Context) error {
24 + return nil
25 +}
26 +
27 +
28 // SendMessage intercepts local requests, forwarding them to a local handler
29 func (lb *Loopback) SendMessage(ctx context.Context, m *dhtpb.Message) error {
30 response := lb.Handler.HandleRequest(ctx, lb.Local, m)
routing/supernode/proxy/standard.go
+14 -4
@@ -18,6 +18,7 @@ const ProtocolSNR = "/ipfs/supernoderouting"
18 var log = eventlog.Logger("supernode/proxy")
19
20 type Proxy interface {
21 + Bootstrap(context.Context) error
22 HandleStream(inet.Stream)
23 SendMessage(ctx context.Context, m *dhtpb.Message) error
24 SendRequest(ctx context.Context, m *dhtpb.Message) (*dhtpb.Message, error)
@@ -25,13 +26,22 @@ type Proxy interface {
26
27 type standard struct {
28 Host host.Host
28 - Remotes []peer.ID
29 + Remotes []peer.PeerInfo
30 }
31
31 -func Standard(h host.Host, remotes []peer.ID) Proxy {
32 +func Standard(h host.Host, remotes []peer.PeerInfo) Proxy {
33 return &standard{h, remotes}
34 }
35
36 +func (px *standard) Bootstrap(ctx context.Context) error {
37 + for _, info := range px.Remotes {
38 + if err := px.Host.Connect(ctx, info); err != nil {
39 + return err // TODO
40 + }
41 + }
42 + return nil
43 +}
44 +
45 func (p *standard) HandleStream(s inet.Stream) {
46 // TODO(brian): Should clients be able to satisfy requests?
47 log.Error("supernode client received (dropped) a routing message from", s.Conn().RemotePeer())
@@ -44,7 +54,7 @@ func (p *standard) HandleStream(s inet.Stream) {
54 func (px *standard) SendMessage(ctx context.Context, m *dhtpb.Message) error {
55 var err error
56 for _, i := range rand.Perm(len(px.Remotes)) {
47 - remote := px.Remotes[i]
57 + remote := px.Remotes[i].ID
58 if err = px.sendMessage(ctx, m, remote); err != nil { // careful don't re-declare err!
59 continue
60 }
@@ -82,7 +92,7 @@ func (px *standard) sendMessage(ctx context.Context, m *dhtpb.Message, remote pe
92 func (px *standard) SendRequest(ctx context.Context, m *dhtpb.Message) (*dhtpb.Message, error) {
93 var err error
94 for _, i := range rand.Perm(len(px.Remotes)) {
85 - remote := px.Remotes[i]
95 + remote := px.Remotes[i].ID
96 var reply *dhtpb.Message
97 reply, err = px.sendRequest(ctx, m, remote) // careful don't redeclare err!
98 if err != nil {
routing/supernode/server.go
+4
@@ -32,6 +32,10 @@ func NewServer(ds datastore.ThreadSafeDatastore, ps peer.Peerstore, local peer.I
32 return s, nil
33 }
34
35 +func (_ *Server) Bootstrap(ctx context.Context) error {
36 + return nil
37 +}
38 +
39 // HandleLocalRequest implements the proxy.RequestHandler interface. This is
40 // where requests are received from the outside world.
41 func (s *Server) HandleRequest(ctx context.Context, p peer.ID, req *dhtpb.Message) *dhtpb.Message {