示例#1
0
//headWithRange checks node n has records with range and adds records which should be downloaded to downloadmanager.
func headWithRange(n *node.Node, c *thread.Cache, dm *Manager) bool {
	begin := time.Now().Unix() - cfg.GetRange
	if rec, err := recentlist.Newest(c.Datfile); err == nil {
		begin = rec.Stamp - cfg.GetRange
	}
	if cfg.GetRange == 0 || begin < 0 {
		begin = 0
	}
	res, err := n.Talk(fmt.Sprintf("/head/%s/%d-", c.Datfile, begin), nil)
	if err != nil {
		return false
	}
	if len(res) == 0 {
		ress, errr := n.Talk(fmt.Sprintf("/have/%s", c.Datfile), nil)
		if errr != nil || len(ress) == 0 || ress[0] != "YES" {
			manager.RemoveFromTable(c.Datfile, n)
		} else {
			manager.AppendToTable(c.Datfile, n)
		}
		return false
	}
	manager.AppendToTable(c.Datfile, n)
	dm.Set(res, n)
	return true
}
示例#2
0
//doUpdate adds remote node to searchlist and lookuptable with datfile specified in url.
//if stamp is in range of defaultUpdateRange adds to updateque.
func doUpdate(w http.ResponseWriter, r *http.Request) {
	s, err := new(w, r)
	if err != nil {
		log.Println(err)
		log.Println("failed to create cgi struct")
		return
	}
	reg := regexp.MustCompile(`^update/(\w+)/(\d+)/(\w+)/([^\+]*)(\+.*)`)
	m := reg.FindStringSubmatch(s.Path())
	if m == nil {
		log.Println("illegal url")
		return
	}
	datfile, stamp, id, hostport, path := m[1], m[2], m[3], m[4], m[5]
	host, portstr, err := net.SplitHostPort(hostport)
	if err != nil {
		log.Println(err)
		return
	}
	port, err := strconv.Atoi(portstr)
	if err != nil {
		log.Println(err)
		return
	}
	host = s.remoteIP(host)
	if host == "" {
		log.Println("host is null")
		return
	}

	n, err := node.MakeNode(host, path, port)
	if err != nil || !n.IsAllowed() {
		log.Println("detects spam")
		return
	}
	manager.AppendToTable(datfile, n)
	nstamp, err := strconv.ParseInt(stamp, 10, 64)
	if err != nil {
		log.Println(err)
		return
	}

	if !recentlist.IsInUpdateRange(nstamp) {
		return
	}
	rec := record.New(datfile, id, nstamp)
	go updateque.UpdateNodes(rec, n)
	fmt.Fprintln(w, "OK")
}