|
From: <tho...@us...> - 2011-10-17 21:07:29
|
Revision: 453
http://openautomation.svn.sourceforge.net/openautomation/?rev=453&view=rev
Author: thomas_s
Date: 2011-10-17 21:07:20 +0000 (Mon, 17 Oct 2011)
Log Message:
-----------
- removed some 'using' from headers
- did some refactoring
- implemented clean shutdown with CTRL-C and SIGTERM
- use range-based for loop (just for fun)
- some preperations for unit-testing
Modified Paths:
--------------
xPLHAL/branches/thomas_s_dev/CMakeLists.txt
xPLHAL/branches/thomas_s_dev/cmake/Modules/FindxPL.cmake
xPLHAL/branches/thomas_s_dev/src/CMakeLists.txt
xPLHAL/branches/thomas_s_dev/src/devicemanager.cpp
xPLHAL/branches/thomas_s_dev/src/devicemanager.h
xPLHAL/branches/thomas_s_dev/src/globals.h
xPLHAL/branches/thomas_s_dev/src/main.cpp
xPLHAL/branches/thomas_s_dev/src/xhcp.cpp
xPLHAL/branches/thomas_s_dev/src/xhcp.h
xPLHAL/branches/thomas_s_dev/src/xhcpthread.cpp
xPLHAL/branches/thomas_s_dev/src/xhcpthread.h
xPLHAL/branches/thomas_s_dev/src/xplcache.cpp
xPLHAL/branches/thomas_s_dev/src/xplcache.h
xPLHAL/branches/thomas_s_dev/src/xplhandler.cpp
xPLHAL/branches/thomas_s_dev/src/xplhandler.h
xPLHAL/branches/thomas_s_dev/src/xplmessagequeue.cpp
xPLHAL/branches/thomas_s_dev/src/xplmessagequeue.h
Added Paths:
-----------
xPLHAL/branches/thomas_s_dev/ChangeLog
xPLHAL/branches/thomas_s_dev/src/i_properties.h
xPLHAL/branches/thomas_s_dev/src/xpldevice.h
xPLHAL/branches/thomas_s_dev/src/xplmessage.h
xPLHAL/branches/thomas_s_dev/test/
xPLHAL/branches/thomas_s_dev/test/CMakeLists.txt
xPLHAL/branches/thomas_s_dev/test/test_test.cpp
Modified: xPLHAL/branches/thomas_s_dev/CMakeLists.txt
===================================================================
--- xPLHAL/branches/thomas_s_dev/CMakeLists.txt 2011-10-17 20:29:50 UTC (rev 452)
+++ xPLHAL/branches/thomas_s_dev/CMakeLists.txt 2011-10-17 21:07:20 UTC (rev 453)
@@ -37,7 +37,7 @@
#set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
-find_package(Boost 1.38 COMPONENTS thread system filesystem date_time regex )
+find_package(Boost 1.38 COMPONENTS thread system filesystem date_time regex unit_test_framework)
find_package(xPL REQUIRED)
set(LIBS ${LIBS} ${Boost_LIBRARIES} ${xPL_LIBRARIES} pthread)
@@ -65,4 +65,5 @@
endif(Boost_USE_STATIC_LIBS)
add_subdirectory(src)
+add_subdirectory(test)
Added: xPLHAL/branches/thomas_s_dev/ChangeLog
===================================================================
--- xPLHAL/branches/thomas_s_dev/ChangeLog (rev 0)
+++ xPLHAL/branches/thomas_s_dev/ChangeLog 2011-10-17 21:07:20 UTC (rev 453)
@@ -0,0 +1,8 @@
+
+2011-10-17:
+Experimentally using c++0x features available with gcc-4.6.
+To compile create clean 'build' directory and call from build-dir:
+"CXX=g++-4.6 cmake .."
+
+Boost-Test is not working yet.
+
Modified: xPLHAL/branches/thomas_s_dev/cmake/Modules/FindxPL.cmake
===================================================================
--- xPLHAL/branches/thomas_s_dev/cmake/Modules/FindxPL.cmake 2011-10-17 20:29:50 UTC (rev 452)
+++ xPLHAL/branches/thomas_s_dev/cmake/Modules/FindxPL.cmake 2011-10-17 21:07:20 UTC (rev 453)
@@ -3,11 +3,13 @@
find_path( xPL_INCLUDE_DIR
NAMES xPL.h
PATHS /usr/local/include
+ PATHS /home/thomas/projects/xPLLib
)
find_library( xPL_LIBRARY
NAMES libxPL.so
PATHS /usr/local/lib
+ PATHS /home/thomas/projects/xPLLib
)
set( xPL_FOUND TRUE )
Modified: xPLHAL/branches/thomas_s_dev/src/CMakeLists.txt
===================================================================
--- xPLHAL/branches/thomas_s_dev/src/CMakeLists.txt 2011-10-17 20:29:50 UTC (rev 452)
+++ xPLHAL/branches/thomas_s_dev/src/CMakeLists.txt 2011-10-17 21:07:20 UTC (rev 453)
@@ -17,6 +17,8 @@
project(xPLHAL)
cmake_minimum_required(VERSION 2.6)
+#set(CMAKE_CXX_FLAGS "-g -O2 -std=c++0x")
+set(CMAKE_CXX_FLAGS "-g -std=c++0x")
set(xPLHAL_SRCS xplmessagequeue.cpp devicemanager.cpp xplhandler.cpp xplcache.cpp xhcpthread.cpp log.cpp xhcp.cpp main.cpp)
add_executable(xPLHAL ${xPLHAL_SRCS})
Modified: xPLHAL/branches/thomas_s_dev/src/devicemanager.cpp
===================================================================
--- xPLHAL/branches/thomas_s_dev/src/devicemanager.cpp 2011-10-17 20:29:50 UTC (rev 452)
+++ xPLHAL/branches/thomas_s_dev/src/devicemanager.cpp 2011-10-17 21:07:20 UTC (rev 453)
@@ -1,20 +1,20 @@
/*
- xPLHAL implementation in C++
- Copyright (C) 2009 by Christian Mayer - xpl at ChristianMayer dot de
+ 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 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.
+ 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/>.
-*/
+ 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/algorithm/string.hpp>
#include <boost/regex.hpp>
@@ -23,379 +23,370 @@
using namespace boost::algorithm;
#include "log.h"
-
#include "devicemanager.h"
+using namespace boost::posix_time;
+
void deviceManagerClass::add( const xPLDevice& device ) const
{
- writeLog( "deviceManagerClass::add("+device.VDI+")", logLevel::debug );
- if( contains( device.VDI ) )
- update( device );
- else
- {
- string newtag = "device." + device.VDI + ".";
- ptime expires = second_clock::local_time() + minutes( 2*device.Interval + 1 );
+ writeLog( "deviceManagerClass::add("+device.VDI+")", logLevel::debug );
+ if( contains( device.VDI ) ) {
+ update( device );
+ } else {
+ std::string newtag = "device." + device.VDI + ".";
+ ptime expires = second_clock::local_time() + minutes( 2*device.Interval + 1 );
- xPLCache->updateEntry(newtag + "vdi", device.VDI, false);
- xPLCache->updateEntry(newtag + "configdone", device.ConfigDone?"true":"false", false);
- xPLCache->updateEntry(newtag + "configmissing", device.ConfigMissing?"true":"false", false);
- xPLCache->updateEntry(newtag + "configsource", device.ConfigSource, false);
- xPLCache->updateEntry(newtag + "configtype", device.ConfigType?"true":"false", false);
- xPLCache->updateEntry(newtag + "waitingconfig", device.WaitingConfig?"true":"false", false);
- xPLCache->updateEntry(newtag + "current", device.Current?"true":"false", false);
- xPLCache->updateEntry(newtag + "configlistsent", device.ConfigListSent?"true":"false", false);
- xPLCache->updateEntry(newtag + "suspended", device.Suspended?"true":"false", false);
- xPLCache->updateEntry(newtag + "interval", lexical_cast<string>(device.Interval), false);
- xPLCache->updateEntry(newtag + "expires", timeConverter( expires ), false);
- }
+ xPLCache->updateEntry(newtag + "vdi", device.VDI, false);
+ xPLCache->updateEntry(newtag + "configdone", device.ConfigDone?"true":"false", false);
+ xPLCache->updateEntry(newtag + "configmissing", device.ConfigMissing?"true":"false", false);
+ xPLCache->updateEntry(newtag + "configsource", device.ConfigSource, false);
+ xPLCache->updateEntry(newtag + "configtype", device.ConfigType?"true":"false", false);
+ xPLCache->updateEntry(newtag + "waitingconfig", device.WaitingConfig?"true":"false", false);
+ xPLCache->updateEntry(newtag + "current", device.Current?"true":"false", false);
+ xPLCache->updateEntry(newtag + "configlistsent", device.ConfigListSent?"true":"false", false);
+ xPLCache->updateEntry(newtag + "suspended", device.Suspended?"true":"false", false);
+ xPLCache->updateEntry(newtag + "interval", lexical_cast<std::string>(device.Interval), false);
+ xPLCache->updateEntry(newtag + "expires", timeConverter( expires ), false);
+ }
}
-bool deviceManagerClass::remove( const string& deviceTag ) const
+bool deviceManagerClass::remove( const std::string& deviceTag ) const
{
- writeLog( "deviceManagerClass::remove("+deviceTag+")", logLevel::debug );
- if( contains( deviceTag ) )
- {
- vector<string> list = xPLCache->childNodes( "device." + deviceTag );
- for( vector<string>::const_iterator it = list.begin(); it != list.end(); it++ )
- xPLCache->deleteEntry( *it );
- return true;
- } else
- return false;
+ writeLog( "deviceManagerClass::remove("+deviceTag+")", logLevel::debug );
+ if( contains( deviceTag ) ) {
+ std::vector<std::string> list = xPLCache->childNodes( "device." + deviceTag );
+ //for( std::vector<std::string>::const_iterator it = list.begin(); it != list.end(); it++ )
+ for( auto it : list )
+ xPLCache->deleteEntry( it );
+ return true;
+ } else {
+ return false;
+ }
}
-bool deviceManagerClass::removeConfig( const string& deviceTag ) const
+bool deviceManagerClass::removeConfig( const std::string& deviceTag ) const
{
- writeLog( "deviceManagerClass::remove("+deviceTag+")", logLevel::debug );
- if( containsConfig( deviceTag ) )
- {
- vector<string> list = xPLCache->childNodes( "config." + deviceTag );
- for( vector<string>::const_iterator it = list.begin(); it != list.end(); it++ )
- xPLCache->deleteEntry( *it );
- return true;
- } else
- return false;
+ writeLog( "deviceManagerClass::remove("+deviceTag+")", logLevel::debug );
+ if( containsConfig( deviceTag ) ) {
+ std::vector<std::string> list = xPLCache->childNodes( "config." + deviceTag );
+ //for( std::vector<std::string>::const_iterator it = list.begin(); it != list.end(); it++ )
+ for (auto it : list)
+ xPLCache->deleteEntry( it );
+ return true;
+ } else {
+ return false;
+ }
}
-xPLDevice deviceManagerClass::getDevice( const string& deviceTag ) const
+xPLDevice deviceManagerClass::getDevice( const std::string& deviceTag ) const
{
- xPLDevice retval;
- if( contains( deviceTag ) )
- {
- retval.ConfigDone = xPLCache->objectValue( "device." + deviceTag + ".configdone" ) == "true";
- retval.ConfigMissing = xPLCache->objectValue( "device." + deviceTag + ".configmissing" ) == "true";
- retval.ConfigSource = xPLCache->objectValue( "device." + deviceTag + ".configsource" );
- retval.ConfigType = xPLCache->objectValue( "device." + deviceTag + ".configtype" ) == "true";
- retval.Current = xPLCache->objectValue( "device." + deviceTag + ".current" ) == "true";
- retval.Expires = timeConverter( xPLCache->objectValue( "device." + deviceTag + ".expires" ) );
- retval.Interval = atoi(xPLCache->objectValue( "device." + deviceTag + ".interval" ).c_str());
- retval.VDI = xPLCache->objectValue( "device." + deviceTag + ".vdi" );
- retval.WaitingConfig = xPLCache->objectValue( "device." + deviceTag + ".waitingconfig" ) == "true";
- retval.ConfigListSent = xPLCache->objectValue( "device." + deviceTag + ".configlistsent" ) == "true";
- retval.Suspended = xPLCache->objectValue( "device." + deviceTag + ".suspended" ) == "true";
- }
- return retval;
+ xPLDevice retval;
+ if( contains( deviceTag ) ) {
+ retval.ConfigDone = xPLCache->objectValue( "device." + deviceTag + ".configdone" ) == "true";
+ retval.ConfigMissing = xPLCache->objectValue( "device." + deviceTag + ".configmissing" ) == "true";
+ retval.ConfigSource = xPLCache->objectValue( "device." + deviceTag + ".configsource" );
+ retval.ConfigType = xPLCache->objectValue( "device." + deviceTag + ".configtype" ) == "true";
+ retval.Current = xPLCache->objectValue( "device." + deviceTag + ".current" ) == "true";
+ retval.Expires = timeConverter( xPLCache->objectValue( "device." + deviceTag + ".expires" ) );
+ retval.Interval = atoi(xPLCache->objectValue( "device." + deviceTag + ".interval" ).c_str());
+ retval.VDI = xPLCache->objectValue( "device." + deviceTag + ".vdi" );
+ retval.WaitingConfig = xPLCache->objectValue( "device." + deviceTag + ".waitingconfig" ) == "true";
+ retval.ConfigListSent = xPLCache->objectValue( "device." + deviceTag + ".configlistsent" ) == "true";
+ retval.Suspended = xPLCache->objectValue( "device." + deviceTag + ".suspended" ) == "true";
+ }
+ return retval;
}
-vector<string> deviceManagerClass::getAllDeviceNames() const
+std::vector<std::string> deviceManagerClass::getAllDeviceNames() const
{
- vector<string> retval;
- vector<string> nodes = xPLCache->childNodes( "device." );
- for( vector<string>::iterator it = nodes.begin(); it != nodes.end(); ++it )
- {
- if( ends_with( *it, ".vdi" ) )
- {
- retval.push_back( it->substr( 7, it->size() - 7 - 4 ) ); // leave "device." and ".vdi" away
- writeLog( "deviceManagerClass::getAllDeviceNames() = "+ it->substr( 7, it->size() - 7 - 4 ), logLevel::debug );
+ std::vector<std::string> retval;
+ std::vector<std::string> nodes = xPLCache->childNodes( "device." );
+// for( std::vector<std::string>::iterator it = nodes.begin(); it != nodes.end(); ++it )
+ for (auto node : nodes) {
+ if( ends_with( node, ".vdi" ) ) {
+ retval.push_back( node.substr( 7, node.size() - 7 - 4 ) ); // leave "device." and ".vdi" away
+ writeLog( "deviceManagerClass::getAllDeviceNames() = "+ node.substr( 7, node.size() - 7 - 4 ), logLevel::debug );
+ }
}
- }
- return retval;
+ return retval;
}
void deviceManagerClass::update( const xPLDevice& device ) const
{
- writeLog( "deviceManagerClass::update("+device.VDI+") - known: "+ (contains( device.VDI )?"true":"false"), logLevel::debug );
+ writeLog( "deviceManagerClass::update("+device.VDI+") - known: "+ (contains( device.VDI )?"true":"false"), logLevel::debug );
- if( contains( device.VDI ) )
- remove( device.VDI );
- writeLog( "deviceManagerClass::update("+device.VDI+") -> add", logLevel::debug );
- add( device );
+ if( contains( device.VDI ) ) {
+ remove( device.VDI );
+ }
+ writeLog( "deviceManagerClass::update("+device.VDI+") -> add", logLevel::debug );
+ add( device );
}
void deviceManagerClass::processConfigList( const xPL_MessagePtr message ) const
{
- string source = string("")
- + xPL_getSourceVendor ( message ) + "-"
- + xPL_getSourceDeviceID ( message ) + "."
- + xPL_getSourceInstanceID( message );
- xPLDevice device = getDevice( source );
- writeLog( "deviceManagerClass::processConfigList("+source+") - found ["+device.VDI+"]", logLevel::debug );
- if( "" == device.VDI )
- { // A config list turned up that we haven't asked for...
- // create a new device...
- const char* intervalString = xPL_getMessageNamedValue( message, "interval" );
- int interval = intervalString ? atoi(intervalString) : 5; // default to 5 minutes
- ptime expires = second_clock::local_time() + minutes( 2*interval + 1 );
- device.VDI = source; // vendor / device / instance = unique id
- device.ConfigDone = false; // false = new waiting check, true = sent/not required
- device.ConfigMissing = false; // true = no config file, no response from device, false = have/waiting config
- device.ConfigSource = "objectcache"; // v-d.xml / v-d.cache.xml or empty
- device.ConfigType = true; // true = config. false = hbeat.
- device.WaitingConfig = false; // false = waiting check or not needed, true = manual intervention
- device.Current = false; // asked for current config
- device.ConfigListSent = true; // Have we asked this device for it's config?
- device.Suspended = false; // lost heartbeat
- device.Interval = interval; // current heartbeat interval
- device.Expires = expires; // time expires
- update( device );
- }
+ std::string source = std::string("")
+ + xPL_getSourceVendor ( message ) + "-"
+ + xPL_getSourceDeviceID ( message ) + "."
+ + xPL_getSourceInstanceID( message );
+ xPLDevice device = getDevice( source );
+ writeLog( "deviceManagerClass::processConfigList("+source+") - found ["+device.VDI+"]", logLevel::debug );
+ if( "" == device.VDI ) {
+ // A config list turned up that we haven't asked for...
+ // create a new device...
+ const char* intervalString = xPL_getMessageNamedValue( message, "interval" );
+ int interval = intervalString ? atoi(intervalString) : 5; // default to 5 minutes
+ ptime expires = second_clock::local_time() + minutes( 2*interval + 1 );
+ device.VDI = source; // vendor / device / instance = unique id
+ device.ConfigDone = false; // false = new waiting check, true = sent/not required
+ device.ConfigMissing = false; // true = no config file, no response from device, false = have/waiting config
+ device.ConfigSource = "objectcache"; // v-d.xml / v-d.cache.xml or empty
+ device.ConfigType = true; // true = config. false = hbeat.
+ device.WaitingConfig = false; // false = waiting check or not needed, true = manual intervention
+ device.Current = false; // asked for current config
+ device.ConfigListSent = true; // Have we asked this device for it's config?
+ device.Suspended = false; // lost heartbeat
+ device.Interval = interval; // current heartbeat interval
+ device.Expires = expires; // time expires
+ update( device );
+ }
- xPL_NameValueListPtr nvList = xPL_getMessageBody( message );
- xPL_NameValuePairPtr nvPair = NULL;
- int nvCount = xPL_getNamedValueCount(nvList);
- for( int nvIndex = 0; nvIndex < nvCount; nvIndex++ )
- {
- string newtag = "config." + source + ".options.";
- nvPair = xPL_getNamedValuePairAt( nvList, nvIndex );
- string value = nvPair->itemValue; to_lower( value );
- string key = nvPair->itemName ; to_lower( key );
- boost::regex re( "([a-z0-9]{1,16})\\[(\\d{1,3})\\]" );
- boost::smatch matches;
- if( boost::regex_match( value, matches, re) )
- {
- xPLCache->updateEntry( newtag + matches[1] , "" , false );
- xPLCache->updateEntry( newtag + matches[1] + ".type" , key , false );
- xPLCache->updateEntry( newtag + matches[1] + ".count", matches[2], false );
- } else {
- xPLCache->updateEntry( newtag + value , "" , false );
- xPLCache->updateEntry( newtag + value + ".type" , key , false );
- xPLCache->updateEntry( newtag + value + ".count", "" , false );
+ xPL_NameValueListPtr nvList = xPL_getMessageBody( message );
+ xPL_NameValuePairPtr nvPair = NULL;
+ int nvCount = xPL_getNamedValueCount(nvList);
+ for( int nvIndex = 0; nvIndex < nvCount; nvIndex++ ) {
+ std::string newtag = "config." + source + ".options.";
+ nvPair = xPL_getNamedValuePairAt( nvList, nvIndex );
+ std::string value = nvPair->itemValue; to_lower( value );
+ std::string key = nvPair->itemName ; to_lower( key );
+ boost::regex re( "([a-z0-9]{1,16})\\[(\\d{1,3})\\]" );
+ boost::smatch matches;
+ if( boost::regex_match( value, matches, re) ) {
+ xPLCache->updateEntry( newtag + matches[1] , "" , false );
+ xPLCache->updateEntry( newtag + matches[1] + ".type" , key , false );
+ xPLCache->updateEntry( newtag + matches[1] + ".count", matches[2], false );
+ } else {
+ xPLCache->updateEntry( newtag + value , "" , false );
+ xPLCache->updateEntry( newtag + value + ".type" , key , false );
+ xPLCache->updateEntry( newtag + value + ".count", "" , false );
+ }
}
- }
- // send the config response, but only if we have a config (e.g. from the use via XHCP)
- if( xPLCache->exists( "config." + source + ".current" ) )
- {
- xPLCache->updateEntry( "device." + source + ".configdone", "true", false );
- sendConfigResponse( source, false );
- } else {
- // try to get at least the current config
- xPLMessage::namedValueList command_request; command_request.push_back( make_pair( "command", "request" ) );
- xPL->sendMessage( xPL_MESSAGE_COMMAND,
- xPL_getSourceVendor( message ), xPL_getSourceDeviceID( message ), xPL_getSourceInstanceID( message ),
- "config", "current", command_request );
- }
+ // send the config response, but only if we have a config (e.g. from the use via XHCP)
+ if( xPLCache->exists( "config." + source + ".current" ) ) {
+ xPLCache->updateEntry( "device." + source + ".configdone", "true", false );
+ sendConfigResponse( source, false );
+ } else {
+ // try to get at least the current config
+ xPLMessage::namedValueList command_request; command_request.push_back( std::make_pair( "command", "request" ) );
+ xPL->sendMessage( xPL_MESSAGE_COMMAND,
+ xPL_getSourceVendor( message ), xPL_getSourceDeviceID( message ), xPL_getSourceInstanceID( message ),
+ "config", "current", command_request );
+ }
}
void deviceManagerClass::processConfigHeartBeat( const xPL_MessagePtr message ) const
{
- string source = string("")
- + xPL_getSourceVendor ( message ) + "-"
- + xPL_getSourceDeviceID ( message ) + "."
- + xPL_getSourceInstanceID( message );
- xPLDevice device = getDevice( source );
- writeLog( "deviceManagerClass::processConfigHeartBeat("+source+") - found ["+device.VDI+"]", logLevel::debug );
- if( "" == device.VDI )
- { // this handles a new application that identifies itself with a hbeat straight away.
- // it must either be storing it's config locally, can't be configured, or is configured somewhere else.
- const char* intervalString = xPL_getMessageNamedValue( message, "interval" );
- int interval = intervalString ? atoi(intervalString) : 5; // default to 5 minutes
- ptime expires = second_clock::local_time() + minutes( 2*interval + 1 );
- device.VDI = source; // vendor / device / instance = unique id
- device.ConfigDone = false; // false = new waiting check, true = sent/not required
- device.ConfigMissing = true; // true = no config file, no response from device, false = have/waiting config
- device.ConfigSource = "xplmessage"; // v-d.xml / v-d.cache.xml or empty
- device.ConfigType = true; // true = config. false = hbeat.
- device.WaitingConfig = true; // false = waiting check or not needed, true = manual intervention
- device.Current = false; // asked for current config
- device.ConfigListSent = false; // Have we asked this device for it's config?
- device.Suspended = false; // lost heartbeat
- device.Interval = interval; // current heartbeat interval
- device.Expires = expires; // time expires
- update( device );
- } else {
- xPLCache->updateEntry( "device." + source + ".expires", timeConverter( second_clock::local_time() + minutes( 2*device.Interval + 1 ) ), false );
- // FIXME - that line look like that in the VB code - but it seems really strange!
- xPLCache->updateEntry( "device." + source + ".expires.configdone", "objectcache", false );
- }
+ std::string source = std::string("")
+ + xPL_getSourceVendor ( message ) + "-"
+ + xPL_getSourceDeviceID ( message ) + "."
+ + xPL_getSourceInstanceID( message );
+ xPLDevice device = getDevice( source );
+ writeLog( "deviceManagerClass::processConfigHeartBeat("+source+") - found ["+device.VDI+"]", logLevel::debug );
+ if( "" == device.VDI ) {
+ // this handles a new application that identifies itself with a hbeat straight away.
+ // it must either be storing it's config locally, can't be configured, or is configured somewhere else.
+ const char* intervalString = xPL_getMessageNamedValue( message, "interval" );
+ int interval = intervalString ? atoi(intervalString) : 5; // default to 5 minutes
+ ptime expires = second_clock::local_time() + minutes( 2*interval + 1 );
+ device.VDI = source; // vendor / device / instance = unique id
+ device.ConfigDone = false; // false = new waiting check, true = sent/not required
+ device.ConfigMissing = true; // true = no config file, no response from device, false = have/waiting config
+ device.ConfigSource = "xplmessage"; // v-d.xml / v-d.cache.xml or empty
+ device.ConfigType = true; // true = config. false = hbeat.
+ device.WaitingConfig = true; // false = waiting check or not needed, true = manual intervention
+ device.Current = false; // asked for current config
+ device.ConfigListSent = false; // Have we asked this device for it's config?
+ device.Suspended = false; // lost heartbeat
+ device.Interval = interval; // current heartbeat interval
+ device.Expires = expires; // time expires
+ update( device );
+ } else {
+ xPLCache->updateEntry( "device." + source + ".expires", timeConverter( second_clock::local_time() + minutes( 2*device.Interval + 1 ) ), false );
+ // FIXME - that line look like that in the VB code - but it seems really strange!
+ xPLCache->updateEntry( "device." + source + ".expires.configdone", "objectcache", false );
+ }
- // ask device for configuration (if haven't asked it before...)
- if( !device.ConfigListSent )
- {
- xPLMessage::namedValueList command_request; command_request.push_back( make_pair( "command", "request" ) );
- xPL->sendMessage( xPL_MESSAGE_COMMAND,
- xPL_getSourceVendor( message ), xPL_getSourceDeviceID( message ), xPL_getSourceInstanceID( message ),
- "config", "list", command_request );
- xPLCache->updateEntry( "device." + source + ".configlistsend", "true", false );
- }
+ // ask device for configuration (if haven't asked it before...)
+ if( !device.ConfigListSent ) {
+ xPLMessage::namedValueList command_request; command_request.push_back( std::make_pair( "command", "request" ) );
+ xPL->sendMessage( xPL_MESSAGE_COMMAND,
+ xPL_getSourceVendor( message ), xPL_getSourceDeviceID( message ), xPL_getSourceInstanceID( message ),
+ "config", "list", command_request );
+ xPLCache->updateEntry( "device." + source + ".configlistsend", "true", false );
+ }
}
void deviceManagerClass::processCurrentConfig( const xPL_MessagePtr message ) const
{
- string source = string("")
- + xPL_getSourceVendor ( message ) + "-"
- + xPL_getSourceDeviceID ( message ) + "."
- + xPL_getSourceInstanceID( message );
- xPLDevice device = getDevice( source );
- writeLog( "deviceManagerClass::processCurrentConfig("+source+") - found ["+device.VDI+"]", logLevel::debug );
- if( "" == device.VDI )
- { // A current config turned up for a device that we don't know...
- // Processing the current config doesn't make sense as long as we don't know the config list
- // => just treat it like an heartbeat
- return processConfigHeartBeat( message );
- } else {
- xPLCache->updateEntry( "config." + source + ".expires", timeConverter( second_clock::local_time() + minutes( 2*device.Interval + 1 ) ), false );
- xPLCache->updateEntry( "config." + source + ".current", "true", false );
- }
+ std::string source = std::string("")
+ + xPL_getSourceVendor ( message ) + "-"
+ + xPL_getSourceDeviceID ( message ) + "."
+ + xPL_getSourceInstanceID( message );
+ xPLDevice device = getDevice( source );
+ writeLog( "deviceManagerClass::processCurrentConfig("+source+") - found ["+device.VDI+"]", logLevel::debug );
+ if( "" == device.VDI ) {
+ // A current config turned up for a device that we don't know...
+ // Processing the current config doesn't make sense as long as we don't know the config list
+ // => just treat it like an heartbeat
+ return processConfigHeartBeat( message );
+ } else {
+ xPLCache->updateEntry( "config." + source + ".expires", timeConverter( second_clock::local_time() + minutes( 2*device.Interval + 1 ) ), false );
+ xPLCache->updateEntry( "config." + source + ".current", "true", false );
+ }
- xPL_NameValueListPtr nvList = xPL_getMessageBody( message );
- xPL_NameValuePairPtr nvPair = NULL;
- int nvCount = xPL_getNamedValueCount(nvList);
- string multiKey;
- int multiCount = 0;
- for( int nvIndex = 0; nvIndex < nvCount; nvIndex++ )
- {
- nvPair = xPL_getNamedValuePairAt( nvList, nvIndex );
- string value = nvPair->itemValue; to_lower( value );
- string key = nvPair->itemName ; to_lower( key );
- string count = xPLCache->objectValue( "config." + source + ".options." + key + ".count" );
- if( "" != count )
- {
- if( multiKey == key )
- multiCount++;
- else
- {
- multiKey = key;
- multiCount = 0;
- }
- char buf[6];
- snprintf( buf, 6, "{%.3d}", multiCount );
- key += buf;
+ xPL_NameValueListPtr nvList = xPL_getMessageBody( message );
+ xPL_NameValuePairPtr nvPair = NULL;
+ int nvCount = xPL_getNamedValueCount(nvList);
+ std::string multiKey;
+ int multiCount = 0;
+ for( int nvIndex = 0; nvIndex < nvCount; nvIndex++ ) {
+ nvPair = xPL_getNamedValuePairAt( nvList, nvIndex );
+ std::string value = nvPair->itemValue; to_lower( value );
+ std::string key = nvPair->itemName ; to_lower( key );
+ std::string count = xPLCache->objectValue( "config." + source + ".options." + key + ".count" );
+ if( "" != count ) {
+ if( multiKey == key ) {
+ multiCount++;
+ } else {
+ multiKey = key;
+ multiCount = 0;
+ }
+ char buf[6];
+ snprintf( buf, 6, "{%.3d}", multiCount );
+ key += buf;
+ }
+ xPLCache->updateEntry( "config." + source + ".current." + key, value, false );
}
- xPLCache->updateEntry( "config." + source + ".current." + key, value, false );
- }
}
void deviceManagerClass::processHeartbeat( xPL_MessagePtr message ) const
{
- string source = string("")
- + xPL_getSourceVendor ( message ) + "-"
- + xPL_getSourceDeviceID ( message ) + "."
- + xPL_getSourceInstanceID( message );
+ std::string source = std::string("")
+ + xPL_getSourceVendor ( message ) + "-"
+ + xPL_getSourceDeviceID ( message ) + "."
+ + xPL_getSourceInstanceID( message );
- xPLDevice device = getDevice( source );
- int interval = atoi(string( xPL_getMessageNamedValue( message, "interval" ) ).c_str());
- ptime expires = second_clock::local_time() + minutes( 2*interval + 1 );
- writeLog( "deviceManagerClass::processHeartbeat("+source+") - found ["+device.VDI+"]", logLevel::debug );
+ xPLDevice device = getDevice( source );
+ int interval = atoi(std::string( xPL_getMessageNamedValue( message, "interval" ) ).c_str());
+ ptime expires = second_clock::local_time() + minutes( 2*interval + 1 );
+ writeLog( "deviceManagerClass::processHeartbeat("+source+") - found ["+device.VDI+"]", logLevel::debug );
- if( "" == device.VDI )
- {
- // this handles a new application that identifies itself with a hbeat straight away.
- // it must either be storing it's config locally, can't be configured, or is configured somewhere else.
- device.VDI = source; // vendor / device / instance = unique id
- device.ConfigDone = true; // false = new waiting check, true = sent/not required
- device.ConfigMissing = true; // true = no config file, no response from device, false = have/waiting config
- device.ConfigSource = "objectcache"; // v-d.xml / v-d.cache.xml or empty
- device.ConfigType = false; // true = config. false = hbeat.
- device.WaitingConfig = false; // false = waiting check or not needed, true = manual intervention
- device.Current = false; // asked for current config
- device.ConfigListSent = true; // Have we asked this device for it's config?
- device.Suspended = false; // lost heartbeat
- device.Interval = interval; // current heartbeat interval
- device.Expires = expires; // time expires
- update( device );
+ if( "" == device.VDI ) {
+ // this handles a new application that identifies itself with a hbeat straight away.
+ // it must either be storing it's config locally, can't be configured, or is configured somewhere else.
+ device.VDI = source; // vendor / device / instance = unique id
+ device.ConfigDone = true; // false = new waiting check, true = sent/not required
+ device.ConfigMissing = true; // true = no config file, no response from device, false = have/waiting config
+ device.ConfigSource = "objectcache"; // v-d.xml / v-d.cache.xml or empty
+ device.ConfigType = false; // true = config. false = hbeat.
+ device.WaitingConfig = false; // false = waiting check or not needed, true = manual intervention
+ device.Current = false; // asked for current config
+ device.ConfigListSent = true; // Have we asked this device for it's config?
+ device.Suspended = false; // lost heartbeat
+ device.Interval = interval; // current heartbeat interval
+ device.Expires = expires; // time expires
+ update( device );
- // Throw it a config request anyway, see what turns up..
- xPLMessage::namedValueList command_request; command_request.push_back( make_pair( "command", "request" ) );
- xPL->sendMessage( xPL_MESSAGE_COMMAND,
- xPL_getSourceVendor( message ), xPL_getSourceDeviceID( message ), xPL_getSourceInstanceID( message ),
- "config", "list" , command_request );
- /* // that code below was from the VB - but we don't need it as processConfigList will
- // itself send the config.current request...
- xPL->sendMessage( xPL_MESSAGE_COMMAND,
- xPL_getSourceVendor( message ), xPL_getSourceDeviceID( message ), xPL_getSourceInstanceID( message ),
- "config", "current", command_request );
- */
- } else {
- xPLCache->updateEntry( "device." + source + ".expires", timeConverter( expires ), false );
- }
+ // Throw it a config request anyway, see what turns up..
+ xPLMessage::namedValueList command_request; command_request.push_back( std::make_pair( "command", "request" ) );
+ xPL->sendMessage( xPL_MESSAGE_COMMAND,
+ xPL_getSourceVendor( message ), xPL_getSourceDeviceID( message ), xPL_getSourceInstanceID( message ),
+ "config", "list" , command_request );
+ /* // that code below was from the VB - but we don't need it as processConfigList will
+ // itself send the config.current request...
+ xPL->sendMessage( xPL_MESSAGE_COMMAND,
+ xPL_getSourceVendor( message ), xPL_getSourceDeviceID( message ), xPL_getSourceInstanceID( message ),
+ "config", "current", command_request );
+ */
+ } else {
+ xPLCache->updateEntry( "device." + source + ".expires", timeConverter( expires ), false );
+ }
}
void deviceManagerClass::processRemove( xPL_MessagePtr message ) const
{
- string source = string("")
- + xPL_getSourceVendor ( message ) + "-"
- + xPL_getSourceDeviceID ( message ) + "."
- + xPL_getSourceInstanceID( message );
+ std::string source = std::string("")
+ + xPL_getSourceVendor ( message ) + "-"
+ + xPL_getSourceDeviceID ( message ) + "."
+ + xPL_getSourceInstanceID( message );
- remove( source );
- removeConfig( source );
+ remove( source );
+ removeConfig( source );
}
-void deviceManagerClass::sendConfigResponse( const string& source, const bool removeOldValue ) const
+void deviceManagerClass::sendConfigResponse( const std::string& source, const bool removeOldValue ) const
{
- writeLog( "deviceManagerClass::sendConfigResponse("+source+", "+(removeOldValue?"true":"false")+")", logLevel::debug );
- boost::regex re( "config\\." + source + "\\.current\\.([a-z0-9]{1,16})(?:\\{([0-9]{1,3})\\})?" );
- vector<string> entries = xPLCache->filterByRegEx( re );
- xPLMessage::namedValueList list;
- for( vector<string>::const_iterator it = entries.begin(); it != entries.end(); ++it )
- {
- boost::smatch matches;
- boost::regex_match( *it, matches, re );
- writeLog( "deviceManagerClass::sendConfigResponse: ["+*it+"] -> ["+matches[1]+"]("+matches[2]+")",logLevel::debug );
- if( matches.size() > 1 )
- list.push_back( make_pair( matches[1], xPLCache->objectValue( *it ) ) );
- }
+ writeLog( "deviceManagerClass::sendConfigResponse("+source+", "+(removeOldValue?"true":"false")+")", logLevel::debug );
+ boost::regex re( "config\\." + source + "\\.current\\.([a-z0-9]{1,16})(?:\\{([0-9]{1,3})\\})?" );
+ std::vector<std::string> entries = xPLCache->filterByRegEx( re );
+ xPLMessage::namedValueList list;
+// for( std::vector<std::string>::const_iterator it = entries.begin(); it != entries.end(); ++it )
+ for( auto entry : entries) {
+ boost::smatch matches;
+ boost::regex_match( entry, matches, re );
+ writeLog( "deviceManagerClass::sendConfigResponse: ["+entry+"] -> ["+matches[1]+"]("+matches[2]+")",logLevel::debug );
+ if( matches.size() > 1 ) {
+ list.push_back( std::make_pair( matches[1], xPLCache->objectValue( entry ) ) );
+ }
+ }
- if( list.size() > 0 )
- {
- xPL->sendMessage( xPL_MESSAGE_COMMAND, source, "config", "response", list );
- xPLCache->updateEntry( "config." + source + ".configmissing", "false", false );
- xPLCache->updateEntry( "config." + source + ".waitingconfig", "false", false );
- xPLCache->updateEntry( "config." + source + ".configdone" , "true" , false ); // FIXME the VB code says "false"...
- }
+ if( list.size() > 0 ) {
+ xPL->sendMessage( xPL_MESSAGE_COMMAND, source, "config", "response", list );
+ xPLCache->updateEntry( "config." + source + ".configmissing", "false", false );
+ xPLCache->updateEntry( "config." + source + ".waitingconfig", "false", false );
+ xPLCache->updateEntry( "config." + source + ".configdone" , "true" , false ); // FIXME the VB code says "false"...
+ }
- if( removeOldValue )
- {
- remove( source );
- removeConfig( source );
- }
+ if( removeOldValue ) {
+ remove( source );
+ removeConfig( source );
+ }
}
-bool deviceManagerClass::storeNewConfig( const string& source, const string& config ) const
+bool deviceManagerClass::storeNewConfig( const std::string& source, const std::string& config ) const
{
- writeLog( "deviceManagerClass::storeNewConfig("+source+", "+config+")", logLevel::debug );
- if( !contains( source ) )
- return false;
+ writeLog( "deviceManagerClass::storeNewConfig("+source+", "+config+")", logLevel::debug );
+ if( !contains( source ) ) {
+ return false;
+ }
- vector<string> list;
- split( list, config, is_any_of("\r\n"), token_compress_on );
- string multiKey;
- int multiCount = 0;
- for( vector<string>::const_iterator it = list.begin(); it != list.end(); ++it )
- {
- vector<string> tags;
- split( tags, *it, is_any_of("=") );
- to_lower( tags[0] );
- to_lower( tags[1] );
- string count = xPLCache->objectValue( "config." + source + ".options." + tags[0] + ".count" );
- if( "" != count )
- {
- if( multiKey != tags[0] )
- {
- multiKey = tags[0];
- multiCount = 0;
- } else
- multiCount++;
+ std::vector<std::string> list;
+ split( list, config, is_any_of("\r\n"), token_compress_on );
+ std::string multiKey;
+ int multiCount = 0;
+// for( std::vector<std::string>::const_iterator it = list.begin(); it != list.end(); ++it )
+ for( auto it : list) {
+ std::vector<std::string> tags;
+ split( tags, it, is_any_of("=") );
+ to_lower( tags[0] );
+ to_lower( tags[1] );
+ std::string count = xPLCache->objectValue( "config." + source + ".options." + tags[0] + ".count" );
+ if( "" != count ) {
+ if( multiKey != tags[0] ) {
+ multiKey = tags[0];
+ multiCount = 0;
+ } else {
+ multiCount++;
+ }
- char buf[6];
- snprintf( buf, 6, "{%.3d}", multiCount );
- tags[0] += buf;
+ char buf[6];
+ snprintf( buf, 6, "{%.3d}", multiCount );
+ tags[0] += buf;
+ }
+ xPLCache->updateEntry( "config." + source + ".current." + tags[0], tags[1], false );
}
- xPLCache->updateEntry( "config." + source + ".current." + tags[0], tags[1], false );
- }
- if( xPLCache->childNodes( "config." + source + ".current" ).size() > 0 )
- {
- sendConfigResponse( source, false ); // FIXME the VB said true - but why should I delete the values that I've just stored?!?
- }
- return true;
+ if( xPLCache->childNodes( "config." + source + ".current" ).size() > 0 ) {
+ sendConfigResponse( source, false ); // FIXME the VB said true - but why should I delete the values that I've just stored?
+ }
+ return true;
}
Modified: xPLHAL/branches/thomas_s_dev/src/devicemanager.h
===================================================================
--- xPLHAL/branches/thomas_s_dev/src/devicemanager.h 2011-10-17 20:29:50 UTC (rev 452)
+++ xPLHAL/branches/thomas_s_dev/src/devicemanager.h 2011-10-17 21:07:20 UTC (rev 453)
@@ -20,52 +20,34 @@
#define DEVICEMANAGER_H
#include <string>
-#include <boost/date_time/posix_time/posix_time.hpp>
-using namespace std;
-using namespace boost::posix_time;
-
// this is also including the xPL.h with its data structures:
#include "xplhandler.h"
#include "xplcache.h"
#include "globals.h"
+#include "xpldevice.h"
-struct xPLDevice
-{
- string VDI; /** \brief vendor / device / instance = unique id */
- ptime Expires; /** \brief time expires */
- int Interval; /** \brief current heartbeat interval */
- bool ConfigType; /** \brief true = config. false = hbeat. */
- bool ConfigDone; /** \brief false = new waiting check, true = sent/not required */
- bool WaitingConfig; /** \brief false = waiting check or not needed, true = manual intervention */
- bool ConfigListSent; /** \brief Have we asked this device for it's config? */
- string ConfigSource; /** \brief v-d.xml / v-d.cache.xml or empty */
- bool ConfigMissing; /** \brief true = no config file, no response from device, false = have/waiting config */
- bool Suspended; /** \brief lost heartbeat */
- bool Current; /** \brief asked for current */
-};
-
class deviceManagerClass
{
public:
/** \brief Looks if the device deviceTag is known. */
- bool contains( const string& deviceTag ) const
+ bool contains( const std::string& deviceTag ) const
{ return xPLCache->childNodes( "device." + deviceTag ).size() > 0; }
/** \brief Looks if the config configTag is known. */
- bool containsConfig( const string& configTag ) const
+ bool containsConfig( const std::string& configTag ) const
{ return xPLCache->childNodes( "config." + configTag ).size() > 0; }
void add( const xPLDevice& device ) const;
- bool remove( const string& deviceTag ) const;
- bool removeConfig( const string& deviceTag ) const;
+ bool remove( const std::string& deviceTag ) const;
+ bool removeConfig( const std::string& deviceTag ) const;
/** \brief Returns the device deviceTag when it's known or an empty device */
- xPLDevice getDevice( const string& deviceTag ) const;
+ xPLDevice getDevice( const std::string& deviceTag ) const;
/** \brief Returns all known device names */
- vector<string> getAllDeviceNames() const;
+ std::vector<std::string> getAllDeviceNames() const;
void update( const xPLDevice& device ) const;
@@ -80,10 +62,10 @@
void processHeartbeat ( const xPL_MessagePtr message ) const;
void processRemove ( const xPL_MessagePtr message ) const;
- void sendConfigResponse( const string& source, const bool removeOldValue ) const;
+ void sendConfigResponse( const std::string& source, const bool removeOldValue ) const;
/** \brief A new configuration arrived via XHCP, handle it... */
- bool storeNewConfig( const string& source, const string& config ) const;
+ bool storeNewConfig( const std::string& source, const std::string& config ) const;
};
#endif // DEVICEMANAGER_H
Modified: xPLHAL/branches/thomas_s_dev/src/globals.h
===================================================================
--- xPLHAL/branches/thomas_s_dev/src/globals.h 2011-10-17 20:29:50 UTC (rev 452)
+++ xPLHAL/branches/thomas_s_dev/src/globals.h 2011-10-17 21:07:20 UTC (rev 453)
@@ -20,7 +20,6 @@
#define GLOBALS_H
#include "boost/filesystem.hpp"
-using namespace boost::filesystem;
/**
* Define all global variables:
@@ -41,9 +40,9 @@
extern xPLMessageQueueClass *xPLMessageQueue;
/** The working directories */
-extern path xPLHalRootFolder;
-extern path DataFileFolder;
-extern path ScriptEngineFolder;
-extern path rulesFolder;
+extern boost::filesystem::path xPLHalRootFolder;
+extern boost::filesystem::path DataFileFolder;
+extern boost::filesystem::path ScriptEngineFolder;
+extern boost::filesystem::path rulesFolder;
#endif // GLOBALS_H
Added: xPLHAL/branches/thomas_s_dev/src/i_properties.h
===================================================================
--- xPLHAL/branches/thomas_s_dev/src/i_properties.h (rev 0)
+++ xPLHAL/branches/thomas_s_dev/src/i_properties.h 2011-10-17 21:07:20 UTC (rev 453)
@@ -0,0 +1,23 @@
+#ifndef __I_PROPERTIES_H__
+#define __I_PROPERTIES_H__
+
+//! \brief Properties Interface
+class IProperties
+{
+ public:
+ virtual ~IProperties() {};
+
+ /** \returns true if the element name exists. */
+ virtual bool hasValue(const std::string& name) const = 0;
+
+ /** \returns the value of element name if it exists - or an empty std::string otherwise. */
+ virtual std::string getValue(const std::string& name) const = 0;
+
+ /** \brief sets value of property */
+ virtual void setValue(const std::string& name, const std::string& value) = 0;
+
+ /** \brief deletes property */
+ virtual void deleteProperty(const std::string& name) = 0;
+};
+
+#endif
Modified: xPLHAL/branches/thomas_s_dev/src/main.cpp
===================================================================
--- xPLHAL/branches/thomas_s_dev/src/main.cpp 2011-10-17 20:29:50 UTC (rev 452)
+++ xPLHAL/branches/thomas_s_dev/src/main.cpp 2011-10-17 21:07:20 UTC (rev 453)
@@ -16,6 +16,7 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
+#include <signal.h>
#include "log.h"
#include "xplcache.h"
#include "devicemanager.h"
@@ -23,6 +24,7 @@
// load globas and give them their space to live
#include "globals.h"
+using namespace boost::filesystem;
path xPLHalRootFolder;
path DataFileFolder;
@@ -34,91 +36,119 @@
xPLHandler *xPL;
xPLMessageQueueClass *xPLMessageQueue;
-//bool xPLHandlerInitialized = false;
+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();
+ }
+}
+
/**
* Setup the whole program.
*/
int main(int UNUSED argc, char** UNUSED argv)
{
- /** Make sure the necessary infrastructure exists: */
- xPLHalRootFolder = initial_path();
- DataFileFolder = xPLHalRootFolder / "data";
- ScriptEngineFolder = DataFileFolder / "scripts";
- rulesFolder = DataFileFolder / "determinator";
- //vendorFileFolder = DataFileFolder / "vendors";
- //ConfigFileFolder = DataFileFolder / "configs";
+ /** Make sure the necessary infrastructure exists: */
+ xPLHalRootFolder = initial_path();
+ DataFileFolder = xPLHalRootFolder / "data";
+ ScriptEngineFolder = DataFileFolder / "scripts";
+ rulesFolder = DataFileFolder / "determinator";
+ //vendorFileFolder = DataFileFolder / "vendors";
+ //ConfigFileFolder = DataFileFolder / "configs";
- // FIXME : add exception handling for directory operations!!!
- if(false)//FIXME if( !QDir::setCurrent( xPLHalRootFolder.path() ) )
- {
- writeLog( "Error changing to working directory \"" + xPLHalRootFolder.string() + "\"!", logLevel::error );
- return -1;
- }
+ signal(SIGINT, handle_signal);
+ signal(SIGTERM, handle_signal);
- if( !exists( DataFileFolder ) )
- {
- writeLog( "Directory \"" + DataFileFolder.string() + "\" for DataFileFolder doesn't exist. Creating it...", logLevel::debug );
- if( !create_directory( DataFileFolder ) )
+ // FIXME : add exception handling for directory operations!!!
+ if(false)//FIXME if( !QDir::setCurrent( xPLHalRootFolder.path() ) )
{
- writeLog( "Error creating data directory \"" + DataFileFolder.string() + "\"!", logLevel::error );
- return -1;
+ writeLog( "Error changing to working directory \"" + xPLHalRootFolder.string() + "\"!", logLevel::error );
+ return -1;
}
- }
- if( !exists( ScriptEngineFolder ) )
- {
- writeLog( "Directory \"" + ScriptEngineFolder.string() + "\" for ScriptEngineFolder doesn't exist. Creating it...", logLevel::debug );
- if( !create_directory( ScriptEngineFolder ) )
+ if( !exists( DataFileFolder ) )
{
- writeLog( "Error creating script directory \"" + ScriptEngineFolder.string() + "\"!", logLevel::error );
- return -1;
+ writeLog( "Directory \"" + DataFileFolder.string() + "\" for DataFileFolder doesn't exist. Creating it...", logLevel::debug );
+ if( !create_directory( DataFileFolder ) )
+ {
+ writeLog( "Error creating data directory \"" + DataFileFolder.string() + "\"!", logLevel::error );
+ return -1;
+ }
}
- }
- if( !exists( rulesFolder ) )
- {
- writeLog( "Directory \"" + rulesFolder.string() + "\" for rulesFolder doesn't exist. Creating it...", logLevel::debug );
- if( !create_directory( rulesFolder ) )
+ if( !exists( ScriptEngineFolder ) )
{
- writeLog( "Error creating determinator directory \"" + rulesFolder.string() + "\"!", logLevel::error );
- return -1;
+ writeLog( "Directory \"" + ScriptEngineFolder.string() + "\" for ScriptEngineFolder doesn't exist. Creating it...", logLevel::debug );
+ if( !create_directory( ScriptEngineFolder ) )
+ {
+ writeLog( "Error creating script directory \"" + ScriptEngineFolder.string() + "\"!", logLevel::error );
+ return -1;
+ }
}
- }
- xPLMessageQueue = new xPLMessageQueueClass;
- xPLCache = new xPLCacheClass;
- XHCPServer *xhcpServer = new XHCPServer();
- xPL = new xPLHandler( boost::asio::ip::host_name() ); //xPL->start();
- deviceManager = new deviceManagerClass();
- writeLog( "started", logLevel::all );
+ if( !exists( rulesFolder ) )
+ {
+ writeLog( "Directory \"" + rulesFolder.string() + "\" for rulesFolder doesn't exist. Creating it...", logLevel::debug );
+ if( !create_directory( rulesFolder ) )
+ {
+ writeLog( "Error creating determinator directory \"" + rulesFolder.string() + "\"!", logLevel::error );
+ return -1;
+ }
+ }
- // force everyone to send their configuration so that we start up to date...
- xPLMessage::namedValueList command_request; command_request.push_back( make_pair( "command", "request" ) );
- xPL->sendBroadcastMessage( "config", "list", command_request );
+ xPLMessageQueue = new xPLMessageQueueClass;
+ xPLCache = new xPLCacheClass;
+ XHCPServer *xhcpServer = new XHCPServer();
+ xPL = new xPLHandler( boost::asio::ip::host_name() ); //xPL->start();
+ deviceManager = new deviceManagerClass();
+ writeLog( "started", logLevel::all );
- // main loop
- for( int count = 0;; ++count )
- {
- sleep( 10 );
- writeLog( "main: run events", logLevel::all ); // run events
+ // 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 );
- if( 0 == count%6 )
+ // main loop
+ for( int count = 0;; ++count )
{
- writeLog( "main: <tick>", logLevel::all );
- writeLog( "xPLCache:\n" + xPLCache->listAllObjects(), logLevel::debug );
+ {
+ 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
+
+ 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;
+ }
}
- 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
- delete xhcpServer;
- delete deviceManager;
- delete xPLCache;
- delete xPL;
+ // 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/xhcp.cpp
===================================================================
--- xPLHAL/branches/thomas_s_dev/src/xhcp.cpp 2011-10-17 20:29:50 UTC (rev 452)
+++ xPLHAL/branches/thomas_s_dev/src/xhcp.cpp 2011-10-17 21:07:20 UTC (rev 453)
@@ -21,26 +21,50 @@
#include "xhcp.h"
+using boost::asio::ip::tcp;
+
XHCPServer::XHCPServer()
: acceptor(io_service, tcp::endpoint(tcp::v4(), 3865)),
- m_stoprequested(false),
+ m_stoprequested(false),
m_thread(boost::bind(&XHCPServer::waitForConnection, this))
-{}
+{
+ startAccept();
+}
-void XHCPServer::waitForConnection( void )
+XHCPServer::~XHCPServer()
{
- writeLog( "XHCPServer::waitForConnection", logLevel::debug );
- while (!m_stoprequested)
- {
- std::cerr << "for(;;) start \n";
+ m_stoprequested = true;
+ acceptor.cancel();
+ acceptor.close();
+ io_service.stop();
+ m_thread.join();
+}
+
+void XHCPServer::startAccept()
+{
socket_ptr sockPtr(new tcp::socket(io_service));
- std::cerr << "for(;;) 1 \n";
- acceptor.accept(*sockPtr);
- std::cerr << "for(;;) 2 \n";
- //boost::thread t(boost::bind(&XHCPServer::session, this, sock));
- std::cerr << "for(;;) use_count: " << sockPtr.use_count() << "\n";
+ acceptor.async_accept(*sockPtr, boost::bind(&XHCPServer::handleAccept, this, sockPtr));
+}
+
+void XHCPServer::handleAccept(socket_ptr sockPtr)
+{
+ startAccept();
XHCPThread* foo = new XHCPThread( sockPtr );
- std::cerr << "for(;;) foo: " << int(foo) << "\n";
- std::cerr << "for(;;) end \n";
- }
}
+
+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-10-17 20:29:50 UTC (rev 452)
+++ xPLHAL/branches/thomas_s_dev/src/xhcp.h 2011-10-17 21:07:20 UTC (rev 453)
@@ -23,8 +23,8 @@
#include <boost/thread.hpp>
#include "xplcache.h"
+#include "xhcpthread.h"
-using boost::asio::ip::tcp;
/**
* \brief Manage all XHCP connetions.
@@ -32,21 +32,19 @@
class XHCPServer
{
boost::asio::io_service io_service;
- tcp::acceptor acceptor;
+ boost::asio::ip::tcp::tcp::acceptor acceptor;
volatile bool m_stoprequested;
boost::thread m_thread;
public:
XHCPServer();
- ~XHCPServer()
- {
- m_stoprequested = true;
- m_thread.join();
- }
+ ~XHCPServer();
protected:
/** \brief Create a new XHCPThread for a new connection. */
void waitForConnection( void );
+ void handleAccept(socket_ptr sockPtr);
+ void startAccept();
};
#endif // XHCP_H
Modified: xPLHAL/branches/thomas_s_dev/src/xhcpthread.cpp
===================================================================
--- xPLHAL/branches/thomas_s_dev/src/xhcpthread.cpp 2011-10-17 20:29:50 UTC (rev 452)
+++ xPLHAL/branches/thomas_s_dev/src/xhcpthread.cpp 2011-10-17 21:07:20 UTC (rev 453)
@@ -28,13 +28,15 @@
#include "xhcpthread.h"
using namespace boost::algorithm;
+using boost::asio::ip::tcp;
XHCPThread::XHCPThread( socket_ptr socket )
: m_stoprequested(false), tab( "\t" ), newLine( "\r\n" ), endMultiLine( ".\r\n" ),
sock( socket ), quit( false ),
inMultilineRequest( false ), multilineRequestHandler( 0 ),
m_thread(boost::bind(&XHCPThread::run, this))
-{}
+{
+}
XHCPThread::~XHCPThread()
{
@@ -50,17 +52,17 @@
*/
bool endDifferent( std::string str, std::string end )
{
- size_t str_l = str.size();
- size_t end_l = end.size();
- if( str_l < end_l ) return true;
- return str.compare( str_l-end_l, end_l, end );
+ size_t str_l = str.size();
+ size_t end_l = end.size();
+ if( str_l < end_l ) return true;
+ ...
[truncated message content] |