made obd2 updates happen on an idle callback
authorKevron Rees <tripzero.kev@gmail.com>
Sun, 30 Sep 2012 21:23:06 +0000 (14:23 -0700)
committerKevron Rees <tripzero.kev@gmail.com>
Sun, 30 Sep 2012 21:23:06 +0000 (14:23 -0700)
plugins/obd2plugin/asyncqueuewatcher.cpp
plugins/obd2plugin/asyncqueuewatcher.h
plugins/obd2plugin/obd2source.cpp

index f8ed09f..7a9d378 100644 (file)
@@ -18,74 +18,43 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
 #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);
-}
index aa2dc50..0ff02a8 100644 (file)
@@ -18,17 +18,24 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 #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;
 };
 
 
index f86df36..0aea6dc 100644 (file)
@@ -276,13 +276,11 @@ void threadLoop(gpointer data)
        }
        
 }
-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")
@@ -333,6 +331,8 @@ static void updateProperties(gpointer retval, gpointer data)
                //46 interior temp
                delete reply;
        }
+
+       return true;
 }
 void OBD2Source::updateProperty(VehicleProperty::Property property,AbstractPropertyType* value)
 {
@@ -421,6 +421,7 @@ void OBD2Source::setConfiguration(map<string, string> config)
        requ->arg = port + ":" + baud;
        g_async_queue_push(commandQueue,requ);
 }
+
 OBD2Source::OBD2Source(AbstractRoutingEngine *re, map<string, string> config) : AbstractSource(re, config)
 {
        clientConnected = false;
@@ -449,7 +450,11 @@ OBD2Source::OBD2Source(AbstractRoutingEngine *re, map<string, string> config) :
 
        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()