udds examples

udds provides a minimalistic DDS api, that can be used to read and write data into DDS domain.

Creating a Participant

This is how you create a Participant. In the constructor, you tell which DDS domain you wish to join, and the participantId that is used. These two parameters affects what port numbers are assigned to Participant being created.

 Participant p = new Participant(0, 2); // Create participant; domainId 0, participantId 2

With DDS(and udds), changes to data are communicated as samples. A Sample holds the timestamp when the corresponding change occured, and lifecycle information of the change. I.e. if the sample represents an update to data, or a disposal of data.

Using a DataWriter

To create a DataWriter, you need to specify at least the type of the data that will be written by the created DataWriter. Using a DataWriter, that will writes Samples of type HelloMessage, can be as simple as this:

 DataWriter<HelloMessage> dw = p.createDataWriter(HelloMessage.class);
 dw.write(new HelloMessage(i, "Hello"));

Note, that in the example above, a builtin Marshaller is used, which uses java serialization to marshall HelloMessage to wire. More on Marshallers later.

Using a DataReader

Creating a DataReader is done similarly. Using any of the get methods in DataReader, one gets current snapshot of the available Samples in readers history cache.

 DataReader<HelloMessage> dr = p.createDataReader(HelloMessage.class);
 List<Sample<HelloMessage>> samples = dr.getSamples(); // Get all the Samples reader knows about 

Alternatively, one can add a SampleListener to DataReader. SampleListener gets called when Samples arrive at DataReader.

 dr.addSampleListener(new SampleListener<HelloMessage>() {
   public void onSamples(List<Sample<HelloMessage>> samples) {
       System.out.println("Got samples: " + samples);
   }
 });

About Marshallers

udds uses Marshallers to read/write Data from/to network. When creating a DataReader or DataWriter, entitys' type is checked (HelloMessage in the examples above), and a matching Marshaller is assigned to entity being created.

Two builtin Marshallers are provided by udds; ExternalizableMarshaller and SerializableMarshaller. Using these Marshallers is a matter of implementing either java.io.Externalizable or java.io.Serializable by the type (HelloMessage above). One can also provide custom Marshaller to be assigned during creation of entities. Custom Marshallers are registered with a call to Participants setMarshaller(java.lang.Class, net.sf.jrtps.Marshaller)