us.ihmc.pubsub.examples.SubscriberExample Maven / Gradle / Ivy
/**
* Copyright 2024 Florida Institute for Human and Machine Cognition (IHMC)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package us.ihmc.pubsub.examples;
import com.eprosima.xmlschemas.fastrtps_profiles.DurabilityQosKindPolicyType;
import com.eprosima.xmlschemas.fastrtps_profiles.HistoryQosKindPolicyType;
import com.eprosima.xmlschemas.fastrtps_profiles.ReliabilityQosKindPolicyType;
import us.ihmc.idl.generated.chat.ChatMessage;
import us.ihmc.idl.generated.chat.ChatMessagePubSubType;
import us.ihmc.pubsub.Domain;
import us.ihmc.pubsub.DomainFactory;
import us.ihmc.pubsub.attributes.ParticipantProfile;
import us.ihmc.pubsub.attributes.SubscriberAttributes;
import us.ihmc.pubsub.common.LogLevel;
import us.ihmc.pubsub.common.MatchingInfo;
import us.ihmc.pubsub.common.SampleInfo;
import us.ihmc.pubsub.common.Time;
import us.ihmc.pubsub.participant.Participant;
import us.ihmc.pubsub.participant.ParticipantDiscoveryInfo;
import us.ihmc.pubsub.participant.ParticipantListener;
import us.ihmc.pubsub.subscriber.Subscriber;
import us.ihmc.pubsub.subscriber.SubscriberListener;
import java.io.IOException;
import java.util.Collections;
public class SubscriberExample
{
private class ParticipantListenerImpl implements ParticipantListener
{
@Override
public void onParticipantDiscovery(Participant participant, ParticipantDiscoveryInfo info)
{
System.out.println("New participant discovered");
System.out.println("Status: " + info.getStatus());
System.out.println("Guid: " + info.getGuid().toString());
System.out.println("Name: " + info.getName());
}
}
private class SubscriberListenerImpl implements SubscriberListener
{
private final ChatMessage data = new ChatMessage();
private final SampleInfo info = new SampleInfo();
@Override
public void onNewDataMessage(Subscriber subscriber)
{
if (subscriber.takeNextData(data, info))
{
System.out.println(data.getSender().toString() + ": " + data.getMsg().toString());
}
}
@Override
public void onSubscriptionMatched(Subscriber subscriber, MatchingInfo info)
{
System.out.println("New publisher matched");
System.out.println("Status: " + info.getStatus());
System.out.println("Guid: " + info.getGuid().toString());
}
}
public SubscriberExample() throws IOException
{
Domain domain = DomainFactory.getDomain();
domain.setLogLevel(LogLevel.INFO);
ParticipantProfile attributes2 = ParticipantProfile.create()
.domainId(1)
.useOnlyIntraProcessDelivery()
.name("ParticipantExample")
.discoveryLeaseDuration(Time.Infinite);
//.discoveryServer("127.0.0.1", 4);
Participant participant = domain.createParticipant(attributes2, new ParticipantListenerImpl());
ChatMessagePubSubType dataType = new ChatMessagePubSubType();
domain.registerType(participant, dataType);
SubscriberAttributes subscriberAttributes = SubscriberAttributes.create()
.topicDataType(dataType)
.topicName("chatter")
.reliabilityKind(ReliabilityQosKindPolicyType.RELIABLE)
.partitions(Collections.singletonList("us/ihmc"))
.durabilityKind(DurabilityQosKindPolicyType.TRANSIENT_LOCAL)
.historyQosPolicyKind(HistoryQosKindPolicyType.KEEP_LAST)
.historyDepth(50);
Subscriber subscriber = domain.createSubscriber(participant, subscriberAttributes, new SubscriberListenerImpl());
}
public static void main(String[] args) throws IOException, InterruptedException
{
new SubscriberExample();
Thread.currentThread().join();
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy