using System; using System.Net; using System.Threading; using System.IO; using System.Linq; using MonoTouch.Foundation; namespace MonoCatalog { public class HttpDebug { static HttpListener listener; static Thread handlerThread; static public void Start () { handlerThread = new Thread (HttpServer); handlerThread.Start (); } static byte [] Encode (string str) { return System.Text.Encoding.UTF8.GetBytes (str); } static void HttpServer () { listener = new HttpListener (); listener.Prefixes.Add ("http://*:5000/"); listener.Start (); while (true){ var context = listener.GetContext (); var request = context.Request; var response = context.Response; var tw = new StreamWriter (response.OutputStream); var path = request.Url.AbsolutePath; if (path.StartsWith ("/type/")){ ShowInstancesOf (tw, path.Substring (6)); } else { Summary (tw); } tw.Flush (); response.OutputStream.Close (); } } static void Header (TextWriter c, string title, string script = null) { c.WriteLine ( "{0}\n" + "\n" + " "); if (script != null) c.WriteLine ("", script); c.WriteLine (""); } static void Summary (TextWriter c) { Header (c, "Summary", "$(\"a.type\").click (function (e) { $(this).append ('
'); $(this).children ().load ('/type/'); console.log ($(this).contents ()); e.preventDefault ();});"); //Header (c, "Summary", "alert ('loaded');"); var weakList = MonoTouch.ObjCRuntime.Runtime.GetSurfacedObjects (); c.WriteLine ("
foo
"); c.WriteLine ("

Total surfaced objects: {0}", weakList.Count); var groups = from weak in weakList let nso = weak.Target where nso != null let typeName = nso.GetType ().FullName orderby typeName group nso by typeName into g let gCount = g.ToList ().Count orderby gCount descending select new { Type = g.Key, Instances = g }; var list = groups.ToList (); c.WriteLine ("

Have {0} different types surfaced", list.Count); c.WriteLine ("

"); c.WriteLine (""); } static void ShowInstancesOf (TextWriter c, string type) { var weakList = MonoTouch.ObjCRuntime.Runtime.GetSurfacedObjects (); var res = from weak in weakList let nso = weak.Target where nso != null let typeName = nso.GetType ().FullName where typeName == type select nso; c.WriteLine (""); } } }