Java协议栈开发的最佳实践
Java协议栈开发的最佳实践是什么?
在这种特定情况下,我的 Java 应用程序将与 PC 外设“对话”,其总线将以协议格式传输数据。
示例:
假设我的协议有一条由一个整数、一个字符串和一个整数列表组成的消息:
class MyMessage { int filed1; String filed2; LinkedList<int> field3;}
我想要的最终产品是允许这样做的:
// Message to fill
MyMessage msg = new MyMessage();
// InputStream with the data to bind
InputStream stream = myPeripheralBus.getInputSTream();
msg.fill(stream);
// Here, msg fields are filled with the values that were on the InputStream
What are the best practices protocol stack development in Java?
In this specific case, my Java app will be "talking" to a PC peripheral, whose bus will transmit data in a protocol format.
Example:
Imagine that my protocol have a message composed by one integer, a String and a list of integers:
class MyMessage { int filed1; String filed2; LinkedList<int> field3;}
What I want as a final product it's something that allows to do that:
// Message to fill
MyMessage msg = new MyMessage();
// InputStream with the data to bind
InputStream stream = myPeripheralBus.getInputSTream();
msg.fill(stream);
// Here, msg fields are filled with the values that were on the InputStream
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
google protocol buffer 项目满足您的大部分要求。
除了 field3 上的 LinkedList 数据结构,但由于 gpb 保留了重复值的顺序,我想这对您来说已经足够了。
Protocol Buffers 是一种以高效但可扩展的格式编码结构化数据的方法。 Google 几乎所有内部 RPC 协议和文件格式都使用 Protocol Buffers。
第 1 步,从 http://code.google.com/apis/protocolbuffers/ 安装 gpb ,阅读文档。
步骤2,像这样定义你的message.proto:
步骤3,使用protoc编译.proto并生成UserDetail.java文件。
第4步,简单调用
gpb有其他语言插件,检查http://code。 google.com/p/protobuf/wiki/ThirdPartyAddOns
google protocol buffer project match most of your requirements .
except the LinkedList data structure on field3 , but since g-p-b preserved the order of the repeated values, i guess that's enough for you.
Protocol Buffers are a way of encoding structured data in an efficient yet extensible format. Google uses Protocol Buffers for almost all of its internal RPC protocols and file formats.
step 1, install g-p-b from http://code.google.com/apis/protocolbuffers/ , read docs.
step 2, define your message.proto like this:
step 3, use protoc compile .proto and generate UserDetail.java file.
step 4, simple call
g-p-b has other language addon, check http://code.google.com/p/protobuf/wiki/ThirdPartyAddOns