dnsproxy: Only one copy of the relevant buffers will be made to a TCP request
[framework/connectivity/connman.git] / src / counter.c
index 9dea4b7..1dd7d6d 100644 (file)
@@ -2,7 +2,7 @@
  *
  *  Connection Manager
  *
- *  Copyright (C) 2007-2010  Intel Corporation. All rights reserved.
+ *  Copyright (C) 2007-2012  Intel Corporation. All rights reserved.
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License version 2 as
@@ -23,6 +23,8 @@
 #include <config.h>
 #endif
 
+#include <errno.h>
+
 #include <gdbus.h>
 
 #include "connman.h"
@@ -35,7 +37,7 @@ static GHashTable *owner_mapping;
 struct connman_counter {
        char *owner;
        char *path;
-       guint timeout;
+       unsigned int interval;
        guint watch;
 };
 
@@ -45,11 +47,9 @@ static void remove_counter(gpointer user_data)
 
        DBG("owner %s path %s", counter->owner, counter->path);
 
-       if (counter->watch > 0)
-               g_dbus_remove_watch(connection, counter->watch);
+       __connman_rtnl_update_interval_remove(counter->interval);
 
-       if (counter->timeout > 0)
-               g_source_remove(counter->timeout);
+       __connman_service_counter_unregister(counter->path);
 
        g_free(counter->owner);
        g_free(counter->path);
@@ -66,29 +66,11 @@ static void owner_disconnect(DBusConnection *connection, void *user_data)
        g_hash_table_remove(counter_table, counter->path);
 }
 
-static gboolean counter_timeout(gpointer user_data)
-{
-       struct connman_counter *counter = user_data;
-       DBusMessage *message;
-
-       DBG("owner %s path %s", counter->owner, counter->path);
-
-       message = dbus_message_new_method_call(counter->owner, counter->path,
-                                       CONNMAN_COUNTER_INTERFACE, "Usage");
-       if (message == NULL)
-               return TRUE;
-
-       dbus_message_set_no_reply(message, TRUE);
-
-       g_dbus_send_message(connection, message);
-
-       return TRUE;
-}
-
 int __connman_counter_register(const char *owner, const char *path,
                                                unsigned int interval)
 {
        struct connman_counter *counter;
+       int err;
 
        DBG("owner %s path %s interval %u", owner, path, interval);
 
@@ -103,11 +85,19 @@ int __connman_counter_register(const char *owner, const char *path,
        counter->owner = g_strdup(owner);
        counter->path = g_strdup(path);
 
+       err = __connman_service_counter_register(counter->path);
+       if (err < 0) {
+               g_free(counter->owner);
+               g_free(counter->path);
+               g_free(counter);
+               return err;
+       }
+
        g_hash_table_replace(counter_table, counter->path, counter);
        g_hash_table_replace(owner_mapping, counter->owner, counter);
 
-       counter->timeout = g_timeout_add_seconds(interval,
-                                               counter_timeout, counter);
+       counter->interval = interval;
+       __connman_rtnl_update_interval_add(counter->interval);
 
        counter->watch = g_dbus_add_disconnect_watch(connection, owner,
                                        owner_disconnect, counter, NULL);
@@ -128,12 +118,33 @@ int __connman_counter_unregister(const char *owner, const char *path)
        if (g_strcmp0(owner, counter->owner) != 0)
                return -EACCES;
 
+       if (counter->watch > 0)
+               g_dbus_remove_watch(connection, counter->watch);
+
        g_hash_table_remove(owner_mapping, counter->owner);
        g_hash_table_remove(counter_table, counter->path);
 
        return 0;
 }
 
+void __connman_counter_send_usage(const char *path,
+                                       DBusMessage *message)
+{
+       struct connman_counter *counter;
+
+       counter = g_hash_table_lookup(counter_table, path);
+       if (counter == NULL)
+               return;
+
+       dbus_message_set_destination(message, counter->owner);
+       dbus_message_set_path(message, counter->path);
+       dbus_message_set_interface(message, CONNMAN_COUNTER_INTERFACE);
+       dbus_message_set_member(message, "Usage");
+       dbus_message_set_no_reply(message, TRUE);
+
+       g_dbus_send_message(connection, message);
+}
+
 static void release_counter(gpointer key, gpointer value, gpointer user_data)
 {
        struct connman_counter *counter = value;
@@ -141,6 +152,9 @@ static void release_counter(gpointer key, gpointer value, gpointer user_data)
 
        DBG("owner %s path %s", counter->owner, counter->path);
 
+       if (counter->watch > 0)
+               g_dbus_remove_watch(connection, counter->watch);
+
        message = dbus_message_new_method_call(counter->owner, counter->path,
                                        CONNMAN_COUNTER_INTERFACE, "Release");
        if (message == NULL)