[gnome-talos/wip/events] Some bits for events



commit f05ace710a453ceb4682d8af9d3d0083c7c351f2
Author: Colin Walters <walters verbum org>
Date:   Thu Oct 13 08:43:12 2011 -0400

    Some bits for events

 README            |    6 --
 src/gnome-talos.c |  134 +++++++++++++++++++++++++++++++++++++++++++++++++---
 2 files changed, 126 insertions(+), 14 deletions(-)
---
diff --git a/README b/README
index 59f6678..3277a7d 100644
--- a/README
+++ b/README
@@ -46,9 +46,3 @@ OPEN QUESTIONS:
   probably shouldn't expose the pid/uid either).
 
   This would need a server to accept data.
-
-
-
-
-
-
diff --git a/src/gnome-talos.c b/src/gnome-talos.c
index 2851063..e48454f 100644
--- a/src/gnome-talos.c
+++ b/src/gnome-talos.c
@@ -26,6 +26,11 @@
 #include <json-glib/json-glib.h>
 
 #include <string.h>
+#include <stdlib.h>
+
+#ifdef G_OS_UNIX
+#include <gio/gunixfdlist.h>
+#endif
 
 #include "gnome-talos-procinfo.h"
 #include "gnome-talos-gpu.h"
@@ -49,13 +54,115 @@ static gint timeout = 5;
 static char *log_file_path = NULL;
 static char *to_html_file_path = NULL;
 
+static GDBusNodeInfo *introspection_data = NULL;
+
+typedef struct {
+  GFile *log_file;
+  GFileOutputStream *out;
+  GMainLoop *loop;
+  
+  GPtrArray *pending_events; /* multistring component\0description\0 */
+} GnomeTalosApp;
+
+static const gchar introspection_xml[] =
+  "<node>"
+  "  <interface name='org.gnome.Talos'>"
+  "    <method name='Event'>"
+  "      <arg type='t' name='timestamp' direction='in'/>"
+  "      <arg type='s' name='component' direction='in'/>"
+  "      <arg type='s' name='description' direction='in'/>"
+  "    </method>"
+  "  </interface>"
+  "</node>";
+
+static void
+handle_method_call (GDBusConnection       *connection,
+                    const gchar           *sender,
+                    const gchar           *object_path,
+                    const gchar           *interface_name,
+                    const gchar           *method_name,
+                    GVariant              *parameters,
+                    GDBusMethodInvocation *invocation,
+                    gpointer               user_data)
+{
+  GnomeTalosApp *app = user_data;
+
+  if (g_strcmp0 (method_name, "Event") == 0)
+    {
+      g_ptr_array_add (app->pending_events, g_variant_ref_sink (parameters));
+      if (!(g_dbus_message_get_flags (g_dbus_method_invocation_get_message (invocation))
+            & G_DBUS_MESSAGE_FLAGS_NO_REPLY_EXPECTED))
+        {
+          g_dbus_method_invocation_return_value (invocation, NULL);
+        }
+    }
+}
+
+static const GDBusInterfaceVTable interface_vtable =
+{
+  handle_method_call,
+  NULL,
+  NULL
+};
+
+static void
+on_bus_acquired (GDBusConnection *connection,
+                 const gchar     *name,
+                 gpointer         user_data)
+{
+  guint registration_id;
+
+  registration_id = g_dbus_connection_register_object (connection,
+                                                       "/org/gnome/Talos",
+                                                       introspection_data->interfaces[0],
+                                                       &interface_vtable,
+                                                       NULL,
+                                                       NULL,
+                                                       NULL);
+  g_assert (registration_id > 0);
+}
+
+static void
+on_name_acquired (GDBusConnection *connection,
+                  const gchar     *name,
+                  gpointer         user_data)
+{
+}
+
+static void
+on_name_lost (GDBusConnection *connection,
+              const gchar     *name,
+              gpointer         user_data)
+{
+  exit (1);
+}
+
+static GVariant *
+consume_events (GnomeTalosApp *app)
+{
+  GVariantBuilder builder;
+  int i;
+
+  g_variant_builder_init (&builder, G_VARIANT_TYPE ("(a(tss))"));
+
+  for (i = 0; i < app->pending_events->len; i++)
+    {
+      GVariant *event = app->pending_events->pdata[i];
+      g_variant_builder_add (&builder, "@(tss)", event);
+      g_variant_unref (event);
+    }
+  
+  return g_variant_builder_end (&builder);
+}
+
 static GVariant *
-gather_snapshot (void)
+gather_snapshot (GnomeTalosApp *app)
 {
   GError *error = NULL;
   gint64 start_timestamp, end_timestamp;
   GVariant *proc_data;
   GVariant *gpu_data;
+  GVariant *event_data;
   GVariantBuilder builder;
   GVariant *data;
 
@@ -83,6 +190,8 @@ gather_snapshot (void)
   /* data */
   g_variant_builder_open (&builder, G_VARIANT_TYPE ("a{sv}"));
 
+  events = consume_events (app);
+  g_variant_builder_add (&builder, "{sv}", "events", events);
   g_variant_builder_add (&builder, "{sv}", "processes", proc_data);
   g_variant_builder_add (&builder, "{sv}", "gpu", gpu_data);
 
@@ -116,12 +225,6 @@ write_variant (GVariant       *variant,
   return ret;
 }
 
-typedef struct {
-  GFile *log_file;
-  GFileOutputStream *out;
-  GMainLoop *loop;
-} GnomeTalosApp;
-
 static gboolean
 timeout_gather_data (gpointer user_data)
 {
@@ -129,7 +232,7 @@ timeout_gather_data (gpointer user_data)
   GVariant *snapshot;
   GError *error = NULL;
 
-  snapshot = gather_snapshot ();
+  snapshot = gather_snapshot (app);
   if (!write_variant (snapshot, NULL, (GOutputStream*)app->out, &error))
     fatal_gerror (&error);
   g_variant_unref (snapshot);
@@ -168,6 +271,16 @@ run_collection (GnomeTalosApp *app)
   GVariant *meta;
   GVariant *sysinfo;
   GVariant *gpu_meta;
+  guint owner_id;
+
+  owner_id = g_bus_own_name (G_BUS_TYPE_SESSION,
+                             "org.gnome.Talos",
+                             G_BUS_NAME_OWNER_FLAGS_NONE,
+                             on_bus_acquired,
+                             on_name_acquired,
+                             on_name_lost,
+                             NULL,
+                             NULL);
 
   g_unix_signal_add (SIGINT,
                      on_interrupt_received,
@@ -176,6 +289,8 @@ run_collection (GnomeTalosApp *app)
                      on_interrupt_received,
                      app);
 
+  app->pending_events = g_ptr_array_new ();
+
   app->out = g_file_replace (app->log_file, NULL, FALSE,
                              G_FILE_CREATE_REPLACE_DESTINATION,
                              NULL, &error);
@@ -322,6 +437,9 @@ main (int    argc,
   app.loop = g_main_loop_new (NULL, TRUE);
   app.log_file = g_file_new_for_path (log_file_path);
 
+  introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, NULL);
+  g_assert (introspection_data != NULL);
+
   if (to_html_file_path == NULL)
     run_collection (&app);
   else



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]