1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
|
// Copyright 2016 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// 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.
/**
* @file TestReaderSocket.cpp
*
*/
#include "TestReaderSocket.h"
#include "fastrtps/rtps/reader/RTPSReader.h"
#include "fastrtps/rtps/participant/RTPSParticipant.h"
#include "fastrtps/rtps/RTPSDomain.h"
#include "fastrtps/rtps/attributes/RTPSParticipantAttributes.h"
#include "fastrtps/rtps/attributes/ReaderAttributes.h"
#include "fastrtps/rtps/attributes/HistoryAttributes.h"
#include "fastrtps/rtps/history/ReaderHistory.h"
#include "fastrtps/utils/IPLocator.h"
using namespace eprosima::fastrtps;
using namespace eprosima::fastrtps::rtps;
TestReaderSocket::TestReaderSocket()
: mp_participant(nullptr)
, mp_reader(nullptr)
, mp_history(nullptr)
{
}
TestReaderSocket::~TestReaderSocket()
{
RTPSDomain::removeRTPSParticipant(mp_participant);
delete(mp_history);
}
bool TestReaderSocket::init(
std::string ip,
uint32_t port)
{
//CREATE PARTICIPANT
RTPSParticipantAttributes PParam;
PParam.builtin.discovery_config.discoveryProtocol = eprosima::fastrtps::rtps::DiscoveryProtocol_t::NONE;
PParam.builtin.use_WriterLivelinessProtocol = false;
mp_participant = RTPSDomain::createParticipant(0, PParam);
if (mp_participant == nullptr)
{
return false;
}
//CREATE READERHISTORY
HistoryAttributes hatt;
hatt.payloadMaxSize = 255;
mp_history = new ReaderHistory(hatt);
//CREATE READER
ReaderAttributes ratt;
ratt.endpoint.setEntityID(3);
Locator_t loc;
IPLocator::setIPv4(loc, ip);
loc.port = static_cast<uint16_t>(port);
ratt.endpoint.multicastLocatorList.push_back(loc);
mp_reader = RTPSDomain::createRTPSReader(mp_participant, ratt, mp_history, &m_listener);
mp_reader->enableMessagesFromUnkownWriters(true);
if (mp_reader == nullptr)
{
return false;
}
return true;
}
void TestReaderSocket::run()
{
printf("Enter number to stop reader.\n");
int aux;
std::cin >> aux;
}
void TestReaderSocket::MyListener::onNewCacheChangeAdded(
RTPSReader* reader,
const CacheChange_t* const change)
{
printf("Received: %s\n", change->serializedPayload.data);
reader->getHistory()->remove_change((CacheChange_t*)change);
m_received++;
}
|