#include "asyncqueuewatcher.h"
-struct QueueWatch{
- GSource source;
- GAsyncQueue* queue;
-};
+AsyncQueueWatcher::AsyncQueueWatcher(GAsyncQueue *q, AsyncQueueWatcherCallback cb, void *data)
+ :Glib::Source()
+{
+ queue = g_async_queue_ref(q);
+ userData = data;
+ callback = cb;
+ set_priority(G_PRIORITY_DEFAULT);
+ //set_can_recurse(true);
+
+}
-static gboolean prepare (GSource *source, gint *timeout)
+bool AsyncQueueWatcher::prepare(int &timeout)
{
- QueueWatch *watch = (QueueWatch *)source;
- *timeout = -1;
- int size = g_async_queue_length (watch->queue);
+ timeout = -1;
+ int size = g_async_queue_length (queue);
return (size > 0);
}
-static gboolean check (GSource *source)
+bool AsyncQueueWatcher::check()
{
- QueueWatch *watch = (QueueWatch *)source;
- return (g_async_queue_length (watch->queue) > 0);
+ return (g_async_queue_length (queue) > 0);
}
-static gboolean dispatch (GSource *source, GSourceFunc callback, gpointer data)
+bool AsyncQueueWatcher::dispatch(sigc::slot_base *)
{
- QueueWatch *watch = (QueueWatch *)source;
- AsyncQueueWatcherCallback cb = (AsyncQueueWatcherCallback)callback;
-
- gpointer item = g_async_queue_try_pop (watch->queue);
+ gpointer item = g_async_queue_try_pop (queue);
if (item == NULL)
{
return true;
}
- if (cb == NULL)
+ if (callback == NULL)
{
return false;
}
- cb(item, data);
+ callback(item, userData);
return true;
}
-
-static void finalize (GSource *source)
-{
- QueueWatch *watch = (QueueWatch *)source;
-
- if (watch->queue) {
- g_async_queue_unref(watch->queue);
- watch->queue = NULL;
- }
-}
-
-static GSourceFuncs funcs = {
- prepare,
- check,
- dispatch,
- finalize
-};
-
-
-AsyncQueueWatcher::AsyncQueueWatcher(GAsyncQueue *queue, AsyncQueueWatcherCallback callback, void *data)
-{
- GSource *src = g_source_new(&funcs, sizeof(QueueWatch));
-
- QueueWatch* watch = (QueueWatch*)src;
- watch->queue = g_async_queue_ref(queue);
-
- g_source_set_callback(src, (GSourceFunc)callback, data, NULL);
-
- id = g_source_attach(src,NULL);
-
- g_source_unref(src);
-}
#ifndef ASYNCQUEUEWATCHER_H_
#define ASYNCQUEUEWATCHER_H_
-#include <glib.h>
+#include <glibmm/main.h>
typedef void (*AsyncQueueWatcherCallback) (gpointer, void* data);
-class AsyncQueueWatcher
+class AsyncQueueWatcher: public Glib::Source
{
public:
- AsyncQueueWatcher(GAsyncQueue* queue, AsyncQueueWatcherCallback callback, void* data);
+ AsyncQueueWatcher(GAsyncQueue* q, AsyncQueueWatcherCallback cb, void* data);
+
+ bool prepare(int& timeout);
+ bool check();
+ bool dispatch(sigc::slot_base* slot);
private:
int id;
+ GAsyncQueue* queue;
+ AsyncQueueWatcherCallback callback;
+ void* userData;
};
}
}
-static void updateProperties(gpointer retval, gpointer data)
+static int updateProperties(/*gpointer retval,*/ gpointer data)
{
OBD2Source* src = (OBD2Source*)data;
- //src->randomizeProperties();
- //gpointer retval = g_async_queue_try_pop(src->responseQueue);
- if (retval != nullptr)
+ while(gpointer retval = g_async_queue_try_pop(src->responseQueue))
{
ObdReply *reply = (ObdReply*)retval;
if (reply->req == "05")
//46 interior temp
delete reply;
}
+
+ return true;
}
void OBD2Source::updateProperty(VehicleProperty::Property property,AbstractPropertyType* value)
{
requ->arg = port + ":" + baud;
g_async_queue_push(commandQueue,requ);
}
+
OBD2Source::OBD2Source(AbstractRoutingEngine *re, map<string, string> config) : AbstractSource(re, config)
{
clientConnected = false;
setConfiguration(config);
- AsyncQueueWatcher watcher(responseQueue, (AsyncQueueWatcherCallback) updateProperties, this);
+ //AsyncQueueWatcher * watcher = new AsyncQueueWatcher(responseQueue, (AsyncQueueWatcherCallback) updateProperties, this);
+
+ //g_timeout_add(1,updateProperties, this);
+ g_idle_add(updateProperties, this);
+
}
PropertyList OBD2Source::supported()