feat(corerouting) add package for custom routing options
fix(corerouting): connect to routing servers when setting up GCR client doc(corerouting) add TODO to move stuff to routing bootstrap
Brian Tiger Chow committed
Jan 30, 2015 at 07:58 UTC
73b544c932e39b336ccd586fad6d02c22abf87c4
1 file changed
+76
core/corerouting/core.go
new
+76
@@ -0,0 +1,76 @@
1
+package corerouting
2
+
3
+import (
4
+ "errors"
5
+
6
+ context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
7
+ datastore "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
8
+ core "github.com/jbenet/go-ipfs/core"
9
+ "github.com/jbenet/go-ipfs/p2p/peer"
10
+ routing "github.com/jbenet/go-ipfs/routing"
11
+ grandcentral "github.com/jbenet/go-ipfs/routing/grandcentral"
12
+ gcproxy "github.com/jbenet/go-ipfs/routing/grandcentral/proxy"
13
+)
14
+
15
+// NB: DHT option is included in the core to avoid 1) because it's a sane
16
+// default and 2) to avoid a circular dependency (it needs to be referenced in
17
+// the core if it's going to be the default)
18
+
19
+var (
20
+ errHostMissing = errors.New("grandcentral client requires a Host component")
21
+ errIdentityMissing = errors.New("grandcentral server requires a peer ID identity")
22
+ errPeerstoreMissing = errors.New("grandcentral server requires a peerstore")
23
+ errServersMissing = errors.New("grandcentral client requires at least 1 server peer")
24
+)
25
+
26
+// TODO doc
27
+func GrandCentralServer(recordSource datastore.ThreadSafeDatastore) core.RoutingOption {
28
+ return func(ctx context.Context, node *core.IpfsNode) (routing.IpfsRouting, error) {
29
+ if node.Peerstore == nil {
30
+ return nil, errPeerstoreMissing
31
+ }
32
+ if node.Identity == "" {
33
+ return nil, errIdentityMissing
34
+ }
35
+ server, err := grandcentral.NewServer(recordSource, node.Peerstore, node.Identity)
36
+ if err != nil {
37
+ return nil, err
38
+ }
39
+ proxy := &gcproxy.Loopback{
40
+ Handler: server,
41
+ Local: node.Identity,
42
+ }
43
+ return grandcentral.NewClient(proxy, node.Peerstore, node.Identity)
44
+ }
45
+}
46
+
47
+// TODO doc
48
+func GrandCentralClient(remotes ...peer.PeerInfo) core.RoutingOption {
49
+ return func(ctx context.Context, node *core.IpfsNode) (routing.IpfsRouting, error) {
50
+ if len(remotes) < 1 {
51
+ return nil, errServersMissing
52
+ }
53
+ if node.PeerHost == nil {
54
+ return nil, errHostMissing
55
+ }
56
+ if node.Identity == "" {
57
+ return nil, errIdentityMissing
58
+ }
59
+ if node.Peerstore == nil {
60
+ return nil, errors.New("need peerstore")
61
+ }
62
+
63
+ // TODO move to bootstrap method
64
+ for _, info := range remotes {
65
+ if err := node.PeerHost.Connect(ctx, info); err != nil {
66
+ return nil, err // TODO
67
+ }
68
+ }
69
+
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
74
+ return grandcentral.NewClient(proxy, node.Peerstore, node.Identity)
75
+ }
76
+}