Curl Scol plugin
sService.cpp
1/*
2-----------------------------------------------------------------------------
3This source file is part of OpenSpace3D
4For the latest info, see http://www.openspace3d.com
5
6Copyright (c) 2012 I-maginer
7
8This program is free software; you can redistribute it and/or modify it under
9the terms of the GNU Lesser General Public License as published by the Free Software
10Foundation; either version 2 of the License, or (at your option) any later
11version.
12
13This program is distributed in the hope that it will be useful, but WITHOUT
14ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
16
17You should have received a copy of the GNU Lesser General Public License along with
18this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19Place - Suite 330, Boston, MA 02111-1307, USA, or go to
20http://www.gnu.org/copyleft/lesser.txt
21
22-----------------------------------------------------------------------------
23*/
24#include <scolPlugin.h>
25#include "sService.h"
26
27// Initialize singleton
28SService* SService::_singleton = 0;
29
30SService::SService()
31{
32 m_work.reset(new boost::asio::io_service::work(m_service));
33
34 // init 2 parallele threads
35 for (std::size_t i = 0; i < 2; ++i)
36 {
37 try
38 {
39 m_threads.create_thread(boost::bind(&boost::asio::io_service::run, &m_service));
40 }
41 catch (std::exception &e)
42 {
43 // no more ressource available for additionnal thread
44 MMechostr(MSKDEBUG, "Curl Service error : %s", e.what());
45 }
46 }
47}
48
49SService::~SService()
50{
51 m_service.stop();
52 m_work.reset();
53 m_threads.join_all();
54}
55
56SService* SService::GetInstance()
57{
58 if (0 == _singleton)
59 {
60 _singleton = new SService();
61 }
62
63 return _singleton;
64}
65
66void SService::Kill()
67{
68 if (0 != _singleton)
69 {
70 delete _singleton;
71 _singleton = 0;
72 }
73}
74
75void SService::addWork(boost::function<void()> func)
76{
77 m_service.post(func);
78}