Esempio n. 1
0
//doUpdateNode broadcast and get data for each new records.
//if can get data (even if spam) return true, if fails to get, return false.
//if no fail, broadcast updates to node in cache and added n to nodelist and searchlist.
func doUpdateNode(rec *record.Record, n *node.Node) bool {
	mutex.Lock()
	deleteOldUpdated()
	if _, exist := updated[rec.Hash()]; exist {
		log.Println("already broadcasted", rec.ID)
		mutex.Unlock()
		return true
	}
	updated[rec.Hash()] = time.Now()
	mutex.Unlock()

	ca := thread.NewCache(rec.Datfile)
	var err error
	if !ca.Exists() || n == nil {
		log.Println("no cache or updates by myself, broadcast updates.")
		UpdatedRecord.register(rec.Head)
		manager.TellUpdate(ca.Datfile, rec.Stamp, rec.ID, n)
		if UpdatedRecord.wait() || n != nil {
			log.Println(rec.ID, "was gotten or don't have the record")
		} else {
			log.Println(rec.ID, "was NOT gotten, will call updates later")
			go func() {
				time.Sleep(10 * time.Minute)
				UpdateNodes(rec, n)
			}()
		}
		return true
	}
	log.Println("cache exists. get record from node n.")
	err = rec.GetData(n)
	switch err {
	case cfg.ErrGet:
		log.Println("could not get")
		return false
	case cfg.ErrSpam:
		log.Println("marked spam")
		return true
	default:
		log.Println("telling update")
		manager.TellUpdate(ca.Datfile, rec.Stamp, rec.ID, nil)
		manager.Join(n)
		return true
	}
}