/soc/2012/sanket/statscollector-2.x.y: 88aa23762c8d: Send statis...

Sanket Agarwal sanket at soc.pidgin.im
Tue Jul 10 00:36:25 EDT 2012


Changeset: 88aa23762c8dd70d223b83b9cd3a7bdd0c47f53a
Author:	 Sanket Agarwal <sanket at soc.pidgin.im>
Date:	 2012-05-30 09:15 +0000
Branch:	 soc.2012.statscollector
URL: http://hg.pidgin.im/soc/2012/sanket/statscollector-2.x.y/rev/88aa23762c8d

Description:

Send statistics to a predefined server; implemented timed sending

Statistics can now be sent to a server accepting cross-site POST requests
in the form of a string containing XML informaton. I have also implemented
helper functions which trigger this sending every few fixed time intervals.

diffstat:

 libpurple/plugins/statscollector.c |  105 +++++++++++++++++++++++++++++++++++-
 1 files changed, 100 insertions(+), 5 deletions(-)

diffs (161 lines):

diff --git a/libpurple/plugins/statscollector.c b/libpurple/plugins/statscollector.c
--- a/libpurple/plugins/statscollector.c
+++ b/libpurple/plugins/statscollector.c
@@ -26,6 +26,12 @@
 #include<sys/sysctl.h>
 #endif
 
+/* Timeout */
+#define RESEND_SEC 40
+
+/* Sending URL */
+#define SEND_URL "http://localhost:8000/collectstats/collect"
+
 /* POSIX compliance is an issue that I have looked into some detail now
  * It seems like presence of unistd.h and _POSIX_VERSION being defined
  * confirms the presence of POSIX compliance. For this ofcourse we need
@@ -50,6 +56,19 @@
 enum OS_TYPES {WINDOWS, APPLE, UNIX};
 enum BIT_32_64 {BIT_32, BIT_64};
 
+static void schedule_send();
+static gboolean send_stats();
+
+static glong
+get_time_sec(){
+
+  /* Gets current time in seconds */
+  GTimeVal res;
+  g_get_current_time(&res);
+  return res.tv_sec;
+
+}
+
 static void
 save_xml(){
 
@@ -233,13 +252,13 @@
   switch(sys_info.wProcessorArchitecture){
 
     case 9:
-      arch_str = "AMD64";
+      arch_str = "amd64"; // same as x86_64 in windows
       break;
     case 6:
-      arch_str = "IA64";
+      arch_str = "ia64";
       break;
     case 0:
-      arch_str = "INTEL";
+      arch_str = "x86";
       break;
     default:
       arch_str = "UNKNOWN";
@@ -665,6 +684,75 @@
 
 }
 
+static void
+url_callback(PurpleUtilFetchUrlData *url_data, gpointer user_data, \
+    const gchar *url_text, gsize len, const gchar *error_message){
+
+  purple_debug_info("STATS WWW", "%s", url_text);
+
+}
+
+static gboolean
+send_stats()
+{
+
+  /* Read from stats.xml and send it over the wire */
+
+  PurpleUtilFetchUrlData *urldata;
+  gchar *host, *path, *request, *filename, *postdata, *url= SEND_URL;
+  GError *error;
+  int port;
+  gsize length;
+
+  /* Load the xml as post-data */
+  filename = g_build_filename(purple_user_dir(), "stats.xml", NULL);
+  g_file_get_contents(filename, &postdata, &length, &error);
+
+  purple_url_parse(url, &host, &port, &path, NULL, NULL);
+
+  request = g_strdup_printf(\
+          "POST /%s HTTP/1.0\r\n"
+          "Connection: keep-alive\r\n"
+          "Host: %s:%d\r\n"
+          "Content-Type: application/x-www-form-urlencoded; charset=UTF-8\r\n"
+          "Content-Length: %" G_GSIZE_FORMAT "\r\n\r\n%s",
+          path, host, port, strlen(postdata), postdata);
+
+  urldata = purple_util_fetch_url_request(NULL, url, TRUE, NULL, FALSE, request, FALSE, -1, url_callback, (gpointer)postdata);
+
+
+  purple_prefs_set_int("/plugins/core/statscollector/last-sent",\
+      get_time_sec());
+  schedule_send();
+
+  g_free(host);
+  g_free(path);
+  g_free(request);
+
+  return FALSE;
+}
+
+
+static void
+schedule_send(){
+
+  glong last_saved, curr_epoch, time_to_send;
+
+  last_saved = purple_prefs_get_int("/plugins/core/statscollector/last-sent");
+  curr_epoch = get_time_sec();
+
+  if(last_saved==0 || last_saved + RESEND_SEC < curr_epoch)
+    /* Send the first set of statistics right away */
+    time_to_send = 10;
+  else time_to_send = last_saved + RESEND_SEC - curr_epoch;
+  purple_debug_info("STATS SEND", "scheduling sending from %ld at %ld \
+                    for %ld",last_saved, curr_epoch, time_to_send);
+
+  purple_timeout_add_seconds(time_to_send, send_stats, NULL);
+}
+
+
+
 static xmlnode *
 init_stats(){
 
@@ -786,15 +874,16 @@
   root_stats = init_stats();
 
   /* Register the account signals for sign-on */
-
   purple_signal_connect(purple_accounts_get_handle(), "account-signed-on",
                       plugin, PURPLE_CALLBACK(acc_sign_on_event), NULL);
 
    /* Register the plugin signals for sign-on */
-
   purple_signal_connect(purple_plugins_get_handle(), "plugin-load",
                       plugin, PURPLE_CALLBACK(plugin_load_event), NULL);
 
+  /* Schedule the sending of statistics */
+  schedule_send();
+
  return TRUE;
 }
 
@@ -837,6 +926,12 @@
 static void
 init_plugin(PurplePlugin *plugin)
 {
+  /* Set up the preferences for the plugin */
+  purple_prefs_add_none("/plugins/core/statscollector");
+
+  /* Add public key information */
+  purple_prefs_add_int("/plugins/core/statscollector/last-sent", 0);
+
 }
 
 PURPLE_INIT_PLUGIN(statscollector, init_plugin, info)



More information about the Commits mailing list