Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed AddDataChangeCallback to mirror python-opcua implementation #293

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/opc/ua/server/address_space.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class AddressSpace
DEFINE_CLASS_POINTERS(AddressSpace)

//Server side methods
virtual uint32_t AddDataChangeCallback(const NodeId & node, AttributeId attribute, std::function<DataChangeCallback> callback) = 0;
virtual std::pair<StatusCode,uint32_t> AddDataChangeCallback(const NodeId & node, AttributeId attribute, std::function<DataChangeCallback> callback) = 0;
virtual void DeleteDataChangeCallback(uint32_t clienthandle) = 0;
virtual StatusCode SetValueCallback(const NodeId & node, AttributeId attribute, std::function<DataValue(void)> callback) = 0;
virtual void SetMethod(const NodeId & node, std::function<std::vector<OpcUa::Variant> (NodeId context, std::vector<OpcUa::Variant> arguments)> callback) = 0;
Expand Down
4 changes: 2 additions & 2 deletions python/src/py_opcua_module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ static void Node_SetValue(Node & self, const object & obj, VariantType vtype)
// UaClient helpers
//--------------------------------------------------------------------------

static std::shared_ptr<Subscription> UaClient_CreateSubscription(UaClient & self, uint period, PySubscriptionHandler & callback)
static std::shared_ptr<Subscription> UaClient_CreateSubscription(UaClient & self, uint32_t period, PySubscriptionHandler & callback)
{
return self.CreateSubscription(period, callback);
}
Expand All @@ -256,7 +256,7 @@ static Node UaClient_GetNode(UaClient & self, ObjectId objectid)
// UaServer helpers
//--------------------------------------------------------------------------

static std::shared_ptr<Subscription> UaServer_CreateSubscription(UaServer & self, uint period, PySubscriptionHandler & callback)
static std::shared_ptr<Subscription> UaServer_CreateSubscription(UaServer & self, uint32_t period, PySubscriptionHandler & callback)
{
return self.CreateSubscription(period, callback);
}
Expand Down
4 changes: 2 additions & 2 deletions src/core/common/uri_facade_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
namespace Common
{

void Uri::Initialize(const char * uriString, std::size_t size)
void Uri::Initialize(const std::string & uriString)
{
URL_COMPONENTS url = {0};
url.dwStructSize = sizeof(url);
Expand All @@ -31,7 +31,7 @@ void Uri::Initialize(const char * uriString, std::size_t size)

// TODO msdn says do not use this function in services and in server patforms. :(
// TODO http://msdn.microsoft.com/en-us/library/windows/desktop/aa384376(v=vs.85).aspx
if (!InternetCrackUrl(uriString, size, options, &url))
if (!InternetCrackUrl(uriString.c_str(), uriString.size(), options, &url))
{
THROW_ERROR1(CannotParseUri, uriString);
}
Expand Down
2 changes: 1 addition & 1 deletion src/server/address_space_addon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ std::vector<StatusCode> AddressSpaceAddon::Write(const std::vector<OpcUa::WriteV
return Registry->Write(filter);
}

uint32_t AddressSpaceAddon::AddDataChangeCallback(const NodeId & node, AttributeId attribute, std::function<Server::DataChangeCallback> callback)
std::pair<StatusCode,uint32_t> AddressSpaceAddon::AddDataChangeCallback(const NodeId & node, AttributeId attribute, std::function<Server::DataChangeCallback> callback)
{
return Registry->AddDataChangeCallback(node, attribute, callback);
}
Expand Down
2 changes: 1 addition & 1 deletion src/server/address_space_addon.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class AddressSpaceAddon
virtual std::vector<CallMethodResult> Call(const std::vector<CallMethodRequest> & methodsToCall);

public: // Server internal methods
virtual uint32_t AddDataChangeCallback(const NodeId & node, AttributeId attribute, std::function<Server::DataChangeCallback> callback);
virtual std::pair<StatusCode,uint32_t> AddDataChangeCallback(const NodeId & node, AttributeId attribute, std::function<Server::DataChangeCallback> callback);
virtual void DeleteDataChangeCallback(uint32_t clienthandle);
virtual StatusCode SetValueCallback(const NodeId & node, AttributeId attribute, std::function<DataValue(void)> callback);
virtual void SetMethod(const NodeId & node, std::function<std::vector<OpcUa::Variant> (NodeId context, std::vector<OpcUa::Variant> arguments)> callback);
Expand Down
8 changes: 4 additions & 4 deletions src/server/address_space_internal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ DataValue AddressSpaceInMemory::GetValue(const NodeId & node, AttributeId attrib
return value;
}

uint32_t AddressSpaceInMemory::AddDataChangeCallback(const NodeId & node, AttributeId attribute, std::function<Server::DataChangeCallback> callback)
std::pair<StatusCode,uint32_t> AddressSpaceInMemory::AddDataChangeCallback(const NodeId & node, AttributeId attribute, std::function<Server::DataChangeCallback> callback)
{
boost::unique_lock<boost::shared_mutex> lock(DbMutex);

Expand All @@ -283,23 +283,23 @@ uint32_t AddressSpaceInMemory::AddDataChangeCallback(const NodeId & node, Attrib
if (it == Nodes.end())
{
LOG_ERROR(Logger, "address_space_internal| Node: '{}' not found", node);
throw std::runtime_error("address_space_internal| NodeId not found");
return std::make_pair(StatusCode::BadNodeIdUnknown, 0u);
}

AttributesMap::iterator ait = it->second.Attributes.find(attribute);

if (ait == it->second.Attributes.end())
{
LOG_ERROR(Logger, "address_space_internal| Attribute: {} of node: ‘{}‘ not found", (unsigned)attribute, node);
throw std::runtime_error("Attribute not found");
return std::make_pair(StatusCode::BadAttributeIdInvalid, 0u);
}

uint32_t handle = ++DataChangeCallbackHandle;
DataChangeCallbackData data;
data.Callback = callback;
ait->second.DataChangeCallbacks[handle] = data;
ClientIdToAttributeMap[handle] = NodeAttribute(node, attribute);
return handle;
return std::make_pair(StatusCode::Good,handle);
}

void AddressSpaceInMemory::DeleteDataChangeCallback(uint32_t serverhandle)
Expand Down
2 changes: 1 addition & 1 deletion src/server/address_space_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class AddressSpaceInMemory : public Server::AddressSpace

/// @brief Add callback which will be called when values of attribute is changed.
/// @return handle of a callback which should be passed to the DeletDataChangeCallabck
uint32_t AddDataChangeCallback(const NodeId & node, AttributeId attribute, std::function<Server::DataChangeCallback> callback);
std::pair<StatusCode,uint32_t> AddDataChangeCallback(const NodeId & node, AttributeId attribute, std::function<Server::DataChangeCallback> callback);

/// @bried Delete data change callback assosioated with handle.
void DeleteDataChangeCallback(uint32_t serverhandle);
Expand Down
10 changes: 9 additions & 1 deletion src/server/internal_subscription.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,10 +305,18 @@ MonitoredItemCreateResult InternalSubscription::CreateMonitoredItem(const Monito
LOG_DEBUG(Logger, "internal_subscription | id: {}, subscribe to data changes", Data.SubscriptionId);

uint32_t id = result.MonitoredItemId;
callbackHandle = AddressSpace.AddDataChangeCallback(request.ItemToMonitor.NodeId, request.ItemToMonitor.AttributeId, [this, id](const OpcUa::NodeId & nodeId, OpcUa::AttributeId attr, const DataValue & value)
std::pair<OpcUa::StatusCode,uint32_t> addCallbackResult = AddressSpace.AddDataChangeCallback(request.ItemToMonitor.NodeId, request.ItemToMonitor.AttributeId, [this, id](const OpcUa::NodeId & nodeId, OpcUa::AttributeId attr, const DataValue & value)
{
this->DataChangeCallback(id, value);
});

result.Status = addCallbackResult.first;
callbackHandle = addCallbackResult.second;

if (result.Status != StatusCode::Good)
{
return result;
}
}

MonitoredDataChange mdata;
Expand Down
3 changes: 3 additions & 0 deletions src/server/tcp_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
///

#ifdef _WIN32
#define NOMINMAX
#include <WinSock2.h>
#include <windows.h>
#define SHUT_RDWR SD_BOTH
#endif

#include "tcp_server.h"
Expand Down
4 changes: 3 additions & 1 deletion tests/server/address_space_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,16 @@ TEST_F(AddressSpace, CallsDataChangeCallbackOnWrite)
OpcUa::AttributeId callbackAttr;
OpcUa::DataValue callbackValue;
bool callbackCalled = false;
unsigned callbackHandle = NameSpace->AddDataChangeCallback(valueId, OpcUa::AttributeId::Value, [&](const OpcUa::NodeId & id, OpcUa::AttributeId attr, const OpcUa::DataValue & value)
std::pair<OpcUa::StatusCode,uint32_t> addCallbackResult = NameSpace->AddDataChangeCallback(valueId, OpcUa::AttributeId::Value, [&](const OpcUa::NodeId & id, OpcUa::AttributeId attr, const OpcUa::DataValue & value)
{
callbackId = id;
callbackAttr = attr;
callbackValue = value;
callbackCalled = true;
});

unsigned callbackHandle = addCallbackResult.second;

EXPECT_NE(callbackHandle, 0);

OpcUa::WriteValue value;
Expand Down
4 changes: 4 additions & 0 deletions tests/server/model_object_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
#include <gmock/gmock.h>
#include <gtest/gtest.h>

#ifdef _WIN32
#undef GetObject
#endif

using namespace testing;


Expand Down