在脚本中使用预先解析的协议定义并使其保持最新

发布于 2024-09-30 08:14:05 字数 328 浏览 3 评论 0原文

对于我的工作,我有时必须处理来自二进制协议的日志文件(日志文件包含消息的十六进制转储)。我想编写一个 Perl 脚本,它可以为我解释二进制数据并以更友好的格式打印内容。

我有一个专有格式的协议消息的(机器可读)描述,并且我(大部分)已经弄清楚如何解析该格式(我无法完全理解的部分与我的目标无关,所以我可以忽略它们),所以我可以将描述转换为数据结构以在我的脚本中使用,

因为协议描述很少改变,所以每次我想分析日志文件时重新解析协议描述似乎是一种浪费。另一方面,如果描述确实发生了变化,或者如果我不小心丢弃了预先解析的描述形式,那么我希望我的脚本能够自动触发对描述的重新解析。
实现这一点的最佳方法是什么?

For my work, I sometimes have to deal with logfiles from a binary protocol (the logfiles contain hexdumps of the messages). I want to write a Perl script that can interpret the binary data for me and print the contents in a more friendly format.

I have a (machine readable) description of the protocol messages in a proprietary format and I have (mostly) figured out how to parse that format (the parts I can"t fully understand are not related to my goal, so I can just ignore them), so I can convert the description into a data structure for use in my script.

Because the protocol description only rarely changes, it seems a waste to re-parse the protocol description each time I want to analyse a logfile, but on the other hand, if the description does change or if I accidentally throw away my pre-parsed form of the description, then I would like my script to automatically trigger a re-parsing of the description.
What is the best way to realise this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

盛夏尉蓝 2024-10-07 08:14:05

假设协议描述位于脚本可访问的文件中,则具有读取解析数据的函数,该函数将解析结果缓存在中间文件中。逻辑非常非常简单,但步骤看起来非常冗长,因为我试图写出完整的规范 - 实际上它应该需要 <10 行 Perl 代码。

  1. 检查中间文件是否存在。如果不存在(或无法读取),请跳至专有解析步骤 (#4)

  2. 如果可以读取中间缓存文件,请读取“协议描述时间戳”字段(如下所述)。然后通过stat()找出“协议描述”文件的修改时间并进行比较。如果“协议描述”文件的修改时间 >= 缓存文件存储的时间戳,则跳到专有解析步骤(#4)

  3. 否则(例如“协议描述”文件的时间<缓存文件的存储时间戳),通过 Data::Dumper 或 Storable 读取中间缓存文件数据。

    否则

  4. 如果由于 #1 或 #2 中的逻辑需要重新解析,请读取“协议描述”文件,将其解析到您的数据结构中。

  5. 然后创建一个包含 2 个键的哈希:“protocol_description_timestamp”(其值为从 stat 调用派生的协议描述文件的修改时间)和第二个键“data”,其值为引用您刚刚作为解析结果生成的数据结构。

  6. 然后使用 StorableData::Dumper 或您选择的任何其他用于存储 Perl 数据结构的方法将该数据结构保存到中间缓存文件中。

Assuming that the protocol description lives in a file accessible to the script, have a function to read in the parsed data which caches the parsed results in intermediate file. The logic is very very simple but the steps look very verbose since I tried to write out the full spec - in reality it should take <10 lines of Perl code.

  1. Check if intermediate file exists. If it does not (or can not be read), skip to proprietary parsing step (#4)

  2. If you can read in the intermediate cache file, read in the "protocol description timestamp" field (described below). Then find out modification time of "protocol description" file via stat() and compare. If modification time of "protocol description" file is >= cache file's stored timestamp, skip to proprietary parsing step (#4)

  3. Else (e.g. the time of "protocol description" file is < cache file's stored timestamp), read the intermediary cache file data via Data::Dumper or Storable. End.

  4. If you need to re-parse because of logic in #1 or #2, read in "protocol description" file, parse it into your data structure.

  5. Then create a hash with 2 keys: "protocol_description_timestamp" (with the value being the modification time of protocol description file derived from stat call) and second key "data", with the value being a reference to the data structure you just produced as a result of parsing.

  6. Then save that data structure into the intermediate cache file using Storable or Data::Dumper or any other method of your choice for storing Perl data structires.

懒猫 2024-10-07 08:14:05

您可以为此使用 Makefile。使您使用的数据结构取决于协议描述的 Makefile 目标。
当 Make 注意到协议更新时间比脚本更新时间更新时,它将运行您指定的命令来重新创建数据。

You can use a Makefile for this. Make the data structure you use a Makefile target that depends on the protocol description.
When Make notices that the protocol was updated more recently than the script, it will run the commands you specify to recreate your data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文