soc.2012.statscollector: 56b9ea71: Send statistics to a predefined server; ...

sanket at soc.pidgin.im sanket at soc.pidgin.im
Wed May 30 14:17:59 EDT 2012


----------------------------------------------------------------------
Revision: 56b9ea714317aee75ea9e8920fa09ac1aada45b1
Parent:   de879e3a12f5c18577a3fa7f1af06a317d4e88fe
Author:   sanket at soc.pidgin.im
Date:     05/30/12 05:15:04
Branch:   im.pidgin.soc.2012.statscollector
URL: http://d.pidgin.im/viewmtn/revision/info/56b9ea714317aee75ea9e8920fa09ac1aada45b1

Changelog: 

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.

Changes against parent de879e3a12f5c18577a3fa7f1af06a317d4e88fe

  patched  libpurple/plugins/statscollector.c

-------------- next part --------------
============================================================
--- libpurple/plugins/statscollector.c	8f90e3108c5e6a35c1e68069a95aee4a1fa9cff8
+++ libpurple/plugins/statscollector.c	029288b9d8e7a425fe84bfcc72750489ee665736
@@ -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 BIT_32_64 {BIT_32, BIT_64};
 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 @@ get_arch(){
   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 @@ plugin_load_event(PurplePlugin *plugin){
 
 }
 
+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 @@ plugin_load(PurplePlugin *plugin) {
   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 @@ init_plugin(PurplePlugin *plugin)
 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