Example #1
0
int main()
{
	ParticipantAttributes participant_attributes;
	Participant* participant = Domain::createParticipant(participant_attributes);

	if(participant == nullptr)
		return 1;

    HelloWorldType type; 
	Domain::registerType(participant,&type);

    PubListener listener;

	//CREATE THE PUBLISHER
	PublisherAttributes publisher_attributes;
	publisher_attributes.topic.topicKind = NO_KEY;
	publisher_attributes.topic.topicDataType = type.getName();
	publisher_attributes.topic.topicName = "HelloWorldTopic";
	publisher_attributes.qos.m_reliability.kind = RELIABLE_RELIABILITY_QOS;
	Publisher* publisher = Domain::createPublisher(participant, publisher_attributes, &listener);
	if(publisher == nullptr)
    {
        Domain::removeParticipant(participant);
		return 1;
    }

    while(listener.matched_ == 0)
		eClock::my_sleep(250);

    HelloWorld data;
    data.index(1);
    data.message("HelloWorld");

    while(1)
    {
        publisher->write((void*)&data);

        if(data.index() == 4)
            data.index() = 1;
        else
            ++data.index();

		eClock::my_sleep(250);
    };

	Domain::removeParticipant(participant);

	return 0;
}
void PubSubKeepAllTransientReader::read(uint16_t lastvalue, const std::chrono::seconds &seconds)
{
    std::unique_lock<std::mutex> lock(mutex_);
    lastvalue_ = lastvalue;
    while(!msgs_.empty() && lastvalue_ == *msgs_.rbegin())
    {
        if(data_received_)
        {
            HelloWorld hello;
            SampleInfo_t info;

            if(subscriber_->takeNextData((void*)&hello, &info))
            {
                if(info.sampleKind == ALIVE)
                {
                    newNumber(hello.index());
                }
            }

            --data_received_;
        }
        else
        {
            if(cv_.wait_for(lock, seconds) == std::cv_status::timeout)
                break;
        }
    }
}
void ReqRepHelloWorldReplier::newNumber(SampleIdentity sample_identity, uint16_t number)
{
    waitDiscovery();

    WriteParams wparams;
    HelloWorld hello;
    hello.index(number);
    hello.message("GoodBye");
    wparams.related_sample_identity(sample_identity);
    ASSERT_EQ(reply_publisher_->write((void*)&hello, wparams), true);
}
void PubSubKeepAllTransientWriter::send(const std::list<uint16_t> &msgs)
{
    waitDiscovery();

	for(std::list<uint16_t>::const_iterator it = msgs.begin(); it != msgs.end(); ++it)
	{
        HelloWorld hello;
        hello.index(*it);
        hello.message("HelloWorld");
        publisher_->write((void*)&hello);
	}
}
void ReqRepHelloWorldRequester::send(const uint16_t number)
{
    waitDiscovery();

    WriteParams wparams;
    HelloWorld hello;
    hello.index(number);
    hello.message("HelloWorld");

    std::unique_lock<std::mutex> lock(mutex_);

    ASSERT_EQ(request_publisher_->write((void*)&hello, wparams), true);
    related_sample_identity_ = wparams.sample_identity();
    current_number_ = number;
}
void PubSubHelloWorldReader::Listener::onNewDataMessage(Subscriber *sub)
{
    ASSERT_NE(sub, nullptr);

    HelloWorld hello;
    SampleInfo_t info;

	if(sub->takeNextData((void*)&hello, &info))
	{
		if(info.sampleKind == ALIVE)
		{
            reader_.newNumber(hello.index());
		}
	}
}
void ReqRepHelloWorldRequester::ReplyListener::onNewDataMessage(Subscriber *sub)
{
    ASSERT_NE(sub, nullptr);

    HelloWorld hello;
    SampleInfo_t info;

    if(sub->takeNextData((void*)&hello, &info))
    {
        if(info.sampleKind == ALIVE)
        {
            ASSERT_EQ(hello.message().compare("GoodBye"), 0);
            requester_.newNumber(info.related_sample_identity, hello.index());
        }
    }
}