standard.go 4.16 KB
Newer Older
1 2 3
package proxy

import (
4 5
	"math/rand"

6 7
	context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
	ggio "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/gogoprotobuf/io"
8
	host "github.com/jbenet/go-ipfs/p2p/host"
9 10 11 12
	inet "github.com/jbenet/go-ipfs/p2p/net"
	peer "github.com/jbenet/go-ipfs/p2p/peer"
	dhtpb "github.com/jbenet/go-ipfs/routing/dht/pb"
	eventlog "github.com/jbenet/go-ipfs/thirdparty/eventlog"
13
	errors "github.com/jbenet/go-ipfs/util/debugerror"
14 15
)

16
const ProtocolSNR = "/ipfs/supernoderouting"
17

18
var log = eventlog.Logger("supernode/proxy")
Brian Tiger Chow's avatar
Brian Tiger Chow committed
19

20
type Proxy interface {
21
	Bootstrap(context.Context) error
22
	HandleStream(inet.Stream)
23 24 25 26 27
	SendMessage(ctx context.Context, m *dhtpb.Message) error
	SendRequest(ctx context.Context, m *dhtpb.Message) (*dhtpb.Message, error)
}

type standard struct {
28
	Host    host.Host
29
	Remotes []peer.PeerInfo
30 31
}

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
	var cxns []peer.PeerInfo
38 39
	for _, info := range px.Remotes {
		if err := px.Host.Connect(ctx, info); err != nil {
40
			continue
41
		}
42 43 44 45 46
		cxns = append(cxns, info)
	}
	if len(cxns) == 0 {
		log.Critical("unable to bootstrap to any supernode routers")
	} else {
Brian Tiger Chow's avatar
fix log  
Brian Tiger Chow committed
47
		log.Infof("bootstrapped to %d supernode routers: %s", len(cxns), cxns)
48
	}
49 50 51
	return nil
}

52
func (p *standard) HandleStream(s inet.Stream) {
53
	// TODO(brian): Should clients be able to satisfy requests?
54
	log.Error("supernode client received (dropped) a routing message from", s.Conn().RemotePeer())
55
	s.Close()
56
}
57

Brian Tiger Chow's avatar
Brian Tiger Chow committed
58 59 60
// SendMessage sends message to each remote sequentially (randomized order),
// stopping after the first successful response. If all fail, returns the last
// error.
61
func (px *standard) SendMessage(ctx context.Context, m *dhtpb.Message) error {
62
	var err error
63
	for _, i := range rand.Perm(len(px.Remotes)) {
64
		remote := px.Remotes[i].ID
65 66 67 68 69 70 71 72
		if err = px.sendMessage(ctx, m, remote); err != nil { // careful don't re-declare err!
			continue
		}
		return nil // success
	}
	return err // NB: returns the last error
}

Brian Tiger Chow's avatar
Brian Tiger Chow committed
73 74 75 76 77 78 79 80 81
func (px *standard) sendMessage(ctx context.Context, m *dhtpb.Message, remote peer.ID) (err error) {
	e := log.EventBegin(ctx, "sendRoutingMessage", px.Host.ID(), remote, m)
	defer func() {
		if err != nil {
			e.SetError(err)
		}
		e.Done()
	}()
	if err = px.Host.Connect(ctx, peer.PeerInfo{ID: remote}); err != nil {
82 83
		return err
	}
84
	s, err := px.Host.NewStream(ProtocolSNR, remote)
85 86 87 88 89 90 91 92 93 94 95
	if err != nil {
		return err
	}
	defer s.Close()
	pbw := ggio.NewDelimitedWriter(s)
	if err := pbw.WriteMsg(m); err != nil {
		return errors.Wrap(err)
	}
	return nil
}

Brian Tiger Chow's avatar
Brian Tiger Chow committed
96 97 98
// SendRequest sends the request to each remote sequentially (randomized order),
// stopping after the first successful response. If all fail, returns the last
// error.
99
func (px *standard) SendRequest(ctx context.Context, m *dhtpb.Message) (*dhtpb.Message, error) {
100
	var err error
101
	for _, i := range rand.Perm(len(px.Remotes)) {
102
		remote := px.Remotes[i].ID
103 104 105 106 107 108 109 110 111 112
		var reply *dhtpb.Message
		reply, err = px.sendRequest(ctx, m, remote) // careful don't redeclare err!
		if err != nil {
			continue
		}
		return reply, nil // success
	}
	return nil, err // NB: returns the last error
}

113 114 115 116 117
func (px *standard) sendRequest(ctx context.Context, m *dhtpb.Message, remote peer.ID) (*dhtpb.Message, error) {
	e := log.EventBegin(ctx, "sendRoutingRequest", px.Host.ID(), remote, eventlog.Pair("request", m))
	defer e.Done()
	if err := px.Host.Connect(ctx, peer.PeerInfo{ID: remote}); err != nil {
		e.SetError(err)
118 119
		return nil, err
	}
120
	s, err := px.Host.NewStream(ProtocolSNR, remote)
121
	if err != nil {
122
		e.SetError(err)
123 124 125 126 127
		return nil, err
	}
	defer s.Close()
	r := ggio.NewDelimitedReader(s, inet.MessageSizeMax)
	w := ggio.NewDelimitedWriter(s)
Brian Tiger Chow's avatar
Brian Tiger Chow committed
128
	if err = w.WriteMsg(m); err != nil {
129
		e.SetError(err)
130 131 132
		return nil, err
	}

133 134 135
	response := &dhtpb.Message{}
	if err = r.ReadMsg(response); err != nil {
		e.SetError(err)
136 137 138
		return nil, err
	}
	// need ctx expiration?
139 140 141 142
	if response == nil {
		err := errors.New("no response to request")
		e.SetError(err)
		return nil, err
143
	}
144 145 146
	e.Append(eventlog.Pair("response", response))
	e.Append(eventlog.Pair("uuid", eventlog.Uuid("foo")))
	return response, nil
147
}