// New creates a DHT node. If config is nil, DefaultConfig will be used. // Changing the config after calling this function has no effect. // // This method replaces NewDHTNode. func New(config *Config) (node *DHT, err error) { if config == nil { config = DefaultConfig } // Copy to avoid changes. cfg := *config node = &DHT{ config: cfg, routingTable: newRoutingTable(), peerStore: newPeerStore(cfg.MaxInfoHashes, cfg.MaxInfoHashPeers), PeersRequestResults: make(chan map[InfoHash][]string, 1), stop: make(chan bool), exploredNeighborhood: false, // Buffer to avoid blocking on sends. remoteNodeAcquaintance: make(chan remoteNodeAcquaintanceInfo, 100), // Buffer to avoid deadlocks and blocking on sends. peersRequest: make(chan ihReq, 100), nodesRequest: make(chan ihReq, 100), pingRequest: make(chan *remoteNode), portRequest: make(chan int), clientThrottle: nettools.NewThrottler(), tokenSecrets: []string{newTokenSecret(), newTokenSecret()}, } //c := openStore(cfg.Port, cfg.SaveRoutingTable) //node.store = c if len(cfg.NodeID) != 20 { nid := randNodeId() //c.Id = randNodeId() cfg.NodeID = string(nid) log.V(4).Infof("Using a new random node ID: %x %d", nid, len(nid)) //saveStore(*c) } // The types don't match because JSON marshalling needs []byte. //node.nodeId = string(c.Id) node.nodeId = cfg.NodeID // XXX refactor. node.routingTable.nodeId = node.nodeId // This is called before the engine is up and ready to read from the // underlying channel. /*go func() { for addr, _ := range c.Remotes { node.AddNode(addr) } }()*/ return }
func NewDHTNode(port, numTargetPeers int, storeEnabled bool) (node *DHT, err error) { node = &DHT{ port: port, routingTable: newRoutingTable(), peerStore: newPeerStore(), PeersRequestResults: make(chan map[InfoHash][]string, 1), exploredNeighborhood: false, // Buffer to avoid blocking on sends. remoteNodeAcquaintance: make(chan string, 100), // Buffer to avoid deadlocks and blocking on sends. peersRequest: make(chan ihReq, 100), nodesRequest: make(chan ihReq, 100), pingRequest: make(chan *remoteNode), numTargetPeers: numTargetPeers, clientThrottle: nettools.NewThrottler(), } c := openStore(port, storeEnabled) node.store = c if len(c.Id) != 20 { c.Id = randNodeId() l4g.Info("newId: %x %d", c.Id, len(c.Id)) saveStore(*c) } // The types don't match because JSON marshalling needs []byte. node.nodeId = string(c.Id) expNodeIds.Add(fmt.Sprintf("%x", node.nodeId), 0) // XXX refactor. node.routingTable.nodeId = node.nodeId // This is called before the engine is up and ready to read from the // underlying channel. go func() { for addr, _ := range c.Remotes { node.AddNode(addr) } }() return }