|
From: <tho...@us...> - 2011-12-19 21:35:22
|
Revision: 601
http://openautomation.svn.sourceforge.net/openautomation/?rev=601&view=rev
Author: thomas_s
Date: 2011-12-19 21:35:14 +0000 (Mon, 19 Dec 2011)
Log Message:
-----------
- more refactoring
Modified Paths:
--------------
xPLHAL/branches/thomas_s_dev/src/CMakeLists.txt
xPLHAL/branches/thomas_s_dev/src/devicemanager.cpp
xPLHAL/branches/thomas_s_dev/src/devicemanager.h
xPLHAL/branches/thomas_s_dev/src/main.cpp
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/test/CMakeLists.txt
xPLHAL/branches/thomas_s_dev/test/test_devicemanager.cpp
Added Paths:
-----------
xPLHAL/branches/thomas_s_dev/src/xplmessage.cpp
xPLHAL/branches/thomas_s_dev/test/mock_xplhandler.h
Modified: xPLHAL/branches/thomas_s_dev/src/CMakeLists.txt
===================================================================
--- xPLHAL/branches/thomas_s_dev/src/CMakeLists.txt 2011-12-19 17:29:18 UTC (rev 600)
+++ xPLHAL/branches/thomas_s_dev/src/CMakeLists.txt 2011-12-19 21:35:14 UTC (rev 601)
@@ -19,7 +19,7 @@
#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 main.cpp)
+set(xPLHAL_SRCS xplmessagequeue.cpp devicemanager.cpp xplhandler.cpp xplcache.cpp xhcpthread.cpp log.cpp xhcp.cpp xplmessage.cpp main.cpp)
add_executable(xPLHAL ${xPLHAL_SRCS})
#message(STATUS "Boost_LIBRARIES=${Boost_LIBRARIES}")
Modified: xPLHAL/branches/thomas_s_dev/src/devicemanager.cpp
===================================================================
--- xPLHAL/branches/thomas_s_dev/src/devicemanager.cpp 2011-12-19 17:29:18 UTC (rev 600)
+++ xPLHAL/branches/thomas_s_dev/src/devicemanager.cpp 2011-12-19 21:35:14 UTC (rev 601)
@@ -15,8 +15,6 @@
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-
-
#include <boost/algorithm/string.hpp>
#include <boost/regex.hpp>
#include <cstdio>
@@ -125,16 +123,14 @@
void deviceManagerClass::processConfigList( const xPLMessagePtr message )
{
- std::string source = extractSourceFromXplMessage(message);
+ std::string source = message->getSourceVDI();
xPLDevice device = getDevice( source );
writeLog( "deviceManagerClass::processConfigList("+source+") - found ["+device.VDI+"]", logLevel::debug );
if( "" == device.VDI ) {
// A config list turned up that we haven't asked for...
// create a new device...
int interval = 5;
- //\TODO: implement xPL_getMessageNamedValue
-// ptime expires = calculateExpireTime(xPL_getMessageNamedValue(message, "interval"), &interval);
- ptime expires = calculateExpireTime("5", &interval);
+ ptime expires = calculateExpireTime(message->getNamedValue("interval"), &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
@@ -178,18 +174,16 @@
// 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,
- message->vendor,
- message->deviceID,
- message->instanceID,
+ source,
"config", "current",
command_request) );
m_sigSendXplMessage(msg);
}
}
-ptime deviceManagerClass::calculateExpireTime(const char* string_interval, int *pInterval)
+ptime deviceManagerClass::calculateExpireTime(const std::string& string_interval, int *pInterval)
{
- int interval = string_interval ? atoi(string_interval) : 5; // default to 5 minutes
+ int interval = string_interval.empty() ? atoi(string_interval.c_str()) : 5; // default to 5 minutes
if (pInterval) {
*pInterval = interval;
}
@@ -203,7 +197,7 @@
void deviceManagerClass::processConfigHeartBeat( const xPLMessagePtr message )
{
- std::string source = extractSourceFromXplMessage(message);
+ std::string source = message->getSourceVDI();
xPLDevice device = getDevice( source );
writeLog( "deviceManagerClass::processConfigHeartBeat("+source+") - found ["+device.VDI+"]", logLevel::debug );
@@ -211,9 +205,7 @@
// 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;
- //\TODO
-// ptime expires = calculateExpireTime(xPL_getMessageNamedValue(message, "interval"), &interval);
- ptime expires = calculateExpireTime("5", &interval);
+ ptime expires = calculateExpireTime(message->getNamedValue("interval"), &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
@@ -237,9 +229,7 @@
if( !device.ConfigListSent ) {
xPLMessage::namedValueList command_request; command_request.push_back( std::make_pair( "command", "request" ) );
xPLMessagePtr msg( new xPLMessage(xPL_MESSAGE_COMMAND,
- message->vendor,
- message->deviceID,
- message->instanceID,
+ source,
"config", "list",
command_request) );
m_sigSendXplMessage(msg);
@@ -249,7 +239,7 @@
void deviceManagerClass::processCurrentConfig( const xPLMessagePtr message )
{
- std::string source = extractSourceFromXplMessage(message);
+ std::string source = message->getSourceVDI();
xPLDevice device = getDevice( source );
writeLog( "deviceManagerClass::processCurrentConfig("+source+") - found ["+device.VDI+"]", logLevel::debug );
if( "" == device.VDI ) {
@@ -286,29 +276,13 @@
}
}
-std::string deviceManagerClass::extractSourceFromXplMessage( xPL_MessagePtr message )
-{
- return std::string(xPL_getSourceVendor(message)) + "-"
- + xPL_getSourceDeviceID ( message ) + "."
- + xPL_getSourceInstanceID( message );
-}
-
-std::string deviceManagerClass::extractSourceFromXplMessage( xPLMessagePtr message )
-{
- return std::string(message->vendor + "-"
- + message->deviceID + "."
- + message->instanceID);
-}
-
void deviceManagerClass::processHeartbeat( xPLMessagePtr message )
{
- std::string source = extractSourceFromXplMessage(message);
+ std::string source = message->getSourceVDI();
xPLDevice device = getDevice( source );
int interval = 5;
- //\TODO
-// ptime expires = calculateExpireTime(xPL_getMessageNamedValue(message, "interval"), &interval);
- ptime expires = calculateExpireTime("5", &interval);
+ ptime expires = calculateExpireTime(message->getNamedValue("interval"), &interval);
writeLog( "deviceManagerClass::processHeartbeat("+source+") - found ["+device.VDI+"]", logLevel::debug );
if( "" == device.VDI ) {
@@ -330,9 +304,7 @@
// 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,
- message->vendor,
- message->deviceID,
- message->instanceID,
+ source,
"config", "list",
command_request) );
m_sigSendXplMessage(msg);
@@ -349,7 +321,7 @@
void deviceManagerClass::processRemove( xPLMessagePtr message )
{
- std::string source = extractSourceFromXplMessage(message);
+ std::string source = message->getSourceVDI();
remove( source );
removeConfig( source );
@@ -371,15 +343,8 @@
}
if( list.size() > 0 ) {
- 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,
+ source,
"config", "response",
list) );
m_sigSendXplMessage(msg);
@@ -442,7 +407,3 @@
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-19 17:29:18 UTC (rev 600)
+++ xPLHAL/branches/thomas_s_dev/src/devicemanager.h 2011-12-19 21:35:14 UTC (rev 601)
@@ -32,8 +32,6 @@
public:
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;
@@ -68,16 +66,12 @@
/** \brief A new configuration arrived via XHCP, handle it... */
bool storeNewConfig( const std::string& source, const std::string& config );
-
+
+ xPLHandler::signal_t m_sigSendXplMessage;
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);
+ boost::posix_time::ptime calculateExpireTime(const std::string& string_interval, int *pInterval = 0);
IxPLCacheClass* m_xPLCache;
std::map<std::string, xPLDevice> mDeviceMap;
- xPLHandler::signal_t m_sigSendXplMessage;
};
Modified: xPLHAL/branches/thomas_s_dev/src/main.cpp
===================================================================
--- xPLHAL/branches/thomas_s_dev/src/main.cpp 2011-12-19 17:29:18 UTC (rev 600)
+++ xPLHAL/branches/thomas_s_dev/src/main.cpp 2011-12-19 21:35:14 UTC (rev 601)
@@ -108,7 +108,8 @@
XHCPServer *xhcpServer = new XHCPServer();
xPL = new xPLHandler( boost::asio::ip::host_name() ); //xPL->start();
deviceManager = new deviceManagerClass(xPLCache);
- xPL->connect(boost::bind(&deviceManagerClass::processXplMessage, deviceManager, _1));
+ deviceManager->m_sigSendXplMessage.connect(boost::bind(&xPLMessageQueueClass::add, xPLMessageQueue, _1));
+ xPL->m_sigRceivedXplMessage.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/xplhandler.cpp
===================================================================
--- xPLHAL/branches/thomas_s_dev/src/xplhandler.cpp 2011-12-19 17:29:18 UTC (rev 600)
+++ xPLHAL/branches/thomas_s_dev/src/xplhandler.cpp 2011-12-19 21:35:14 UTC (rev 601)
@@ -152,83 +152,6 @@
xPLMessageQueue->add( xPLMessagePtr( new xPLMessage( type, vendor, device, instance, msgClass, msgType, namedValues ) ) );
}
-void xPLHandler::printXPLMessage( xPL_MessagePtr theMessage )
-{
- std::string result;
-
- /* Source Info */
- result += xPL_getSourceVendor(theMessage) + std::string("-");
- result += xPL_getSourceDeviceID(theMessage) + std::string(".");
- result += xPL_getSourceInstanceID(theMessage);
-
- result += " -> ";
- /* Handle various target types */
- if (xPL_isBroadcastMessage(theMessage)) {
- result += "*";
- } else {
- if (xPL_isGroupMessage(theMessage)) {
- result += "XPL-GROUP.";
- result += xPL_getTargetGroup(theMessage);
- } else {
- result += xPL_getTargetVendor(theMessage) + std::string("-");
- result += xPL_getTargetDeviceID(theMessage) + std::string(".");
- result += xPL_getTargetInstanceID(theMessage);
- }
- }
-
- /* Print hop count */
- result += " (";
- result += lexical_cast<std::string>(xPL_getHopCount(theMessage));
- result += " hops) ";
-
- result += "[";
- switch(xPL_getMessageType(theMessage))
- {
- case xPL_MESSAGE_COMMAND:
- result += "xpl-cmnd";
- break;
- case xPL_MESSAGE_STATUS:
- result += "xpl-stat";
- break;
- case xPL_MESSAGE_TRIGGER:
- result += "xpl-trig";
- break;
- default:
- result += "!UNKNOWN!";
- break;
- }
- result += "] ";
-
- /* Echo Schema Info */
- result += xPL_getSchemaClass(theMessage) + std::string(".");
- result += xPL_getSchemaType(theMessage) + std::string(": ");
-
- xPL_NameValueListPtr nvList = xPL_getMessageBody(theMessage);
- xPL_NameValuePairPtr nvPair = NULL;
- int nvIndex = 0;
- int nvCount = xPL_getNamedValueCount(nvList);
- /* Write Name/Value Pairs out */
- for (nvIndex = 0; nvIndex < nvCount; nvIndex++) {
- nvPair = xPL_getNamedValuePairAt(nvList, nvIndex);
- result += nvPair->itemName; //WRITE_TEXT(nvPair->itemName);
- result += "="; //WRITE_TEXT("=");
-
- /* Write data content out */
- if (nvPair->itemValue != NULL) {
- if (nvPair->isBinary)
- ;//writeBinaryValue(nvPair->itemValue, nvPair->binaryLength);
- else
- result += nvPair->itemValue; //WRITE_TEXT(nvPair->itemValue);
- }
-
- /* Terminate line/entry */
- result += "\n"; //WRITE_TEXT("\n");
- }
- replace_all( result, "\n", ";" );
-
- writeLog( result, logLevel::debug );
-}
-
void xPLHandler::xpl_message_callback( xPL_MessagePtr theMessage, void *userValue )
{
xPLHandler* obj = static_cast<xPLHandler*>(userValue);
@@ -237,8 +160,6 @@
void xPLHandler::handleXPLMessage( xPL_MessagePtr theMessage)
{
- printXPLMessage( theMessage );
-
xPLMessage::namedValueList values;
xPL_NameValueListPtr nvList = xPL_getMessageBody(theMessage);
xPL_NameValuePairPtr nvPair = NULL;
@@ -260,12 +181,18 @@
xPL_getSchemaType(theMessage),
values) );
- m_sigRceivedXplMessage(msg);
+ msg->isBroadcastMessage = xPL_isBroadcastMessage(theMessage);
+ msg->isGroupMessage = xPL_isGroupMessage(theMessage);
+ if (!msg->isBroadcastMessage) {
+ msg->targetVendor = xPL_getTargetVendor(theMessage);
+ msg->targetDevice = xPL_getTargetDeviceID(theMessage);
+ msg->targetInstance = xPL_getTargetInstanceID(theMessage);
+ }
+ if (msg->isGroupMessage) {
+ msg->targetGroup = xPL_getTargetGroup(theMessage);
+ }
+
+ writeLog(msg->printXPLMessage(), logLevel::debug );
+ m_sigRceivedXplMessage(msg);
}
-
-boost::signals2::connection xPLHandler::connect(const signal_t::slot_type &subscriber)
-{
- return m_sigRceivedXplMessage.connect(subscriber);
-}
-
Modified: xPLHAL/branches/thomas_s_dev/src/xplhandler.h
===================================================================
--- xPLHAL/branches/thomas_s_dev/src/xplhandler.h 2011-12-19 17:29:18 UTC (rev 600)
+++ xPLHAL/branches/thomas_s_dev/src/xplhandler.h 2011-12-19 21:35:14 UTC (rev 601)
@@ -42,8 +42,6 @@
xPLHandler( const std::string& host_name);
~xPLHandler();
- boost::signals2::connection connect(const signal_t::slot_type &subscriber);
-
void run();
/** \brief Broadcast one message to the xPL network. */
@@ -57,11 +55,11 @@
/** \brief Send a directed message to the xPL network. */
void sendMessage( const xPL_MessageType type, const std::string& VDI,
const std::string& msgClass, const std::string& msgType, const xPLMessage::namedValueList& namedValues ) const;
+
+ public:
+ signal_t m_sigRceivedXplMessage;
private:
- /** \brief Print via the logging facility the whole content of a xPL message. */
- static void printXPLMessage( xPL_MessagePtr theMessage );
-
/** \brief Handle an incomming xPL message. */
void handleXPLMessage( xPL_MessagePtr theMessage);
@@ -75,5 +73,4 @@
boost::thread* m_thread;
static int m_refcount;
bool m_exit_thread;
- signal_t m_sigRceivedXplMessage;
};
Added: xPLHAL/branches/thomas_s_dev/src/xplmessage.cpp
===================================================================
--- xPLHAL/branches/thomas_s_dev/src/xplmessage.cpp (rev 0)
+++ xPLHAL/branches/thomas_s_dev/src/xplmessage.cpp 2011-12-19 21:35:14 UTC (rev 601)
@@ -0,0 +1,119 @@
+/*
+ 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 "xplmessage.h"
+#include <boost/algorithm/string/replace.hpp>
+#include <boost/lexical_cast.hpp>
+#include <algorithm>
+
+using namespace boost::algorithm;
+using boost::lexical_cast;
+
+std::string xPLMessage::getTypeString() const
+{
+ switch(type)
+ {
+ case xPL_MESSAGE_COMMAND: return "xpl-cmnd";
+ case xPL_MESSAGE_STATUS: return "xpl-stat";
+ case xPL_MESSAGE_TRIGGER: return "xpl-trig";
+ default: return "!UNKNOWN!";
+ }
+}
+
+std::string xPLMessage::getSourceVDI() const
+{
+ return vendor + std::string("-") + deviceID + std::string(".") + instanceID;
+}
+
+std::string xPLMessage::getTargetVDI() const
+{
+ return targetVendor + std::string("-") + targetDevice + std::string(".") + targetInstance;
+}
+
+bool xPLMessage::setSourceFromVDI(const std::string& source)
+{
+ size_t marker1 = source.find( "-" );
+ size_t marker2 = source.find( "." );
+ vendor = source.substr( 0, marker1 );
+ deviceID = source.substr( marker1+1, marker2 - (marker1+1) );
+ instanceID = source.substr( marker2+1 );
+
+ return true;
+}
+
+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 );
+
+ return true;
+}
+
+std::string xPLMessage::getNamedValue(const std::string& tag) const
+{
+ for (auto entry : namedValues) {
+ if (entry.first == tag) {
+ return entry.second;
+ }
+ }
+ return "";
+}
+
+std::string xPLMessage::printXPLMessage() const
+{
+ std::string result;
+ result += "<" + getTypeString() + " ";
+
+ /* Source Info */
+ result += getSourceVDI();
+ result += " -> ";
+ /* Handle various target types */
+ if (isBroadcastMessage) {
+ result += "*";
+ } else {
+ if (isGroupMessage) {
+ result += "XPL-GROUP." + targetGroup;
+ } else {
+ result += getTargetVDI();
+ }
+ }
+
+ /* Print hop count */
+ result += " (" + lexical_cast<std::string>(hopcount) + " hops) ";
+
+ /* Echo Schema Info */
+ result += "[" + msgClass + "." + msgType + "]: ";
+
+ for (auto entry : namedValues) {
+ result += entry.first + "=" + entry.second + "\n";
+ }
+ replace_all(result, "\n", ";");
+
+ result += ">";
+
+ return result;
+}
+
+std::ostream& operator<<(std::ostream& os, const xPLMessage& msg)
+{
+ os << msg.printXPLMessage();
+ return os;
+}
+
Modified: xPLHAL/branches/thomas_s_dev/src/xplmessage.h
===================================================================
--- xPLHAL/branches/thomas_s_dev/src/xplmessage.h 2011-12-19 17:29:18 UTC (rev 600)
+++ xPLHAL/branches/thomas_s_dev/src/xplmessage.h 2011-12-19 21:35:14 UTC (rev 601)
@@ -33,30 +33,67 @@
*/
class xPLMessage
{
- public:
- /** \brief Type that contains all name-value pairs of one xPL message.
- * This type has to preserve the order of it's items and must support
- * multiple entries with the same key. So a map or multimap wan't sufficient.
- */
- typedef std::vector<std::pair<std::string,std::string> > namedValueList;
+ public:
+ /** \brief Type that contains all name-value pairs of one xPL message.
+ * This type has to preserve the order of it's items and must support
+ * multiple entries with the same key. So a map or multimap wan't sufficient.
+ */
+ typedef std::vector<std::pair<std::string,std::string> > namedValueList;
- xPL_MessageType type;
- std::string vendor;
- std::string deviceID;
- std::string instanceID;
- std::string msgClass;
- std::string msgType;
- namedValueList namedValues;
+ public:
+ xPL_MessageType type;
+ std::string vendor;
+ std::string deviceID;
+ std::string instanceID;
+ std::string msgClass;
+ std::string msgType;
- xPLMessage(
- const xPL_MessageType _type,
- const std::string& _vendor,
- const std::string& _deviceID,
- const std::string& _instanceID,
- const std::string& _msgClass,
- const std::string& _msgType,
- const namedValueList& _namedValues
- ) : type(_type), vendor(_vendor), deviceID(_deviceID), instanceID(_instanceID),
- msgClass(_msgClass), msgType(_msgType), namedValues(_namedValues) {}
+ namedValueList namedValues;
+
+ bool isBroadcastMessage;
+ bool isGroupMessage;
+
+ std::string targetVendor;
+ std::string targetDevice;
+ std::string targetInstance;
+ std::string targetGroup;
+
+ int hopcount;
+
+ xPLMessage(
+ const xPL_MessageType _type,
+ const std::string& _vendor,
+ const std::string& _deviceID,
+ const std::string& _instanceID,
+ const std::string& _msgClass,
+ const std::string& _msgType,
+ const namedValueList& _namedValues
+ ) : type(_type), vendor(_vendor), deviceID(_deviceID), instanceID(_instanceID),
+ msgClass(_msgClass), msgType(_msgType), namedValues(_namedValues)
+ {
+ }
+
+ xPLMessage(
+ const xPL_MessageType _type,
+ const std::string& _vdi,
+ const std::string& _msgClass,
+ const std::string& _msgType,
+ const namedValueList& _namedValues
+ ) : type(_type), msgClass(_msgClass), msgType(_msgType), namedValues(_namedValues)
+ {
+ setSourceFromVDI(_vdi);
+ }
+
+ std::string printXPLMessage() const;
+ std::string getTypeString() const;
+ std::string getSourceVDI() const;
+ std::string getTargetVDI() const;
+
+ bool setSourceFromVDI(const std::string& vdi);
+ bool setTargetFromVDI(const std::string& vdi);
+
+ std::string getNamedValue(const std::string& tag) const;
};
typedef boost::shared_ptr<xPLMessage> xPLMessagePtr;
+
+std::ostream& operator<<(std::ostream& os, const xPLMessage& msg);
Modified: xPLHAL/branches/thomas_s_dev/test/CMakeLists.txt
===================================================================
--- xPLHAL/branches/thomas_s_dev/test/CMakeLists.txt 2011-12-19 17:29:18 UTC (rev 600)
+++ xPLHAL/branches/thomas_s_dev/test/CMakeLists.txt 2011-12-19 21:35:14 UTC (rev 601)
@@ -23,11 +23,11 @@
file(GLOB_RECURSE UnitTests_sources *.cpp)
#set(UnitTest_tsrc ../src/devicemanager.cpp ../src/xplcache.cpp ../src/xplhandler.cpp ../src/xplmessagequeue.cpp)
-set(UnitTest_tsrc ../src/devicemanager.cpp)
+set(UnitTest_tsrc ../src/devicemanager.cpp ../src/xplmessage.cpp)
add_executable(test_runner ${UnitTests_sources} ${UnitTest_tsrc})
-include_directories(../src)
+include_directories(../src /usr/local/include)
enable_testing()
@@ -38,7 +38,7 @@
#message(STATUS "LIBRARIES=${LIBS}")
#message(STATUS "SOURCE=${SOURCES}")
#target_link_libraries(xPLHAL ${Boost_LIBRARIES} -dynamic xPL pthread -static)
-target_link_libraries(test_runner ${LIBS})
+target_link_libraries(test_runner ${LIBS} mockpp)
add_test(UnitTests test_runner)
Added: xPLHAL/branches/thomas_s_dev/test/mock_xplhandler.h
===================================================================
--- xPLHAL/branches/thomas_s_dev/test/mock_xplhandler.h (rev 0)
+++ xPLHAL/branches/thomas_s_dev/test/mock_xplhandler.h 2011-12-19 21:35:14 UTC (rev 601)
@@ -0,0 +1,62 @@
+#include <mockpp/visiting/CountedVisitableMethod.h>
+#include "i_xplhandler.h"
+
+using namespace mockpp;
+
+class MockXplHandler : public IxPLHandler, public VisitableMockObject
+{
+ public:
+ MockXplHandler()
+ : VisitableMockObject("MockXplHandler", 0)
+ , sendBroadcastMessage_mocker("sendBroadcastMessage", this)
+ , sendMessage_mocker("sendMessage", this)
+ , sendMessage2_mocker("sendMessage", this)
+ {}
+
+ void sendBroadcastMessage( const std::string& msgClass, const std::string& msgType, const xPLMessage::namedValueList& namedValues ) const
+ {
+#if 0
+ std::cout << "sendBroadcastMessage(";
+ std::cout << msgClass << ",";
+ std::cout << msgType << ",";
+ std::cout << std::endl;
+#endif
+ sendBroadcastMessage_mocker.forward(msgClass, msgType);
+ }
+// VisitableMockMethod<void, std::string, xPLMessage::namedValueList> sendBroadcastMessage_mocker;
+ VisitableMockMethod<void, std::string, std::string> sendBroadcastMessage_mocker;
+
+ /** \brief Send a directed message to the xPL network. */
+ 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
+ {
+#if 0
+ std::cout << "sendMessage(";
+ std::cout << type << ",";
+ std::cout << tgtVendor << "-" << tgtDeviceID << "." << tgtInstanceID << ",";
+ std::cout << msgClass << "," << msgType;
+ std::cout << std::endl;
+#endif
+ sendMessage_mocker.forward(type, tgtVendor, tgtDeviceID, tgtInstanceID, msgClass, msgType);
+ }
+// VisitableMockMethod<void, std::string, std::string, std::string, std::string, std::string, xPLMessage::namedValueList> sendMessage_mocker;
+ VisitableMockMethod<void, xPL_MessageType, std::string, std::string, std::string, std::string, std::string> sendMessage_mocker;
+
+ /** \brief Send a directed message to the xPL network. */
+ void sendMessage( const xPL_MessageType type, const std::string& VDI,
+ const std::string& msgClass, const std::string& msgType, const xPLMessage::namedValueList& namedValues ) const
+ {
+#if 0
+ std::cout << "sendMessage2(";
+ std::cout << type << ",";
+ std::cout << VDI << ",";
+ std::cout << msgClass << "," << msgType;
+ std::cout << std::endl;
+#endif
+ sendMessage2_mocker.forward(type, VDI, msgClass, msgType);
+ }
+// VisitableMockMethod<void, std::string, std::string, std::string, xPLMessage::namedValueList> sendMessage2_mocker;
+ VisitableMockMethod<void, xPL_MessageType, std::string, std::string, std::string> sendMessage2_mocker;
+};
+
Modified: xPLHAL/branches/thomas_s_dev/test/test_devicemanager.cpp
===================================================================
--- xPLHAL/branches/thomas_s_dev/test/test_devicemanager.cpp 2011-12-19 17:29:18 UTC (rev 600)
+++ xPLHAL/branches/thomas_s_dev/test/test_devicemanager.cpp 2011-12-19 21:35:14 UTC (rev 601)
@@ -1,10 +1,13 @@
#include "../src/devicemanager.h"
#define BOOST_TEST_MODULE "DeviceManager"
#include <boost/test/unit_test.hpp>
+//#include "mock_xplhandler.h"
//#include "xplcache.h"
#include "devicemanager.h"
+#include <boost/regex.hpp>
+
// load globas and give them their space to live
//#include "globals.h"
@@ -17,6 +20,7 @@
#endif
+#if 0
class MockXplHandler: public IxPLHandler
{
public:
@@ -43,6 +47,7 @@
std::cerr << std::endl;
}
};
+#endif
class MockXplCache: public IxPLCacheClass
{
@@ -83,10 +88,26 @@
virtual void saveCache( void ) const { }
};
+class MockSignalReceiver
+{
+ public:
+ void handleSignal(const xPLMessagePtr message)
+ {
+ std::cout << "received signal" << *message << std::endl;
+ }
+};
+
BOOST_AUTO_TEST_SUITE(DeviceManagerSuite);
BOOST_AUTO_TEST_CASE( add_device )
{
+ xPLMessage::namedValueList values;
+ values.push_back( std::make_pair( "tag", "value" ) );
+ xPLMessagePtr msg( new xPLMessage(xPL_MESSAGE_TRIGGER,
+ "pnxs", "hs485", "default1",
+ "blah", "blub",
+ values) );
+#if 0
xPL_MessagePtr msg = new xPL_Message;
msg->messageType = xPL_MESSAGE_TRIGGER;
msg->hopCount = 0;
@@ -103,18 +124,73 @@
msg->schemaClass = "blah";
msg->schemaType = "blub";
xPL_addMessageNamedValue(msg, "tag", "value");
+#endif
- MockXplHandler mockHandler;
+// MockXplHandler mockHandler;
MockXplCache mockCache;
- deviceManagerClass dm(&mockHandler, &mockCache);
+ deviceManagerClass dm(&mockCache);
BOOST_CHECK( dm.contains("pnxs-hs485.default1") == false);
dm.processHeartbeat(msg);
BOOST_CHECK( dm.contains("pnxs-hs485.default1") == true);
}
+#define m2s(match, group) std::string(match[group].first, match[group].second)
+
+//xPL_MessagePtr createXplMessage(const char* srcVDI, const char* dstVDI, const char* schema, std::map<std::string, std::string> tags)
+xPL_MessagePtr createXplMessage(xPL_MessageType msgType, const std::string& srcVDI, const std::string& dstVDI, const char* schema)
+{
+ std::string regex = R"(^([\w\d]+)-([\w\d]+)\.([\w\d]+)$)";
+ xPL_MessagePtr msg = new xPL_Message;
+ msg->messageType = msgType;
+ msg->hopCount = 0;
+ msg->receivedMessage = TRUE; /* TRUE if received, FALSE if being sent */
+ msg->isGroupMessage = FALSE;
+ msg->groupName = 0;
+ msg->isBroadcastMessage = TRUE;
+ msg->schemaClass = "blah";
+ msg->schemaType = "blub";
+// xPL_addMessageNamedValue(msg, "tag", "value");
+
+
+ boost::regex re_vdi(regex);
+ boost::smatch match;
+ if( ! boost::regex_match(srcVDI, match, re_vdi) ) {
+ delete msg;
+ return 0;
+ }
+ msg->sourceVendor = m2s(match, 1).c_str();
+ msg->sourceDeviceID = m2s(match, 2).c_str();
+ msg->sourceInstanceID = m2s(match, 3).c_str();
+
+ if( dstVDI == "*") {
+ msg->targetVendor = "*";
+ msg->targetDeviceID = "";
+ msg->targetInstanceID = "";
+ }
+ else {
+ if( ! boost::regex_match(dstVDI, match, re_vdi) ) {
+ delete msg;
+ return 0;
+ }
+ }
+ msg->targetVendor = m2s(match, 1).c_str();
+ msg->targetDeviceID = m2s(match, 2).c_str();
+ msg->targetInstanceID = m2s(match, 3).c_str();
+
+ return 0;
+}
+
BOOST_AUTO_TEST_CASE( remove_device )
{
+// xPL_MessagePtr mymsg = createXplMessage(xPL_MESSAGE_TRIGGER, "pnxs-hs485.default1", "*", "blah.blub");
+ xPLMessage::namedValueList values;
+ values.push_back( std::make_pair( "tag", "value" ) );
+ xPLMessagePtr msg( new xPLMessage(xPL_MESSAGE_TRIGGER,
+ "pnxs", "hs485", "default1",
+ "blah", "blub",
+ values) );
+#if 0
xPL_MessagePtr msg = new xPL_Message;
msg->messageType = xPL_MESSAGE_TRIGGER;
msg->hopCount = 0;
@@ -131,17 +207,27 @@
msg->schemaClass = "blah";
msg->schemaType = "blub";
xPL_addMessageNamedValue(msg, "tag", "value");
+#endif
- MockXplHandler mockHandler;
+// MockXplHandler mockHandler;
MockXplCache mockCache;
+ MockSignalReceiver sigrecv;
- deviceManagerClass dm(&mockHandler, &mockCache);
+// xPLMessage::namedValueList nvl;
+// mockHandler.sendMessage(xPL_MESSAGE_COMMAND, "pnxs", "hs485", "default1", "config", "list", nvl);
+// mockHandler.activate();
+
+ deviceManagerClass dm(&mockCache);
+ dm.m_sigSendXplMessage.connect(boost::bind(&MockSignalReceiver::handleSignal, &sigrecv, _1));
+
BOOST_CHECK( dm.contains("pnxs-hs485.default1") == false);
dm.processHeartbeat(msg);
BOOST_CHECK( dm.contains("pnxs-hs485.default1") == true);
dm.processRemove(msg);
BOOST_CHECK( dm.contains("pnxs-hs485.default1") == false);
+
+ //mockHandler.verify();
}
BOOST_AUTO_TEST_SUITE_END();
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|