4

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
rnunes
  • 2,785
  • 7
  • 28
  • 56
  • This may help: http://stackoverflow.com/questions/7106762/how-to-send-such-complicated-hex-binary-protocol-data-accurately-using-java-byte – beny23 Sep 20 '11 at 10:08
  • Possibly related: http://stackoverflow.com/questions/644737/are-there-any-java-frameworks-for-binary-file-parsing – beny23 Sep 20 '11 at 10:09
  • Asking what the best way is for devices to communicate is a little vague given no application. How much data actually needs to be communicated? How much back-and-forth needs to happen between your device and pc and that sort of thing? – Sheena Sep 20 '11 at 10:14
  • It's not about how to communicate, but what's the best way to bind the values. – rnunes Sep 20 '11 at 10:56

1 Answers1

2

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:

message UserDetail {

  required string id = 1;
  optional string nick = 2;
  repeated double money = 3;

}

step 3, use protoc compile .proto and generate UserDetail.java file.

...
public interface UserDetailOrBuilder
        extends com.google.protobuf.MessageOrBuilder {

    // required string id = 1;
    boolean hasId();

    String getId();

    // optional string nick = 2;
    boolean hasNick();

    String getNick();

    // repeated double money = 3;
    java.util.List<java.lang.Double> getMoneyList();

}

public static final class UserDetail extends
        com.google.protobuf.GeneratedMessage
        implements UserDetailOrBuilder ...

step 4, simple call

UserDetail.parseFrom(input);
User.UserDetail t.writeTo(output);

g-p-b has other language addon, check http://code.google.com/p/protobuf/wiki/ThirdPartyAddOns

swanliu
  • 781
  • 3
  • 8
  • I don't like that approach but will save me a lot of work. Does GPB allow extensions? One of my problem is that some messages are not very linear, so I need some flexibility on serialization and deserialization. – rnunes Sep 26 '11 at 16:40