用Python写协议规范

This is a writeup of a talk I did recently at Software Passion Summit in Gothenburg, Sweden. For more background info, see the post I did prior to the conference.

Writing a specification in a full-blown programming language like Python has upsides and downsides. On the downside, Python is not designed as a declarative language, so any attempt to make it declarative (apart from just listing native data types) will require some kind of customization and/or tooling to work. On the upside, having a declaration in the language you write your servers in, you can use the specification itself, rather than a generated derivative of that specification, and writing custom - in this case minimal - generators for other languages is simple, since you can you Python introspection to traverse your specification, and the templating logic of your choice to generate source - this makes it possible, for example, to target a J2ME terminal that just won't accept existing solutions, and where dropping a 150K jar file for protocol implementation is not an alternative.

For me, this journey started around 2006 when I started to lose control over protocol documentation and protocol versions for the protocol used between terminals and servers in the fleet management solutionVisual Units Logistics. After looking for, and discarding, several existing tools, and after being inspired by the fact that we usually configure Javascript. in Javascript, I started to sketch (as in, ink on paper) on what a protocol specification in Python would look like. This is a transcription of what I came up with at the time:

CODE:

1.imei = long
2.log_message = string
3.timestamp = long
4.voltage = float
5.log = Message(imei, timestamp,
6.log_message, voltage)
7.protocol = Protocol(log, ...)
8.protocol.parse(data)With this as a target, I created the first version of a protocol implementation. It looked similar to the target version, but suffered from an abundance of repetition:

CODE:

01.#protocol.py
02.LOG = 0x023
03.ALIVE = 0x021
04.message = Token('message', 'String', 'X')
05.timestamp = Token('timestamp', 'long', 'q')
06.signal = Token('signal', 'short', 'h')
07.voltage = Token('voltage', 'short', 'h')
08.msg_log = Message('LOG', LOG, timestamp, signal, voltage)
09.msg_alive = Message('ALIVE', ALIVE, timestamp)
10.protocol = Protocol(version=1.0, messages=[msg_log,msg_alive])
11.#usage
12.from protocol import protocol
13.parsed_data = protocol.parse(data)
14.open('Protocol.java’,'w').write(protocol.java_protocol())

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/301743/viewspace-720004/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/301743/viewspace-720004/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值