当前位置: 首页>>代码示例>>Golang>>正文


Golang FileRecord.InfoHash方法代码示例

本文整理汇总了Golang中github.com/mdlayher/goat/goat/data.FileRecord.InfoHash方法的典型用法代码示例。如果您正苦于以下问题:Golang FileRecord.InfoHash方法的具体用法?Golang FileRecord.InfoHash怎么用?Golang FileRecord.InfoHash使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/mdlayher/goat/goat/data.FileRecord的用法示例。


在下文中一共展示了FileRecord.InfoHash方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: Announce

// Announce generates and triggers a tracker announces request
func Announce(tracker TorrentTracker, user data.UserRecord, query url.Values) []byte {
	// Store announce information in struct
	announce := new(data.AnnounceLog)
	err := announce.FromValues(query)
	if err != nil {
		return tracker.Error("Malformed announce")
	}

	// Request to store announce
	go func(announce *data.AnnounceLog) {
		if err := announce.Save(); err != nil {
			log.Println(err.Error())
		}
	}(announce)

	// Only report event when needed
	event := ""
	if announce.Event != "" {
		event = announce.Event + " "
	}

	log.Printf("announce: [%s %s:%d] %s%s", tracker.Protocol(), announce.IP, announce.Port, event, announce.InfoHash)

	// Check for a matching file via info_hash
	file, err := new(data.FileRecord).Load(announce.InfoHash, "info_hash")
	if err != nil {
		log.Println(err.Error())
		return tracker.Error(ErrAnnounceFailure.Error())
	}

	// Torrent is currently unregistered
	if file == (data.FileRecord{}) {
		log.Printf("tracker: detected new file, awaiting manual approval [hash: %s]", announce.InfoHash)

		// Create an entry in file table for this hash, but mark it as unverified
		file.InfoHash = announce.InfoHash
		file.Verified = false

		// Save file asynchronously
		go func(file data.FileRecord) {
			if err := file.Save(); err != nil {
				log.Println(err.Error())
			}
		}(file)

		// Report error
		return tracker.Error("Unregistered torrent")
	}

	// Ensure file is verified, meaning we will permit tracking of it
	if !file.Verified {
		return tracker.Error("Unverified torrent")
	}

	// Launch peer reaper asynchronously to remove old peers from this file
	go func(file data.FileRecord) {
		// Start peer reaper
		count, err := file.PeerReaper()
		if err != nil {
			log.Println(err.Error())
		}

		// Report peers reaped
		if count > 0 {
			log.Println("peerReaper: reaped %d peers on file ID: %d", count, file.ID)
		}
	}(file)

	// If UDP tracker, we cannot reliably detect user, so we announce anonymously
	if _, ok := tracker.(UDPTracker); ok {
		return tracker.Announce(query, file)
	}

	// Check existing record for this user with this file and this IP
	fileUser, err := new(data.FileUserRecord).Load(file.ID, user.ID, query.Get("ip"))
	if err != nil {
		log.Println(err.Error())
		return tracker.Error(ErrAnnounceFailure.Error())
	}

	// New user, starting torrent
	if fileUser == (data.FileUserRecord{}) {
		// Create new relationship
		fileUser.FileID = file.ID
		fileUser.UserID = user.ID
		fileUser.IP = query.Get("ip")
		fileUser.Active = true
		fileUser.Announced = 1

		// If announce reports 0 left, but no existing record, user is probably the initial seeder
		if announce.Left == 0 {
			fileUser.Completed = true
		} else {
			fileUser.Completed = false
		}

		// Track the initial uploaded, download, and left values
		// NOTE: clients report absolute values, so delta should NEVER be calculated for these
		fileUser.Uploaded = announce.Uploaded
//.........这里部分代码省略.........
开发者ID:gernest,项目名称:goat,代码行数:101,代码来源:tracker.go


注:本文中的github.com/mdlayher/goat/goat/data.FileRecord.InfoHash方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。