2

I have been looking for an answer on stackoverflow and on the web more generally for the past two days and I couldn't find a decent answer. However I am surely not the first one to do that:

I am implementing a p2p overlay in Java. The aim is to exchange messages between peers. The messages are represented by a Message class which contains a sender, a receiver and a content. The content is represented as a class extending a Context interface (for each type of messages a new class extending Context is created which can contain additional fields, it is application specific). The className of the context is also store in the message. However I want to keep the messages transmitted over the network language-agnostic which is why I convert my converted them to a JSON object before transmitting them across the network. So far nothing marvelous.

The questions arise when receiving a message: I receive a JSON object which I assume is of class Message (a type checking is performed). It contains a content of the class stored in the message as a className.

  1. Is it wise to store the classname like that and then create an instance of the class to store the content dynamically when receiving a message (i.e. the content will be an instance of the SpecificContent class but seen as the interface Content). The content will then be available at the application level and can be casted to access its specific methods.

  2. is there any better/standard way to send messages over network without simply serializing the java class? (in this case it could not communicate with a program written in C++, etc ) (without talking about CORBA which is nothing less than a Rube Goldberg machine)

Thank you very much for your time, any comment/question/answer is appreciated.

Regards,

B.F.

4

2 回答 2

0

Yours is the challenge of integration of heterogeneous systems. What you are doing is loosely the practice being followed (at least in principle).

The idea that a text based (JSON in your case here) protocol is used to exchange the information between heterogeneous systems, is central to the system-integration or interoperability. In fact the standard practice of web-services is nothing but just this. Here the XML used as text based protocol among heterogeneous system.

To answer your question:

  1. If you using your own protocol then its fair to send a classname coz that's where you interpret the message object you received. If you used standard practices like SOAP-Web services, these things are taken care automatically. All you need to define is service-contract of message exchange called WSDL.

  2. XML is the language of internet. Use xml to exchange information over network. For all the widely used languages (Java,C#,PHP etc) ready made programs are available to convert a native language object into equivalent xml representation (e.g Jaxb is used to convert Java object into xml representation).

于 2012-06-12T10:42:25.413 回答
0

When selecting a particular messaging layer implementation is it generally boils down to balancing between performance and flexibility. The most flexible and least performing is text-based self-descriptive encoding (JSON, XML/SOUP, YAML, tag/type/value variations, etc.) SOUP web services or FIX would be an example, the ones of best performance are ones with fixed fields offsets and binary encoding, ASN.1 would be an example. Somwhat balanced but biased towards the latter would be Google Protocol Buffers.

于 2012-06-12T10:44:25.863 回答