--- a
+++ b/trunk/misc/bw/serverstats.srp
@@ -0,0 +1,77 @@
+# serverstats.srp -- publish and log server statistics
+#
+# RBD
+# Feb 2014
+
+require "http"
+
+hdrs = "User|ConnectedTime|DropRateToHub|DropRateToPlayer|RoundTripMin|RoundTripMax|RoundTripMean#"
+
+publish_server_req = nil
+
+server_stats_log = open("serverstats.log", "w")
+server_stats_log.write("GLOBAL NET ORCHESTRA SERVER STATS LOG FILE\n\n")
+server_stats_log.write(str(int(time_get())) + ": START\n")
+
+
+def publish_server_stats()
+    if publish_server_req:
+        print "Cannot publish new stats because previous HTTP request is still running."
+        return
+    var req = Http_post()
+    req.add_field("username", "server")
+    req.add_field("password", prefs.get('webauth'))
+    req.add_field("request", "reportstats")
+    var data = [hdrs]
+    for entry in roster:
+        var client = clients.find_client(entry[2])
+        var row
+        if client.addr: // connected
+            row = [client.name, "|", str(real_now - client.connected_since), "|",
+                   str(client.droprate_to_server), "|",
+                   str(client.droprate_to_client), "|",
+                   str(client.round_trip_min), "|",
+                   str(client.round_trip_max), "|",
+                   str(client.round_trip_mean), "#"]
+        else:
+            row = [client.name, "|Not|-|-|-|-|-#"]
+        data.append(row)
+    data = flatten(data)
+    req.add_field("data", data)
+    req.post("http://globalnetorchestra.org/gnoinfo.php",
+             "globalnetorchestra.org")
+    publish_server_req = req
+    server_stats_log.write("\n" + str(int(time_get())) + ":")
+    server_stats_log.write(data)
+
+
+def publish_server_poll()
+    var req = publish_server_req
+    if req:
+        req.poll()
+        if req.state == 'fail'
+            print "Failed to publish stats to website"
+        elif req.state == 'done'
+            print "Published stats to website"
+        else:
+            return nil // not done yet
+        publish_server_req = nil
+        return t
+
+def publish_server_finish()
+    server_stats_log.write("\n")
+    server_stats_log.close()
+    var req = Http_post()
+    req.add_field("username", "server")
+    req.add_field("password", prefs.get('webauth'))
+    req.add_field("request", "endstatsreport")
+    req.add_field("data", "")
+    req.post("http://globalnetorchestra.org/gnoinfo.php",
+             "globalnetorchestra.org")
+    publish_server_req = req
+    for i = 0 to 20:
+        if publish_server_poll():
+            return
+        time_sleep(0.1)
+    wxs_message_box("Failed to post endstatsreport to website",
+                    "Error", WXS_STYLE_EXCLAMATION, 0)