Converted type EVENT to a class instead
This commit is contained in:
parent
3be92d58df
commit
801ac343cc
7 changed files with 87 additions and 27 deletions
|
@ -39,6 +39,7 @@ IF (APPLE) #### Mac OS X ####
|
|||
)
|
||||
LIST(APPEND telldus-service_SRCS
|
||||
main_unix.cpp
|
||||
Event_unix.cpp
|
||||
EventHandler_unix.cpp
|
||||
)
|
||||
|
||||
|
@ -67,6 +68,7 @@ ELSE (APPLE) #### Linux ####
|
|||
SET( telldus-service_TARGET telldusd )
|
||||
LIST(APPEND telldus-service_SRCS
|
||||
main_unix.cpp
|
||||
Event_unix.cpp
|
||||
EventHandler_unix.cpp
|
||||
)
|
||||
LIST(APPEND telldus-service_LIBRARIES
|
||||
|
|
|
@ -10,7 +10,7 @@ class ConnectionListener
|
|||
public:
|
||||
ConnectionListener(const std::wstring &name);
|
||||
virtual ~ConnectionListener(void);
|
||||
void listen(EVENT waitEvent);
|
||||
void listen(Event *waitEvent);
|
||||
Socket *retrieveClientSocket();
|
||||
|
||||
private:
|
||||
|
|
|
@ -1,12 +1,24 @@
|
|||
#ifndef EVENT_H
|
||||
#define EVENT_H
|
||||
|
||||
#ifdef _WINDOWS
|
||||
#include <windows.h>
|
||||
typedef HANDLE EVENT;
|
||||
#else
|
||||
#include <pthread.h>
|
||||
typedef pthread_cond_t EVENT;
|
||||
#endif
|
||||
class EventHandler;
|
||||
|
||||
class Event {
|
||||
public:
|
||||
Event(EventHandler *handler);
|
||||
virtual ~Event();
|
||||
|
||||
bool isSignaled();
|
||||
void signal();
|
||||
|
||||
protected:
|
||||
void setSignaled();
|
||||
|
||||
private:
|
||||
class PrivateData;
|
||||
PrivateData *d;
|
||||
|
||||
friend class EventHandler;
|
||||
};
|
||||
|
||||
#endif //EVENT_H
|
||||
|
|
|
@ -1,19 +1,18 @@
|
|||
#ifndef EVENTHANDLER_H
|
||||
#define EVENTHANDLER_H
|
||||
|
||||
#include "Event.h"
|
||||
class Event;
|
||||
|
||||
class EventHandler {
|
||||
public:
|
||||
EventHandler();
|
||||
virtual ~EventHandler(void);
|
||||
|
||||
void addEvent(EVENT event);
|
||||
EVENT addEvent();
|
||||
Event *addEvent();
|
||||
|
||||
EVENT waitForAny();
|
||||
void signal(Event *event);
|
||||
void waitForAny();
|
||||
|
||||
static EVENT createEvent();
|
||||
private:
|
||||
class PrivateData;
|
||||
PrivateData *d;
|
||||
|
|
|
@ -1,28 +1,46 @@
|
|||
#include "EventHandler.h"
|
||||
#include "Event.h"
|
||||
#include <pthread.h>
|
||||
|
||||
class EventHandler::PrivateData {
|
||||
public:
|
||||
pthread_cond_t event;
|
||||
pthread_mutex_t mutex;
|
||||
bool hasEvent;
|
||||
};
|
||||
|
||||
EventHandler::EventHandler() {
|
||||
d = new PrivateData;
|
||||
pthread_cond_init(&d->event, NULL);
|
||||
pthread_mutex_init(&d->mutex, NULL);
|
||||
}
|
||||
|
||||
EventHandler::~EventHandler(void) {
|
||||
pthread_mutex_destroy(&d->mutex);
|
||||
pthread_cond_destroy(&d->event);
|
||||
delete d;
|
||||
}
|
||||
|
||||
void EventHandler::addEvent(EVENT event) {
|
||||
}
|
||||
|
||||
EVENT EventHandler::addEvent() {
|
||||
EVENT event = EventHandler::createEvent();
|
||||
this->addEvent(event);
|
||||
Event *EventHandler::addEvent() {
|
||||
Event *event = new Event(this);
|
||||
return event;
|
||||
}
|
||||
|
||||
EVENT EventHandler::waitForAny() {
|
||||
void EventHandler::signal(Event *event) {
|
||||
pthread_mutex_lock(&d->mutex);
|
||||
event->setSignaled();
|
||||
d->hasEvent = true;
|
||||
pthread_cond_signal(&d->event);
|
||||
pthread_mutex_unlock(&d->mutex);
|
||||
}
|
||||
|
||||
EVENT EventHandler::createEvent() {
|
||||
void EventHandler::waitForAny() {
|
||||
pthread_mutex_lock(&d->mutex);
|
||||
while(!d->hasEvent) {
|
||||
pthread_cond_wait(&d->event, &d->mutex);
|
||||
}
|
||||
d->hasEvent = false;
|
||||
pthread_mutex_unlock(&d->mutex);
|
||||
|
||||
return;
|
||||
}
|
29
telldus-core/service/Event_unix.cpp
Normal file
29
telldus-core/service/Event_unix.cpp
Normal file
|
@ -0,0 +1,29 @@
|
|||
#include "Event.h"
|
||||
#include "EventHandler.h"
|
||||
|
||||
class Event::PrivateData {
|
||||
public:
|
||||
bool signaled;
|
||||
EventHandler *handler;
|
||||
};
|
||||
|
||||
Event::Event(EventHandler *handler) {
|
||||
d = new PrivateData;
|
||||
d->signaled = false;
|
||||
}
|
||||
|
||||
Event::~Event(void) {
|
||||
delete d;
|
||||
}
|
||||
|
||||
bool Event::isSignaled() {
|
||||
return d->signaled;
|
||||
}
|
||||
|
||||
void Event::signal() {
|
||||
d->handler->signal(this);
|
||||
}
|
||||
|
||||
void Event::setSignaled() {
|
||||
d->signaled = true;
|
||||
}
|
|
@ -13,7 +13,7 @@ TelldusMain::~TelldusMain(void)
|
|||
|
||||
void TelldusMain::start(void){
|
||||
EventHandler eventHandler;
|
||||
EVENT clientEvent = eventHandler.addEvent();
|
||||
Event *clientEvent = eventHandler.addEvent();
|
||||
|
||||
ConnectionListener clientListener(L"TelldusClient");
|
||||
//TODO: eventlistener
|
||||
|
@ -24,15 +24,15 @@ void TelldusMain::start(void){
|
|||
while(running) {
|
||||
|
||||
|
||||
EVENT signaledEvent = eventHandler.waitForAny();
|
||||
if (signaledEvent == clientEvent) {
|
||||
//Event *signaledEvent = eventHandler.waitForAny();
|
||||
/*if (signaledEvent == clientEvent) {
|
||||
//New client connection
|
||||
Socket *s = clientListener.retrieveClientSocket();
|
||||
std::wstring clientMessage = s->read();
|
||||
|
||||
delete s; //TODO: Cleanup
|
||||
clientListener.listen(clientEvent);
|
||||
}
|
||||
}*/
|
||||
#ifdef _WINDOWS
|
||||
Sleep(1000);
|
||||
#else
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue