opc_tcp_async_addon.cpp
Go to the documentation of this file.
00001 /******************************************************************************
00002  *   Copyright (C) 2013-2014 by Alexander Rykovanov                        *
00003  *   rykovanov.as@gmail.com                                                   *
00004  *                                                                            *
00005  *   This library is free software; you can redistribute it and/or modify     *
00006  *   it under the terms of the GNU Lesser General Public License as           *
00007  *   published by the Free Software Foundation; version 3 of the License.     *
00008  *                                                                            *
00009  *   This library is distributed in the hope that it will be useful,          *
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of           *
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
00012  *   GNU Lesser General Public License for more details.                      *
00013  *                                                                            *
00014  *   You should have received a copy of the GNU Lesser General Public License *
00015  *   along with this library; if not, write to the                            *
00016  *   Free Software Foundation, Inc.,                                          *
00017  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.                *
00018  ******************************************************************************/
00019 
00020 #include "opc_tcp_async_parameters.h"
00021 
00022 #include "endpoints_parameters.h"
00023 
00024 #include <opc/common/uri_facade.h>
00025 #include <opc/ua/server/addons/asio_addon.h>
00026 #include <opc/ua/server/addons/endpoints_services.h>
00027 #include <opc/ua/server/addons/opc_tcp_async.h>
00028 #include <opc/ua/server/addons/services_registry.h>
00029 #include <opc/ua/server/opc_tcp_async.h>
00030 
00031 #include <iostream>
00032 #include <vector>
00033 
00034 namespace
00035 {
00036   using namespace OpcUa::Server;
00037 
00038   class AsyncOpcTcpAddon : public Common::Addon
00039   {
00040   public:
00041     DEFINE_CLASS_POINTERS(AsyncOpcTcpAddon);
00042 
00043   public:
00044     virtual void Initialize(Common::AddonsManager& addons, const Common::AddonParameters& params) override;
00045     virtual void Stop() override;
00046 
00047   public:
00048     void PublishApplicationsInformation(std::vector<OpcUa::ApplicationDescription> applications, std::vector<OpcUa::EndpointDescription> endpoints, const Common::AddonsManager& addons) const;
00049 
00050   private:
00051     AsyncOpcTcp::SharedPtr Endpoint;
00052   };
00053 
00054 
00055   void AsyncOpcTcpAddon::Initialize(Common::AddonsManager& addons, const Common::AddonParameters& addonParams)
00056   {
00057     AsyncOpcTcp::Parameters params = GetOpcTcpParameters(addonParams);
00058     if (params.DebugMode)
00059     {
00060       std::cout << "opc_tcp_async| Parameters:" << std::endl;
00061       std::cout << "opc_tcp_async|   Debug mode: " << params.DebugMode << std::endl;
00062     }
00063     const std::vector<OpcUa::Server::ApplicationData> applications = OpcUa::ParseEndpointsParameters(addonParams.Groups, params.DebugMode);
00064     if (params.DebugMode)
00065     {
00066       for (OpcUa::Server::ApplicationData d: applications)
00067       {
00068         std::cout << "opc_tcp_async| Endpoint is: " << d.Endpoints.front().EndpointUrl << std::endl;
00069       }
00070     }
00071 
00072     std::vector<OpcUa::ApplicationDescription> applicationDescriptions;
00073     std::vector<OpcUa::EndpointDescription> endpointDescriptions;
00074     for (const OpcUa::Server::ApplicationData application : applications)
00075     {
00076       applicationDescriptions.push_back(application.Application);
00077       endpointDescriptions.insert(endpointDescriptions.end(), application.Endpoints.begin(), application.Endpoints.end());
00078     }
00079 
00080     if (endpointDescriptions.empty())
00081     {
00082       std::cerr << "opc_tcp_async| Endpoints parameters does not present in the configuration file." << std::endl;
00083       return;
00084     }
00085     if (endpointDescriptions.size() > 1)
00086     {
00087       std::cerr << "opc_tcp_async| Too many endpoints specified in the configuration file." << std::endl;
00088       return;
00089     }
00090 
00091     PublishApplicationsInformation(applicationDescriptions, endpointDescriptions, addons);
00092     OpcUa::Server::ServicesRegistry::SharedPtr internalServer = addons.GetAddon<OpcUa::Server::ServicesRegistry>(OpcUa::Server::ServicesRegistryAddonId);
00093     OpcUa::Server::AsioAddon::SharedPtr asio = addons.GetAddon<OpcUa::Server::AsioAddon>(OpcUa::Server::AsioAddonId);
00094 
00095     params.Port = Common::Uri(endpointDescriptions[0].EndpointUrl).Port();
00096     Endpoint = CreateAsyncOpcTcp(params, internalServer->GetServer(), asio->GetIoService());
00097     Endpoint->Listen();
00098   }
00099 
00100   void AsyncOpcTcpAddon::PublishApplicationsInformation(std::vector<OpcUa::ApplicationDescription> applications, std::vector<OpcUa::EndpointDescription> endpoints, const Common::AddonsManager& addons) const
00101   {
00102     OpcUa::Server::EndpointsRegistry::SharedPtr endpointsAddon = addons.GetAddon<OpcUa::Server::EndpointsRegistry>(OpcUa::Server::EndpointsRegistryAddonId);
00103     if (!endpointsAddon)
00104     {
00105       std::cerr << "Cannot publish information about endpoints. Endpoints services addon didn't' registered." << std::endl;
00106       return;
00107     }
00108     endpointsAddon->AddEndpoints(endpoints);
00109     endpointsAddon->AddApplications(applications);
00110   }
00111 
00112   void AsyncOpcTcpAddon::Stop()
00113   {
00114     Endpoint->Shutdown();
00115     Endpoint.reset();
00116   }
00117 
00118 }
00119 
00120 namespace OpcUa
00121 {
00122   namespace Server
00123   {
00124 
00125     Common::Addon::UniquePtr AsyncOpcTcpAddonFactory::CreateAddon()
00126     {
00127       return Common::Addon::UniquePtr(new AsyncOpcTcpAddon());
00128     }
00129 
00130   }
00131 }


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Sat Jun 8 2019 18:24:56