Skip to content

Commit

Permalink
Refactor - moved classes to their own modules and all method implemen…
Browse files Browse the repository at this point in the history
…tation to *.cpp
  • Loading branch information
kzangeli committed May 17, 2024
1 parent 740284d commit e67156c
Show file tree
Hide file tree
Showing 9 changed files with 512 additions and 252 deletions.
3 changes: 3 additions & 0 deletions src/lib/orionld/dds/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ SET (SOURCES
NgsildEntity.cxx
NgsildEntityPubSubTypes.cxx
NgsildPublisher.cpp
NgsildSubscriber.cpp
DdsNotificationReceiver.cpp
DdsNotificationSender.cpp
ddsSubscribe.cpp
ddsPublish.cpp
kjTreeLog.cpp
Expand Down
139 changes: 139 additions & 0 deletions src/lib/orionld/dds/DdsNotificationReceiver.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/*
*
* Copyright 2024 FIWARE Foundation e.V.
*
* This file is part of Orion-LD Context Broker.
*
* Orion-LD Context Broker is free software: you can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* Orion-LD Context Broker is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
* General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Orion-LD Context Broker. If not, see http://www.gnu.org/licenses/.
*
* For those usages not covered by this license please contact with
* orionld at fiware dot org
*
*/
#include "fastdds/dds/domain/DomainParticipant.hpp"
#include "fastdds/dds/domain/DomainParticipantFactory.hpp"
#include "fastdds/dds/subscriber/DataReader.hpp"
#include "fastdds/dds/subscriber/DataReaderListener.hpp"
#include "fastdds/dds/subscriber/qos/DataReaderQos.hpp"
#include "fastdds/dds/subscriber/SampleInfo.hpp"
#include "fastdds/dds/subscriber/Subscriber.hpp"
#include "fastdds/dds/topic/TypeSupport.hpp"

extern "C"
{
#include "ktrace/kTrace.h" // trace messages - ktrace library
#include "kjson/KjNode.h" // KjNode
#include "kjson/kjBuilder.h" // kjObject, kjString, ...
#include "kjson/kjParse.h" // kjParse
#include "kjson/kjClone.h" // kjClone
}

#include "orionld/common/orionldState.h" // orionldState
#include "orionld/common/traceLevels.h" // Trace levels
#include "orionld/dds/config.h" // DDS_RELIABLE, ...
#include "orionld/dds/kjTreeLog.h" // kjTreeLog2
#include "orionld/dds/NgsildEntity.h" // NgsildEntity
#include "orionld/dds/DdsNotificationReceiver.h" // The class

using namespace eprosima::fastdds::dds;



// -----------------------------------------------------------------------------
//
// ddsDumpArray - accumulating data from DDS notifications
//
extern KjNode* ddsDumpArray;



void DdsNotificationReceiver::on_subscription_matched(DataReader*, const SubscriptionMatchedStatus& info)
{
if (info.current_count_change == 1)
KT_T(StDds, "Subscriber matched.");
else if (info.current_count_change == -1)
KT_T(StDds, "Subscriber unmatched.");
else
KT_T(StDds, "'%d' is not a valid value for SubscriptionMatchedStatus current count change", info.current_count_change);
}

void DdsNotificationReceiver::on_data_available(DataReader* reader)
{
SampleInfo info;

KT_T(StDds, "Notification arrived");

if (reader->take_next_sample(&ngsildEntity_, &info) == ReturnCode_t::RETCODE_OK)
{
if (info.valid_data)
{
samples_++;

//
// This is "more or less" how it should work:
// KjNode* entityP = kjEntityFromDds(&ngsildEntity_);
// notificationReceived(entityP);
// The callback 'notificationReceived' is set in some constructor or init() method
//
KT_T(StDds, "Entity Id: %s with type: %s RECEIVED.", ngsildEntity_.id().c_str(), ngsildEntity_.type().c_str());

//
// Accumulate notifications
//
KjNode* dump = kjObject(NULL, "item"); // No name as it is part of an array
KjNode* tenantP = (ngsildEntity_.tenant() != "")? kjString(NULL, "tenant", ngsildEntity_.tenant().c_str()) : NULL;
KjNode* idP = (ngsildEntity_.id() != "")? kjString(NULL, "id", ngsildEntity_.id().c_str()) : NULL;
KjNode* typeP = (ngsildEntity_.type() != "")? kjString(NULL, "type", ngsildEntity_.type().c_str()) : NULL;
KjNode* scopeP = (ngsildEntity_.scope() != "")? kjString(NULL, "scope", ngsildEntity_.scope().c_str()) : NULL;
KjNode* createdAtP = (ngsildEntity_.createdAt() != 0)? kjInteger(NULL, "createdAt", ngsildEntity_.createdAt()) : NULL;
KjNode* modifiedAtP = (ngsildEntity_.modifiedAt() != 0)? kjInteger(NULL, "modifiedAt", ngsildEntity_.modifiedAt()) : NULL;
char* attributes = (ngsildEntity_.attributes() != "")? (char*) ngsildEntity_.attributes().c_str() : NULL;

if (tenantP != NULL) kjChildAdd(dump, tenantP);
if (idP != NULL) kjChildAdd(dump, idP);
if (typeP != NULL) kjChildAdd(dump, typeP);
if (scopeP != NULL) kjChildAdd(dump, scopeP);
if (createdAtP != NULL) kjChildAdd(dump, createdAtP);
if (modifiedAtP != NULL) kjChildAdd(dump, modifiedAtP);

if (attributes != NULL)
{
KT_T(StDds, "Entity '%s' has attributes: '%s'", ngsildEntity_.id().c_str(), attributes);

// Initializing orionldState, to call kjParse (not really necessary, it's overkill)
orionldStateInit(NULL);

// parse the string 'attributes' and add all attributes to 'dump'
KjNode* attrsNode = kjParse(orionldState.kjsonP, attributes);
if (attrsNode != NULL)
attrsNode = kjClone(NULL, attrsNode);
KT_T(StDds, "After kjParse");

kjTreeLog2(attrsNode, "attrsNode", StDds);
kjTreeLog2(dump, "dump w/o attrs", StDds);
// Concatenate the attributes to the "dump entity"
dump->lastChild->next = attrsNode->value.firstChildP;
dump->lastChild = attrsNode->lastChild;
kjTreeLog2(dump, "dump with attrs", StDds);
}
else
KT_T(StDds, "Entity Id: %s has no attributes", ngsildEntity_.id().c_str());

if (ddsDumpArray == NULL)
ddsDumpArray = kjArray(NULL, "ddsDumpArray");

kjChildAdd(ddsDumpArray, dump);
}
}
}
79 changes: 79 additions & 0 deletions src/lib/orionld/dds/DdsNotificationReceiver.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#ifndef SRC_LIB_ORIONLD_DDS_DDSNOTIFICATIONRECEIVER_H_
#define SRC_LIB_ORIONLD_DDS_DDSNOTIFICATIONRECEIVER_H_

/*
*
* Copyright 2024 FIWARE Foundation e.V.
*
* This file is part of Orion-LD Context Broker.
*
* Orion-LD Context Broker is free software: you can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* Orion-LD Context Broker is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
* General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Orion-LD Context Broker. If not, see http://www.gnu.org/licenses/.
*
* For those usages not covered by this license please contact with
* orionld at fiware dot org
*
*/

//
// 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.
//
#include "fastdds/dds/domain/DomainParticipant.hpp"
#include "fastdds/dds/domain/DomainParticipantFactory.hpp"
#include "fastdds/dds/subscriber/DataReader.hpp"
#include "fastdds/dds/subscriber/DataReaderListener.hpp"
#include "fastdds/dds/subscriber/qos/DataReaderQos.hpp"
#include "fastdds/dds/subscriber/SampleInfo.hpp"
#include "fastdds/dds/subscriber/Subscriber.hpp"
#include "fastdds/dds/topic/TypeSupport.hpp"

#include "orionld/dds/config.h" // DDS_RELIABLE, ...
#include "orionld/dds/kjTreeLog.h" // kjTreeLog2



using namespace eprosima::fastdds::dds;



// -----------------------------------------------------------------------------
//
// DdsNotificationReceiver -
//
// FIXME: All the implementation to DdsNotificationReceiver.cpp
//
class DdsNotificationReceiver : public DataReaderListener
{
public:
DdsNotificationReceiver() : samples_(0) { }
~DdsNotificationReceiver() override { }

void on_subscription_matched(DataReader*, const SubscriptionMatchedStatus& info) override;
void on_data_available(DataReader* reader) override;
NgsildEntity ngsildEntity_;
std::atomic_int samples_;
};

#endif // SRC_LIB_ORIONLD_DDS_DDSNOTIFICATIONRECEIVER_H_
64 changes: 64 additions & 0 deletions src/lib/orionld/dds/DdsNotificationSender.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
*
* Copyright 2024 FIWARE Foundation e.V.
*
* This file is part of Orion-LD Context Broker.
*
* Orion-LD Context Broker is free software: you can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* Orion-LD Context Broker is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
* General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Orion-LD Context Broker. If not, see http://www.gnu.org/licenses/.
*
* For those usages not covered by this license please contact with
* orionld at fiware dot org
*
*/
#include <fastdds/dds/domain/DomainParticipant.hpp>
#include <fastdds/dds/domain/DomainParticipantFactory.hpp>
#include <fastdds/dds/publisher/DataWriter.hpp>
#include <fastdds/dds/publisher/DataWriterListener.hpp>
#include <fastdds/dds/publisher/Publisher.hpp>
#include <fastdds/dds/topic/TypeSupport.hpp>

extern "C"
{
#include "ktrace/kTrace.h" // trace messages - ktrace library
#include "kjson/KjNode.h" // KjNode
}

#include "orionld/common/traceLevels.h" // Trace Levels
#include "orionld/dds/NgsildPublisher.h" // The class



// -----------------------------------------------------------------------------
//
// DdsNotificationSender::on_publication_matched -
//
void DdsNotificationSender::on_publication_matched(DataWriter*, const PublicationMatchedStatus& info)
{
// FIXME: Don't Publish until entering here! (mutex)
KT_V("info.current_count_change: %d", info.current_count_change);
if (info.current_count_change == 1)
{
matched_ = info.total_count;
KT_T(StDds, "Publisher matched.");
ready_ = true;
}
else if (info.current_count_change == -1)
{
matched_ = info.total_count;
KT_T(StDds, "Publisher unmatched.");
ready_ = false;
}
else
KT_T(StDds, "'%d' is not a valid value for PublicationMatchedStatus current count change.", info.total_count);
}
65 changes: 65 additions & 0 deletions src/lib/orionld/dds/DdsNotificationSender.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#ifndef SRC_LIB_ORIONLD_DDS_DDSNOTIFICATIONSENDER_H_
#define SRC_LIB_ORIONLD_DDS_DDSNOTIFICATIONSENDER_H_

/*
*
* Copyright 2024 FIWARE Foundation e.V.
*
* This file is part of Orion-LD Context Broker.
*
* Orion-LD Context Broker is free software: you can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* Orion-LD Context Broker is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
* General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Orion-LD Context Broker. If not, see http://www.gnu.org/licenses/.
*
* For those usages not covered by this license please contact with
* orionld at fiware dot org
*
*/

// 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.

#include <fastdds/dds/publisher/DataWriter.hpp>
#include <fastdds/dds/publisher/DataWriterListener.hpp>

using namespace eprosima::fastdds::dds;



// -----------------------------------------------------------------------------
//
// DdsNotificationSender -
//
class DdsNotificationSender : public DataWriterListener
{
public:
bool ready_;

DdsNotificationSender() : ready_(false), matched_(0) {}
~DdsNotificationSender() override {}

void on_publication_matched(DataWriter*, const PublicationMatchedStatus& info) override;
std::atomic_int matched_;
};

#endif // SRC_LIB_ORIONLD_DDS_DDSNOTIFICATIONSENDER_H_
Loading

0 comments on commit e67156c

Please sign in to comment.