示例#1
0
文件: link.go 项目: Javantea/h0tb0x
// Add a new friend, or if the friend exists, update the host and port data.
func (this *LinkMgr) AddUpdateFriend(fp *crypto.Digest, host string, port uint16) {
	this.cmut.Lock()
	defer this.cmut.Unlock()
	// Make or insert friend
	this.Db.Exec(
		"INSERT OR IGNORE INTO Friend (id, fingerprint, host, port) VALUES (NULL, ?, ?, ?)",
		fp.Bytes(), host, port)

	row := this.Db.SingleQuery("SELECT id FROM Friend WHERE fingerprint = ?", fp.Bytes())
	var id int
	this.Db.Scan(row, &id)

	this.Db.Exec("UPDATE Friend SET host = ?, port = ? WHERE id = ?",
		host, port, id)

	_, ok := this.friendsFp[fp.String()]
	fi := &friendInfo{id: id, fingerprint: fp, host: host, port: port}
	this.friendsFp[fp.String()] = fi
	this.friendsId[id] = fi
	if !ok {
		// If it was added, signal upper layer
		for _, f := range this.listeners {
			f(id, fp, FriendAdded)
		}
	}
}