|
From: <tho...@us...> - 2012-01-01 21:56:22
|
Revision: 623
http://openautomation.svn.sourceforge.net/openautomation/?rev=623&view=rev
Author: thomas_s
Date: 2012-01-01 21:56:16 +0000 (Sun, 01 Jan 2012)
Log Message:
-----------
- improved recurring timer
- refactored main function as Object
Modified Paths:
--------------
xPLHAL/branches/thomas_s_dev/src/main.cpp
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/main.cpp
===================================================================
--- xPLHAL/branches/thomas_s_dev/src/main.cpp 2012-01-01 20:50:01 UTC (rev 622)
+++ xPLHAL/branches/thomas_s_dev/src/main.cpp 2012-01-01 21:56:16 UTC (rev 623)
@@ -24,29 +24,115 @@
// load globas and give them their space to live
#include "globals.h"
-using namespace boost::filesystem;
+using boost::filesystem::path;
+using boost::filesystem::initial_path;
+
path xPLHalRootFolder;
path DataFileFolder;
path ScriptEngineFolder;
path rulesFolder;
xPLCacheClass *xPLCache;
-deviceManagerClass *deviceManager;
xPLHandler *xPL;
xPLMessageQueueClass *xPLMessageQueue;
-
+
static boost::asio::io_service* g_ioservice = nullptr;
+class XplHalApplication
+{
+ public:
+ XplHalApplication()
+ :mXplMessageQueue(new xPLMessageQueueClass)
+ ,mXplCache(new xPLCacheClass)
+ ,mDeviceManager(mXplCache)
+ ,mXHCPServer(new XHCPServer(m_ioservice, &mDeviceManager))
+ ,mXpl(new xPLHandler( boost::asio::ip::host_name() ))
+ ,mTimerListAllObjects(m_ioservice, boost::posix_time::seconds(60), true)
+ ,mTimerFlushExpiredEntries(m_ioservice, boost::posix_time::minutes(5), true)
+ {
+ mDeviceManager.m_sigSendXplMessage.connect(boost::bind(&xPLMessageQueueClass::add, mXplMessageQueue, _1));
+ mXpl->m_sigRceivedXplMessage.connect(boost::bind(&DeviceManager::processXplMessage, &mDeviceManager, _1));
+ installTimer();
+
+ /* set global variables */
+ xPLCache = mXplCache;
+ xPL = mXpl;
+ xPLMessageQueue = mXplMessageQueue;
+
+ writeLog( "initialized", logLevel::all );
+ }
+
+ ~XplHalApplication()
+ {
+ // clean up
+ delete mXHCPServer;
+ writeLog( "main: xhcp shutdown", logLevel::all );
+ // delete deviceManager;
+ // writeLog( "main: deviceManager shutdown", logLevel::all );
+ delete mXplCache;
+ writeLog( "main: xPLCache shutdown", logLevel::all );
+ delete mXpl;
+ writeLog( "main: xPL shutdown", logLevel::all );
+ }
+
+ void installTimer()
+ {
+ mTimerListAllObjects.sigExpired.connect([](const boost::system::error_code& e) {
+ writeLog( "main: <tick>", logLevel::all );
+ writeLog( "xPLCache:\n" + xPLCache->listAllObjects(), logLevel::debug );
+ });
+
+ mTimerFlushExpiredEntries.sigExpired.connect([](const boost::system::error_code& e) {
+ writeLog( "main: flush cache", logLevel::all );
+ xPLCache->flushExpiredEntries(); // flush cache
+ });
+ }
+
+ static void stop()
+ {
+ m_ioservice.stop();
+ }
+
+ int exec()
+ {
+ // force everyone to send their configuration so that we start up to date...
+ xPLMessageQueue->add(xPLMessagePtr( new xPLMessage(xPL_MESSAGE_COMMAND, "*", "config", "current", {{"command", "request"}}) ));
+
+ writeLog( "started, run mainloop", logLevel::all );
+ m_ioservice.run();
+ writeLog( "main: shutdown xPLHal", logLevel::all );
+ return 0;
+ }
+
+ static void dispatchEvent(void (*event)())
+ {
+ m_ioservice.dispatch(event);
+ }
+
+ private:
+ static boost::asio::io_service m_ioservice;
+
+ xPLMessageQueueClass *mXplMessageQueue;
+ xPLCacheClass *mXplCache;
+ DeviceManager mDeviceManager;
+ XHCPServer *mXHCPServer;
+ xPLHandler *mXpl;
+
+ RecurringTimer mTimerListAllObjects;
+ RecurringTimer mTimerFlushExpiredEntries;
+};
+
+boost::asio::io_service XplHalApplication::m_ioservice;
+
void handle_signal(int signal)
{
if (signal == SIGINT || signal == SIGTERM) {
- if (g_ioservice) {
- g_ioservice->stop();
- }
+ XplHalApplication::stop();
}
}
+
/**
* Setup the whole program.
*/
@@ -64,82 +150,37 @@
signal(SIGTERM, handle_signal);
// FIXME : add exception handling for directory operations!!!
- if(false)//FIXME if( !QDir::setCurrent( xPLHalRootFolder.path() ) )
- {
+ if(false) {
+ //FIXME if( !QDir::setCurrent( xPLHalRootFolder.path() ) )
writeLog( "Error changing to working directory \"" + xPLHalRootFolder.string() + "\"!", logLevel::error );
return -1;
}
- if( !exists( DataFileFolder ) )
- {
+ if( !exists( DataFileFolder ) ) {
writeLog( "Directory \"" + DataFileFolder.string() + "\" for DataFileFolder doesn't exist. Creating it...", logLevel::debug );
- if( !create_directory( DataFileFolder ) )
- {
+ if( !create_directory( DataFileFolder ) ) {
writeLog( "Error creating data directory \"" + DataFileFolder.string() + "\"!", logLevel::error );
return -1;
}
}
- if( !exists( ScriptEngineFolder ) )
- {
+ if( !exists( ScriptEngineFolder ) ) {
writeLog( "Directory \"" + ScriptEngineFolder.string() + "\" for ScriptEngineFolder doesn't exist. Creating it...", logLevel::debug );
- if( !create_directory( ScriptEngineFolder ) )
- {
+ if( !create_directory( ScriptEngineFolder ) ) {
writeLog( "Error creating script directory \"" + ScriptEngineFolder.string() + "\"!", logLevel::error );
return -1;
}
}
- if( !exists( rulesFolder ) )
- {
+ if( !exists( rulesFolder ) ) {
writeLog( "Directory \"" + rulesFolder.string() + "\" for rulesFolder doesn't exist. Creating it...", logLevel::debug );
- if( !create_directory( rulesFolder ) )
- {
+ if( !create_directory( rulesFolder ) ) {
writeLog( "Error creating determinator directory \"" + rulesFolder.string() + "\"!", logLevel::error );
return -1;
}
}
-
- boost::asio::io_service io;
- g_ioservice = &io;
- xPLMessageQueue = new xPLMessageQueueClass;
- xPLCache = new xPLCacheClass;
- 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));
- xPL->m_sigRceivedXplMessage.connect(boost::bind(&deviceManagerClass::processXplMessage, deviceManager, _1));
- writeLog( "started", logLevel::all );
+ XplHalApplication app;
+ return app.exec();
+}
- // 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
- });
-
- io.run();
- g_ioservice = nullptr;
-
- writeLog( "main: shutdown xPLHal", logLevel::all );
-
- // clean up
- delete xhcpServer;
- writeLog( "main: xhcp shutdown", logLevel::all );
- delete deviceManager;
- writeLog( "main: deviceManager shutdown", logLevel::all );
- delete xPLCache;
- writeLog( "main: xPLCache shutdown", logLevel::all );
- delete xPL;
- writeLog( "main: xPL shutdown", logLevel::all );
-}
Modified: xPLHAL/branches/thomas_s_dev/src/recurring_timer.cpp
===================================================================
--- xPLHAL/branches/thomas_s_dev/src/recurring_timer.cpp 2012-01-01 20:50:01 UTC (rev 622)
+++ xPLHAL/branches/thomas_s_dev/src/recurring_timer.cpp 2012-01-01 21:56:16 UTC (rev 623)
@@ -24,7 +24,6 @@
:m_timer(io_service, expiry_time)
,m_delay(expiry_time)
,m_running(false)
-,m_expireFunc(nullptr)
{
if (startTimer) {
start();
@@ -36,11 +35,6 @@
stop();
}
-void RecurringTimer::setExpireHandler(void (*handler)(const boost::system::error_code& e))
-{
- m_expireFunc = handler;
-}
-
void RecurringTimer::start()
{
m_running = true;
@@ -59,8 +53,6 @@
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);
- }
+ sigExpired(e);
}
Modified: xPLHAL/branches/thomas_s_dev/src/recurring_timer.h
===================================================================
--- xPLHAL/branches/thomas_s_dev/src/recurring_timer.h 2012-01-01 20:50:01 UTC (rev 622)
+++ xPLHAL/branches/thomas_s_dev/src/recurring_timer.h 2012-01-01 21:56:16 UTC (rev 623)
@@ -18,6 +18,7 @@
*/
#include <boost/asio.hpp>
+#include <boost/signals2/signal.hpp>
class RecurringTimer
{
@@ -25,10 +26,10 @@
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();
+
+ boost::signals2::signal<void (const boost::system::error_code& e)> sigExpired;
private:
void onExpire(const boost::system::error_code& e);
@@ -36,6 +37,5 @@
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&);
};
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|