asio_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/ua/server/addons/asio_addon.h>
00021 
00022 #include <boost/asio.hpp>
00023 #include <iostream>
00024 #include <thread>
00025 
00026 namespace
00027 {
00028   class AsioAddonImpl : public OpcUa::Server::AsioAddon
00029   {
00030   public:
00031     AsioAddonImpl()
00032       : Work(IoService)
00033     {
00034     }
00035 
00036 
00037     void Initialize(Common::AddonsManager&, const Common::AddonParameters& params) override
00038     {
00039       const unsigned threadsNumber = GetThreadsNumber(params);
00040       //std::cout << "asio| Starting " << threadsNumber << "threads." << std::endl;
00041       for (unsigned i = 0; i < threadsNumber; ++i)
00042       {
00043         Threads.emplace_back([this, i](){
00044           //std::cout << "asio| Starting thread " << i << "." << std::endl;
00045           IoService.run();
00046           //std::cout << "asio| Thread " << i << "exited." << std::endl;
00047         });
00048       }
00049     }
00050 
00051     void Stop() override
00052     {
00053       //std::cout << "asio| stopping io service." << std::endl;
00054       IoService.stop();
00055       //std::cout << "asio| joining threads." << std::endl;
00056       std::for_each(Threads.begin(), Threads.end(), [](std::thread& thread){
00057         thread.join();
00058       });
00059     }
00060 
00061     virtual boost::asio::io_service& GetIoService() override
00062     {
00063       return IoService;
00064     }
00065 
00066   unsigned GetThreadsNumber(const Common::AddonParameters& params) const
00067   {
00068     unsigned num = 1;
00069     for (auto paramIt : params.Parameters)
00070     {
00071       if (paramIt.Name == "threads")
00072       {
00073         num = std::stoi(paramIt.Value);
00074         break;
00075       }
00076     }
00077     return num;
00078   }
00079 
00080   private:
00081     boost::asio::io_service IoService;
00082     boost::asio::io_service::work Work;
00083     std::vector<std::thread> Threads;
00084   };
00085 }
00086 
00087 
00088 namespace OpcUa
00089 {
00090   namespace Server
00091   {
00092 
00093     Common::Addon::UniquePtr AsioAddonFactory::CreateAddon()
00094     {
00095       return Common::Addon::UniquePtr(new AsioAddonImpl);
00096     }
00097 
00098   }
00099 }


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