BT源码阅读兼移植三:种子文件的生成(1)

开发板推荐:天空星STM32F407VET6开发板

超高性价比 STM32主控 | 超高主频 | 一板兼容百芯 | 比赛神器 | 沉金彩色丝印

知道了bencoding编码,我们来看一下种子文件是如何生成的。

第一篇文章中提到Bittorrent/makematafle.py中的函数实现了生成种子功能。

生成种子的总的思路就是将文件切片后用sha1取得摘要,摘要和目录结构、TrackerUrl等信息bencoding后生成一个文件。

 

下面说明细节:

 

先看一下sha1是啥:

sha1是security hash algrithom 的缩写,其实跟MD5类似,也是一种单向哈希映射。我们一般把它用作数字签名(因为雪崩效应)。对于长度小于2^64位的消息,SHA1会产生一个160位的消息摘要。 在BT种子中,把文件按照2^n位进行切分,每段用sha1生成一个160位的摘要信息。这段摘要信息能够帮助确定下载的片段是否正确。

 

再看一下官方协议:

http://wiki.theory.org/BitTorrentSpecification 的MetaInfo File Structure部分。

 -----------------------------------------------------------------转帖协议----------------------------------------------------------------------

All data in a metainfo file is bencoded. The specification for bencoding is defined above.

The content of a metainfo file (the file ending in ".torrent") is a bencoded dictionary, containing the keys listed below. All character string values are UTF-8 encoded.

  • info: a dictionary that describes the file(s) of the torrent. There are two possible forms: one for the case of a 'single-file' torrent with no directory structure, and one for the case of a 'multi-file' torrent (see below for details)
  • announce: The announce URL of the tracker (string)
  • announce-list: (optional) this is an extention to the official specification, offering backwards-compatibility. (list of lists of strings).
    • The official request for a specification change is here.
  • creation date: (optional) the creation time of the torrent, in standard UNIX epoch format (integer, seconds since 1-Jan-1970 00:00:00 UTC)
  • comment: (optional) free-form textual comments of the author (string)
  • created by: (optional) name and version of the program used to create the .torrent (string)
  • encoding: (optional) the string encoding format used to generate the pieces part of the info dictionary in the .torrent metafile (string)

Info Dictionary

This section contains the field which are common to both mode, "single file" and "multiple file".

  • piece length: number of bytes in each piece (integer)
  • pieces: string consisting of the concatenation of all 20-byte SHA1 hash values, one per piece (byte string, i.e. not urlencoded)
  • private: (optional) this field is an integer. If it is set to "1", the client MUST publish its presence to get other peers ONLY via the trackers explicitly described in the metainfo file. If this field is set to "0" or is not present, the client may obtain peer from other means, e.g. PEX peer exchange, dht. Here, "private" may be read as "no external peer source".
    • NOTE: There is much debate surrounding private trackers.
    • The official request for a specification change is here.
    • Azureus was the first client to respect private trackers, see their wiki for more details.

Info in Single File Mode

For the case of the single-file mode, the info dictionary contains the following structure:

  • name: the filename. This is purely advisory. (string)
  • length: length of the file in bytes (integer)
  • md5sum: (optional) a 32-character hexadecimal string corresponding to the MD5 sum of the file. This is not used by BitTorrent at all, but it is included by some programs for greater compatibility.

Info in Multiple File Mode

For the case of the multi-file mode, the info dictionary contains the following structure:

  • name: the filename of the directory in which to store all the files. This is purely advisory. (string)
  • files: a list of dictionaries, one for each file. Each dictionary in this list contains the following keys:
    • length: length of the file in bytes (integer)
    • md5sum: (optional) a 32-character hexadecimal string corresponding to the MD5 sum of the file. This is not used by BitTorrent at all, but it is included by some programs for greater compatibility.
    • path: a list containing one or more string elements that together represent the path and filename. Each element in the list corresponds to either a directory name or (in the case of the final element) the filename. For example, a the file "dir1/dir2/file.ext" would consist of three string elements: "dir1", "dir2", and "file.ext". This is encoded as a bencoded list of strings such as l4:dir14:dir28:file.exte

Notes

  • The piece length specifies the nominal piece size, and is usually a power of 2. The piece size is typically chosen based on the total amount of file data in the torrent, and is constrained by the fact that too-large piece sizes cause inefficiency, and too-small piece sizes cause large .torrent metadata file. Historically, piece size was chosen to result in a .torrent file no greater than approx. 50 - 75 kB (presumably to ease the load on the server hosting the torrent files).
    • Current best-practice is to keep the piece size to 512KB or less, for torrents around 8-10GB, even if that results in a larger .torrent file. This results in a more efficient swarm for sharing files. The most common sizes are 256 kB, 512 kB, and 1 MB.
    • Every piece is of equal length except for the final piece, which is irregular. The number of pieces is thus determined by 'ceil( total length / piece size )'.
    • For the purposes of piece boundaries in the multi-file case, consider the file data as one long continuous stream, composed of the concatenation of each file in the order listed in the files list. The number of pieces and their boundaries are then determined in the same manner as the case of a single file. Pieces may overlap file boundaries.
  • Each piece has a corresponding SHA1 hash of the data contained within that piece. These hashes are concatenated to form the pieces value in the above info dictionary. Note that this is not a list but rather a single string. The length of the string must be a multiple of 20.

--------------------------------------------------转帖完毕-----------------------------------------------------------------------------

协议明确的规定出了文件的结构,可以直接看一下python的代码了。

makematafle.py 的主函数 makematafile 实现了单个文件的torrent文件的制作,makemetafiles实现了多个文件的torrent文件制作。 makeinfo完成了infodictionary的制作。

 

先看一下makeinfo函数

 

 

开发板推荐:天空星STM32F407VET6开发板

超高性价比 STM32主控 | 超高主频 | 一板兼容百芯 | 比赛神器 | 沉金彩色丝印

01、数据简介 出口韧性是地级市在面对外部震荡和压力时,能够承受并迅速适应、应对变化的能力。这种能力体现在地级市经济结构的灵活性、创新能力和竞争力,以及地方政府的政策支持和产业调整能力等多个方面。 城市出口韧性对于城市的经济发展、就业稳定、国际贸易地位以及风险抵御能力等方面都具有重要影响。因此,城市应加强出口韧性的建设,提高应对外部冲击的能力,以推动其经济的可持续发展。 数据名称:地级市-城市出口韧性数据 数据年份:2011-2022年 02、相关数据 代码 年份 地区 城市 省份 城市出口韧性 距离港口的最近距离 最终进口额_百万人民币2 最终出口额_百万人民币2 人均道路面积2 年末金融机构各项贷款余额万元2 地区生产总值万元2 科学支出万元2 地方财政一般预算内支出万元2 城镇居民人均可支配收入元2 固定资产投资2 实际使用外商投资额百万美元2 城镇化率2 外贸依存度 出口贸易 年平均汇率 实际使用外商投资额百万人民币2 外资依存度 金融发展水平 财政投资力度 科学技术水平 出口偏离度 x_地区生产总值万元2 x_城镇化率2 x_人均道路面积2 x_外贸依存度 x_出口贸易 x_出口偏离度 x_金融发展水平 x_城镇居民人均可支配收入元2 x_财政投资力度 x_科学技术水平 x_距离港口的最近距离 x_外资依存度 地区生产总值万元2_sum y_地区生产总值万元2 城镇化率2_sum y_城镇化率2 人均道路面积2_sum y_人均道路面积2 外贸依存度_sum y_外贸依存度 出口贸易_sum y_出口贸易 出口偏离度_sum y_出口偏离度 金融发展水平_sum y_金融发展水平 城镇居民人均可支配收入元2_sum y_城镇居民人均可支配收入元2 财政投资力度_sum y_财政投资力度 科学技术水平_sum y_科学技术水平
内容概要:本文档详细介绍了一个基于Matlab实现的无人机空中通信仿真资源包,系统涵盖了无人机通信、维路径规划、状态估计与多机协同等多个核心技术模块的仿真代码与案例研究。内容聚焦于无人机在复杂环境下的维路径规划(如基于遗传算法GA、粒子群算法PSO、动态窗口法DWA等)、无人机姿态与轨迹的状态估计算法(如扩展卡尔曼滤波器EKF、UKF、不变扩展卡尔曼滤波IEKF、粒子滤波PF等),以及无人机通信链路建模与优化,并融合智能优化算法对系统性能进行提升。此外,资源包还拓展至微电网优化、MIMO检测、图像融合、信号处理等相关科研领域,构建了一个以无人机技术为核心、多学科交叉融合的综合性仿真研究体系。; 适合人群:具备一定Matlab编程能力与控制系统基础知识,从事无人机系统设计、无线通信、自动化控制、智能优化算法或相关领域研究的科研人员、高校研究生及工程技术人员。; 使用场景及目标:①开展无人机通信系统建模与性能仿真分析;②实现复杂动态环境中无人机维路径规划与实时避障;③研究基于多源传感器融合的无人机导航与状态估计方法;④结合智能优化算法提升无人机任务执行效率与系统鲁棒性; 阅读建议:建议读者依据资源包提供的模块化结构系统学习,优先掌握Matlab/Simulink基本仿真技能,重点研读路径规划与状态估计部分的算法实现与代码细节,并通过实际调试与二次开发加深对无人机系统集成与优化策略的理解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值