2

I have two sources:

  • an xml-file containing definitions of several hundred data structures, basically structs with fields consisting of simple data types (e.g. int, short, boolean, enum), and
  • a c header-file containing the same data structures represented a c structs. (I cannot modify this header-file or the c structs.)

I also have a library, written in c, that is able to use these c structs to perform a specific task.

Last I have a Java application which needs to use this library somehow.

Currently I generate Java classes from the xml-file representing the data structures, now I "just" need to provide them to the library somehow.

My current approach is to generate jni code, to call a c-function for each struct from Java, accepting the Java generated class as a parameter. Then generate c-code which takes the Java classes and fill in the appropriate fields in the c-structs.

Is this a good approach? Does anybody have suggestions for a simpler approach?

I am considering using sockets and e.g. protobuf, but I do not see this as any simpler.

Thanks in advance for any suggestions.

5
  • You could have a look at JNA - it handles conversion at the Java side, then calls the C functions. Commented Jul 4, 2011 at 13:09
  • @Paŭlo JNA looks very interesting, that's the kind of input I was hoping for. I will have a look, thanks.... Commented Jul 4, 2011 at 14:23
  • @Paŭlo: Do you have any example of JNA helps filling out a c-struct from Java, without duplicating the structs? I am unable to find any appropriate examples? Commented Jul 5, 2011 at 6:54
  • I never used JNA, sorry - I only used a library which relied on it, and then (one year ago) read the documentation a bit. Commented Jul 5, 2011 at 13:30
  • Hmm, definitely seems swig is an option for this. Forgetting the xml, and automatically generating java code using swig, which handles the c-structs in native code, without ever duplicating the structures in java. Commented Jul 7, 2011 at 9:40

2 Answers 2

1

The solution I am ending up with is using the Javolution struct library

Basically I am generating Java classes that inherits the javolution.io.Struct class from the xml, and declaring the fields appropriately such that javolution can interpret it as struct.

This allows me to do a myJavaStructClass.getByteBuffer() and get a directly mapped byte buffer which has the same memory layout as if the Java "struct" where declared in c. And because it is directly mapped, contains a reference which can be passed directly to native code through JNI.

Javolution also supports both packed and non-packed structs, as well as arbitrary endianness.

Another solution would be to use SWIG to generate Java classes directly from the c header-files, and manage them directly in native code through JNI.

Sign up to request clarification or add additional context in comments.

Comments

0

You don't need to use sockets to get value from protobuf here. Serialize into a direct byte buffer passed back and forth between C and Java via JNI. This will be faster and result in writing less annoying code.

As per comment, this still requires copying code between your structs and protobuf structs, but it seems to me that a few CPP macros could make this quite concise. You might even be able to create a code generator that walks some protobuf data structure; it's not impossible that someone else has.

6 Comments

I will still need to do conversion from protobuf generated c-structs to the predefined c-structs right? Or can I omit this somehow?
I would need a macro for each struct right? So I will need to generate those also.
@bjarkef Well since the problem is quite trivial and general, I'd just write a small script or something that generates that code automatically for any given struct. Shouldn't be too hard - especially since you already have the XML file that describes the structures which should allow you to easily generate Java code that writes the data into a buffer (a bit reflection) and C code that reads that buffer into the structs. Probably still depends on how much classes we're talking about; though maybe someone has already written that
@Voo: We are talking about several hundred classes. It seems to me that writing data into a buffer (and thus defining a format for this buffer), and then read that into structs might be a project in itself.
Protobuf itself is open source and has, after all, a code generator. You need a little more or a little different code generator. To me, this suggests a relatively economical approach.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.