fix(grc) move Bootstrap method onto routing interface

This commit is contained in:
Brian Tiger Chow 2015-02-02 07:31:53 -08:00
parent 2298cf66cf
commit 1dfcce9f5a
5 changed files with 28 additions and 16 deletions

View File

@ -66,18 +66,7 @@ func SupernodeClient(remotes ...peer.PeerInfo) core.RoutingOption {
return nil, errors.New("need peerstore")
}
// TODO move to bootstrap method
for _, info := range remotes {
if err := node.PeerHost.Connect(ctx, info); err != nil {
return nil, err // TODO
}
}
var ids []peer.ID
for _, info := range remotes {
ids = append(ids, info.ID)
}
proxy := gcproxy.Standard(node.PeerHost, ids)
proxy := gcproxy.Standard(node.PeerHost, remotes)
node.PeerHost.SetStreamHandler(gcproxy.ProtocolSNR, proxy.HandleStream)
return supernode.NewClient(proxy, node.PeerHost, node.Peerstore, node.Identity)
}

View File

@ -131,4 +131,8 @@ func (c *Client) Ping(ctx context.Context, id peer.ID) (time.Duration, error) {
return time.Nanosecond, errors.New("supernode routing does not support the ping method")
}
func (c *Client) Bootstrap(ctx context.Context) error {
return c.proxy.Bootstrap(ctx)
}
var _ routing.IpfsRouting = &Client{}

View File

@ -20,6 +20,11 @@ type Loopback struct {
Local peer.ID
}
func (_ *Loopback) Bootstrap(ctx context.Context) error {
return nil
}
// SendMessage intercepts local requests, forwarding them to a local handler
func (lb *Loopback) SendMessage(ctx context.Context, m *dhtpb.Message) error {
response := lb.Handler.HandleRequest(ctx, lb.Local, m)

View File

@ -18,6 +18,7 @@ const ProtocolSNR = "/ipfs/supernoderouting"
var log = eventlog.Logger("supernode/proxy")
type Proxy interface {
Bootstrap(context.Context) error
HandleStream(inet.Stream)
SendMessage(ctx context.Context, m *dhtpb.Message) error
SendRequest(ctx context.Context, m *dhtpb.Message) (*dhtpb.Message, error)
@ -25,13 +26,22 @@ type Proxy interface {
type standard struct {
Host host.Host
Remotes []peer.ID
Remotes []peer.PeerInfo
}
func Standard(h host.Host, remotes []peer.ID) Proxy {
func Standard(h host.Host, remotes []peer.PeerInfo) Proxy {
return &standard{h, remotes}
}
func (px *standard) Bootstrap(ctx context.Context) error {
for _, info := range px.Remotes {
if err := px.Host.Connect(ctx, info); err != nil {
return err // TODO
}
}
return nil
}
func (p *standard) HandleStream(s inet.Stream) {
// TODO(brian): Should clients be able to satisfy requests?
log.Error("supernode client received (dropped) a routing message from", s.Conn().RemotePeer())
@ -44,7 +54,7 @@ func (p *standard) HandleStream(s inet.Stream) {
func (px *standard) SendMessage(ctx context.Context, m *dhtpb.Message) error {
var err error
for _, i := range rand.Perm(len(px.Remotes)) {
remote := px.Remotes[i]
remote := px.Remotes[i].ID
if err = px.sendMessage(ctx, m, remote); err != nil { // careful don't re-declare err!
continue
}
@ -82,7 +92,7 @@ func (px *standard) sendMessage(ctx context.Context, m *dhtpb.Message, remote pe
func (px *standard) SendRequest(ctx context.Context, m *dhtpb.Message) (*dhtpb.Message, error) {
var err error
for _, i := range rand.Perm(len(px.Remotes)) {
remote := px.Remotes[i]
remote := px.Remotes[i].ID
var reply *dhtpb.Message
reply, err = px.sendRequest(ctx, m, remote) // careful don't redeclare err!
if err != nil {

View File

@ -32,6 +32,10 @@ func NewServer(ds datastore.ThreadSafeDatastore, ps peer.Peerstore, local peer.I
return s, nil
}
func (_ *Server) Bootstrap(ctx context.Context) error {
return nil
}
// HandleLocalRequest implements the proxy.RequestHandler interface. This is
// where requests are received from the outside world.
func (s *Server) HandleRequest(ctx context.Context, p peer.ID, req *dhtpb.Message) *dhtpb.Message {