github.com/keltia/go-ipfs@v0.3.8-0.20150909044612-210793031c63/core/corerouting/core.go (about)

     1  package corerouting
     2  
     3  import (
     4  	"errors"
     5  
     6  	ds "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
     7  	context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
     8  	core "github.com/ipfs/go-ipfs/core"
     9  	"github.com/ipfs/go-ipfs/p2p/host"
    10  	"github.com/ipfs/go-ipfs/p2p/peer"
    11  	routing "github.com/ipfs/go-ipfs/routing"
    12  	supernode "github.com/ipfs/go-ipfs/routing/supernode"
    13  	gcproxy "github.com/ipfs/go-ipfs/routing/supernode/proxy"
    14  )
    15  
    16  // NB: DHT option is included in the core to avoid 1) because it's a sane
    17  // default and 2) to avoid a circular dependency (it needs to be referenced in
    18  // the core if it's going to be the default)
    19  
    20  var (
    21  	errHostMissing      = errors.New("supernode routing client requires a Host component")
    22  	errIdentityMissing  = errors.New("supernode routing server requires a peer ID identity")
    23  	errPeerstoreMissing = errors.New("supernode routing server requires a peerstore")
    24  	errServersMissing   = errors.New("supernode routing client requires at least 1 server peer")
    25  )
    26  
    27  // SupernodeServer returns a configuration for a routing server that stores
    28  // routing records to the provided datastore. Only routing records are store in
    29  // the datastore.
    30  func SupernodeServer(recordSource ds.ThreadSafeDatastore) core.RoutingOption {
    31  	return func(ctx context.Context, ph host.Host, dstore ds.ThreadSafeDatastore) (routing.IpfsRouting, error) {
    32  		server, err := supernode.NewServer(recordSource, ph.Peerstore(), ph.ID())
    33  		if err != nil {
    34  			return nil, err
    35  		}
    36  		proxy := &gcproxy.Loopback{
    37  			Handler: server,
    38  			Local:   ph.ID(),
    39  		}
    40  		ph.SetStreamHandler(gcproxy.ProtocolSNR, proxy.HandleStream)
    41  		return supernode.NewClient(proxy, ph, ph.Peerstore(), ph.ID())
    42  	}
    43  }
    44  
    45  // TODO doc
    46  func SupernodeClient(remotes ...peer.PeerInfo) core.RoutingOption {
    47  	return func(ctx context.Context, ph host.Host, dstore ds.ThreadSafeDatastore) (routing.IpfsRouting, error) {
    48  		if len(remotes) < 1 {
    49  			return nil, errServersMissing
    50  		}
    51  
    52  		proxy := gcproxy.Standard(ph, remotes)
    53  		ph.SetStreamHandler(gcproxy.ProtocolSNR, proxy.HandleStream)
    54  		return supernode.NewClient(proxy, ph, ph.Peerstore(), ph.ID())
    55  	}
    56  }