asio_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 <boost/asio.hpp>
23 
24 #include <algorithm>
25 #include <iostream>
26 #include <thread>
27 
28 namespace
29 {
30 class AsioAddonImpl : public OpcUa::Server::AsioAddon
31 {
32 public:
33  AsioAddonImpl()
34  : Work(IoService)
35  {
36  }
37 
38 
39  void Initialize(Common::AddonsManager &, const Common::AddonParameters & params) override
40  {
41  const unsigned threadsNumber = GetThreadsNumber(params);
42 
43  //std::cout << "asio| Starting " << threadsNumber << "threads." << std::endl;
44  for (unsigned i = 0; i < threadsNumber; ++i)
45  {
46  Threads.emplace_back([this, i]()
47  {
48  //std::cout << "asio| Starting thread " << i << "." << std::endl;
49  IoService.run();
50  //std::cout << "asio| Thread " << i << "exited." << std::endl;
51  });
52  }
53  }
54 
55  void Stop() override
56  {
57  //std::cout << "asio| stopping io service." << std::endl;
58  IoService.stop();
59  //std::cout << "asio| joining threads." << std::endl;
60  std::for_each(Threads.begin(), Threads.end(), [](std::thread & thread)
61  {
62  thread.join();
63  });
64  }
65 
66  virtual boost::asio::io_service & GetIoService() override
67  {
68  return IoService;
69  }
70 
71  unsigned GetThreadsNumber(const Common::AddonParameters & params) const
72  {
73  unsigned num = 1;
74 
75  for (auto paramIt : params.Parameters)
76  {
77  if (paramIt.Name == "threads")
78  {
79  num = std::stoi(paramIt.Value);
80  break;
81  }
82  }
83 
84  return num;
85  }
86 
87 private:
88  boost::asio::io_service IoService;
89  boost::asio::io_service::work Work;
90  std::vector<std::thread> Threads;
91 };
92 }
93 
94 
95 namespace OpcUa
96 {
97 namespace Server
98 {
99 
100 Common::Addon::UniquePtr AsioAddonFactory::CreateAddon()
101 {
102  return Common::Addon::UniquePtr(new AsioAddonImpl);
103 }
104 
105 }
106 }
Common::Addon * CreateAddon()
Addon interface definition GNU LGPL.
virtual void Stop()=0
Stopping addon work. After calling this method addon should throw exception on any calls...
virtual void Initialize(AddonsManager &manager, const AddonParameters &parameters)=0
initialize addon.
OPC UA Address space part. GNU LGPL.
virtual boost::asio::io_service & GetIoService()=0
const char Server[]
Definition: strings.h:121
std::vector< Parameter > Parameters


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