server.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright (C) 2013-2014 by Olivier Roulet-Dubonnet *
3  * *
4  * This library is free software; you can redistribute it and/or modify *
5  * it under the terms of the GNU Lesser General Public License as *
6  * published by the Free Software Foundation; version 3 of the License. *
7  * *
8  * This library is distributed in the hope that it will be useful, *
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
11  * GNU Lesser General Public License for more details. *
12  * *
13  * You should have received a copy of the GNU Lesser General Public License *
14  * along with this library; if not, write to the *
15  * Free Software Foundation, Inc., *
16  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
17  ******************************************************************************/
18 
19 #include <opc/ua/server/server.h>
20 
23 
26 #include <iostream>
27 
28 namespace OpcUa
29 {
31 {
32 }
33 
35 {
36  Logger = spdlog::get("UaServer");
37  if (!Logger)
38  {
39  Logger = spdlog::stderr_color_mt("UaServer");
40  }
41  if (debug)
42  {
43  Logger->set_level(spdlog::level::debug);
44  }
45  else
46  {
47  Logger->set_level(spdlog::level::info);
48  }
49 }
50 
51 UaServer::UaServer(const Common::Logger::SharedPtr & logger)
52  : Logger(logger)
53 {
54 }
55 
56 void UaServer::SetEndpoint(const std::string & endpoint)
57 {
58  Endpoint = endpoint;
59 }
60 
62 {
63  ProductUri = uri;
64 }
65 
67 {
68  ServerUri = uri;
69 }
70 
72 {
73  Name = name;
74 }
75 
77 {
78  XmlAddressSpaces.push_back(path);
79 }
80 
82 {
83  if (!Registry)
84  {
85  throw (std::runtime_error("Server is not started"));
86  }
87 }
88 
90 {
91  CheckStarted();
92  Node namespacearray(Registry->GetServer(), ObjectId::Server_NamespaceArray);
93  std::vector<std::string> uris = namespacearray.GetValue().As<std::vector<std::string>>();
94  uint32_t index = uris.size();
95  uris.push_back(uri);
96  namespacearray.SetValue(uris);
97  return index;
98 }
99 
101 {
102  CheckStarted();
103  Node namespacearray(Registry->GetServer(), ObjectId::Server_NamespaceArray);
104  std::vector<std::string> uris = namespacearray.GetValue().As<std::vector<std::string>>();;
105 
106  for (uint32_t i = 0; i < uris.size(); ++i)
107  {
108  if (uris[i] == uri)
109  {
110  return i;
111  }
112  }
113 
114  throw (std::runtime_error("Error namespace uri does not exists in server"));
115  //return -1;
116 }
117 
119 {
120  ApplicationDescription appDesc;
122  appDesc.ApplicationUri = ServerUri;
124  appDesc.ProductUri = ProductUri;
125 
127  params.Debug = Logger.get();
128  params.Endpoint.Server = appDesc;
129  params.Endpoint.EndpointUrl = Endpoint;
131  params.Endpoint.SecurityPolicyUri = "http://opcfoundation.org/UA/SecurityPolicy#None";
132  params.Endpoint.TransportProfileUri = "http://opcfoundation.org/UA-Profile/Transport/uatcp-uasc-uabinary";
133  //setting up policy is required for some client, this should be in a constructor
134  UserTokenPolicy policy;
136  params.Endpoint.UserIdentityTokens.push_back(policy);
137 
140  Addons->Start();
141 
144 
146  ServerArray.SetValue(std::vector<std::string>({Endpoint}));
147 
148  EnableEventNotification(); //Enabling event notification, it probably does hurt anyway and users will forgot to set it up
149 }
150 
151 Node UaServer::GetNode(const std::string & nodeid) const
152 {
153  return GetNode(ToNodeId(nodeid));
154 }
155 
156 Node UaServer::GetNode(const NodeId & nodeid) const
157 {
158  CheckStarted();
159  return Node(Registry->GetServer(), nodeid);
160 }
161 
162 Node UaServer::GetNodeFromPath(const std::vector<QualifiedName> & path) const
163 {
164  return GetRootNode().GetChild(path);
165 }
166 
167 Node UaServer::GetNodeFromPath(const std::vector<std::string> & path) const
168 {
169  return GetRootNode().GetChild(path);
170 }
171 
173 {
174  LOG_INFO(Logger, "UaServer | stopping opcua server application");
175  CheckStarted();
176  Addons->Stop();
177 }
178 
180 {
182 }
183 
185 {
187 }
188 
190 {
191  return GetNode(ObjectId::Server);
192 }
193 
195 {
197 
198  uint8_t notifierval = 0;
199  notifierval |= EventNotifier::SubscribeToEvents;
200 
201  DataValue dval(notifierval);
203 
205 }
206 
207 Subscription::SharedPtr UaServer::CreateSubscription(unsigned int period, SubscriptionHandler & callback)
208 {
209  CheckStarted();
211  params.RequestedPublishingInterval = period;
212  return std::make_shared<Subscription>(Registry->GetServer(), params, callback, Logger);
213 }
214 
216 {
217  return ServerOperations(Registry->GetServer());
218 }
219 
221 {
222  SubscriptionService->TriggerEvent(ObjectId::Server, event);
223 }
224 
225 }
OpcUa::MessageSecurityMode SecurityMode
Definition: server.h:100
void SetEndpoint(const std::string &endpoint)
set endpoint uri on wich server will listen. opc.tcp://localhost:4841/opcua/server opc...
Definition: server.cpp:56
Node GetRootNode() const
helper methods for node you will probably want to access
Definition: server.cpp:179
std::vector< OpcUa::UserTokenPolicy > UserIdentityTokens
static const uint8_t SubscribeToEvents
const char SubscriptionServiceAddonId[]
Common::AddonsManager::SharedPtr Addons
Definition: server.h:103
std::string Name
Definition: server.h:97
Server::ServicesRegistry::SharedPtr Registry
Definition: server.h:104
void SetServerURI(const std::string &uri)
Definition: server.cpp:66
void EnableEventNotification()
Enable event notification on Server node this is necessary if you want to be able to send custom even...
Definition: server.cpp:194
Server::SubscriptionService::SharedPtr SubscriptionService
Definition: server.h:105
Node GetNodeFromPath(const std::vector< QualifiedName > &path) const
Get a node using its browsepath.
Definition: server.cpp:162
const char ServicesRegistryAddonId[]
Node GetServerNode() const
Definition: server.cpp:189
name
Definition: setup.py:38
void SetAttribute(AttributeId attr, const DataValue &dval) const
Definition: node.cpp:125
Node GetNode(const NodeId &nodeid) const
Get a specific node by nodeid.
Definition: server.cpp:156
EndpointDescription Endpoint
Definition: common_addons.h:39
void SetSourceTimestamp(const DateTime &t)
Definition: data_value.h:110
uint32_t RegisterNamespace(std::string uri)
Register your application namespace.
Definition: server.cpp:89
void Start()
Start and Stop server.
Definition: server.cpp:118
std::shared_ptr< logger > get(const std::string &name)
Definition: spdlog_impl.h:40
OpcUa::ApplicationDescription Server
#define LOG_INFO(__logger__,...)
Definition: common/logger.h:25
void TriggerEvent(Event event)
Trigger and event.
Definition: server.cpp:220
OpcUa::ApplicationType ApplicationType
std::string ServerUri
Definition: server.h:95
AddonsManager::UniquePtr CreateAddonsManager(const Common::Logger::SharedPtr &logger)
Get instance of addons core.
void SetProductURI(const std::string &uri)
setup server description
Definition: server.cpp:61
UaServer()
create high level server
Definition: server.cpp:30
OpcUa::MessageSecurityMode SecurityMode
OPC UA Address space part. GNU LGPL.
std::string Endpoint
Definition: server.h:94
string uri
Definition: client.py:31
Node GetChild(const std::vector< OpcUa::QualifiedName > &path) const
Definition: node.cpp:270
Variant GetValue() const
Definition: node.cpp:548
std::string ProductUri
Definition: server.h:96
ServerOperations CreateServerOperations()
Create a server operations object.
Definition: server.cpp:215
OpcUa::LocalizedText ApplicationName
void RegisterCommonAddons(const Parameters &params, Common::AddonsManager &addons)
parameters of server. can be used at embedded.
A Node object represent an OPC-UA node. It is high level object intended for developper who want to e...
Definition: node.h:42
NodeId ToNodeId(const std::string &str, uint32_t defaultNamespace=0)
static DateTime Current()
Node GetObjectsNode() const
Definition: server.cpp:184
OpcUa::UserTokenType TokenType
Common::Logger::SharedPtr Logger
Definition: server.h:98
uint32_t GetNamespaceIndex(std::string uri)
Definition: server.cpp:100
T As() const
Definition: variant.h:271
std::vector< std::string > XmlAddressSpaces
Definition: server.h:92
void AddAddressSpace(const std::string &path)
load xml addressspace. This is not implemented yet!!!
Definition: server.cpp:76
const char ServerArray[]
Definition: strings.h:147
std::shared_ptr< logger > stderr_color_mt(const std::string &logger_name)
Definition: spdlog_impl.h:150
void SetServerName(const std::string &name)
Definition: server.cpp:71
void CheckStarted() const
Definition: server.cpp:81
Definition: server.py:1
Subscription::SharedPtr CreateSubscription(unsigned int period, SubscriptionHandler &callback)
Create a subscription objects.
Definition: server.cpp:207


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Tue Jan 19 2021 03:12:07