Update service statistics
authorDaniel Wagner <daniel.wagner@bmw-carit.de>
Wed, 30 Jun 2010 17:22:55 +0000 (19:22 +0200)
committerSamuel Ortiz <sameo@linux.intel.com>
Wed, 30 Jun 2010 17:28:16 +0000 (19:28 +0200)
Instead of collecting statistics on interface name base and storing it
local in counter.c, update the Service object.

counter.c maps interface names to Service objects.
The assumption is made that there is a 1:1 mapping between Service objects
and interface name.

A Counter object will only show Service object statistics for services in
the ready state. There is no interface (yet) for retrieving information on
Service objects in idle/failure/configuration/.. state.

doc/counter-api.txt
src/connman.h
src/counter.c
src/ipconfig.c
src/service.c
test/test-counter

index 593e932..14e5a01 100644 (file)
@@ -12,3 +12,22 @@ Methods              void Release()
                        cleanup tasks. There is no need to unregister the
                        counter, because when this method gets called it has
                        already been unregistered.
+
+               Usage(object service, dict)
+
+                       This signal indicates a change in the counter values
+                       for the service object. The counter is reset by calling
+                       the service ResetCounters method.
+
+                       The dict argument contains following entries:
+
+                               TX.Bytes
+
+                                       Total number of bytes sent.
+
+                               RX.Bytes
+
+                                       Total number of bytes received.
+
+                               Time
+                                       Total number of seconds online.
index 81eda65..1fdada3 100644 (file)
@@ -66,17 +66,22 @@ void __connman_agent_cleanup(void);
 int __connman_agent_register(const char *sender, const char *path);
 int __connman_agent_unregister(const char *sender, const char *path);
 
+struct connman_service;
+struct connman_ipconfig;
+
 int __connman_counter_register(const char *owner, const char *path,
                                                unsigned int interval);
 int __connman_counter_unregister(const char *owner, const char *path);
 
-void __connman_counter_notify(const char *interface,
+void __connman_counter_notify(struct connman_ipconfig *config,
                                unsigned int rx_bytes, unsigned int tx_bytes);
 
+int __connman_counter_add_service(struct connman_service *service);
+void __connman_counter_remove_service(struct connman_service *service);
+
 int __connman_counter_init(void);
 void __connman_counter_cleanup(void);
 
-struct connman_service;
 
 typedef void (* passphrase_cb_t) (struct connman_service *service,
                                const char *passphrase, void *user_data);
index bf9da90..f290179 100644 (file)
@@ -33,12 +33,6 @@ static GHashTable *stats_table;
 static GHashTable *counter_table;
 static GHashTable *owner_mapping;
 
-struct connman_stats {
-       char *interface;
-       unsigned int rx_bytes;
-       unsigned int tx_bytes;
-};
-
 struct connman_counter {
        char *owner;
        char *path;
@@ -46,14 +40,6 @@ struct connman_counter {
        guint watch;
 };
 
-static void remove_stats(gpointer user_data)
-{
-       struct connman_stats *stats = user_data;
-
-       g_free(stats->interface);
-       g_free(stats);
-}
-
 static void remove_counter(gpointer user_data)
 {
        struct connman_counter *counter = user_data;
@@ -130,10 +116,14 @@ int __connman_counter_unregister(const char *owner, const char *path)
 }
 
 static void send_usage(struct connman_counter *counter,
-                                       struct connman_stats *stats)
+                               struct connman_service *service)
 {
        DBusMessage *message;
        DBusMessageIter array, dict;
+       const char *service_path;
+       unsigned long rx_bytes;
+       unsigned long tx_bytes;
+       unsigned long time;
 
        message = dbus_message_new_method_call(counter->owner, counter->path,
                                        CONNMAN_COUNTER_INTERFACE, "Usage");
@@ -142,54 +132,48 @@ static void send_usage(struct connman_counter *counter,
 
        dbus_message_set_no_reply(message, TRUE);
 
+       service_path = __connman_service_get_path(service);
+       dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH,
+                                       &service_path, DBUS_TYPE_INVALID);
+
        dbus_message_iter_init_append(message, &array);
 
        connman_dbus_dict_open(&array, &dict);
 
-       connman_dbus_dict_append_basic(&dict, "Interface",
-                                       DBUS_TYPE_STRING, &stats->interface);
-       connman_dbus_dict_append_basic(&dict, "RX.Bytes",
-                                       DBUS_TYPE_UINT32, &stats->rx_bytes);
-       connman_dbus_dict_append_basic(&dict, "TX.Bytes",
-                                       DBUS_TYPE_UINT32, &stats->tx_bytes);
+       rx_bytes = __connman_service_stats_get_rx_bytes(service);
+       tx_bytes = __connman_service_stats_get_tx_bytes(service);
+       time = __connman_service_stats_get_time(service);
+
+       connman_dbus_dict_append_basic(&dict, "RX.Bytes", DBUS_TYPE_UINT32,
+                               &rx_bytes);
+       connman_dbus_dict_append_basic(&dict, "TX.Bytes", DBUS_TYPE_UINT32,
+                               &tx_bytes);
+       connman_dbus_dict_append_basic(&dict, "Time", DBUS_TYPE_UINT32,
+                               &time);
 
        connman_dbus_dict_close(&array, &dict);
 
        g_dbus_send_message(connection, message);
 }
 
-void __connman_counter_notify(const char *interface,
+void __connman_counter_notify(struct connman_ipconfig *config,
                                unsigned int rx_bytes, unsigned int tx_bytes)
 {
-       struct connman_stats *stats;
+       struct connman_service *service;
        GHashTableIter iter;
        gpointer key, value;
 
-       stats = g_hash_table_lookup(stats_table, interface);
-       if (stats != NULL)
-               goto update;
-
-       stats = g_try_new0(struct connman_stats, 1);
-       if (stats == NULL)
-               return;
-
-       stats->interface = g_strdup(interface);
-
-       g_hash_table_replace(stats_table, stats->interface, stats);
-
-update:
-       if (stats->rx_bytes == rx_bytes && stats->tx_bytes == tx_bytes)
+       service = g_hash_table_lookup(stats_table, config);
+       if (service == NULL)
                return;
 
-       stats->rx_bytes = rx_bytes;
-       stats->tx_bytes = tx_bytes;
+       __connman_service_stats_update(service, rx_bytes, tx_bytes);
 
        g_hash_table_iter_init(&iter, counter_table);
-
        while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
                struct connman_counter *counter = value;
 
-               send_usage(counter, stats);
+               send_usage(counter, service);
        }
 }
 
@@ -210,6 +194,24 @@ static void release_counter(gpointer key, gpointer value, gpointer user_data)
        g_dbus_send_message(connection, message);
 }
 
+int __connman_counter_add_service(struct connman_service *service)
+{
+       struct connman_ipconfig *config;
+
+       config = __connman_service_get_ipconfig(service);
+       g_hash_table_replace(stats_table, config, service);
+
+       return 0;
+}
+
+void __connman_counter_remove_service(struct connman_service *service)
+{
+       struct connman_ipconfig *config;
+
+       config = __connman_service_get_ipconfig(service);
+       g_hash_table_remove(stats_table, config);
+}
+
 int __connman_counter_init(void)
 {
        DBG("");
@@ -218,8 +220,8 @@ int __connman_counter_init(void)
        if (connection == NULL)
                return -1;
 
-       stats_table = g_hash_table_new_full(g_str_hash, g_str_equal,
-                                                       NULL, remove_stats);
+       stats_table = g_hash_table_new_full(g_direct_hash, g_str_equal,
+                                                       NULL, NULL);
 
        counter_table = g_hash_table_new_full(g_str_hash, g_str_equal,
                                                        NULL, remove_counter);
index ac26203..51c4619 100644 (file)
@@ -367,7 +367,7 @@ static void update_stats(struct connman_ipdevice *ipdevice,
        ipdevice->rx_bytes = stats->rx_bytes;
        ipdevice->tx_bytes = stats->tx_bytes;
 
-       __connman_counter_notify(ipdevice->ifname,
+       __connman_counter_notify(ipdevice->config,
                                ipdevice->rx_bytes, ipdevice->tx_bytes);
 }
 
index ff3e8c8..fd694e8 100644 (file)
@@ -371,6 +371,8 @@ static void __connman_service_stats_start(struct connman_service *service)
        service->stats.time_start = service->stats.time;
 
        g_timer_start(service->stats.timer);
+
+       __connman_counter_add_service(service);
 }
 
 static void __connman_service_stats_stop(struct connman_service *service)
@@ -382,6 +384,8 @@ static void __connman_service_stats_stop(struct connman_service *service)
        if (service->stats.timer == NULL)
                return;
 
+       __connman_counter_remove_service(service);
+
        g_timer_stop(service->stats.timer);
 
        seconds = g_timer_elapsed(service->stats.timer, NULL);
index 70a3027..c32cb9e 100755 (executable)
@@ -15,14 +15,12 @@ class Counter(dbus.service.Object):
                mainloop.quit()
 
        @dbus.service.method("org.moblin.connman.Counter",
-                                       in_signature='a{sv}', out_signature='')
-       def Usage(self, stats):
+                                       in_signature='oa{sv}', out_signature='')
+       def Usage(self, path, stats):
+               print "%s" % (path)
                for key in stats.keys():
-                       if key in ["Interface"]:
-                               val = str(stats[key])
-                       else:
-                               val = int(stats[key])
-                       print "%s = %s" % (key, val)
+                       val = int(stats[key])
+                       print "  %s = %s" % (key, val)
 
 if __name__ == '__main__':
        dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)