import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
import com.rti.dds.domain.*;
import com.rti.dds.infrastructure.*;
import com.rti.dds.publication.*;
import com.rti.dds.topic.*;
import com.rti.ndds.config.*;
public class MessageDDSPublisher {
private DomainParticipant participant = null;
private Publisher publisher = null;
private Topic topic = null;
private MessageDataWriter writer = null;
private int domainId;
public MessageDDSPublisher(int domainId) {
this.domainId = domainId;
}
public boolean start() {
String typeName;
try {
participant = DomainParticipantFactory.TheParticipantFactory.
create_participant(
domainId, DomainParticipantFactory.PARTICIPANT_QOS_DEFAULT,
null , StatusKind.STATUS_MASK_NONE);
if (participant == null) {
throw new Exception("Error creating participant");
}
publisher = participant.create_publisher(
DomainParticipant.PUBLISHER_QOS_DEFAULT, null ,
StatusKind.STATUS_MASK_NONE);
if (publisher == null) {
throw new Exception("Error creating publisher");
}
typeName = MessageTypeSupport.get_type_name();
MessageTypeSupport.register_type(participant, typeName);
topic = participant.create_topic(
"Message",
typeName, DomainParticipant.TOPIC_QOS_DEFAULT,
null , StatusKind.STATUS_MASK_NONE);
if (topic == null) {
throw new Exception("Error creating topic");
}
writer = (MessageDataWriter)
publisher.create_datawriter(
topic, Publisher.DATAWRITER_QOS_DEFAULT,
null , StatusKind.STATUS_MASK_NONE);
if (writer == null) {
throw new Exception("Error creating writer");
}
return true;
} catch (Exception e) {
stop();
return false;
}
}
public void stop() {
try {
if(participant != null) {
participant.delete_contained_entities();
writer = null;
topic = null;
publisher = null;
DomainParticipantFactory.TheParticipantFactory.
delete_participant(participant);
participant = null;
}
} catch (Exception e) {
}
}
public boolean publish(Message msg) {
InstanceHandle_t instance_handle = InstanceHandle_t.HANDLE_NIL;
try {
if (writer == null) {
return false;
}
writer.write(msg,instance_handle);
return true;
} catch (Exception e) {
System.out.println(e.getMessage());
return false;
}
}
}