public Server(int port) { httpServer = new HttpServer(); httpServer.EndPoint = new IPEndPoint(new IPAddress(new byte[] { 127, 0, 0, 1 }), port); httpServer.RequestReceived += (s, e) => { string name = "ManagementServer" + e.Request.Path.Replace('/', '.'); ManifestResourceInfo resourceInfo = Assembly.GetExecutingAssembly().GetManifestResourceInfo(name); if (resourceInfo != null) { e.Response.ContentType = MimeTypes.GetMimeType(new FileInfo(name)); using (Stream resourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(name)) { resourceStream.CopyTo(e.Response.OutputStream); } return; } using (var writer = new StreamWriter(e.Response.OutputStream)) { if (e.Request.Path == "/") { e.Response.Redirect("/index.html"); } else if (e.Request.Path == "/localization.js") { name = "ManagementServer.Scripts.lang-" + CultureInfo.CurrentCulture.ThreeLetterISOLanguageName + ".js"; resourceInfo = Assembly.GetExecutingAssembly().GetManifestResourceInfo(name); if (resourceInfo != null) { e.Response.ContentType = MimeTypes.GetMimeType(new FileInfo(name)); using (Stream resourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(name)) { resourceStream.CopyTo(e.Response.OutputStream); } return; } else { name = "ManagementServer.Scripts.lang-neutral.js"; e.Response.ContentType = MimeTypes.GetMimeType(new FileInfo(name)); using (Stream resourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(name)) { resourceStream.CopyTo(e.Response.OutputStream); } return; } } else if (e.Request.Path == "/tree") { e.Response.ContentType = "application/json"; Node.SerializeTree(writer); } else if (e.Request.Path == "/mbean") { string id = e.Request.Params["id"]; IMBean obj = Node.GetObject(id); if (obj == null) { e.Response.StatusCode = 404; e.Response.StatusDescription = "Object not found on this server"; } else { e.Response.ContentType = "application/json"; obj.Serialize(id, writer); } } else if (e.Request.Path == "/set-prop") { try { string id = e.Request.Params["id"]; string propName = e.Request.Params["propName"]; string value = e.Request.Params["val"]; IMBean obj = Node.GetObject(id); obj.SetProperty(propName, value); e.Response.ContentType = "application/json"; writer.WriteLine("{ \"result\": " + JsonConvert.ToString("ok") + ", \"message\": " + JsonConvert.ToString(ManagementServer.strings.PROP_SET_SUCCESSFULLY) + "}"); } catch (Exception ex) { e.Response.ContentType = "application/json"; writer.WriteLine("{ \"result\": " + JsonConvert.ToString("error") + ", \"message\": " + JsonConvert.ToString(ex.Message) + "}"); } } else if (e.Request.Path == "/invoke") { try { string id = e.Request.Params["object-id"]; string methodName = e.Request.Params["method-name"]; string methodSignature = e.Request.Params["method-signature"]; IMBean obj = Node.GetObject(id); MethodInfo methodInfo = obj.FindMethod(methodName, methodSignature); if (methodInfo == null) { throw new ArgumentException(ManagementServer.strings.ERR_METHOD_NOT_FOUND); } object val = obj.InvokeMethod(methodInfo, e.Request.Params); e.Response.ContentType = "application/json"; if (methodInfo.ReturnType == typeof(void)) { writer.WriteLine("{ \"result\": " + JsonConvert.ToString("ok") + ", \"message\": " + JsonConvert.ToString(ManagementServer.strings.VOID_INVOKED_SUCCESSFULLY) + "}"); } else { writer.WriteLine("{ \"result\": " + JsonConvert.ToString("ok") + ", \"message\": " + JsonConvert.ToString( string.Format(ManagementServer.strings.METHOD_INVOKED_SUCCESSFULLY, (val == null ? "null" : val.ToString())) ) + "}"); } } catch (Exception ex) { e.Response.ContentType = "application/json"; writer.WriteLine("{ \"result\": " + JsonConvert.ToString("error") + ", \"message\": " + JsonConvert.ToString(ex.Message) + "}"); } } else { e.Response.StatusCode = 404; e.Response.StatusDescription = "File not found on this server"; } } }; }