예제 #1
0
파일: core.go 프로젝트: mappum/go-ipfs
// NewIpfsNode constructs a new IpfsNode based on the given config.
func NewIpfsNode(cfg *config.Config, online bool) (*IpfsNode, error) {
	if cfg == nil {
		return nil, fmt.Errorf("configuration required")
	}

	d, err := makeDatastore(cfg.Datastore)
	if err != nil {
		return nil, err
	}

	local, err := initIdentity(cfg)
	if err != nil {
		return nil, err
	}

	var (
		net   *swarm.Swarm
		peers *peer.Map
		// TODO: refactor so we can use IpfsRouting interface instead of being DHT-specific
		route *dht.IpfsDHT
		swap  *bitswap.BitSwap
	)

	if online {
		net = swarm.NewSwarm(local)
		err = net.Listen()
		if err != nil {
			return nil, err
		}

		peers = &peer.Map{}

		route = dht.NewDHT(local, net, d)
		route.Start()

		swap = bitswap.NewBitSwap(local, net, d, route)
		swap.SetStrategy(bitswap.YesManStrategy)

		go initConnections(cfg, peers, route)
	}

	bs, err := bserv.NewBlockService(d, swap)
	if err != nil {
		return nil, err
	}

	dag := &merkledag.DAGService{Blocks: bs}

	return &IpfsNode{
		Config:    cfg,
		PeerMap:   peers,
		Datastore: d,
		Blocks:    bs,
		DAG:       dag,
		Resolver:  &path.Resolver{DAG: dag},
		BitSwap:   swap,
		Identity:  local,
		Routing:   route,
	}, nil
}
예제 #2
0
파일: core.go 프로젝트: mappum/go-ipfs
func initConnections(cfg *config.Config, peers *peer.Map, route *dht.IpfsDHT) {
	for _, sp := range cfg.Peers {
		maddr, err := ma.NewMultiaddr(sp.Address)
		if err != nil {
			u.PErr("error: %v\n", err)
			continue
		}

		p, err := route.Connect(maddr)
		if err != nil {
			u.PErr("Bootstrapping error: %v\n", err)
		}

		(*peers)[p.Key()] = p
	}
}