|
From: <tho...@us...> - 2011-12-31 18:18:46
|
Revision: 620
http://openautomation.svn.sourceforge.net/openautomation/?rev=620&view=rev
Author: thomas_s
Date: 2011-12-31 18:18:39 +0000 (Sat, 31 Dec 2011)
Log Message:
-----------
replaced main-loop by boost::asio::io_service
implemented 'events' in main-loop with recurring-timer (boost::asio::deadline_timer)
XHCP now uses io_service given in constructor
Modified Paths:
--------------
xPLHAL/branches/thomas_s_dev/src/CMakeLists.txt
xPLHAL/branches/thomas_s_dev/src/main.cpp
xPLHAL/branches/thomas_s_dev/src/xhcp.cpp
xPLHAL/branches/thomas_s_dev/src/xhcp.h
Added Paths:
-----------
xPLHAL/branches/thomas_s_dev/src/recurring_timer.cpp
xPLHAL/branches/thomas_s_dev/src/recurring_timer.h
Modified: xPLHAL/branches/thomas_s_dev/src/CMakeLists.txt
===================================================================
--- xPLHAL/branches/thomas_s_dev/src/CMakeLists.txt 2011-12-31 18:16:57 UTC (rev 619)
+++ xPLHAL/branches/thomas_s_dev/src/CMakeLists.txt 2011-12-31 18:18:39 UTC (rev 620)
@@ -19,7 +19,9 @@
#set(CMAKE_CXX_FLAGS "-g -O2 -std=c++0x")
set(CMAKE_CXX_FLAGS "-g -Os -std=c++0x")
-set(xPLHAL_SRCS xplmessagequeue.cpp devicemanager.cpp xplhandler.cpp xplcache.cpp xhcpthread.cpp log.cpp xhcp.cpp xplmessage.cpp main.cpp)
+set(xPLHAL_SRCS xplmessagequeue.cpp devicemanager.cpp
+ xplhandler.cpp xplcache.cpp xhcpthread.cpp log.cpp
+ xhcp.cpp xplmessage.cpp recurring_timer.cpp main.cpp)
add_executable(xPLHAL ${xPLHAL_SRCS})
#message(STATUS "Boost_LIBRARIES=${Boost_LIBRARIES}")
Modified: xPLHAL/branches/thomas_s_dev/src/main.cpp
===================================================================
--- xPLHAL/branches/thomas_s_dev/src/main.cpp 2011-12-31 18:16:57 UTC (rev 619)
+++ xPLHAL/branches/thomas_s_dev/src/main.cpp 2011-12-31 18:18:39 UTC (rev 620)
@@ -15,12 +15,12 @@
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-
#include <signal.h>
#include "log.h"
#include "xplcache.h"
#include "devicemanager.h"
#include "xhcp.h"
+#include "recurring_timer.h"
// load globas and give them their space to live
#include "globals.h"
@@ -35,18 +35,15 @@
deviceManagerClass *deviceManager;
xPLHandler *xPL;
xPLMessageQueueClass *xPLMessageQueue;
+
+static boost::asio::io_service* g_ioservice = nullptr;
-static boost::condition_variable g_exit_condition;
-static boost::mutex g_exit_mutex;
-static bool g_exit = false;
-
void handle_signal(int signal)
{
if (signal == SIGINT || signal == SIGTERM) {
- g_exit_mutex.lock();
- g_exit = true;
- g_exit_condition.notify_one();
- g_exit_mutex.unlock();
+ if (g_ioservice) {
+ g_ioservice->stop();
+ }
}
}
@@ -102,10 +99,13 @@
return -1;
}
}
+
+ boost::asio::io_service io;
+ g_ioservice = &io;
xPLMessageQueue = new xPLMessageQueueClass;
xPLCache = new xPLCacheClass;
- XHCPServer *xhcpServer = new XHCPServer();
+ XHCPServer *xhcpServer = new XHCPServer(io);
xPL = new xPLHandler( boost::asio::ip::host_name() ); //xPL->start();
deviceManager = new deviceManagerClass(xPLCache);
deviceManager->m_sigSendXplMessage.connect(boost::bind(&xPLMessageQueueClass::add, xPLMessageQueue, _1));
@@ -115,33 +115,22 @@
// force everyone to send their configuration so that we start up to date...
xPLMessage::namedValueList command_request; command_request.push_back( std::make_pair( "command", "request" ) );
xPL->sendBroadcastMessage( "config", "list", command_request );
+
+ RecurringTimer timer_listAllObjects(io, boost::posix_time::seconds(60), true);
+ timer_listAllObjects.setExpireHandler([](const boost::system::error_code& e) {
+ writeLog( "main: <tick>", logLevel::all );
+ writeLog( "xPLCache:\n" + xPLCache->listAllObjects(), logLevel::debug );
+ });
+
+ RecurringTimer timer_flushExpiredEntries(io, boost::posix_time::minutes(5), true);
+ timer_flushExpiredEntries.setExpireHandler([](const boost::system::error_code& e) {
+ writeLog( "main: flush cache", logLevel::all );
+ xPLCache->flushExpiredEntries(); // flush cache
+ });
- // main loop
- for( int count = 0;; ++count )
- {
- {
- boost::mutex::scoped_lock lock(g_exit_mutex);
- if (g_exit) break;
- boost::system_time const wait_until = boost::get_system_time() + boost::posix_time::seconds(10);
- g_exit_condition.timed_wait(lock, wait_until);
- if (g_exit) break;
- }
-
- writeLog( "main: run events", logLevel::all ); // run events
+ io.run();
+ g_ioservice = nullptr;
- if( 0 == count%6 )
- {
- writeLog( "main: <tick>", logLevel::all );
- writeLog( "xPLCache:\n" + xPLCache->listAllObjects(), logLevel::debug );
- }
- if( 30 == count ) // every 5 minutes
- {
- writeLog( "main: flush cache", logLevel::all );
- xPLCache->flushExpiredEntries(); // flush cache
- count = 0;
- }
- }
-
writeLog( "main: shutdown xPLHal", logLevel::all );
// clean up
Added: xPLHAL/branches/thomas_s_dev/src/recurring_timer.cpp
===================================================================
--- xPLHAL/branches/thomas_s_dev/src/recurring_timer.cpp (rev 0)
+++ xPLHAL/branches/thomas_s_dev/src/recurring_timer.cpp 2011-12-31 18:18:39 UTC (rev 620)
@@ -0,0 +1,66 @@
+/*
+ xPLHAL implementation in C++
+ Copyright (C) 2009 by Christian Mayer - xpl at ChristianMayer dot de
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+#include "recurring_timer.h"
+#include <boost/bind.hpp>
+
+RecurringTimer::RecurringTimer(boost::asio::io_service& io_service,
+ const boost::asio::deadline_timer::duration_type& expiry_time,
+ bool startTimer)
+:m_timer(io_service, expiry_time)
+,m_delay(expiry_time)
+,m_running(false)
+,m_expireFunc(nullptr)
+{
+ if (startTimer) {
+ start();
+ }
+}
+
+RecurringTimer::~RecurringTimer()
+{
+ stop();
+}
+
+void RecurringTimer::setExpireHandler(void (*handler)(const boost::system::error_code& e))
+{
+ m_expireFunc = handler;
+}
+
+void RecurringTimer::start()
+{
+ m_running = true;
+ m_timer.async_wait(boost::bind(&RecurringTimer::onExpire, this, _1));
+}
+
+void RecurringTimer::stop()
+{
+ m_running = false;
+ m_timer.cancel();
+}
+
+void RecurringTimer::onExpire(const boost::system::error_code& e)
+{
+ if (m_running) {
+ m_timer.expires_at(m_timer.expires_at() + m_delay);
+ m_timer.async_wait(boost::bind(&RecurringTimer::onExpire, this, _1));
+ }
+ if (m_expireFunc) {
+ m_expireFunc(e);
+ }
+}
+
Added: xPLHAL/branches/thomas_s_dev/src/recurring_timer.h
===================================================================
--- xPLHAL/branches/thomas_s_dev/src/recurring_timer.h (rev 0)
+++ xPLHAL/branches/thomas_s_dev/src/recurring_timer.h 2011-12-31 18:18:39 UTC (rev 620)
@@ -0,0 +1,41 @@
+#pragma once
+/*
+ xPLHAL implementation in C++
+ Copyright (C) 2009 by Christian Mayer - xpl at ChristianMayer dot de
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include <boost/asio.hpp>
+
+class RecurringTimer
+{
+ public:
+ RecurringTimer(boost::asio::io_service& io_service, const boost::asio::deadline_timer::duration_type& expiry_time, bool startTimer = false);
+ ~RecurringTimer();
+
+ void setExpireHandler(void (*handler)(const boost::system::error_code& e));
+
+ void start();
+ void stop();
+
+ private:
+ void onExpire(const boost::system::error_code& e);
+
+ const boost::asio::deadline_timer::duration_type m_delay;
+ bool m_running;
+ boost::asio::deadline_timer m_timer;
+ void (*m_expireFunc)(const boost::system::error_code&);
+};
+
Modified: xPLHAL/branches/thomas_s_dev/src/xhcp.cpp
===================================================================
--- xPLHAL/branches/thomas_s_dev/src/xhcp.cpp 2011-12-31 18:16:57 UTC (rev 619)
+++ xPLHAL/branches/thomas_s_dev/src/xhcp.cpp 2011-12-31 18:18:39 UTC (rev 620)
@@ -23,27 +23,23 @@
using boost::asio::ip::tcp;
-XHCPServer::XHCPServer()
- : acceptor(io_service, tcp::endpoint(tcp::v4(), 3865)),
- m_stoprequested(false),
- m_thread(boost::bind(&XHCPServer::waitForConnection, this))
+XHCPServer::XHCPServer(boost::asio::io_service& io)
+:m_io(io)
+,m_acceptor(io, tcp::endpoint(tcp::v4(), 3865))
{
startAccept();
}
XHCPServer::~XHCPServer()
{
- m_stoprequested = true;
- acceptor.cancel();
- acceptor.close();
- io_service.stop();
- m_thread.join();
+ m_acceptor.cancel();
+ m_acceptor.close();
}
void XHCPServer::startAccept()
{
- socket_ptr sockPtr(new tcp::socket(io_service));
- acceptor.async_accept(*sockPtr, boost::bind(&XHCPServer::handleAccept, this, sockPtr));
+ socket_ptr sockPtr(new tcp::socket(m_io));
+ m_acceptor.async_accept(*sockPtr, boost::bind(&XHCPServer::handleAccept, this, sockPtr));
}
void XHCPServer::handleAccept(socket_ptr sockPtr)
@@ -52,19 +48,3 @@
XHCPThread* foo = new XHCPThread( sockPtr );
}
-void XHCPServer::waitForConnection( void )
-{
- writeLog( "XHCPServer::waitForConnection", logLevel::debug );
- io_service.run();
- /*
- while (!m_stoprequested)
- {
- socket_ptr sockPtr(new tcp::socket(io_service));
- writeLog("XHCPServer::waitForConnection in accept", logLevel::debug);
- acceptor.accept(*sockPtr);
- writeLog("XHCPServer::waitForConnection left accept", logLevel::debug);
-
- XHCPThread* foo = new XHCPThread( sockPtr );
- }
- */
-}
Modified: xPLHAL/branches/thomas_s_dev/src/xhcp.h
===================================================================
--- xPLHAL/branches/thomas_s_dev/src/xhcp.h 2011-12-31 18:16:57 UTC (rev 619)
+++ xPLHAL/branches/thomas_s_dev/src/xhcp.h 2011-12-31 18:18:39 UTC (rev 620)
@@ -18,7 +18,7 @@
*/
#include <boost/asio.hpp>
-#include <boost/thread.hpp>
+//#include <boost/thread.hpp>
#include "xplcache.h"
#include "xhcpthread.h"
@@ -29,18 +29,21 @@
*/
class XHCPServer
{
- boost::asio::io_service io_service;
- boost::asio::ip::tcp::tcp::acceptor acceptor;
- volatile bool m_stoprequested;
- boost::thread m_thread;
+ //volatile bool m_stoprequested;
+ //boost::thread m_thread;
public:
- XHCPServer();
+ XHCPServer(boost::asio::io_service& io);
~XHCPServer();
protected:
/** \brief Create a new XHCPThread for a new connection. */
- void waitForConnection( void );
+
+// void waitForConnection( void );
void handleAccept(socket_ptr sockPtr);
void startAccept();
+
+ private:
+ boost::asio::io_service& m_io;
+ boost::asio::ip::tcp::tcp::acceptor m_acceptor;
};
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|