<commit_before>package corerouting

import (
	"errors"

	context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
	datastore "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
	core "github.com/jbenet/go-ipfs/core"
	"github.com/jbenet/go-ipfs/p2p/peer"
	routing "github.com/jbenet/go-ipfs/routing"
	grandcentral "github.com/jbenet/go-ipfs/routing/grandcentral"
	gcproxy "github.com/jbenet/go-ipfs/routing/grandcentral/proxy"
)

// NB: DHT option is included in the core to avoid 1) because it's a sane
// default and 2) to avoid a circular dependency (it needs to be referenced in
// the core if it's going to be the default)

var (
	errHostMissing      = errors.New("grandcentral client requires a Host component")
	errIdentityMissing  = errors.New("grandcentral server requires a peer ID identity")
	errPeerstoreMissing = errors.New("grandcentral server requires a peerstore")
	errServersMissing   = errors.New("grandcentral client requires at least 1 server peer")
)

// GrandCentralServer returns a configuration for a routing server that stores
// routing records to the provided datastore. Only routing records are store in
// the datastore.
func GrandCentralServer(recordSource datastore.ThreadSafeDatastore) core.RoutingOption {
	return func(ctx context.Context, node *core.IpfsNode) (routing.IpfsRouting, error) {
		if node.Peerstore == nil {
			return nil, errPeerstoreMissing
		}
		if node.PeerHost == nil {
			return nil, errHostMissing
		}
		if node.Identity == "" {
			return nil, errIdentityMissing
		}
		server, err := grandcentral.NewServer(recordSource, node.Peerstore, node.Identity)
		if err != nil {
			return nil, err
		}
		proxy := &gcproxy.Loopback{
			Handler: server,
			Local:   node.Identity,
		}
		node.PeerHost.SetStreamHandler(gcproxy.ProtocolGCR, proxy.HandleStream)
		return grandcentral.NewClient(proxy, node.PeerHost, node.Peerstore, node.Identity)
	}
}

// TODO doc
func GrandCentralClient(remotes ...peer.PeerInfo) core.RoutingOption {
	return func(ctx context.Context, node *core.IpfsNode) (routing.IpfsRouting, error) {
		if len(remotes) < 1 {
			return nil, errServersMissing
		}
		if node.PeerHost == nil {
			return nil, errHostMissing
		}
		if node.Identity == "" {
			return nil, errIdentityMissing
		}
		if node.Peerstore == nil {
			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
			}
		}

		// TODO right now, I think this has a hidden dependency on the
		// bootstrap peers provided to the core.Node. Careful...

		var ids []peer.ID
		for _, info := range remotes {
			ids = append(ids, info.ID)
		}
		proxy := gcproxy.Standard(node.PeerHost, ids)
		node.PeerHost.SetStreamHandler(gcproxy.ProtocolGCR, proxy.HandleStream)
		return grandcentral.NewClient(proxy, node.PeerHost, node.Peerstore, node.Identity)
	}
}
<commit_msg>fix(corerouting) remove TODO. no longer applies now that routing servers are specified manually<commit_after>package corerouting

import (
	"errors"

	context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
	datastore "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
	core "github.com/jbenet/go-ipfs/core"
	"github.com/jbenet/go-ipfs/p2p/peer"
	routing "github.com/jbenet/go-ipfs/routing"
	grandcentral "github.com/jbenet/go-ipfs/routing/grandcentral"
	gcproxy "github.com/jbenet/go-ipfs/routing/grandcentral/proxy"
)

// NB: DHT option is included in the core to avoid 1) because it's a sane
// default and 2) to avoid a circular dependency (it needs to be referenced in
// the core if it's going to be the default)

var (
	errHostMissing      = errors.New("grandcentral client requires a Host component")
	errIdentityMissing  = errors.New("grandcentral server requires a peer ID identity")
	errPeerstoreMissing = errors.New("grandcentral server requires a peerstore")
	errServersMissing   = errors.New("grandcentral client requires at least 1 server peer")
)

// GrandCentralServer returns a configuration for a routing server that stores
// routing records to the provided datastore. Only routing records are store in
// the datastore.
func GrandCentralServer(recordSource datastore.ThreadSafeDatastore) core.RoutingOption {
	return func(ctx context.Context, node *core.IpfsNode) (routing.IpfsRouting, error) {
		if node.Peerstore == nil {
			return nil, errPeerstoreMissing
		}
		if node.PeerHost == nil {
			return nil, errHostMissing
		}
		if node.Identity == "" {
			return nil, errIdentityMissing
		}
		server, err := grandcentral.NewServer(recordSource, node.Peerstore, node.Identity)
		if err != nil {
			return nil, err
		}
		proxy := &gcproxy.Loopback{
			Handler: server,
			Local:   node.Identity,
		}
		node.PeerHost.SetStreamHandler(gcproxy.ProtocolGCR, proxy.HandleStream)
		return grandcentral.NewClient(proxy, node.PeerHost, node.Peerstore, node.Identity)
	}
}

// TODO doc
func GrandCentralClient(remotes ...peer.PeerInfo) core.RoutingOption {
	return func(ctx context.Context, node *core.IpfsNode) (routing.IpfsRouting, error) {
		if len(remotes) < 1 {
			return nil, errServersMissing
		}
		if node.PeerHost == nil {
			return nil, errHostMissing
		}
		if node.Identity == "" {
			return nil, errIdentityMissing
		}
		if node.Peerstore == nil {
			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)
		node.PeerHost.SetStreamHandler(gcproxy.ProtocolGCR, proxy.HandleStream)
		return grandcentral.NewClient(proxy, node.PeerHost, node.Peerstore, node.Identity)
	}
}

{'fraction_non_alphanumeric': 0.10826738508166359, 'fraction_numerical': 0.0016837851490149856, 'mean_word_length': 4.004212299915754, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7186587', 'n_tokens_mistral': 2081, 'n_tokens_neox': 1915, 'n_words': 507}