Merge branch t102, this closes #102

This commit is contained in:
Micke Prag 2011-10-06 14:12:55 +00:00
parent 4ee0542c11
commit 9535ecd0ae
4 changed files with 117 additions and 32 deletions

View file

@ -12,7 +12,7 @@
using namespace TelldusCore;
TDDeviceEventDispatcher::TDDeviceEventDispatcher(CallbackStruct<TDDeviceEvent> *data, int id, int m, const std::string &strD)
:Thread(), d(data), deviceId(id), method(m), strData(strD)
:Thread(), d(data), deviceId(id), method(m), strData(strD), doneRunning(false)
{
d->mutex.lock();
this->start();
@ -23,13 +23,18 @@ TDDeviceEventDispatcher::~TDDeviceEventDispatcher() {
d->mutex.unlock();
}
bool TDDeviceEventDispatcher::done() const {
return doneRunning;
}
void TDDeviceEventDispatcher::run() {
d->event(deviceId, method, strData.c_str(), d->id, d->context);
doneRunning = true;
}
TDDeviceChangeEventDispatcher::TDDeviceChangeEventDispatcher(CallbackStruct<TDDeviceChangeEvent> *data, int id, int event, int type)
:Thread(), d(data), deviceId(id), changeEvent(event), changeType(type)
:Thread(), d(data), deviceId(id), changeEvent(event), changeType(type), doneRunning(false)
{
d->mutex.lock();
this->start();
@ -40,12 +45,17 @@ TDDeviceChangeEventDispatcher::~TDDeviceChangeEventDispatcher() {
d->mutex.unlock();
}
bool TDDeviceChangeEventDispatcher::done() const {
return doneRunning;
}
void TDDeviceChangeEventDispatcher::run() {
d->event(deviceId, changeEvent, changeType, d->id, d->context);
doneRunning = true;
}
TDRawDeviceEventDispatcher::TDRawDeviceEventDispatcher( CallbackStruct<TDRawDeviceEvent> *data, const std::string &strD, int id)
:Thread(), d(data), controllerId(id), strData(strD)
:Thread(), d(data), controllerId(id), strData(strD), doneRunning(false)
{
d->mutex.lock();
this->start();
@ -56,12 +66,17 @@ TDRawDeviceEventDispatcher::~TDRawDeviceEventDispatcher() {
d->mutex.unlock();
}
bool TDRawDeviceEventDispatcher::done() const {
return doneRunning;
}
void TDRawDeviceEventDispatcher::run() {
d->event(strData.c_str(), controllerId, d->id, d->context);
doneRunning = true;
}
TDSensorEventDispatcher::TDSensorEventDispatcher( CallbackStruct<TDSensorEvent> *data, const std::string &p, const std::string &m, int id, int type, const std::string &v, int t)
:Thread(), d(data), protocol(p), model(m), sensorId(id), dataType(type), value(v), timestamp(t)
:Thread(), d(data), protocol(p), model(m), sensorId(id), dataType(type), value(v), timestamp(t), doneRunning(false)
{
d->mutex.lock();
this->start();
@ -72,6 +87,11 @@ TDSensorEventDispatcher::~TDSensorEventDispatcher() {
d->mutex.unlock();
}
bool TDSensorEventDispatcher::done() const {
return doneRunning;
}
void TDSensorEventDispatcher::run() {
d->event(protocol.c_str(), model.c_str(), sensorId, dataType, value.c_str(), timestamp, d->id, d->context);
doneRunning = true;
}

View file

@ -27,8 +27,10 @@ namespace TelldusCore {
public:
TDDeviceEventDispatcher(CallbackStruct<TDDeviceEvent> *data, int deviceId, int method, const std::string &strData);
virtual ~TDDeviceEventDispatcher();
bool done() const;
protected:
virtual void run();
bool doneRunning;
private:
CallbackStruct<TDDeviceEvent> *d;
int deviceId, method;
@ -38,8 +40,10 @@ namespace TelldusCore {
public:
TDDeviceChangeEventDispatcher(CallbackStruct<TDDeviceChangeEvent> *data, int deviceId, int changeEvent, int changeType);
virtual ~TDDeviceChangeEventDispatcher();
bool done() const;
protected:
virtual void run();
bool doneRunning;
public:
CallbackStruct<TDDeviceChangeEvent> *d;
int deviceId, changeEvent, changeType;
@ -48,8 +52,10 @@ namespace TelldusCore {
public:
TDRawDeviceEventDispatcher( CallbackStruct<TDRawDeviceEvent> *data, const std::string &strData, int controllerId);
virtual ~TDRawDeviceEventDispatcher();
bool done() const;
protected:
virtual void run();
bool doneRunning;
private:
CallbackStruct<TDRawDeviceEvent> *d;
int controllerId;
@ -59,8 +65,10 @@ namespace TelldusCore {
public:
TDSensorEventDispatcher( CallbackStruct<TDSensorEvent> *data, const std::string &protocol, const std::string &model, int id, int dataType, const std::string &value, int timestamp);
virtual ~TDSensorEventDispatcher();
bool done() const;
protected:
virtual void run();
bool doneRunning;
private:
CallbackStruct<TDSensorEvent> *d;
std::string protocol;

View file

@ -35,6 +35,11 @@ public:
bool running, sensorCached;
std::wstring sensorCache;
TelldusCore::Mutex mutex;
std::list<std::tr1::shared_ptr<TDDeviceEventDispatcher> > deviceEventThreadList;
std::list<std::tr1::shared_ptr<TDDeviceChangeEventDispatcher> > deviceChangeEventThreadList;
std::list<std::tr1::shared_ptr<TDRawDeviceEventDispatcher> > rawDeviceEventThreadList;
std::list<std::tr1::shared_ptr<TDSensorEventDispatcher> > sensorEventThreadList;
};
Client *Client::instance = 0;
@ -72,46 +77,34 @@ Client *Client::getInstance() {
}
void Client::callbackDeviceEvent(int deviceId, int deviceState, const std::wstring &deviceStateValue){
std::list<std::tr1::shared_ptr<TDDeviceEventDispatcher> > list;
{
TelldusCore::MutexLocker locker(&d->mutex);
for(DeviceEventList::iterator callback_it = d->deviceEventList.begin(); callback_it != d->deviceEventList.end(); ++callback_it) {
std::tr1::shared_ptr<TDDeviceEventDispatcher> ptr(new TDDeviceEventDispatcher(*callback_it, deviceId, deviceState, TelldusCore::wideToString(deviceStateValue)));
list.push_back(ptr);
}
TelldusCore::MutexLocker locker(&d->mutex);
for(DeviceEventList::iterator callback_it = d->deviceEventList.begin(); callback_it != d->deviceEventList.end(); ++callback_it) {
std::tr1::shared_ptr<TDDeviceEventDispatcher> ptr(new TDDeviceEventDispatcher(*callback_it, deviceId, deviceState, TelldusCore::wideToString(deviceStateValue)));
d->deviceEventThreadList.push_back(ptr);
}
}
void Client::callbackDeviceChangeEvent(int deviceId, int eventDeviceChanges, int eventChangeType){
std::list<std::tr1::shared_ptr<TDDeviceChangeEventDispatcher> > list;
{
TelldusCore::MutexLocker locker(&d->mutex);
for(DeviceChangeList::iterator callback_it = d->deviceChangeEventList.begin(); callback_it != d->deviceChangeEventList.end(); ++callback_it) {
std::tr1::shared_ptr<TDDeviceChangeEventDispatcher> ptr(new TDDeviceChangeEventDispatcher(*callback_it, deviceId, eventDeviceChanges, eventChangeType));
list.push_back(ptr);
}
TelldusCore::MutexLocker locker(&d->mutex);
for(DeviceChangeList::iterator callback_it = d->deviceChangeEventList.begin(); callback_it != d->deviceChangeEventList.end(); ++callback_it) {
std::tr1::shared_ptr<TDDeviceChangeEventDispatcher> ptr(new TDDeviceChangeEventDispatcher(*callback_it, deviceId, eventDeviceChanges, eventChangeType));
d->deviceChangeEventThreadList.push_back(ptr);
}
}
void Client::callbackRawEvent(std::wstring command, int controllerId) {
std::list<std::tr1::shared_ptr<TDRawDeviceEventDispatcher> > list;
{
TelldusCore::MutexLocker locker(&d->mutex);
for(RawDeviceEventList::iterator callback_it = d->rawDeviceEventList.begin(); callback_it != d->rawDeviceEventList.end(); ++callback_it) {
std::tr1::shared_ptr<TDRawDeviceEventDispatcher> ptr(new TDRawDeviceEventDispatcher(*callback_it, TelldusCore::wideToString(command), controllerId));
list.push_back(ptr);
}
TelldusCore::MutexLocker locker(&d->mutex);
for(RawDeviceEventList::iterator callback_it = d->rawDeviceEventList.begin(); callback_it != d->rawDeviceEventList.end(); ++callback_it) {
std::tr1::shared_ptr<TDRawDeviceEventDispatcher> ptr(new TDRawDeviceEventDispatcher(*callback_it, TelldusCore::wideToString(command), controllerId));
d->rawDeviceEventThreadList.push_back(ptr);
}
}
void Client::callbackSensorEvent(const std::wstring &protocol, const std::wstring &model, int id, int dataType, const std::wstring &value, int timestamp) {
std::list<std::tr1::shared_ptr<TDSensorEventDispatcher> > list;
{
TelldusCore::MutexLocker locker(&d->mutex);
for(SensorEventList::iterator callback_it = d->sensorEventList.begin(); callback_it != d->sensorEventList.end(); ++callback_it) {
std::tr1::shared_ptr<TDSensorEventDispatcher> ptr(new TDSensorEventDispatcher(*callback_it, TelldusCore::wideToString(protocol), TelldusCore::wideToString(model), id, dataType, TelldusCore::wideToString(value), timestamp));
list.push_back(ptr);
}
TelldusCore::MutexLocker locker(&d->mutex);
for(SensorEventList::iterator callback_it = d->sensorEventList.begin(); callback_it != d->sensorEventList.end(); ++callback_it) {
std::tr1::shared_ptr<TDSensorEventDispatcher> ptr(new TDSensorEventDispatcher(*callback_it, TelldusCore::wideToString(protocol), TelldusCore::wideToString(model), id, dataType, TelldusCore::wideToString(value), timestamp));
d->sensorEventThreadList.push_back(ptr);
}
}
@ -222,9 +215,72 @@ void Client::run(){
clientMessage = L""; //cleanup, if message contained garbage/unhandled data
}
}
//Clean up finished callbacks
this->cleanupCallbacks();
}
}
void Client::cleanupCallbacks() {
bool again = false;
//Device Event
do {
again = false;
TelldusCore::MutexLocker locker(&d->mutex);
std::list<std::tr1::shared_ptr<TDDeviceEventDispatcher> >::iterator it = d->deviceEventThreadList.begin();
for (;it != d->deviceEventThreadList.end(); ++it) {
if ((*it)->done()) {
d->deviceEventThreadList.erase(it);
again = true;
break;
}
}
} while (again);
//Device Change Event
do {
again = false;
TelldusCore::MutexLocker locker(&d->mutex);
std::list<std::tr1::shared_ptr<TDDeviceChangeEventDispatcher> >::iterator it = d->deviceChangeEventThreadList.begin();
for (;it != d->deviceChangeEventThreadList.end(); ++it) {
if ((*it)->done()) {
d->deviceChangeEventThreadList.erase(it);
again = true;
break;
}
}
} while (again);
//Raw Device Event
do {
again = false;
TelldusCore::MutexLocker locker(&d->mutex);
std::list<std::tr1::shared_ptr<TDRawDeviceEventDispatcher> >::iterator it = d->rawDeviceEventThreadList.begin();
for (;it != d->rawDeviceEventThreadList.end(); ++it) {
if ((*it)->done()) {
d->rawDeviceEventThreadList.erase(it);
again = true;
break;
}
}
} while (again);
//Sensor Event
do {
again = false;
TelldusCore::MutexLocker locker(&d->mutex);
std::list<std::tr1::shared_ptr<TDSensorEventDispatcher> >::iterator it = d->sensorEventThreadList.begin();
for (;it != d->sensorEventThreadList.end(); ++it) {
if ((*it)->done()) {
d->sensorEventThreadList.erase(it);
again = true;
break;
}
}
} while (again);
}
std::wstring Client::sendToService(const Message &msg) {
Socket s;
s.connect(L"TelldusClient");

View file

@ -37,6 +37,7 @@ namespace TelldusCore {
private:
Client();
static std::wstring sendToService(const Message &msg);
void cleanupCallbacks();
class PrivateData;
PrivateData *d;