|
From: <tho...@us...> - 2011-12-18 20:39:24
|
Revision: 596
http://openautomation.svn.sourceforge.net/openautomation/?rev=596&view=rev
Author: thomas_s
Date: 2011-12-18 20:39:17 +0000 (Sun, 18 Dec 2011)
Log Message:
-----------
- now using pragma once for header files
- added boost::signals2 for better seperation of classes
- use xPLMessage (C++-Version) for internal use
Modified Paths:
--------------
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/i_properties.h
xPLHAL/branches/thomas_s_dev/src/i_xplcache.h
xPLHAL/branches/thomas_s_dev/src/log.h
xPLHAL/branches/thomas_s_dev/src/main.cpp
xPLHAL/branches/thomas_s_dev/src/response_codes.h
xPLHAL/branches/thomas_s_dev/src/xhcp.h
xPLHAL/branches/thomas_s_dev/src/xhcpthread.h
xPLHAL/branches/thomas_s_dev/src/xplcache.h
xPLHAL/branches/thomas_s_dev/src/xpldevice.h
xPLHAL/branches/thomas_s_dev/src/xplhandler.cpp
xPLHAL/branches/thomas_s_dev/src/xplhandler.h
xPLHAL/branches/thomas_s_dev/src/xplmessage.h
xPLHAL/branches/thomas_s_dev/src/xplmessagequeue.h
Removed Paths:
-------------
xPLHAL/branches/thomas_s_dev/src/i_devicemanager.h
xPLHAL/branches/thomas_s_dev/src/i_xplhandler.h
Modified: xPLHAL/branches/thomas_s_dev/src/devicemanager.cpp
===================================================================
--- xPLHAL/branches/thomas_s_dev/src/devicemanager.cpp 2011-12-18 16:24:30 UTC (rev 595)
+++ xPLHAL/branches/thomas_s_dev/src/devicemanager.cpp 2011-12-18 20:39:17 UTC (rev 596)
@@ -29,12 +29,12 @@
using namespace boost::posix_time;
-deviceManagerClass::deviceManagerClass(IxPLHandler* xplhandler, IxPLCacheClass* xplcache)
-:m_xPL(xplhandler)
-,m_xPLCache(xplcache)
+deviceManagerClass::deviceManagerClass(IxPLCacheClass* xplcache)
+:m_xPLCache(xplcache)
{
}
+
void deviceManagerClass::add( const xPLDevice& device )
{
writeLog("deviceManagerClass::add(" + device.VDI + ")", logLevel::debug);
@@ -90,8 +90,40 @@
}
return retval;
}
+
+void deviceManagerClass::processXplMessage( const xPLMessagePtr message )
+{
+ std::string schema = message->msgClass + std::string(".") + message->msgType;
+ if( message->type != xPL_MESSAGE_COMMAND )
+ {
+ if( "config.list" == schema )
+ {
+ // someone (probably we) have asked for the config list - handle it now...
+ processConfigList( message );
+ } else if( "config.current" == schema )
+ {
+ // someone requested the device to send it's current configuration
+ processCurrentConfig( message );
+ } else if( "config.app" == schema || "config.basic" == schema )
+ {
+ // a new device poped up and wants to be configured
+ processConfigHeartBeat( message );
+ } else if( "hbeat.basic" == schema || "hbeat.app" == schema )
+ {
+ /*
+ If msgSource = MySourceTag Then
+ RaiseEvent AddtoCache("xplhal." & msgSource & ".alive", Now.ToString, False)
+ End If
+ */
+ processHeartbeat( message );
+ } else if( "hbeat.end" == schema )
+ {
+ processRemove( message );
+ }
+ }
+}
-void deviceManagerClass::processConfigList( const xPL_MessagePtr message )
+void deviceManagerClass::processConfigList( const xPLMessagePtr message )
{
std::string source = extractSourceFromXplMessage(message);
xPLDevice device = getDevice( source );
@@ -100,7 +132,9 @@
// A config list turned up that we haven't asked for...
// create a new device...
int interval = 5;
- ptime expires = calculateExpireTime(xPL_getMessageNamedValue(message, "interval"), &interval);
+ //\TODO: implement xPL_getMessageNamedValue
+// ptime expires = calculateExpireTime(xPL_getMessageNamedValue(message, "interval"), &interval);
+ ptime expires = calculateExpireTime("5", &interval);
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
@@ -115,14 +149,12 @@
mDeviceMap[device.VDI] = device;
}
- xPL_NameValueListPtr nvList = xPL_getMessageBody( message );
- xPL_NameValuePairPtr nvPair = NULL;
- int nvCount = xPL_getNamedValueCount(nvList);
- for (int nvIndex = 0; nvIndex < nvCount; nvIndex++ ) {
+ for( auto entry : message->namedValues) {
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 );
+ std::string value = entry.first;
+ to_lower(value);
+ std::string key = entry.second;
+ to_lower(key);
boost::regex re( "([a-z0-9]{1,16})\\[(\\d{1,3})\\]" );
boost::smatch matches;
if (boost::regex_match(value, matches, re)) {
@@ -145,9 +177,13 @@
else {
// try to get at least the current config
xPLMessage::namedValueList command_request; command_request.push_back( std::make_pair( "command", "request" ) );
- m_xPL->sendMessage( xPL_MESSAGE_COMMAND,
- xPL_getSourceVendor( message ), xPL_getSourceDeviceID( message ), xPL_getSourceInstanceID( message ),
- "config", "current", command_request );
+ xPLMessagePtr msg( new xPLMessage(xPL_MESSAGE_COMMAND,
+ message->vendor,
+ message->deviceID,
+ message->instanceID,
+ "config", "current",
+ command_request) );
+ m_sigSendXplMessage(msg);
}
}
@@ -165,7 +201,7 @@
return second_clock::local_time() + minutes( 2* interval + 1 );
}
-void deviceManagerClass::processConfigHeartBeat( const xPL_MessagePtr message )
+void deviceManagerClass::processConfigHeartBeat( const xPLMessagePtr message )
{
std::string source = extractSourceFromXplMessage(message);
@@ -175,7 +211,9 @@
// 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.
int interval = 5;
- ptime expires = calculateExpireTime(xPL_getMessageNamedValue(message, "interval"), &interval);
+ //\TODO
+// ptime expires = calculateExpireTime(xPL_getMessageNamedValue(message, "interval"), &interval);
+ ptime expires = calculateExpireTime("5", &interval);
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
@@ -198,14 +236,18 @@
// 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" ) );
- m_xPL->sendMessage( xPL_MESSAGE_COMMAND,
- xPL_getSourceVendor( message ), xPL_getSourceDeviceID( message ), xPL_getSourceInstanceID( message ),
- "config", "list", command_request );
+ xPLMessagePtr msg( new xPLMessage(xPL_MESSAGE_COMMAND,
+ message->vendor,
+ message->deviceID,
+ message->instanceID,
+ "config", "list",
+ command_request) );
+ m_sigSendXplMessage(msg);
mDeviceMap[device.VDI].ConfigListSent = true;
}
}
-void deviceManagerClass::processCurrentConfig( const xPL_MessagePtr message )
+void deviceManagerClass::processCurrentConfig( const xPLMessagePtr message )
{
std::string source = extractSourceFromXplMessage(message);
xPLDevice device = getDevice( source );
@@ -220,16 +262,14 @@
// m_xPLCache->updateEntry( "config." + source + ".expires", timeConverter(calculateExpireTime(device.Interval), false ));
m_xPLCache->updateEntry( "config." + source + ".current", "true", false );
}
-
- 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 );
+ int multiCount = 0;
+ for( auto entry : message->namedValues) {
+ std::string value = entry.first;
+ to_lower(value);
+ std::string key = entry.second;
+ to_lower(key);
std::string count = m_xPLCache->objectValue( "config." + source + ".options." + key + ".count" );
if( "" != count ) {
if( multiKey == key ) {
@@ -253,14 +293,22 @@
+ xPL_getSourceInstanceID( message );
}
+std::string deviceManagerClass::extractSourceFromXplMessage( xPLMessagePtr message )
+{
+ return std::string(message->vendor + "-"
+ + message->deviceID + "."
+ + message->instanceID);
+}
-void deviceManagerClass::processHeartbeat( xPL_MessagePtr message )
+void deviceManagerClass::processHeartbeat( xPLMessagePtr message )
{
std::string source = extractSourceFromXplMessage(message);
xPLDevice device = getDevice( source );
int interval = 5;
- ptime expires = calculateExpireTime(xPL_getMessageNamedValue(message, "interval"), &interval);
+ //\TODO
+// ptime expires = calculateExpireTime(xPL_getMessageNamedValue(message, "interval"), &interval);
+ ptime expires = calculateExpireTime("5", &interval);
writeLog( "deviceManagerClass::processHeartbeat("+source+") - found ["+device.VDI+"]", logLevel::debug );
if( "" == device.VDI ) {
@@ -281,9 +329,13 @@
// Throw it a config request anyway, see what turns up..
xPLMessage::namedValueList command_request; command_request.push_back( std::make_pair( "command", "request" ) );
- m_xPL->sendMessage( xPL_MESSAGE_COMMAND,
- xPL_getSourceVendor( message ), xPL_getSourceDeviceID( message ), xPL_getSourceInstanceID( message ),
- "config", "list" , command_request );
+ xPLMessagePtr msg( new xPLMessage(xPL_MESSAGE_COMMAND,
+ message->vendor,
+ message->deviceID,
+ message->instanceID,
+ "config", "list",
+ command_request) );
+ m_sigSendXplMessage(msg);
/* // that code below was from the VB - but we don't need it as processConfigList will
// itself send the config.current request...
m_xPL->sendMessage( xPL_MESSAGE_COMMAND,
@@ -295,7 +347,7 @@
}
}
-void deviceManagerClass::processRemove( xPL_MessagePtr message )
+void deviceManagerClass::processRemove( xPLMessagePtr message )
{
std::string source = extractSourceFromXplMessage(message);
@@ -319,7 +371,18 @@
}
if( list.size() > 0 ) {
- m_xPL->sendMessage( xPL_MESSAGE_COMMAND, source, "config", "response", list );
+ size_t marker1 = source.find( "-" );
+ size_t marker2 = source.find( "." );
+ std::string vendor = source.substr( 0, marker1 );
+ std::string device = source.substr( marker1+1, marker2 - (marker1+1) );
+ std::string instance = source.substr( marker2+1 );
+ xPLMessagePtr msg( new xPLMessage(xPL_MESSAGE_COMMAND,
+ vendor,
+ device,
+ instance,
+ "config", "response",
+ list) );
+ m_sigSendXplMessage(msg);
m_xPLCache->updateEntry( "config." + source + ".configmissing", "false", false );
m_xPLCache->updateEntry( "config." + source + ".waitingconfig", "false", false );
m_xPLCache->updateEntry( "config." + source + ".configdone" , "true" , false ); // FIXME the VB code says "false"...
@@ -379,3 +442,7 @@
return m_xPLCache->childNodes( "config." + configTag ).size() > 0;
}
+boost::signals2::connection deviceManagerClass::connect(const xPLHandler::signal_t::slot_type &subscriber)
+{
+ return m_sigSendXplMessage.connect(subscriber);
+}
Modified: xPLHAL/branches/thomas_s_dev/src/devicemanager.h
===================================================================
--- xPLHAL/branches/thomas_s_dev/src/devicemanager.h 2011-12-18 16:24:30 UTC (rev 595)
+++ xPLHAL/branches/thomas_s_dev/src/devicemanager.h 2011-12-18 20:39:17 UTC (rev 596)
@@ -1,3 +1,4 @@
+#pragma once
/*
xPLHAL implementation in C++
Copyright (C) 2009 by Christian Mayer - xpl at ChristianMayer dot de
@@ -16,27 +17,22 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-#ifndef DEVICEMANAGER_H
-#define DEVICEMANAGER_H
-
#include <string>
#include <map>
-// this is also including the xPL.h with its data structures:
-#include "xplhandler.h"
-
-class IxPLHandler;
class IxPLCacheClass;
+#include "xplhandler.h"
#include "xplcache.h"
#include "globals.h"
#include "xpldevice.h"
-#include "i_devicemanager.h"
-class deviceManagerClass: public IdeviceManagerClass
+class deviceManagerClass
{
public:
- deviceManagerClass(IxPLHandler* xplhandler, IxPLCacheClass* xplcache);
+ deviceManagerClass(IxPLCacheClass* xplcache);
+
+ boost::signals2::connection connect(const xPLHandler::signal_t::slot_type &subscriber);
/** \brief Looks if the device deviceTag is known. */
bool contains( const std::string& deviceTag ) const;
@@ -54,16 +50,19 @@
/** \brief Returns all known device names */
std::vector<std::string> getAllDeviceNames() const;
+ /** \brief process incoming xpl messages and check if they are for us */
+ void processXplMessage( const xPLMessagePtr message );
+
/** \brief Handle the returned config list that someone (porbably we) have asked for */
- void processConfigList ( const xPL_MessagePtr message );
+ void processConfigList ( const xPLMessagePtr message );
/** \brief Called when a new device poped up and is waiting to be configured */
- void processConfigHeartBeat( const xPL_MessagePtr message );
+ void processConfigHeartBeat( const xPLMessagePtr message );
/** \brief Called when a device sends its configuration */
- void processCurrentConfig ( const xPL_MessagePtr message );
- void processHeartbeat ( const xPL_MessagePtr message );
- void processRemove ( const xPL_MessagePtr message );
+ void processCurrentConfig ( const xPLMessagePtr message );
+ void processHeartbeat ( const xPLMessagePtr message );
+ void processRemove ( const xPLMessagePtr message );
void sendConfigResponse( const std::string& source, const bool removeOldValue );
@@ -73,12 +72,12 @@
private:
/*! \TODO: move to correct class, maybe xplMessage */
std::string extractSourceFromXplMessage( xPL_MessagePtr message );
+ std::string extractSourceFromXplMessage( xPLMessagePtr message );
+
boost::posix_time::ptime calculateExpireTime(int interval);
boost::posix_time::ptime calculateExpireTime(const char* string_interval, int *pInterval = 0);
- IxPLHandler* m_xPL;
IxPLCacheClass* m_xPLCache;
std::map<std::string, xPLDevice> mDeviceMap;
+ xPLHandler::signal_t m_sigSendXplMessage;
};
-
-#endif // DEVICEMANAGER_H
Modified: xPLHAL/branches/thomas_s_dev/src/globals.h
===================================================================
--- xPLHAL/branches/thomas_s_dev/src/globals.h 2011-12-18 16:24:30 UTC (rev 595)
+++ xPLHAL/branches/thomas_s_dev/src/globals.h 2011-12-18 20:39:17 UTC (rev 596)
@@ -1,3 +1,4 @@
+#pragma once
/*
xPLHAL implementation in C++
Copyright (C) 2009 by Christian Mayer - xpl at ChristianMayer dot de
@@ -16,9 +17,6 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-#ifndef GLOBALS_H
-#define GLOBALS_H
-
#include "boost/filesystem.hpp"
/**
@@ -44,5 +42,3 @@
extern boost::filesystem::path DataFileFolder;
extern boost::filesystem::path ScriptEngineFolder;
extern boost::filesystem::path rulesFolder;
-
-#endif // GLOBALS_H
Deleted: xPLHAL/branches/thomas_s_dev/src/i_devicemanager.h
===================================================================
--- xPLHAL/branches/thomas_s_dev/src/i_devicemanager.h 2011-12-18 16:24:30 UTC (rev 595)
+++ xPLHAL/branches/thomas_s_dev/src/i_devicemanager.h 2011-12-18 20:39:17 UTC (rev 596)
@@ -1,42 +0,0 @@
-/*
- 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/>.
-*/
-
-#ifndef IDEVICEMANAGER_H
-#define IDEVICEMANAGER_H
-
-// this is also including the xPL.h with its data structures:
-#include "xplhandler.h"
-
-class IdeviceManagerClass
-{
- public:
- virtual ~IdeviceManagerClass() {}
-
- /** \brief Handle the returned config list that someone (porbably we) have asked for */
- virtual void processConfigList ( const xPL_MessagePtr message ) = 0;
-
- /** \brief Called when a new device poped up and is waiting to be configured */
- virtual void processConfigHeartBeat( const xPL_MessagePtr message ) = 0;
-
- /** \brief Called when a device sends its configuration */
- virtual void processCurrentConfig ( const xPL_MessagePtr message ) = 0;
- virtual void processHeartbeat ( const xPL_MessagePtr message ) = 0;
- virtual void processRemove ( const xPL_MessagePtr message ) = 0;
-};
-
-#endif // IDEVICEMANAGER_H
Modified: xPLHAL/branches/thomas_s_dev/src/i_properties.h
===================================================================
--- xPLHAL/branches/thomas_s_dev/src/i_properties.h 2011-12-18 16:24:30 UTC (rev 595)
+++ xPLHAL/branches/thomas_s_dev/src/i_properties.h 2011-12-18 20:39:17 UTC (rev 596)
@@ -1,5 +1,4 @@
-#ifndef __I_PROPERTIES_H__
-#define __I_PROPERTIES_H__
+#pragma once
//! \brief Properties Interface
class IProperties
@@ -19,5 +18,3 @@
/** \brief deletes property */
virtual void deleteProperty(const std::string& name) = 0;
};
-
-#endif
Modified: xPLHAL/branches/thomas_s_dev/src/i_xplcache.h
===================================================================
--- xPLHAL/branches/thomas_s_dev/src/i_xplcache.h 2011-12-18 16:24:30 UTC (rev 595)
+++ xPLHAL/branches/thomas_s_dev/src/i_xplcache.h 2011-12-18 20:39:17 UTC (rev 596)
@@ -1,3 +1,4 @@
+#pragma once
/*
xPLHAL implementation in C++
Copyright (C) 2009 by Christian Mayer - xpl at ChristianMayer dot de
@@ -16,9 +17,6 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-#ifndef IXPLCACHE_H
-#define IXPLCACHE_H
-
#include <string>
#include <vector>
#include <boost/regex.hpp>
@@ -62,5 +60,3 @@
/** \brief Save object cache to file system */
virtual void saveCache( void ) const = 0;
};
-
-#endif // IXPLCACHE_H
Deleted: xPLHAL/branches/thomas_s_dev/src/i_xplhandler.h
===================================================================
--- xPLHAL/branches/thomas_s_dev/src/i_xplhandler.h 2011-12-18 16:24:30 UTC (rev 595)
+++ xPLHAL/branches/thomas_s_dev/src/i_xplhandler.h 2011-12-18 20:39:17 UTC (rev 596)
@@ -1,50 +0,0 @@
-/*
- 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/>.
-*/
-
-
-#ifndef IXPLHANDLER_H
-#define IXPLHANDLER_H
-
-#include <string>
-
-// this is also including the xPL.h
-#include "xplmessagequeue.h"
-
-/**
- * \brief Handle all xPL communication.
- */
-class IxPLHandler
-{
- public:
- virtual ~IxPLHandler() {}
-
- /** \brief Broadcast one message to the xPL network. */
- virtual void sendBroadcastMessage( const std::string& msgClass, const std::string& msgType, const xPLMessage::namedValueList& namedValues ) const = 0;
-
- /** \brief Send a directed message to the xPL network. */
- virtual void sendMessage( const xPL_MessageType type, const std::string& tgtVendor, const std::string& tgtDeviceID,
- const std::string& tgtInstanceID, const std::string& msgClass, const std::string& msgType,
- const xPLMessage::namedValueList& namedValues ) const = 0;
-
- /** \brief Send a directed message to the xPL network. */
- virtual void sendMessage( const xPL_MessageType type, const std::string& VDI,
- const std::string& msgClass, const std::string& msgType, const xPLMessage::namedValueList& namedValues ) const = 0;
-
-};
-
-#endif // IXPLHANDLER_H
Modified: xPLHAL/branches/thomas_s_dev/src/log.h
===================================================================
--- xPLHAL/branches/thomas_s_dev/src/log.h 2011-12-18 16:24:30 UTC (rev 595)
+++ xPLHAL/branches/thomas_s_dev/src/log.h 2011-12-18 20:39:17 UTC (rev 596)
@@ -1,3 +1,4 @@
+#pragma once
/*
xPLHAL implementation in C++
Copyright (C) 2009 by Christian Mayer - xpl at ChristianMayer dot de
@@ -16,9 +17,6 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-#ifndef LOG_H
-#define LOG_H
-
#include <iostream>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/lexical_cast.hpp>
@@ -119,5 +117,3 @@
doLog( m, level );
}
} static writeLog;
-
-#endif // LOG_H
Modified: xPLHAL/branches/thomas_s_dev/src/main.cpp
===================================================================
--- xPLHAL/branches/thomas_s_dev/src/main.cpp 2011-12-18 16:24:30 UTC (rev 595)
+++ xPLHAL/branches/thomas_s_dev/src/main.cpp 2011-12-18 20:39:17 UTC (rev 596)
@@ -107,8 +107,8 @@
xPLCache = new xPLCacheClass;
XHCPServer *xhcpServer = new XHCPServer();
xPL = new xPLHandler( boost::asio::ip::host_name() ); //xPL->start();
- deviceManager = new deviceManagerClass(xPL, xPLCache);
- xPL->setDeviceManager(deviceManager);
+ deviceManager = new deviceManagerClass(xPLCache);
+ xPL->connect(boost::bind(&deviceManagerClass::processXplMessage, deviceManager, _1));
writeLog( "started", logLevel::all );
// force everyone to send their configuration so that we start up to date...
Modified: xPLHAL/branches/thomas_s_dev/src/response_codes.h
===================================================================
--- xPLHAL/branches/thomas_s_dev/src/response_codes.h 2011-12-18 16:24:30 UTC (rev 595)
+++ xPLHAL/branches/thomas_s_dev/src/response_codes.h 2011-12-18 20:39:17 UTC (rev 596)
@@ -1,3 +1,4 @@
+#pragma once
/*
xPLHAL implementation in C++
Copyright (C) 2009 by Christian Mayer - xpl at ChristianMayer dot de
@@ -16,9 +17,6 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-#ifndef RESPONSE_CODES_H
-#define RESPONSE_CODES_H
-
#include <map>
#include <string>
@@ -108,4 +106,3 @@
std::string operator()( int code ) { return std::string(codes[code]); }
} static responseCode;
-#endif /* RESPONSE_CODES_H */
\ No newline at end of file
Modified: xPLHAL/branches/thomas_s_dev/src/xhcp.h
===================================================================
--- xPLHAL/branches/thomas_s_dev/src/xhcp.h 2011-12-18 16:24:30 UTC (rev 595)
+++ xPLHAL/branches/thomas_s_dev/src/xhcp.h 2011-12-18 20:39:17 UTC (rev 596)
@@ -1,3 +1,4 @@
+#pragma once
/*
xPLHAL implementation in C++
Copyright (C) 2009 by Christian Mayer - xpl at ChristianMayer dot de
@@ -16,9 +17,6 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-#ifndef XHCP_H
-#define XHCP_H
-
#include <boost/asio.hpp>
#include <boost/thread.hpp>
@@ -46,5 +44,3 @@
void handleAccept(socket_ptr sockPtr);
void startAccept();
};
-
-#endif // XHCP_H
Modified: xPLHAL/branches/thomas_s_dev/src/xhcpthread.h
===================================================================
--- xPLHAL/branches/thomas_s_dev/src/xhcpthread.h 2011-12-18 16:24:30 UTC (rev 595)
+++ xPLHAL/branches/thomas_s_dev/src/xhcpthread.h 2011-12-18 20:39:17 UTC (rev 596)
@@ -1,3 +1,4 @@
+#pragma once
/*
xPLHAL implementation in C++
Copyright (C) 2009 by Christian Mayer - xpl at ChristianMayer dot de
@@ -16,9 +17,6 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-#ifndef XHCPTHREAD_H
-#define XHCPTHREAD_H
-
#include <map>
#include <boost/smart_ptr.hpp>
@@ -87,5 +85,3 @@
boost::thread m_thread;
};
-
-#endif // XHCPTHREAD_H
Modified: xPLHAL/branches/thomas_s_dev/src/xplcache.h
===================================================================
--- xPLHAL/branches/thomas_s_dev/src/xplcache.h 2011-12-18 16:24:30 UTC (rev 595)
+++ xPLHAL/branches/thomas_s_dev/src/xplcache.h 2011-12-18 20:39:17 UTC (rev 596)
@@ -1,3 +1,4 @@
+#pragma once
/*
xPLHAL implementation in C++
Copyright (C) 2009 by Christian Mayer - xpl at ChristianMayer dot de
@@ -16,9 +17,6 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-#ifndef XPLCACHE_H
-#define XPLCACHE_H
-
#include <string>
#include <vector>
#include <map>
@@ -155,5 +153,3 @@
/** \brief Save object cache to file system */
void saveCache( void ) const;
};
-
-#endif // XPLCACHE_H
Modified: xPLHAL/branches/thomas_s_dev/src/xpldevice.h
===================================================================
--- xPLHAL/branches/thomas_s_dev/src/xpldevice.h 2011-12-18 16:24:30 UTC (rev 595)
+++ xPLHAL/branches/thomas_s_dev/src/xpldevice.h 2011-12-18 20:39:17 UTC (rev 596)
@@ -1,3 +1,4 @@
+#pragma once
/*
xPLHAL implementation in C++
Copyright (C) 2009 by Christian Mayer - xpl at ChristianMayer dot de
@@ -16,9 +17,6 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-#ifndef __XPLDEVICE_H__
-#define __XPLDEVICE_H__
-
#include <string>
#include <boost/date_time/posix_time/posix_time.hpp>
@@ -36,5 +34,3 @@
bool Suspended; /** \brief lost heartbeat */
bool Current; /** \brief asked for current */
};
-
-#endif
Modified: xPLHAL/branches/thomas_s_dev/src/xplhandler.cpp
===================================================================
--- xPLHAL/branches/thomas_s_dev/src/xplhandler.cpp 2011-12-18 16:24:30 UTC (rev 595)
+++ xPLHAL/branches/thomas_s_dev/src/xplhandler.cpp 2011-12-18 20:39:17 UTC (rev 596)
@@ -21,7 +21,6 @@
using namespace boost::algorithm;
#include "log.h"
-#include "i_devicemanager.h"
#include "xplhandler.h"
#include "globals.h"
@@ -32,13 +31,12 @@
int xPLHandler::m_refcount = 0;
-xPLHandler::xPLHandler( const std::string& host_name, IdeviceManagerClass* devManager )
+xPLHandler::xPLHandler( const std::string& host_name)
: xPLService(0)
, m_exit_thread(false)
, vendor( "CHRISM" )
, deviceID( "xplhalqt" )
, instanceID( host_name )
-, m_deviceManager(devManager)
{
writeLog( "xPLHandler::xPLHandler( "+host_name+" )", logLevel::debug );
//xPL_setDebugging(TRUE);
@@ -102,11 +100,6 @@
}
}
-void xPLHandler::setDeviceManager(IdeviceManagerClass* devManager)
-{
- m_deviceManager = devManager;
-}
-
void xPLHandler::run()
{
writeLog( "xPLHandler::run()", logLevel::debug );
@@ -164,11 +157,9 @@
std::string result;
/* Source Info */
- result += xPL_getSourceVendor(theMessage);
- result += "-";
- result += xPL_getSourceDeviceID(theMessage);
- result += ".",
- result += xPL_getSourceInstanceID(theMessage);
+ result += xPL_getSourceVendor(theMessage) + std::string("-");
+ result += xPL_getSourceDeviceID(theMessage) + std::string(".");
+ result += xPL_getSourceInstanceID(theMessage);
result += " -> ";
/* Handle various target types */
@@ -179,10 +170,8 @@
result += "XPL-GROUP.";
result += xPL_getTargetGroup(the...
[truncated message content] |
|
From: <tho...@us...> - 2011-12-31 18:17:03
|
Revision: 619
http://openautomation.svn.sourceforge.net/openautomation/?rev=619&view=rev
Author: thomas_s
Date: 2011-12-31 18:16:57 +0000 (Sat, 31 Dec 2011)
Log Message:
-----------
improved creation of xPLMessage objects
Modified Paths:
--------------
xPLHAL/branches/thomas_s_dev/src/devicemanager.cpp
xPLHAL/branches/thomas_s_dev/src/xplmessage.cpp
xPLHAL/branches/thomas_s_dev/src/xplmessage.h
Modified: xPLHAL/branches/thomas_s_dev/src/devicemanager.cpp
===================================================================
--- xPLHAL/branches/thomas_s_dev/src/devicemanager.cpp 2011-12-31 14:53:57 UTC (rev 618)
+++ xPLHAL/branches/thomas_s_dev/src/devicemanager.cpp 2011-12-31 18:16:57 UTC (rev 619)
@@ -172,11 +172,10 @@
}
else {
// try to get at least the current config
- xPLMessage::namedValueList command_request; command_request.push_back( std::make_pair( "command", "request" ) );
xPLMessagePtr msg( new xPLMessage(xPL_MESSAGE_COMMAND,
source,
"config", "current",
- command_request) );
+ {{"command", "request"}}) );
m_sigSendXplMessage(msg);
}
}
@@ -227,11 +226,10 @@
// 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" ) );
xPLMessagePtr msg( new xPLMessage(xPL_MESSAGE_COMMAND,
source,
"config", "list",
- command_request) );
+ {{"command", "request"}}));
m_sigSendXplMessage(msg);
mDeviceMap[device.VDI].ConfigListSent = true;
}
@@ -302,11 +300,10 @@
mDeviceMap[device.VDI] = device;
// Throw it a config request anyway, see what turns up..
- xPLMessage::namedValueList command_request; command_request.push_back( std::make_pair( "command", "request" ) );
xPLMessagePtr msg( new xPLMessage(xPL_MESSAGE_COMMAND,
source,
"config", "list",
- command_request) );
+ {{"command", "request"}} ));
m_sigSendXplMessage(msg);
/* // that code below was from the VB - but we don't need it as processConfigList will
// itself send the config.current request...
Modified: xPLHAL/branches/thomas_s_dev/src/xplmessage.cpp
===================================================================
--- xPLHAL/branches/thomas_s_dev/src/xplmessage.cpp 2011-12-31 14:53:57 UTC (rev 618)
+++ xPLHAL/branches/thomas_s_dev/src/xplmessage.cpp 2011-12-31 18:16:57 UTC (rev 619)
@@ -57,11 +57,17 @@
bool xPLMessage::setTargetFromVDI(const std::string& target)
{
- size_t marker1 = target.find( "-" );
- size_t marker2 = target.find( "." );
- targetVendor = target.substr( 0, marker1 );
- targetDevice = target.substr( marker1+1, marker2 - (marker1+1) );
- targetInstance = target.substr( marker2+1 );
+ if (target == "*") {
+ isBroadcastMessage = true;
+ }
+ else {
+ isBroadcastMessage = false;
+ size_t marker1 = target.find( "-" );
+ size_t marker2 = target.find( "." );
+ targetVendor = target.substr( 0, marker1 );
+ targetDevice = target.substr( marker1+1, marker2 - (marker1+1) );
+ targetInstance = target.substr( marker2+1 );
+ }
return true;
}
@@ -110,6 +116,30 @@
return result;
}
+
+bool xPLMessage::operator==(const xPLMessage& right) const
+{
+ if (type != right.type) return false;
+ if (vendor != right.vendor) return false;
+ if (deviceID != right.deviceID) return false;
+ if (instanceID != right.instanceID) return false;
+ if (msgClass != right.msgClass) return false;
+ if (msgType != right.msgType) return false;
+ if (namedValues != right.namedValues) return false;
+ if (isBroadcastMessage != right.isBroadcastMessage) return false;
+ if (isGroupMessage != right.isGroupMessage) return false;
+ if (targetVendor != right.targetVendor) return false;
+ if (targetDevice != right.targetDevice) return false;
+ if (targetInstance != right.targetInstance) return false;
+ if (targetGroup != right.targetGroup) return false;
+ if (hopcount != right.hopcount) return false;
+ return true;
+}
+
+xPLMessage::operator std::string() const
+{
+ return printXPLMessage();
+}
std::ostream& operator<<(std::ostream& os, const xPLMessage& msg)
{
Modified: xPLHAL/branches/thomas_s_dev/src/xplmessage.h
===================================================================
--- xPLHAL/branches/thomas_s_dev/src/xplmessage.h 2011-12-31 14:53:57 UTC (rev 618)
+++ xPLHAL/branches/thomas_s_dev/src/xplmessage.h 2011-12-31 18:16:57 UTC (rev 619)
@@ -59,6 +59,9 @@
std::string targetGroup;
int hopcount;
+
+ xPLMessage() : hopcount(0), isBroadcastMessage(true), isGroupMessage(false)
+ {}
xPLMessage(
const xPL_MessageType _type,
@@ -69,7 +72,8 @@
const std::string& _msgType,
const namedValueList& _namedValues
) : type(_type), vendor(_vendor), deviceID(_deviceID), instanceID(_instanceID),
- msgClass(_msgClass), msgType(_msgType), namedValues(_namedValues)
+ msgClass(_msgClass), msgType(_msgType), namedValues(_namedValues),
+ hopcount(0), isBroadcastMessage(true), isGroupMessage(false)
{
}
@@ -79,7 +83,8 @@
const std::string& _msgClass,
const std::string& _msgType,
const namedValueList& _namedValues
- ) : type(_type), msgClass(_msgClass), msgType(_msgType), namedValues(_namedValues)
+ ) : type(_type), msgClass(_msgClass), msgType(_msgType), namedValues(_namedValues),
+ hopcount(0), isBroadcastMessage(true), isGroupMessage(false)
{
setSourceFromVDI(_vdi);
}
@@ -93,6 +98,9 @@
bool setTargetFromVDI(const std::string& vdi);
std::string getNamedValue(const std::string& tag) const;
+
+ operator std::string() const;
+ bool operator==(const xPLMessage& right) const;
};
typedef boost::shared_ptr<xPLMessage> xPLMessagePtr;
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
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.
|
|
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.
|
|
From: <tho...@us...> - 2012-01-01 21:57:53
|
Revision: 624
http://openautomation.svn.sourceforge.net/openautomation/?rev=624&view=rev
Author: thomas_s
Date: 2012-01-01 21:57:46 +0000 (Sun, 01 Jan 2012)
Log Message:
-----------
added 'using std::string'
replaced 'using namespace' by 'using <class>'
Modified Paths:
--------------
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/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/xplhandler.cpp
xPLHAL/branches/thomas_s_dev/src/xplmessage.cpp
Modified: xPLHAL/branches/thomas_s_dev/src/devicemanager.cpp
===================================================================
--- xPLHAL/branches/thomas_s_dev/src/devicemanager.cpp 2012-01-01 21:56:16 UTC (rev 623)
+++ xPLHAL/branches/thomas_s_dev/src/devicemanager.cpp 2012-01-01 21:57:46 UTC (rev 624)
@@ -19,23 +19,31 @@
#include <boost/regex.hpp>
#include <cstdio>
-using namespace boost::algorithm;
+using boost::algorithm::to_lower;
+using boost::algorithm::split;
+using boost::algorithm::is_any_of;
+using boost::algorithm::token_compress_on;
#include "log.h"
#include "devicemanager.h"
#include "i_xplcache.h"
-using namespace boost::posix_time;
+using boost::posix_time::ptime;
+using boost::posix_time::second_clock;
+using boost::posix_time::minutes;
-deviceManagerClass::deviceManagerClass(IxPLCacheClass* xplcache)
+using std::string;
+using std::vector;
+
+DeviceManager::DeviceManager(IxPLCacheClass* xplcache)
:m_xPLCache(xplcache)
{
}
-void deviceManagerClass::add( const xPLDevice& device )
+void DeviceManager::add( const xPLDevice& device )
{
- writeLog("deviceManagerClass::add(" + device.VDI + ")", logLevel::debug);
+ writeLog("DeviceManager::add(" + device.VDI + ")", logLevel::debug);
if (contains( device.VDI )) {
mDeviceMap[device.VDI] = device;
} else {
@@ -44,9 +52,9 @@
}
}
-bool deviceManagerClass::remove( const std::string& deviceTag )
+bool DeviceManager::remove( const string& deviceTag )
{
- writeLog( "deviceManagerClass::remove("+deviceTag+")", logLevel::debug );
+ writeLog( "DeviceManager::remove("+deviceTag+")", logLevel::debug );
auto device = mDeviceMap.find(deviceTag);
if (device != mDeviceMap.end()) {
mDeviceMap.erase(device);
@@ -55,11 +63,11 @@
return false;
}
-bool deviceManagerClass::removeConfig( const std::string& deviceTag ) const
+bool DeviceManager::removeConfig( const string& deviceTag ) const
{
- writeLog( "deviceManagerClass::remove("+deviceTag+")", logLevel::debug );
+ writeLog( "DeviceManager::remove("+deviceTag+")", logLevel::debug );
if( containsConfig( deviceTag ) ) {
- std::vector<std::string> list = m_xPLCache->childNodes( "config." + deviceTag );
+ vector<string> list = m_xPLCache->childNodes( "config." + deviceTag );
for (auto listitem : list) {
m_xPLCache->deleteEntry(listitem);
}
@@ -68,7 +76,7 @@
return false;
}
-xPLDevice deviceManagerClass::getDevice( const std::string& deviceTag ) const
+xPLDevice DeviceManager::getDevice( const string& deviceTag ) const
{
xPLDevice retval;
auto device = mDeviceMap.find(deviceTag);
@@ -78,20 +86,20 @@
return retval;
}
-std::vector<std::string> deviceManagerClass::getAllDeviceNames() const
+vector<string> DeviceManager::getAllDeviceNames() const
{
- std::vector<std::string> retval;
+ vector<string> retval;
retval.reserve(mDeviceMap.size());
for (auto node : mDeviceMap) {
retval.push_back(node.first);
- writeLog( "deviceManagerClass::getAllDeviceNames() = "+ node.first, logLevel::debug );
+ writeLog( "DeviceManager::getAllDeviceNames() = "+ node.first, logLevel::debug );
}
return retval;
}
-void deviceManagerClass::processXplMessage( const xPLMessagePtr message )
+void DeviceManager::processXplMessage( const xPLMessagePtr message )
{
- std::string schema = message->msgClass + std::string(".") + message->msgType;
+ string schema = message->msgClass + string(".") + message->msgType;
if( message->type != xPL_MESSAGE_COMMAND )
{
if( "config.list" == schema )
@@ -121,11 +129,11 @@
}
}
-void deviceManagerClass::processConfigList( const xPLMessagePtr message )
+void DeviceManager::processConfigList( const xPLMessagePtr message )
{
- std::string source = message->getSourceVDI();
+ string source = message->getSourceVDI();
xPLDevice device = getDevice( source );
- writeLog( "deviceManagerClass::processConfigList("+source+") - found ["+device.VDI+"]", logLevel::debug );
+ writeLog( "DeviceManager::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...
@@ -146,10 +154,10 @@
}
for( auto entry : message->namedValues) {
- std::string newtag = "config." + source + ".options.";
- std::string value = entry.first;
+ string newtag = "config." + source + ".options.";
+ string value = entry.first;
to_lower(value);
- std::string key = entry.second;
+ string key = entry.second;
to_lower(key);
boost::regex re( "([a-z0-9]{1,16})\\[(\\d{1,3})\\]" );
boost::smatch matches;
@@ -180,7 +188,7 @@
}
}
-ptime deviceManagerClass::calculateExpireTime(const std::string& string_interval, int *pInterval)
+ptime DeviceManager::calculateExpireTime(const string& string_interval, int *pInterval)
{
int interval = string_interval.empty() ? atoi(string_interval.c_str()) : 5; // default to 5 minutes
if (pInterval) {
@@ -189,17 +197,17 @@
return calculateExpireTime(interval);
}
-ptime deviceManagerClass::calculateExpireTime(int interval)
+ptime DeviceManager::calculateExpireTime(int interval)
{
return second_clock::local_time() + minutes( 2* interval + 1 );
}
-void deviceManagerClass::processConfigHeartBeat( const xPLMessagePtr message )
+void DeviceManager::processConfigHeartBeat( const xPLMessagePtr message )
{
- std::string source = message->getSourceVDI();
+ string source = message->getSourceVDI();
xPLDevice device = getDevice( source );
- writeLog( "deviceManagerClass::processConfigHeartBeat("+source+") - found ["+device.VDI+"]", logLevel::debug );
+ writeLog( "DeviceManager::processConfigHeartBeat("+source+") - found ["+device.VDI+"]", logLevel::debug );
if( device.VDI.empty() ) {
// 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.
@@ -235,11 +243,11 @@
}
}
-void deviceManagerClass::processCurrentConfig( const xPLMessagePtr message )
+void DeviceManager::processCurrentConfig( const xPLMessagePtr message )
{
- std::string source = message->getSourceVDI();
+ string source = message->getSourceVDI();
xPLDevice device = getDevice( source );
- writeLog( "deviceManagerClass::processCurrentConfig("+source+") - found ["+device.VDI+"]", logLevel::debug );
+ writeLog( "DeviceManager::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
@@ -251,14 +259,14 @@
m_xPLCache->updateEntry( "config." + source + ".current", "true", false );
}
- std::string multiKey;
+ string multiKey;
int multiCount = 0;
for( auto entry : message->namedValues) {
- std::string value = entry.first;
+ string value = entry.first;
to_lower(value);
- std::string key = entry.second;
+ string key = entry.second;
to_lower(key);
- std::string count = m_xPLCache->objectValue( "config." + source + ".options." + key + ".count" );
+ string count = m_xPLCache->objectValue( "config." + source + ".options." + key + ".count" );
if( "" != count ) {
if( multiKey == key ) {
multiCount++;
@@ -274,14 +282,14 @@
}
}
-void deviceManagerClass::processHeartbeat( xPLMessagePtr message )
+void DeviceManager::processHeartbeat( xPLMessagePtr message )
{
- std::string source = message->getSourceVDI();
+ string source = message->getSourceVDI();
xPLDevice device = getDevice( source );
int interval = 5;
ptime expires = calculateExpireTime(message->getNamedValue("interval"), &interval);
- writeLog( "deviceManagerClass::processHeartbeat("+source+") - found ["+device.VDI+"]", logLevel::debug );
+ writeLog( "DeviceManager::processHeartbeat("+source+") - found ["+device.VDI+"]", logLevel::debug );
if( "" == device.VDI ) {
// this handles a new application that identifies itself with a hbeat straight away.
@@ -316,24 +324,24 @@
}
}
-void deviceManagerClass::processRemove( xPLMessagePtr message )
+void DeviceManager::processRemove( xPLMessagePtr message )
{
- std::string source = message->getSourceVDI();
+ string source = message->getSourceVDI();
remove( source );
removeConfig( source );
}
-void deviceManagerClass::sendConfigResponse( const std::string& source, const bool removeOldValue )
+void DeviceManager::sendConfigResponse( const string& source, const bool removeOldValue )
{
- writeLog( "deviceManagerClass::sendConfigResponse("+source+", "+(removeOldValue?"true":"false")+")", logLevel::debug );
+ writeLog( "DeviceManager::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 = m_xPLCache->filterByRegEx( re );
+ vector<string> entries = m_xPLCache->filterByRegEx( re );
xPLMessage::namedValueList list;
for( auto entry : entries) {
boost::smatch matches;
boost::regex_match( entry, matches, re );
- writeLog( "deviceManagerClass::sendConfigResponse: ["+entry+"] -> ["+matches[1]+"]("+matches[2]+")",logLevel::debug );
+ writeLog( "DeviceManager::sendConfigResponse: ["+entry+"] -> ["+matches[1]+"]("+matches[2]+")",logLevel::debug );
if( matches.size() > 1 ) {
list.push_back( std::make_pair( matches[1], m_xPLCache->objectValue( entry ) ) );
}
@@ -356,24 +364,24 @@
}
}
-bool deviceManagerClass::storeNewConfig( const std::string& source, const std::string& config )
+bool DeviceManager::storeNewConfig( const string& source, const string& config )
{
- writeLog( "deviceManagerClass::storeNewConfig("+source+", "+config+")", logLevel::debug );
+ writeLog( "DeviceManager::storeNewConfig("+source+", "+config+")", logLevel::debug );
auto device = mDeviceMap.find(source);
if (device == mDeviceMap.end()) {
return false;
}
- std::vector<std::string> list;
+ vector<string> list;
split( list, config, is_any_of("\r\n"), token_compress_on );
- std::string multiKey;
+ string multiKey;
int multiCount = 0;
for( auto listitem : list) {
- std::vector<std::string> tags;
+ vector<string> tags;
split( tags, listitem, is_any_of("=") );
to_lower( tags[0] );
to_lower( tags[1] );
- std::string count = m_xPLCache->objectValue( "config." + source + ".options." + tags[0] + ".count" );
+ string count = m_xPLCache->objectValue( "config." + source + ".options." + tags[0] + ".count" );
if( "" != count ) {
if( multiKey != tags[0] ) {
multiKey = tags[0];
@@ -394,12 +402,12 @@
return true;
}
-bool deviceManagerClass::contains( const std::string& deviceTag ) const
+bool DeviceManager::contains( const string& deviceTag ) const
{
return mDeviceMap.find(deviceTag) != mDeviceMap.end();
}
-bool deviceManagerClass::containsConfig( const std::string& configTag ) const
+bool DeviceManager::containsConfig( const string& configTag ) const
{
return m_xPLCache->childNodes( "config." + configTag ).size() > 0;
}
Modified: xPLHAL/branches/thomas_s_dev/src/devicemanager.h
===================================================================
--- xPLHAL/branches/thomas_s_dev/src/devicemanager.h 2012-01-01 21:56:16 UTC (rev 623)
+++ xPLHAL/branches/thomas_s_dev/src/devicemanager.h 2012-01-01 21:57:46 UTC (rev 624)
@@ -27,10 +27,10 @@
#include "globals.h"
#include "xpldevice.h"
-class deviceManagerClass
+class DeviceManager
{
public:
- deviceManagerClass(IxPLCacheClass* xplcache);
+ DeviceManager(IxPLCacheClass* xplcache);
/** \brief Looks if the device deviceTag is known. */
bool contains( const std::string& deviceTag ) const;
Modified: xPLHAL/branches/thomas_s_dev/src/globals.h
===================================================================
--- xPLHAL/branches/thomas_s_dev/src/globals.h 2012-01-01 21:56:16 UTC (rev 623)
+++ xPLHAL/branches/thomas_s_dev/src/globals.h 2012-01-01 21:57:46 UTC (rev 624)
@@ -28,9 +28,6 @@
class xPLCacheClass;
extern xPLCacheClass *xPLCache;
-class deviceManagerClass;
-extern deviceManagerClass *deviceManager;
-
class xPLHandler;
extern xPLHandler *xPL;
Modified: xPLHAL/branches/thomas_s_dev/src/xhcp.cpp
===================================================================
--- xPLHAL/branches/thomas_s_dev/src/xhcp.cpp 2012-01-01 21:56:16 UTC (rev 623)
+++ xPLHAL/branches/thomas_s_dev/src/xhcp.cpp 2012-01-01 21:57:46 UTC (rev 624)
@@ -23,9 +23,10 @@
using boost::asio::ip::tcp;
-XHCPServer::XHCPServer(boost::asio::io_service& io)
+XHCPServer::XHCPServer(boost::asio::io_service& io, DeviceManager* dm)
:m_io(io)
,m_acceptor(io, tcp::endpoint(tcp::v4(), 3865))
+,m_dm(dm)
{
startAccept();
}
@@ -45,6 +46,6 @@
void XHCPServer::handleAccept(socket_ptr sockPtr)
{
startAccept();
- XHCPThread* foo = new XHCPThread( sockPtr );
+ XHCPThread* foo = new XHCPThread(sockPtr, m_dm);
}
Modified: xPLHAL/branches/thomas_s_dev/src/xhcp.h
===================================================================
--- xPLHAL/branches/thomas_s_dev/src/xhcp.h 2012-01-01 21:56:16 UTC (rev 623)
+++ xPLHAL/branches/thomas_s_dev/src/xhcp.h 2012-01-01 21:57:46 UTC (rev 624)
@@ -33,7 +33,7 @@
//boost::thread m_thread;
public:
- XHCPServer(boost::asio::io_service& io);
+ XHCPServer(boost::asio::io_service& io, DeviceManager* dm);
~XHCPServer();
protected:
@@ -46,4 +46,5 @@
private:
boost::asio::io_service& m_io;
boost::asio::ip::tcp::tcp::acceptor m_acceptor;
+ DeviceManager* m_dm;
};
Modified: xPLHAL/branches/thomas_s_dev/src/xhcpthread.cpp
===================================================================
--- xPLHAL/branches/thomas_s_dev/src/xhcpthread.cpp 2012-01-01 21:56:16 UTC (rev 623)
+++ xPLHAL/branches/thomas_s_dev/src/xhcpthread.cpp 2012-01-01 21:57:46 UTC (rev 624)
@@ -27,14 +27,27 @@
#include "xhcpthread.h"
-using namespace boost::algorithm;
+using boost::algorithm::to_lower;
+using boost::algorithm::split;
+using boost::algorithm::is_any_of;
+using boost::algorithm::token_compress_on;
+using boost::algorithm::to_lower_copy;
+
using boost::asio::ip::tcp;
+using std::string;
+using std::vector;
-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( socket_ptr socket, DeviceManager* dm )
+ : 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))
+ , m_deviceManager(dm)
{
}
@@ -50,7 +63,7 @@
* Little helper that returns true if str doesn't end with end
* FIXME: use Boost functionality instead...
*/
-bool endDifferent( std::string str, std::string end )
+bool endDifferent( string str, string end )
{
size_t str_l = str.size();
size_t end_l = end.size();
@@ -60,7 +73,7 @@
void XHCPThread::run()
{
- writeLog( std::string("XHCPThread::run() [")+ lexical_cast<std::string>(sock.use_count()) +"]", logLevel::debug );
+ writeLog( string("XHCPThread::run() [")+ lexical_cast<string>(sock.use_count()) +"]", logLevel::debug );
// initialize here to make sure it's done in advance
// commands[ "ADDEVENT" ] = &XHCPThread::;
@@ -109,14 +122,14 @@
boost::asio::streambuf sb;
boost::system::error_code error;
- std::string greeting( "200 CHRISM-XPLHAL.SERVER1 Version 0.0 alpha XHCP 1.5 ready\r\n" );
+ string greeting( "200 CHRISM-XPLHAL.SERVER1 Version 0.0 alpha XHCP 1.5 ready\r\n" );
boost::asio::write(*sock, boost::asio::buffer(greeting));
while (!quit) {
// const int TimeoutInCommand = 5 * 1000; // during transmission of an command
// const int TimeoutExCommand = 300 * 1000; // between different commands
- std::string data;
+ string data;
while ( (!inMultilineRequest && endDifferent(data, "\r\n") )
|| ( inMultilineRequest && endDifferent(data, "\r\n.\r\n") ) ) {
@@ -124,12 +137,12 @@
std::size_t n = boost::asio::read_until(*sock, sb, '\n');
boost::asio::streambuf::const_buffers_type bufs = sb.data();
- std::string newData(
+ string newData(
boost::asio::buffers_begin(bufs),
boost::asio::buffers_begin(bufs) + n);
sb.consume(n);
/*
- std::string newData;
+ string newData;
size_t length = sock->read_some(boost::asio::buffer(newData), error);
if (error == boost::asio::error::eof)
@@ -148,7 +161,7 @@
}
writeLog( "Request: [" + data + "]", logLevel::debug );
- std::string result = (this->*multilineRequestHandler)( data );
+ string result = (this->*multilineRequestHandler)( data );
writeLog( "Result:\n[\n" + result + "]", logLevel::debug );
//socket.write( result );
boost::asio::write(*sock, boost::asio::buffer(result));
@@ -158,11 +171,11 @@
size_t spacePos = data.find( ' ' );
//boost::algorithm::to_upper( data.substr( 0, spacePos ) );
- writeLog( "data.substr( 0, spacePos ) [" + data.substr( 0, spacePos ) + "]["+(std::string::npos == spacePos ? "" : data.substr( spacePos+1 ))+"]", logLevel::error );
- writeLog( "npos: "+lexical_cast<std::string>(std::string::npos) + " - spacePos: + "+lexical_cast<std::string>(spacePos), logLevel::error );
+ writeLog( "data.substr( 0, spacePos ) [" + data.substr( 0, spacePos ) + "]["+(string::npos == spacePos ? "" : data.substr( spacePos+1 ))+"]", logLevel::error );
+ writeLog( "npos: "+lexical_cast<string>(string::npos) + " - spacePos: + "+lexical_cast<string>(spacePos), logLevel::error );
//XHCPcommand command = 0;//commands[ QString(data).section( " ", 0, 0 ).toUpper() ];
XHCPcommand command = commands[ boost::algorithm::to_upper_copy( data.substr( 0, spacePos ) ) ];
- std::string parameters = std::string::npos == spacePos ? "" : data.substr( spacePos+1 );
+ string parameters = string::npos == spacePos ? "" : data.substr( spacePos+1 );
if( !command ) {
writeLog( "Error: Command not implemented! Request: [" + data + "]", logLevel::error );
@@ -170,7 +183,7 @@
}
writeLog( "Request: [" + data + "]", logLevel::debug );
- std::string result = (this->*command)( parameters );
+ string result = (this->*command)( parameters );
writeLog( "Result:\n[\n" + result + "]", logLevel::debug );
boost::asio::write(*sock, boost::asio::buffer(result));
}
@@ -180,7 +193,7 @@
writeLog( "XHCPThread::run() - end", logLevel::debug );
}
-std::string XHCPThread::commandNotRecognised( const std::string& parameter )
+string XHCPThread::commandNotRecognised( const string& parameter )
{
writeLog( "XHCPThread::commandNotRecognised( " + parameter + " )", logLevel::debug );
return responseCode( 500 );
@@ -213,15 +226,15 @@
_return = os.str();
}
- operator std::string() {
+ operator string() {
return _return;
}
private:
- std::string _return;
+ string _return;
};
-std::string XHCPThread::commandCapabilities( const std::string& parameter )
+string XHCPThread::commandCapabilities( const string& parameter )
{
writeLog( "XHCPThread::commandCapabilities( " + parameter + " )", logLevel::debug );
if( "" == parameter || "SCRIPTING" == parameter )
@@ -234,24 +247,24 @@
}
}
-std::string XHCPThread::commandDelGlobal( const std::string& parameter )
+string XHCPThread::commandDelGlobal( const string& parameter )
{
writeLog( "XHCPThread::commandDelGlobal( " + parameter + " )", logLevel::debug );
xPLCache->deleteEntry( parameter );
return responseCode( 233 );
}
-std::string XHCPThread::commandDelDevConfig( const std::string& parameter )
+string XHCPThread::commandDelDevConfig( const string& parameter )
{
writeLog( "XHCPThread::commandDelDevConfig( " + parameter + " )", logLevel::debug );
- deviceManager->removeConfig( parameter );
+ m_deviceManager->removeConfig( parameter );
return responseCode( 235 );
}
-std::string XHCPThread::commandGetDevConfig( const std::string& parameter )
+string XHCPThread::commandGetDevConfig( const string& parameter )
{
writeLog( "XHCPThread::commandGetDevConfig( " + parameter + " )", logLevel::debug );
- xPLDevice device = deviceManager->getDevice( parameter );
+ xPLDevice device = m_deviceManager->getDevice( parameter );
if( "" == device.VDI ) // empty string if device doesn't exist
return responseCode( 417 );
@@ -259,10 +272,10 @@
if( "" == device.ConfigSource )
return responseCode( 416 );
- std::string retval = responseCode( 217 );
+ string retval = responseCode( 217 );
boost::regex regex( "config\\." + device.VDI + "\\.options\\.([a-z0-9]{1,16})" );
- std::vector<std::string> entries = xPLCache->filterByRegEx( regex );
- for( std::vector<std::string>::const_iterator it = entries.begin(); it != entries.end(); ++it )
+ vector<string> entries = xPLCache->filterByRegEx( regex );
+ for( vector<string>::const_iterator it = entries.begin(); it != entries.end(); ++it )
{
boost::smatch matches;
if( boost::regex_match( *it, matches, regex ) )
@@ -276,26 +289,26 @@
return retval;
}
-std::string XHCPThread::commandGetDevConfigValue( const std::string& parameter )
+string XHCPThread::commandGetDevConfigValue( const string& parameter )
{
writeLog( "XHCPThread::commandGetDevConfigValue( " + parameter + " )", logLevel::debug );
- std::vector<std::string> list;
+ vector<string> list;
split( list, parameter, is_any_of( " " ) );
if( list.size() != 2 ) return responseCode( 501 ); // = syntax error
- std::string retval = responseCode( 234 );
+ string retval = responseCode( 234 );
// do we have to handle mutliple entries?
- std::string count = xPLCache->objectValue( "config." + list[0] + ".options." + list[1] + ".count" );
+ string count = xPLCache->objectValue( "config." + list[0] + ".options." + list[1] + ".count" );
if( "" == count )
{ // no
- std::string entry = xPLCache->objectValue( "config." + list[0] + ".current." + list[1] );
+ string entry = xPLCache->objectValue( "config." + list[0] + ".current." + list[1] );
if( "" != entry )
retval += list[1] + "=" + entry + newLine;
} else { // yes
- std::vector<std::string> entries = xPLCache->childNodes( "config." + list[0] + ".current." + list[1] );
- for( std::vector<std::string>::const_iterator it = entries.begin(); it != entries.end(); ++it )
+ vector<string> entries = xPLCache->childNodes( "config." + list[0] + ".current." + list[1] );
+ for( vector<string>::const_iterator it = entries.begin(); it != entries.end(); ++it )
{
- std::string entry = xPLCache->objectValue( *it );
+ string entry = xPLCache->objectValue( *it );
if( "" != entry )
retval += list[1] + "=" + entry + newLine;
}
@@ -304,26 +317,26 @@
return retval;
}
-std::string XHCPThread::commandListAllDevices( const std::string& parameter )
+string XHCPThread::commandListAllDevices( const string& parameter )
{
writeLog( "XHCPThread::commandListAllGlobals( " + parameter + " )", logLevel::debug );
- std::string result = responseCode( 216 );
- std::vector<std::string> names = deviceManager->getAllDeviceNames();
- for( std::vector<std::string>::const_iterator it = names.begin(); it != names.end(); ++it )
+ string result = responseCode( 216 );
+ vector<string> names = m_deviceManager->getAllDeviceNames();
+ for( vector<string>::const_iterator it = names.begin(); it != names.end(); ++it )
result += *it + newLine;
result += endMultiLine;
return result;
}
-std::string XHCPThread::commandListDevices( const std::string& parameter )
+string XHCPThread::commandListDevices( const string& parameter )
{
writeLog( "XHCPThread::commandListGlobals( " + parameter + " )", logLevel::debug );
- std::string result = responseCode( 216 );
- std::vector<std::string> names = deviceManager->getAllDeviceNames();
- std::string type = to_lower_copy( parameter );
- for( std::vector<std::string>::const_iterator it = names.begin(); it != names.end(); ++it )
+ string result = responseCode( 216 );
+ vector<string> names = m_deviceManager->getAllDeviceNames();
+ string type = to_lower_copy( parameter );
+ for( vector<string>::const_iterator it = names.begin(); it != names.end(); ++it )
{
- xPLDevice device = deviceManager->getDevice( *it );
+ xPLDevice device = m_deviceManager->getDevice( *it );
bool showDevice = true;
if( "awaitingconfig" == type && !device.ConfigType ) showDevice = false;
if( "configured" == type && device.ConfigType ) showDevice = false;
@@ -333,7 +346,7 @@
{
result += device.VDI + tab;
result += timeConverter( device.Expires ) + tab;
- result += lexical_cast<std::string>( device.Interval ) + tab;
+ result += lexical_cast<string>( device.Interval ) + tab;
result += (device.ConfigType ? "Y" : "N") + tab;
result += (device.ConfigDone ? "Y" : "N") + tab;
result += (device.WaitingConfig ? "Y" : "N") + tab;
@@ -344,19 +357,19 @@
return result;
}
-std::string XHCPThread::commandListGlobals( const std::string& parameter )
+string XHCPThread::commandListGlobals( const string& parameter )
{
writeLog( "XHCPThread::commandListGlobals( " + parameter + " )", logLevel::debug );
- std::string result = responseCode( 231 );
+ string result = responseCode( 231 );
result += xPLCache->listAllObjects();
result += endMultiLine;
return result;
}
-std::string XHCPThread::commandListOptions( const std::string& parameter )
+string XHCPThread::commandListOptions( const string& parameter )
{
writeLog( "XHCPThread::commandListOptions( " + parameter + " )", logLevel::debug );
- std::string result;
+ string result;
if( "" == parameter )
result = responseCode( 501 );
else
@@ -369,10 +382,10 @@
/**
* \brief List all determinator groups
*/
-std::string XHCPThread::commandListRuleGroups( const std::string& parameter )
+string XHCPThread::commandListRuleGroups( const string& parameter )
{
writeLog( "XHCPThread::commandListRuleGroups( " + parameter + " )", logLevel::debug );
- std::string result = responseCode( 240 );
+ string result = responseCode( 240 );
// FIXME add listing of groups
result += endMultiLine;
return result;
@@ -381,29 +394,29 @@
/**
* \brief List all determinators
*/
-std::string XHCPThread::commandListRules( const std::string& parameter )
+string XHCPThread::commandListRules( const string& parameter )
{
writeLog( "XHCPThread::commandListRules( " + parameter + " )", logLevel::debug );
- std::string result = responseCode( 237 );
+ string result = responseCode( 237 );
// FIXME add listing of groups
result += endMultiLine;
return result;
}
-std::string XHCPThread::commandPutDevConfig( const std::string& parameter )
+string XHCPThread::commandPutDevConfig( const string& parameter )
{
writeLog( "XHCPThread::commandPutDevConfig( " + parameter + " )", logLevel::debug );
- static std::string device;
+ static string device;
if( inMultilineRequest )
{
inMultilineRequest = false;
multilineRequestHandler = 0;
- if( deviceManager->storeNewConfig( device, parameter ) )
+ if( m_deviceManager->storeNewConfig( device, parameter ) )
return responseCode( 220 ); // storing succeded
else
return responseCode( 503 ); // storing failed
}
- if( !deviceManager->contains( parameter ) )
+ if( !m_deviceManager->contains( parameter ) )
return responseCode( 417 );
device = parameter;
@@ -412,11 +425,11 @@
return responseCode( 320 );
}
-std::string XHCPThread::commandSetGlobal( const std::string& parameter )
+string XHCPThread::commandSetGlobal( const string& parameter )
{
writeLog( "XHCPThread::commandSetGlobal( " + parameter + " )", logLevel::debug );
- std::vector<std::string> list;
+ vector<string> list;
split( list, parameter, is_any_of( " " ) );
if( list.size() != 2 ) return resp...
[truncated message content] |
|
From: <tho...@us...> - 2012-02-11 20:40:40
|
Revision: 697
http://openautomation.svn.sourceforge.net/openautomation/?rev=697&view=rev
Author: thomas_s
Date: 2012-02-11 20:40:34 +0000 (Sat, 11 Feb 2012)
Log Message:
-----------
refactored code: used lock_guard in xplmessagequeue
deleted unused iterface class
Modified Paths:
--------------
xPLHAL/branches/thomas_s_dev/src/xplmessagequeue.cpp
xPLHAL/branches/thomas_s_dev/src/xplmessagequeue.h
Removed Paths:
-------------
xPLHAL/branches/thomas_s_dev/src/i_properties.h
Deleted: xPLHAL/branches/thomas_s_dev/src/i_properties.h
===================================================================
--- xPLHAL/branches/thomas_s_dev/src/i_properties.h 2012-02-11 20:39:47 UTC (rev 696)
+++ xPLHAL/branches/thomas_s_dev/src/i_properties.h 2012-02-11 20:40:34 UTC (rev 697)
@@ -1,20 +0,0 @@
-#pragma once
-
-//! \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;
-};
Modified: xPLHAL/branches/thomas_s_dev/src/xplmessagequeue.cpp
===================================================================
--- xPLHAL/branches/thomas_s_dev/src/xplmessagequeue.cpp 2012-02-11 20:39:47 UTC (rev 696)
+++ xPLHAL/branches/thomas_s_dev/src/xplmessagequeue.cpp 2012-02-11 20:40:34 UTC (rev 697)
@@ -17,13 +17,15 @@
*/
#include "log.h"
-
#include "xplmessagequeue.h"
+using std::mutex;
+using std::lock_guard;
+
void xPLMessageQueueClass::add( const xPLMessagePtr& message )
{
writeLog("xPLMessageQueueClass::add", logLevel::debug);
- lock_guard locker( queueLock ); // get exclusive access to the queue
+ lock_guard<mutex> locker( queueLock ); // get exclusive access to the queue
xPLMessages.push( message );
}
@@ -31,7 +33,7 @@
{
xPLMessagePtr message;
{
- lock_guard locker( queueLock ); // get exclusive access to the queue
+ lock_guard<mutex> locker( queueLock ); // get exclusive access to the queue
if( xPLMessages.empty() )
return 0;
message = xPLMessages.front(); // and release is as soon as possible
Modified: xPLHAL/branches/thomas_s_dev/src/xplmessagequeue.h
===================================================================
--- xPLHAL/branches/thomas_s_dev/src/xplmessagequeue.h 2012-02-11 20:39:47 UTC (rev 696)
+++ xPLHAL/branches/thomas_s_dev/src/xplmessagequeue.h 2012-02-11 20:40:34 UTC (rev 697)
@@ -20,11 +20,8 @@
#include <vector>
#include <queue>
#include <string>
+#include <thread>
-#include <boost/shared_ptr.hpp>
-#include <boost/thread/mutex.hpp>
-#include <boost/thread/locks.hpp>
-
#include "xplmessage.h"
/**
@@ -33,8 +30,7 @@
class xPLMessageQueueClass
{
/** \brief variable to ensure that the queue is thread save... */
- mutable boost::mutex queueLock;
- typedef boost::lock_guard<boost::mutex> lock_guard;
+ mutable std::mutex queueLock;
std::queue<xPLMessagePtr> xPLMessages;
public:
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <tho...@us...> - 2012-07-02 20:41:49
|
Revision: 902
http://openautomation.svn.sourceforge.net/openautomation/?rev=902&view=rev
Author: thomas_s
Date: 2012-07-02 20:41:43 +0000 (Mon, 02 Jul 2012)
Log Message:
-----------
use std::bind, std::thread and chrono
Modified Paths:
--------------
xPLHAL/branches/thomas_s_dev/src/log.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/xplhandler.cpp
xPLHAL/branches/thomas_s_dev/src/xplhandler.h
Modified: xPLHAL/branches/thomas_s_dev/src/log.h
===================================================================
--- xPLHAL/branches/thomas_s_dev/src/log.h 2012-07-02 20:41:20 UTC (rev 901)
+++ xPLHAL/branches/thomas_s_dev/src/log.h 2012-07-02 20:41:43 UTC (rev 902)
@@ -18,7 +18,8 @@
*/
#include <iostream>
-#include <boost/date_time/posix_time/posix_time.hpp>
+#include <chrono>
+#include <array>
#include <boost/lexical_cast.hpp>
using boost::lexical_cast;
@@ -87,9 +88,22 @@
bool showLogColor; /**< \brief Status if colored logging should be used. */
bool showLogTimestamp; /**< \brief Status if timestamped logging should be used. */
+ template<class T>
+ std::string to_simple_string(const T& tp)
+ {
+ using namespace std::chrono;
+ using namespace std;
+ array<char, 50> buffer;
+ const time_t now_c = T::clock::to_time_t(tp);
+ strftime(&buffer[0], buffer.size(), "%F %T", localtime(&now_c));
+ const auto secs = duration_cast<seconds>(tp.time_since_epoch());
+ const auto fractional = duration_cast<milliseconds>(tp.time_since_epoch() - secs);
+ return std::string(&buffer[0]) + "." + lexical_cast<std::string>(fractional.count());
+ }
+
void doLog( std::string message, logLevel::logLevel level )
{
- std::string timestamp( to_simple_string(boost::posix_time::microsec_clock::local_time()) );
+ std::string timestamp( to_simple_string( std::chrono::high_resolution_clock::now() ));
if( level >= currentLogLevel )
{
if( showLogColor ) std::cout << escape::FgGray;
Modified: xPLHAL/branches/thomas_s_dev/src/main.cpp
===================================================================
--- xPLHAL/branches/thomas_s_dev/src/main.cpp 2012-07-02 20:41:20 UTC (rev 901)
+++ xPLHAL/branches/thomas_s_dev/src/main.cpp 2012-07-02 20:41:43 UTC (rev 902)
@@ -15,7 +15,9 @@
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 <functional>
#include <signal.h>
+
#include <boost/program_options.hpp>
#include "log.h"
#include "devicemanager.h"
@@ -38,6 +40,7 @@
using std::cout;
using std::endl;
+using namespace std::placeholders;
path xPLHalRootFolder;
path DataFileFolder;
path ScriptEngineFolder;
@@ -60,12 +63,12 @@
,mXHCPServer(new XHCPServer(m_ioservice, &mDeviceManager))
,mXpl(new xPLHandler(m_ioservice, boost::asio::ip::host_name() ))
,mDeterminatorManager("data")
- ,mTimerListAllObjects(m_ioservice, boost::posix_time::seconds(60), true)
- ,mTimerFlushExpiredEntries(m_ioservice, boost::posix_time::minutes(5), true)
+ ,mTimerListAllObjects(m_ioservice, std::chrono::seconds(60), true)
+ ,mTimerFlushExpiredEntries(m_ioservice, std::chrono::minutes(5), true)
{
- mDeviceManager.m_sigSendXplMessage.connect(boost::bind(&xPLHandler::sendMessage, mXpl, _1));
- mXpl->m_sigRceivedXplMessage.connect(boost::bind(&DeviceManager::processXplMessage, &mDeviceManager, _1));
+ mDeviceManager.m_sigSendXplMessage.connect([&](const xPLMessagePtr ptr) {mXpl->sendMessage(ptr);});
+ mXpl->m_sigRceivedXplMessage.connect(std::bind(&DeviceManager::processXplMessage, &mDeviceManager, _1));
mXpl->m_sigRceivedXplMessage.connect(mDeterminatorManager.m_sigRceivedXplMessage);
installTimer();
Modified: xPLHAL/branches/thomas_s_dev/src/xhcp.cpp
===================================================================
--- xPLHAL/branches/thomas_s_dev/src/xhcp.cpp 2012-07-02 20:41:20 UTC (rev 901)
+++ xPLHAL/branches/thomas_s_dev/src/xhcp.cpp 2012-07-02 20:41:43 UTC (rev 902)
@@ -41,7 +41,7 @@
void XHCPServer::startAccept()
{
socket_ptr sockPtr(new tcp::socket(m_io));
- m_acceptor.async_accept(*sockPtr, boost::bind(&XHCPServer::handleAccept, this, sockPtr));
+ m_acceptor.async_accept(*sockPtr, std::bind(&XHCPServer::handleAccept, this, sockPtr));
}
void XHCPServer::handleAccept(socket_ptr sockPtr)
Modified: xPLHAL/branches/thomas_s_dev/src/xhcp.h
===================================================================
--- xPLHAL/branches/thomas_s_dev/src/xhcp.h 2012-07-02 20:41:20 UTC (rev 901)
+++ xPLHAL/branches/thomas_s_dev/src/xhcp.h 2012-07-02 20:41:43 UTC (rev 902)
@@ -18,8 +18,6 @@
*/
#include <boost/asio.hpp>
-//#include <boost/thread.hpp>
-
#include "xplcache.h"
#include "xhcpthread.h"
@@ -29,9 +27,6 @@
*/
class XHCPServer
{
- //volatile bool m_stoprequested;
- //boost::thread m_thread;
-
public:
XHCPServer(boost::asio::io_service& io, DeviceManager* dm);
~XHCPServer();
@@ -39,7 +34,6 @@
protected:
/** \brief Create a new XHCPThread for a new connection. */
-// void waitForConnection( void );
void handleAccept(socket_ptr sockPtr);
void startAccept();
Modified: xPLHAL/branches/thomas_s_dev/src/xhcpthread.cpp
===================================================================
--- xPLHAL/branches/thomas_s_dev/src/xhcpthread.cpp 2012-07-02 20:41:20 UTC (rev 901)
+++ xPLHAL/branches/thomas_s_dev/src/xhcpthread.cpp 2012-07-02 20:41:43 UTC (rev 902)
@@ -49,7 +49,7 @@
, quit( false )
, inMultilineRequest( false )
, multilineRequestHandler( 0 )
- , m_thread(boost::bind(&XHCPThread::run, this))
+ , m_thread(std::bind(&XHCPThread::run, this))
, m_deviceManager(dm)
{
}
Modified: xPLHAL/branches/thomas_s_dev/src/xhcpthread.h
===================================================================
--- xPLHAL/branches/thomas_s_dev/src/xhcpthread.h 2012-07-02 20:41:20 UTC (rev 901)
+++ xPLHAL/branches/thomas_s_dev/src/xhcpthread.h 2012-07-02 20:41:43 UTC (rev 902)
@@ -20,8 +20,8 @@
#include <map>
#include <boost/smart_ptr.hpp>
-#include <boost/thread.hpp>
#include <boost/asio.hpp>
+#include <thread>
#include "xplcache.h"
@@ -85,5 +85,5 @@
std::string commandSendXPlMessage ( const std::string& parameter );
std::string commandQuit ( const std::string& parameter );
- boost::thread m_thread;
+ std::thread m_thread;
};
Modified: xPLHAL/branches/thomas_s_dev/src/xplhandler.cpp
===================================================================
--- xPLHAL/branches/thomas_s_dev/src/xplhandler.cpp 2012-07-02 20:41:20 UTC (rev 901)
+++ xPLHAL/branches/thomas_s_dev/src/xplhandler.cpp 2012-07-02 20:41:43 UTC (rev 902)
@@ -20,8 +20,10 @@
#include "xplmessagequeue.h"
#include <boost/algorithm/string/replace.hpp>
+#include <functional>
using std::string;
+using namespace std::placeholders;
#include "log.h"
@@ -177,12 +179,12 @@
void xPLHandler::startAsyncRead()
{
- m_xplSocket.async_read_some(boost::asio::null_buffers(), boost::bind(&xPLHandler::handleReadableXplSocket, this, _1));
+ m_xplSocket.async_read_some(boost::asio::null_buffers(), std::bind(&xPLHandler::handleReadableXplSocket, this, _1));
}
void xPLHandler::startAsyncWrite()
{
- m_xplWriteSocket.async_read_some(boost::asio::null_buffers(), boost::bind(&xPLHandler::handleReadableXplMessagequeue, this, _1));
+ m_xplWriteSocket.async_read_some(boost::asio::null_buffers(), std::bind(&xPLHandler::handleReadableXplMessagequeue, this, _1));
}
void xPLHandler::handleReadableXplMessagequeue(boost::system::error_code ec)
@@ -210,7 +212,7 @@
}
}
-void xPLHandler::sendMessage( const xPLMessagePtr& message )
+void xPLHandler::sendMessage( const xPLMessagePtr message )
{
mXplMessageQueue->add(message);
}
Modified: xPLHAL/branches/thomas_s_dev/src/xplhandler.h
===================================================================
--- xPLHAL/branches/thomas_s_dev/src/xplhandler.h 2012-07-02 20:41:20 UTC (rev 901)
+++ xPLHAL/branches/thomas_s_dev/src/xplhandler.h 2012-07-02 20:41:43 UTC (rev 902)
@@ -20,11 +20,13 @@
#include <vector>
#include <string>
+#define BOOST_BIND_NO_PLACEHOLDERS
#include <boost/signals2/signal.hpp>
#include <boost/asio.hpp>
#include "xplmessage.h"
+#include "xplmessagequeue.h"
-class XplMessageQueue;
+//class XplMessageQueue;
/**
* \brief Handle all xPL communication.
@@ -40,7 +42,7 @@
void run();
- void sendMessage( const xPLMessagePtr& message );
+ void sendMessage( const xPLMessagePtr message );
/** \brief Broadcast one message to the xPL network. */
void sendBroadcastMessage( const std::string& msgClass, const std::string& msgType, const xPLMessage::namedValueList& namedValues );
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <tho...@us...> - 2012-07-02 20:42:10
|
Revision: 903
http://openautomation.svn.sourceforge.net/openautomation/?rev=903&view=rev
Author: thomas_s
Date: 2012-07-02 20:42:04 +0000 (Mon, 02 Jul 2012)
Log Message:
-----------
removed more global variables
Modified Paths:
--------------
xPLHAL/branches/thomas_s_dev/src/globals.h
xPLHAL/branches/thomas_s_dev/src/main.cpp
xPLHAL/branches/thomas_s_dev/src/xplcache.cpp
xPLHAL/branches/thomas_s_dev/src/xplcache.h
Modified: xPLHAL/branches/thomas_s_dev/src/globals.h
===================================================================
--- xPLHAL/branches/thomas_s_dev/src/globals.h 2012-07-02 20:41:43 UTC (rev 902)
+++ xPLHAL/branches/thomas_s_dev/src/globals.h 2012-07-02 20:42:04 UTC (rev 903)
@@ -17,8 +17,6 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-#include "boost/filesystem.hpp"
-
/**
* Define all global variables:
*/
@@ -28,11 +26,4 @@
class xPLCacheClass;
extern xPLCacheClass *xPLCache;
-class xPLHandler;
-extern xPLHandler *xPL;
-/** The working directories */
-extern boost::filesystem::path xPLHalRootFolder;
-extern boost::filesystem::path DataFileFolder;
-extern boost::filesystem::path ScriptEngineFolder;
-extern boost::filesystem::path rulesFolder;
Modified: xPLHAL/branches/thomas_s_dev/src/main.cpp
===================================================================
--- xPLHAL/branches/thomas_s_dev/src/main.cpp 2012-07-02 20:41:43 UTC (rev 902)
+++ xPLHAL/branches/thomas_s_dev/src/main.cpp 2012-07-02 20:42:04 UTC (rev 903)
@@ -41,15 +41,15 @@
using std::endl;
using namespace std::placeholders;
-path xPLHalRootFolder;
-path DataFileFolder;
-path ScriptEngineFolder;
-path rulesFolder;
xPLCacheClass *xPLCache;
-xPLHandler *xPL;
-static boost::asio::io_service* g_ioservice = nullptr;
+struct filefolders {
+ path dataFileFolder;
+ path xPLHalRootFolder;
+ path scriptEngineFolder;
+ path rulesFolder;
+};
/**
* \brief Main application
@@ -57,8 +57,8 @@
class XplHalApplication
{
public:
- XplHalApplication()
- :mXplCache(new xPLCacheClass)
+ XplHalApplication(struct filefolders& folders)
+ :mXplCache(new xPLCacheClass(folders.dataFileFolder))
,mDeviceManager(mXplCache)
,mXHCPServer(new XHCPServer(m_ioservice, &mDeviceManager))
,mXpl(new xPLHandler(m_ioservice, boost::asio::ip::host_name() ))
@@ -74,7 +74,7 @@
/* set global variables */
xPLCache = mXplCache;
- xPL = mXpl;
+ //xPL = mXpl;
writeLog( "initialized", logLevel::all );
}
@@ -164,11 +164,12 @@
*/
int main(int UNUSED argc, char** UNUSED argv)
{
+ struct filefolders folders;
/** Make sure the necessary infrastructure exists: */
- xPLHalRootFolder = initial_path();
- DataFileFolder = xPLHalRootFolder / "data";
- ScriptEngineFolder = DataFileFolder / "scripts";
- rulesFolder = DataFileFolder / "determinator";
+ folders.xPLHalRootFolder = current_path();
+ folders.dataFileFolder = folders.xPLHalRootFolder / "data";
+ folders.scriptEngineFolder = folders.dataFileFolder / "scripts";
+ folders.rulesFolder = folders.dataFileFolder / "determinator";
//vendorFileFolder = DataFileFolder / "vendors";
//ConfigFileFolder = DataFileFolder / "configs";
@@ -192,35 +193,35 @@
// 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 );
+ writeLog( "Error changing to working directory \"" + folders.xPLHalRootFolder.string() + "\"!", logLevel::error );
return -1;
}
- if( !exists( DataFileFolder ) ) {
- 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 );
+ if( !exists( folders.dataFileFolder ) ) {
+ writeLog( "Directory \"" + folders.dataFileFolder.string() + "\" for DataFileFolder doesn't exist. Creating it...", logLevel::debug );
+ if( !create_directory( folders.dataFileFolder ) ) {
+ writeLog( "Error creating data directory \"" + folders.dataFileFolder.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 ) ) {
- writeLog( "Error creating script directory \"" + ScriptEngineFolder.string() + "\"!", logLevel::error );
+ if( !exists( folders.scriptEngineFolder ) ) {
+ writeLog( "Directory \"" + folders.scriptEngineFolder.string() + "\" for ScriptEngineFolder doesn't exist. Creating it...", logLevel::debug );
+ if( !create_directory( folders.scriptEngineFolder ) ) {
+ writeLog( "Error creating script directory \"" + folders.scriptEngineFolder.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 ) ) {
- writeLog( "Error creating determinator directory \"" + rulesFolder.string() + "\"!", logLevel::error );
+ if( !exists( folders.rulesFolder ) ) {
+ writeLog( "Directory \"" + folders.rulesFolder.string() + "\" for rulesFolder doesn't exist. Creating it...", logLevel::debug );
+ if( !create_directory( folders.rulesFolder ) ) {
+ writeLog( "Error creating determinator directory \"" + folders.rulesFolder.string() + "\"!", logLevel::error );
return -1;
}
}
- XplHalApplication app;
+ XplHalApplication app(folders);
return app.exec();
}
Modified: xPLHAL/branches/thomas_s_dev/src/xplcache.cpp
===================================================================
--- xPLHAL/branches/thomas_s_dev/src/xplcache.cpp 2012-07-02 20:41:43 UTC (rev 902)
+++ xPLHAL/branches/thomas_s_dev/src/xplcache.cpp 2012-07-02 20:42:04 UTC (rev 903)
@@ -57,7 +57,8 @@
mfSchemClass = list[5];
}
-xPLCacheClass::xPLCacheClass()
+xPLCacheClass::xPLCacheClass(const boost::filesystem::path& dataFileFolder)
+:mDataFileFolder(dataFileFolder)
{
loadCache();
}
@@ -156,7 +157,7 @@
{
mCache.clear(); // erase all entries
- BFS::path objectCacheFile( DataFileFolder / "object_mCache.xml" );
+ BFS::path objectCacheFile( mDataFileFolder / "object_mCache.xml" );
writeLog( "loadCache: ["+objectCacheFile.string()+"]", logLevel::debug );
if( BFS::exists( objectCacheFile ) ) {
writeLog( "Loading cached xPL object from file: "+objectCacheFile.string(), logLevel::debug );
Modified: xPLHAL/branches/thomas_s_dev/src/xplcache.h
===================================================================
--- xPLHAL/branches/thomas_s_dev/src/xplcache.h 2012-07-02 20:41:43 UTC (rev 902)
+++ xPLHAL/branches/thomas_s_dev/src/xplcache.h 2012-07-02 20:42:04 UTC (rev 903)
@@ -24,6 +24,7 @@
#include <mutex>
#include <boost/regex.hpp>
+#include <boost/filesystem.hpp>
#include "i_xplcache.h"
@@ -70,6 +71,7 @@
* the CacheEntry with fast lookup. */
typedef std::map< std::string, CacheEntry > cachemap_t;
cachemap_t mCache; /**< \brief The cache itself. */
+ const boost::filesystem::path& mDataFileFolder;
/** \brief variable to lock write operations on the cache for safe
* multithreading operation. */
@@ -100,7 +102,7 @@
};
public:
- xPLCacheClass();
+ xPLCacheClass(const boost::filesystem::path& dataFileFolder);
/** \returns true if the element name exists. */
bool exists( const std::string& name ) const {
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|