opc_tcp_async_addon.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright (C) 2013-2014 by Alexander Rykovanov *
3  * rykovanov.as@gmail.com *
4  * *
5  * This library is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU Lesser General Public License as *
7  * published by the Free Software Foundation; version 3 of the License. *
8  * *
9  * This library is distributed in the hope that it will be useful, *
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12  * GNU Lesser General Public License for more details. *
13  * *
14  * You should have received a copy of the GNU Lesser General Public License *
15  * along with this library; if not, write to the *
16  * Free Software Foundation, Inc., *
17  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
18  ******************************************************************************/
19 
21 
22 #include "endpoints_parameters.h"
23 
24 #include <opc/common/uri_facade.h>
30 
31 #include <iostream>
32 #include <vector>
33 
34 namespace
35 {
36 using namespace OpcUa::Server;
37 
38 class AsyncOpcTcpAddon : public Common::Addon
39 {
40 public:
41  DEFINE_CLASS_POINTERS(AsyncOpcTcpAddon)
42 
43 public:
44  virtual void Initialize(Common::AddonsManager & addons, const Common::AddonParameters & params) override;
45  virtual void Stop() override;
46 
47 public:
48  void PublishApplicationsInformation(std::vector<OpcUa::ApplicationDescription> applications, std::vector<OpcUa::EndpointDescription> endpoints, const Common::AddonsManager & addons) const;
49 
50 private:
51  Common::Logger::SharedPtr Logger;
52  AsyncOpcTcp::SharedPtr Endpoint;
53 };
54 
55 
56 void AsyncOpcTcpAddon::Initialize(Common::AddonsManager & addons, const Common::AddonParameters & addonParams)
57 {
58  Logger = addons.GetLogger();
59  AsyncOpcTcp::Parameters params = GetOpcTcpParameters(addonParams);
60 
61  LOG_DEBUG(Logger, "opc_tcp_async | parameters:");
62  LOG_DEBUG(Logger, "opc_tcp_async | DebugMode: {}", params.DebugMode);
63 
64  const std::vector<OpcUa::Server::ApplicationData> applications = OpcUa::ParseEndpointsParameters(addonParams.Groups, Logger);
65 
66  if (Logger && Logger->should_log(spdlog::level::debug))
67  {
68  for (OpcUa::Server::ApplicationData d : applications)
69  {
70  Logger->debug("opc_tcp_async | Endpoint is: {}", d.Endpoints.front().EndpointUrl);
71  }
72  }
73 
74  std::vector<OpcUa::ApplicationDescription> applicationDescriptions;
75  std::vector<OpcUa::EndpointDescription> endpointDescriptions;
76 
77  for (const OpcUa::Server::ApplicationData application : applications)
78  {
79  applicationDescriptions.push_back(application.Application);
80  endpointDescriptions.insert(endpointDescriptions.end(), application.Endpoints.begin(), application.Endpoints.end());
81  }
82 
83  if (endpointDescriptions.empty())
84  {
85  LOG_ERROR(Logger, "opc_tcp_async | Endpoints parameter not present in configuration");
86  return;
87  }
88 
89  if (endpointDescriptions.size() > 1)
90  {
91  LOG_ERROR(Logger, "opc_tcp_async | too many endpoints specified in configuration");
92  return;
93  }
94 
95  PublishApplicationsInformation(applicationDescriptions, endpointDescriptions, addons);
96  OpcUa::Server::ServicesRegistry::SharedPtr internalServer = addons.GetAddon<OpcUa::Server::ServicesRegistry>(OpcUa::Server::ServicesRegistryAddonId);
97  OpcUa::Server::AsioAddon::SharedPtr asio = addons.GetAddon<OpcUa::Server::AsioAddon>(OpcUa::Server::AsioAddonId);
98 
99  params.Port = Common::Uri(endpointDescriptions[0].EndpointUrl).Port();
100  Endpoint = CreateAsyncOpcTcp(params, internalServer->GetServer(), asio->GetIoService(), Logger);
101  Endpoint->Listen();
102 }
103 
104 void AsyncOpcTcpAddon::PublishApplicationsInformation(std::vector<OpcUa::ApplicationDescription> applications, std::vector<OpcUa::EndpointDescription> endpoints, const Common::AddonsManager & addons) const
105 {
106  OpcUa::Server::EndpointsRegistry::SharedPtr endpointsAddon = addons.GetAddon<OpcUa::Server::EndpointsRegistry>(OpcUa::Server::EndpointsRegistryAddonId);
107 
108  if (!endpointsAddon)
109  {
110  LOG_ERROR(Logger, "opc_tcp_async | cannot publish information about endpoints. Endpoints services addon not registered.");
111  return;
112  }
113 
114  endpointsAddon->AddEndpoints(endpoints);
115  endpointsAddon->AddApplications(applications);
116 }
117 
118 void AsyncOpcTcpAddon::Stop()
119 {
120  Endpoint->Shutdown();
121  Endpoint.reset();
122 }
123 
124 }
125 
126 namespace OpcUa
127 {
128 namespace Server
129 {
130 
131 Common::Addon::UniquePtr AsyncOpcTcpAddonFactory::CreateAddon()
132 {
133  return Common::Addon::UniquePtr(new AsyncOpcTcpAddon());
134 }
135 
136 }
137 }
d
const char EndpointsRegistryAddonId[]
Addon interface definition GNU LGPL.
virtual std::shared_ptr< Addon > GetAddon(const AddonId &id) const =0
getting addon by id
#define LOG_ERROR(__logger__,...)
Definition: common/logger.h:27
#define LOG_DEBUG(__logger__,...)
Definition: common/logger.h:24
const char ServicesRegistryAddonId[]
unsigned Port() const
Definition: uri_facade.h:46
const char AsioAddonId[]
Definition: asio_addon.h:31
OPC UA Address space part. GNU LGPL.
AsyncOpcTcp::Parameters GetOpcTcpParameters(const Common::AddonParameters &addonParams)
virtual Common::Addon::UniquePtr CreateAddon() override
Create instance of addon.
std::vector< Server::ApplicationData > ParseEndpointsParameters(const std::vector< Common::ParametersGroup > &rootGroup, const Common::Logger::SharedPtr &logger)
#define DEFINE_CLASS_POINTERS(ClassName)
Exception declarations GNU LGPL.
AsyncOpcTcp::UniquePtr CreateAsyncOpcTcp(const AsyncOpcTcp::Parameters &params, Services::SharedPtr server, boost::asio::io_service &io, const Common::Logger::SharedPtr &logger)
const char Server[]
Definition: strings.h:121


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