Changed the source formatting of the HttpServer class to match the rest of the project.
authormoel.mich
Sun, 28 Oct 2012 14:06:50 +0000
changeset 3867094d5dd924b
parent 385 8f16f03797f5
child 387 87093432c843
Changed the source formatting of the HttpServer class to match the rest of the project.
GUI/MainForm.cs
Utilities/HttpServer.cs
     1.1 --- a/GUI/MainForm.cs	Sun Oct 28 11:34:53 2012 +0000
     1.2 +++ b/GUI/MainForm.cs	Sun Oct 28 14:06:50 2012 +0000
     1.3 @@ -267,9 +267,9 @@
     1.4          runWebServerMenuItem, settings);
     1.5        runWebServer.Changed += delegate(object sender, EventArgs e) {
     1.6          if (runWebServer.Value)
     1.7 -          runWebServer.Value = server.startHTTPListener();
     1.8 +          runWebServer.Value = server.StartHTTPListener();
     1.9          else
    1.10 -          server.stopHTTPListener();
    1.11 +          server.StopHTTPListener();
    1.12        };
    1.13  
    1.14        InitializePlotForm();
     2.1 --- a/Utilities/HttpServer.cs	Sun Oct 28 11:34:53 2012 +0000
     2.2 +++ b/Utilities/HttpServer.cs	Sun Oct 28 14:06:50 2012 +0000
     2.3 @@ -5,369 +5,335 @@
     2.4    file, You can obtain one at http://mozilla.org/MPL/2.0/.
     2.5   
     2.6  	Copyright (C) 2012 Prince Samuel <prince.samuel@gmail.com>
     2.7 +  Copyright (C) 2012 Michael Möller <mmoeller@openhardwaremonitor.org>
     2.8  
     2.9  */
    2.10  
    2.11  using System;
    2.12 -using System.Collections.Generic;
    2.13 +using System.Drawing;
    2.14 +using System.Drawing.Imaging;
    2.15 +using System.IO;
    2.16 +using System.Net;
    2.17 +using System.Reflection;
    2.18  using System.Text;
    2.19 -using System.Net;
    2.20  using System.Threading;
    2.21 -using System.IO;
    2.22  using OpenHardwareMonitor.GUI;
    2.23  using OpenHardwareMonitor.Hardware;
    2.24 -using System.Reflection;
    2.25 -using System.Drawing;
    2.26 -using System.Drawing.Imaging;
    2.27  
    2.28 -namespace OpenHardwareMonitor.Utilities
    2.29 -{
    2.30 -    public class HttpServer
    2.31 +namespace OpenHardwareMonitor.Utilities {
    2.32 +
    2.33 +  public class HttpServer {
    2.34 +    private HttpListener listener;
    2.35 +    private int listenerPort, nodeCount;
    2.36 +    private Thread listenerThread;
    2.37 +    private Node root;
    2.38 +
    2.39 +    public HttpServer(Node r, int p) {
    2.40 +      root = r;
    2.41 +      listenerPort = p;
    2.42 +
    2.43 +      //JSON node count. 
    2.44 +      nodeCount = 0;
    2.45 +      listener = new HttpListener();
    2.46 +    }
    2.47 +
    2.48 +    public Boolean StartHTTPListener() {
    2.49 +      try {
    2.50 +        if (listener.IsListening)
    2.51 +          return true;
    2.52 +
    2.53 +        string prefix = "http://+:" + listenerPort + "/";
    2.54 +        listener.Prefixes.Clear();
    2.55 +        listener.Prefixes.Add(prefix);
    2.56 +        listener.Start();
    2.57 +
    2.58 +        if (listenerThread == null) {
    2.59 +          listenerThread = new Thread(HandleRequests);
    2.60 +          listenerThread.Start();
    2.61 +        }
    2.62 +      } catch (Exception) {
    2.63 +        return false;
    2.64 +      }
    2.65 +
    2.66 +      return true;
    2.67 +    }
    2.68 +
    2.69 +    public Boolean StopHTTPListener() {
    2.70 +      try {
    2.71 +        listenerThread.Abort();
    2.72 +        listener.Stop();
    2.73 +        listenerThread = null;
    2.74 +      } catch (HttpListenerException) {
    2.75 +      } catch (ThreadAbortException) {
    2.76 +      } catch (NullReferenceException) {
    2.77 +      } catch (Exception) {
    2.78 +      }
    2.79 +      return true;
    2.80 +    }
    2.81 +
    2.82 +    public void HandleRequests() {
    2.83 +
    2.84 +      while (listener.IsListening) {
    2.85 +        var context = listener.BeginGetContext(
    2.86 +          new AsyncCallback(ListenerCallback), listener);
    2.87 +        context.AsyncWaitHandle.WaitOne();
    2.88 +      }
    2.89 +    }
    2.90 +
    2.91 +    public void ListenerCallback(IAsyncResult result) {
    2.92 +      HttpListener listener = (HttpListener)result.AsyncState;
    2.93 +      if (listener == null || !listener.IsListening)
    2.94 +        return;
    2.95 +      // Call EndGetContext to complete the asynchronous operation.
    2.96 +      HttpListenerContext context = listener.EndGetContext(result);
    2.97 +      HttpListenerRequest request = context.Request;
    2.98 +
    2.99 +      var requestedFile = request.RawUrl.Substring(1);
   2.100 +      if (requestedFile == "data.json") {
   2.101 +        SendJSON(context);
   2.102 +        return;
   2.103 +      }
   2.104 +
   2.105 +      if (requestedFile.Contains("images_icon")) {
   2.106 +        ServeResourceImage(context, requestedFile.Replace("images_icon/", ""));
   2.107 +        return;
   2.108 +      }
   2.109 +
   2.110 +      // default file to be served
   2.111 +      if (string.IsNullOrEmpty(requestedFile))
   2.112 +        requestedFile = "index.html";
   2.113 +
   2.114 +      string[] splits = requestedFile.Split('.');
   2.115 +      string ext = splits[splits.Length - 1];
   2.116 +      ServeResourceFile(context, "Web." + requestedFile.Replace('/', '.'), ext);
   2.117 +    }
   2.118 +
   2.119 +    private void ServeResourceFile(HttpListenerContext context, string name, 
   2.120 +      string ext) 
   2.121      {
   2.122 -        private HttpListener listener;
   2.123 -        private int listenerPort, nodeCount;
   2.124 -        private Thread listenerThread;
   2.125 -        private Node root;
   2.126  
   2.127 -        public HttpServer(Node r, int p)
   2.128 -        {
   2.129 -            root = r;
   2.130 -            listenerPort = p;
   2.131 -            //JSON node count. 
   2.132 -            nodeCount = 0;
   2.133 -            listener = new HttpListener();
   2.134 +      // resource names do not support the hyphen
   2.135 +      name = "OpenHardwareMonitor.Resources." + 
   2.136 +        name.Replace("custom-theme", "custom_theme");
   2.137 +
   2.138 +      string[] names =
   2.139 +        Assembly.GetExecutingAssembly().GetManifestResourceNames();
   2.140 +      for (int i = 0; i < names.Length; i++) {
   2.141 +        if (names[i].Replace('\\', '.') == name) {
   2.142 +          using (Stream stream = Assembly.GetExecutingAssembly().
   2.143 +            GetManifestResourceStream(names[i])) {
   2.144 +            context.Response.ContentType = GetcontentType("." + ext);
   2.145 +            context.Response.ContentLength64 = stream.Length;
   2.146 +            byte[] buffer = new byte[512 * 1024];
   2.147 +            int len;
   2.148 +            while ((len = stream.Read(buffer, 0, buffer.Length)) > 0) {
   2.149 +              context.Response.OutputStream.Write(buffer, 0, len);
   2.150 +            }
   2.151 +            context.Response.OutputStream.Close();
   2.152 +          }
   2.153 +          return;
   2.154          }
   2.155 +      }
   2.156 +      context.Response.OutputStream.Close();
   2.157 +      context.Response.StatusCode = 404;
   2.158 +      context.Response.Close();
   2.159 +    }
   2.160  
   2.161 -        public Boolean startHTTPListener()
   2.162 -        {
   2.163 -            try
   2.164 -            {
   2.165 -                if (listener.IsListening)
   2.166 -                    return true;
   2.167 +    private void ServeResourceImage(HttpListenerContext context, string name) {
   2.168 +      name = "OpenHardwareMonitor.Resources." + name;
   2.169  
   2.170 -                string prefix = "http://+:" + listenerPort + "/";
   2.171 -                listener.Prefixes.Clear();
   2.172 -                listener.Prefixes.Add(prefix);
   2.173 -                listener.Start();
   2.174 +      string[] names =
   2.175 +        Assembly.GetExecutingAssembly().GetManifestResourceNames();
   2.176 +      for (int i = 0; i < names.Length; i++) {
   2.177 +        if (names[i].Replace('\\', '.') == name) {
   2.178 +          using (Stream stream = Assembly.GetExecutingAssembly().
   2.179 +            GetManifestResourceStream(names[i])) {
   2.180  
   2.181 -                if (listenerThread == null)
   2.182 -                {
   2.183 -                    listenerThread = new Thread(HandleRequests);
   2.184 -                    listenerThread.Start();
   2.185 -                }
   2.186 +            Image image = Image.FromStream(stream);
   2.187 +            context.Response.ContentType = "image/png";
   2.188 +            using (MemoryStream ms = new MemoryStream()) {
   2.189 +              image.Save(ms, ImageFormat.Png);
   2.190 +              ms.WriteTo(context.Response.OutputStream);
   2.191              }
   2.192 -            catch (Exception e)
   2.193 -            {
   2.194 -                return false;
   2.195 -            }
   2.196 +            context.Response.OutputStream.Close();
   2.197 +            image.Dispose();
   2.198 +            return;
   2.199 +          }
   2.200 +        }
   2.201 +      }
   2.202 +      context.Response.OutputStream.Close();
   2.203 +      context.Response.StatusCode = 404;
   2.204 +      context.Response.Close();
   2.205 +    }
   2.206  
   2.207 -            return true;
   2.208 -        }
   2.209 +    private void SendJSON(HttpListenerContext context) {
   2.210  
   2.211 -        public Boolean stopHTTPListener()
   2.212 -        {
   2.213 -            try
   2.214 -            {
   2.215 -                listenerThread.Abort();
   2.216 -                listener.Stop();
   2.217 -                listenerThread = null;
   2.218 -            }
   2.219 -            catch (System.Net.HttpListenerException e)
   2.220 -            {
   2.221 -            }
   2.222 -            catch (System.Threading.ThreadAbortException e)
   2.223 -            {
   2.224 -            }
   2.225 -            catch (System.NullReferenceException e)
   2.226 -            {
   2.227 -            }
   2.228 -            catch (Exception e)
   2.229 -            {
   2.230 -            }
   2.231 -            return true;
   2.232 -        }
   2.233 +      string JSON = "{\"id\": 0, \"Text\": \"Sensor\", \"Children\": [";
   2.234 +      nodeCount = 1;
   2.235 +      JSON += GenerateJSON(root);
   2.236 +      JSON += "]";
   2.237 +      JSON += ", \"Min\": \"Min\"";
   2.238 +      JSON += ", \"Value\": \"Value\"";
   2.239 +      JSON += ", \"Max\": \"Max\"";
   2.240 +      JSON += ", \"ImageURL\": \"\"";
   2.241 +      JSON += "}";
   2.242  
   2.243 -        public void HandleRequests()
   2.244 -        {
   2.245 +      var responseContent = JSON;
   2.246 +      byte[] buffer = Encoding.UTF8.GetBytes(responseContent);
   2.247  
   2.248 -            while (listener.IsListening)
   2.249 -            {
   2.250 -                var context = listener.BeginGetContext(new AsyncCallback(ListenerCallback), listener);
   2.251 -                context.AsyncWaitHandle.WaitOne();
   2.252 -            }
   2.253 -        }
   2.254 +      context.Response.ContentLength64 = buffer.Length;
   2.255 +      context.Response.ContentType = "application/json";
   2.256  
   2.257 -        public void ListenerCallback(IAsyncResult result)
   2.258 -        {
   2.259 -            HttpListener listener = (HttpListener)result.AsyncState;
   2.260 -            if (listener == null || !listener.IsListening)
   2.261 -                return;
   2.262 -            // Call EndGetContext to complete the asynchronous operation.
   2.263 -            HttpListenerContext context = listener.EndGetContext(result);
   2.264 -            HttpListenerRequest request = context.Request;
   2.265 +      Stream outputStream = context.Response.OutputStream;
   2.266 +      outputStream.Write(buffer, 0, buffer.Length);
   2.267 +      outputStream.Close();
   2.268  
   2.269 -            var requestedFile = request.RawUrl.Substring(1);
   2.270 -            if (requestedFile == "data.json")
   2.271 -            {
   2.272 -                sendJSON(context);
   2.273 -                return;
   2.274 -            }
   2.275 +    }
   2.276  
   2.277 -            if (requestedFile.Contains("images_icon"))
   2.278 -            {
   2.279 -                serveResourceImage(context, requestedFile.Replace("images_icon/", ""));
   2.280 -                return;
   2.281 -            }
   2.282 +    private string GenerateJSON(Node n) {
   2.283 +      string JSON = "{\"id\": " + nodeCount + ", \"Text\": \"" + n.Text 
   2.284 +        + "\", \"Children\": [";
   2.285 +      nodeCount++;
   2.286  
   2.287 -            //default file to be served
   2.288 -            if (string.IsNullOrEmpty(requestedFile))
   2.289 -                requestedFile = "index.html";
   2.290 +      foreach (Node child in n.Nodes)
   2.291 +        JSON += GenerateJSON(child) + ", ";
   2.292 +      if (JSON.EndsWith(", "))
   2.293 +        JSON = JSON.Remove(JSON.LastIndexOf(","));
   2.294 +      JSON += "]";
   2.295  
   2.296 -            string[] splits = requestedFile.Split('.');
   2.297 -            string ext = splits[splits.Length - 1];
   2.298 -            serveResourceFile(context, "Web." + requestedFile.Replace('/', '.'), ext);
   2.299 -        }
   2.300 +      if (n is SensorNode) {
   2.301 +        JSON += ", \"Min\": \"" + ((SensorNode)n).Min + "\"";
   2.302 +        JSON += ", \"Value\": \"" + ((SensorNode)n).Value + "\"";
   2.303 +        JSON += ", \"Max\": \"" + ((SensorNode)n).Max + "\"";
   2.304 +        JSON += ", \"ImageURL\": \"images/transparent.png\"";
   2.305 +      } else if (n is HardwareNode) {
   2.306 +        JSON += ", \"Min\": \"\"";
   2.307 +        JSON += ", \"Value\": \"\"";
   2.308 +        JSON += ", \"Max\": \"\"";
   2.309 +        JSON += ", \"ImageURL\": \"images_icon/" + 
   2.310 +          GetHardwareImageFile((HardwareNode)n) + "\"";
   2.311 +      } else if (n is TypeNode) {
   2.312 +        JSON += ", \"Min\": \"\"";
   2.313 +        JSON += ", \"Value\": \"\"";
   2.314 +        JSON += ", \"Max\": \"\"";
   2.315 +        JSON += ", \"ImageURL\": \"images_icon/" + 
   2.316 +          GetTypeImageFile((TypeNode)n) + "\"";
   2.317 +      } else {
   2.318 +        JSON += ", \"Min\": \"\"";
   2.319 +        JSON += ", \"Value\": \"\"";
   2.320 +        JSON += ", \"Max\": \"\"";
   2.321 +        JSON += ", \"ImageURL\": \"images_icon/computer.png\"";
   2.322 +      }
   2.323  
   2.324 -        private void serveResourceFile(HttpListenerContext context ,string name, string ext) {
   2.325 +      JSON += "}";
   2.326 +      return JSON;
   2.327 +    }
   2.328  
   2.329 -          //hack! resource names do not support the hyphen
   2.330 -          name = "OpenHardwareMonitor.Resources." + name.Replace("custom-theme", "custom_theme");
   2.331 -    
   2.332 -          string[] names = 
   2.333 -            Assembly.GetExecutingAssembly().GetManifestResourceNames();
   2.334 -          for (int i = 0; i < names.Length; i++) {
   2.335 -            if (names[i].Replace('\\', '.') == name) {
   2.336 -              using (Stream stream = Assembly.GetExecutingAssembly().
   2.337 -                GetManifestResourceStream(names[i])) {
   2.338 -                    context.Response.ContentType = getcontentType("." + ext);
   2.339 -                    context.Response.ContentLength64 = stream.Length;
   2.340 -                    byte[] buffer = new byte[512 * 1024];
   2.341 -                    int len;
   2.342 -                    while ((len = stream.Read(buffer, 0, buffer.Length)) > 0)
   2.343 -                    {
   2.344 -                        context.Response.OutputStream.Write(buffer, 0, len);
   2.345 -                    }
   2.346 -                    context.Response.OutputStream.Close();
   2.347 -                }
   2.348 -              return;
   2.349 -            }
   2.350 -          }
   2.351 -          context.Response.OutputStream.Close();
   2.352 -          context.Response.StatusCode = 404;
   2.353 -          context.Response.Close();
   2.354 -        }
   2.355 +    private static void ReturnFile(HttpListenerContext context, string filePath) 
   2.356 +    {
   2.357 +      context.Response.ContentType = 
   2.358 +        GetcontentType(Path.GetExtension(filePath));
   2.359 +      const int bufferSize = 1024 * 512; //512KB
   2.360 +      var buffer = new byte[bufferSize];
   2.361 +      using (var fs = File.OpenRead(filePath)) {
   2.362  
   2.363 -        private void serveResourceImage(HttpListenerContext context ,string name) {
   2.364 -          name = "OpenHardwareMonitor.Resources." + name;
   2.365 -    
   2.366 -          string[] names = 
   2.367 -            Assembly.GetExecutingAssembly().GetManifestResourceNames();
   2.368 -          for (int i = 0; i < names.Length; i++) {
   2.369 -            if (names[i].Replace('\\', '.') == name) {
   2.370 -              using (Stream stream = Assembly.GetExecutingAssembly().
   2.371 -                GetManifestResourceStream(names[i])) {
   2.372 -    
   2.373 -                Image image = Image.FromStream(stream);
   2.374 -                context.Response.ContentType = "image/png";
   2.375 -                using (MemoryStream ms = new MemoryStream())
   2.376 -                {
   2.377 -                    image.Save(ms, ImageFormat.Png);
   2.378 -                    ms.WriteTo(context.Response.OutputStream);
   2.379 -                }
   2.380 -                context.Response.OutputStream.Close();
   2.381 -                image.Dispose();
   2.382 -                return;
   2.383 -              }
   2.384 -            }
   2.385 -          } 
   2.386 -          context.Response.OutputStream.Close();
   2.387 -          context.Response.StatusCode = 404;
   2.388 -          context.Response.Close();
   2.389 -        }
   2.390 +        context.Response.ContentLength64 = fs.Length;
   2.391 +        int read;
   2.392 +        while ((read = fs.Read(buffer, 0, buffer.Length)) > 0)
   2.393 +          context.Response.OutputStream.Write(buffer, 0, read);
   2.394 +      }
   2.395  
   2.396 -        private void sendJSON(HttpListenerContext context)
   2.397 -        {
   2.398 +      context.Response.OutputStream.Close();
   2.399 +    }
   2.400  
   2.401 -            string JSON = "{\"id\": 0, \"Text\": \"Sensor\", \"Children\": [";
   2.402 -            nodeCount = 1;
   2.403 -            JSON += generateJSON(root);
   2.404 -            JSON += "]";
   2.405 -            JSON += ", \"Min\": \"Min\"";
   2.406 -            JSON += ", \"Value\": \"Value\"";
   2.407 -            JSON += ", \"Max\": \"Max\"";
   2.408 -            JSON += ", \"ImageURL\": \"\"";
   2.409 -            JSON += "}";
   2.410 +    private static string GetcontentType(string extension) {
   2.411 +      switch (extension) {
   2.412 +        case ".avi": return "video/x-msvideo";
   2.413 +        case ".css": return "text/css";
   2.414 +        case ".doc": return "application/msword";
   2.415 +        case ".gif": return "image/gif";
   2.416 +        case ".htm":
   2.417 +        case ".html": return "text/html";
   2.418 +        case ".jpg":
   2.419 +        case ".jpeg": return "image/jpeg";
   2.420 +        case ".js": return "application/x-javascript";
   2.421 +        case ".mp3": return "audio/mpeg";
   2.422 +        case ".png": return "image/png";
   2.423 +        case ".pdf": return "application/pdf";
   2.424 +        case ".ppt": return "application/vnd.ms-powerpoint";
   2.425 +        case ".zip": return "application/zip";
   2.426 +        case ".txt": return "text/plain";
   2.427 +        default: return "application/octet-stream";
   2.428 +      }
   2.429 +    }
   2.430  
   2.431 -            var responseContent = JSON;
   2.432 -            byte[] buffer = Encoding.UTF8.GetBytes(responseContent);
   2.433 +    private static string GetHardwareImageFile(HardwareNode hn) {
   2.434  
   2.435 -            context.Response.ContentLength64 = buffer.Length;
   2.436 -            context.Response.ContentType = "application/json";
   2.437 +      switch (hn.Hardware.HardwareType) {
   2.438 +        case HardwareType.CPU:
   2.439 +          return "cpu.png";
   2.440 +        case HardwareType.GpuNvidia:
   2.441 +          return "nvidia.png";
   2.442 +        case HardwareType.GpuAti:
   2.443 +          return "ati.png";
   2.444 +        case HardwareType.HDD:
   2.445 +          return "hdd.png";
   2.446 +        case HardwareType.Heatmaster:
   2.447 +          return "bigng.png";
   2.448 +        case HardwareType.Mainboard:
   2.449 +          return "mainboard.png";
   2.450 +        case HardwareType.SuperIO:
   2.451 +          return "chip.png";
   2.452 +        case HardwareType.TBalancer:
   2.453 +          return "bigng.png";
   2.454 +        case HardwareType.RAM:
   2.455 +          return "ram.png";
   2.456 +        default:
   2.457 +          return "cpu.png";
   2.458 +      }
   2.459  
   2.460 -            Stream outputStream = context.Response.OutputStream;
   2.461 -            outputStream.Write(buffer, 0, buffer.Length);
   2.462 -            outputStream.Close();
   2.463 +    }
   2.464  
   2.465 -        }
   2.466 +    private static string GetTypeImageFile(TypeNode tn) {
   2.467  
   2.468 -        private string generateJSON(Node n)
   2.469 -        {
   2.470 -            string JSON = "{\"id\": " + nodeCount + ", \"Text\": \"" + n.Text + "\", \"Children\": [";
   2.471 -            nodeCount++;
   2.472 +      switch (tn.SensorType) {
   2.473 +        case SensorType.Voltage:
   2.474 +          return "voltage.png";
   2.475 +        case SensorType.Clock:
   2.476 +          return "clock.png";
   2.477 +        case SensorType.Load:
   2.478 +          return "load.png";
   2.479 +        case SensorType.Temperature:
   2.480 +          return "temperature.png";
   2.481 +        case SensorType.Fan:
   2.482 +          return "fan.png";
   2.483 +        case SensorType.Flow:
   2.484 +          return "flow.png";
   2.485 +        case SensorType.Control:
   2.486 +          return "control.png";
   2.487 +        case SensorType.Level:
   2.488 +          return "level.png";
   2.489 +        case SensorType.Power:
   2.490 +          return "power.png";
   2.491 +        default:
   2.492 +          return "power.png";
   2.493 +      }
   2.494  
   2.495 -            foreach (Node child in n.Nodes)
   2.496 -                JSON += generateJSON(child) + ", ";
   2.497 -            if (JSON.EndsWith(", "))
   2.498 -                JSON = JSON.Remove(JSON.LastIndexOf(","));
   2.499 -            JSON += "]";
   2.500 +    }
   2.501  
   2.502 -            if (n is SensorNode)
   2.503 -            {
   2.504 -                JSON += ", \"Min\": \"" + ((SensorNode)n).Min + "\"";
   2.505 -                JSON += ", \"Value\": \"" + ((SensorNode)n).Value + "\"";
   2.506 -                JSON += ", \"Max\": \"" + ((SensorNode)n).Max + "\"";
   2.507 -                JSON += ", \"ImageURL\": \"images/transparent.png\"";
   2.508 -            }
   2.509 -            else if (n is HardwareNode)
   2.510 -            {
   2.511 -                JSON += ", \"Min\": \"\"";
   2.512 -                JSON += ", \"Value\": \"\"";
   2.513 -                JSON += ", \"Max\": \"\"";
   2.514 -                JSON += ", \"ImageURL\": \"images_icon/" + getHardwareImageFile((HardwareNode)n) + "\"";
   2.515 -            }
   2.516 -            else if (n is TypeNode)
   2.517 -            {
   2.518 -                JSON += ", \"Min\": \"\"";
   2.519 -                JSON += ", \"Value\": \"\"";
   2.520 -                JSON += ", \"Max\": \"\"";
   2.521 -                JSON += ", \"ImageURL\": \"images_icon/" + getTypeImageFile((TypeNode)n) + "\"";
   2.522 -            }
   2.523 -            else
   2.524 -            {
   2.525 -                JSON += ", \"Min\": \"\"";
   2.526 -                JSON += ", \"Value\": \"\"";
   2.527 -                JSON += ", \"Max\": \"\"";
   2.528 -                JSON += ", \"ImageURL\": \"images_icon/computer.png\"";
   2.529 -            }
   2.530 +    public int ListenerPort {
   2.531 +      get { return listenerPort; }
   2.532 +      set { listenerPort = value; }
   2.533 +    }
   2.534  
   2.535 -            JSON += "}";
   2.536 -            return JSON;
   2.537 -        }
   2.538 +    ~HttpServer() {
   2.539 +      StopHTTPListener();
   2.540 +      listener.Abort();
   2.541 +    }
   2.542  
   2.543 -        private static void returnFile(HttpListenerContext context, string filePath)
   2.544 -        {
   2.545 -            context.Response.ContentType = getcontentType(Path.GetExtension(filePath));
   2.546 -            const int bufferSize = 1024 * 512; //512KB
   2.547 -            var buffer = new byte[bufferSize];
   2.548 -            using (var fs = File.OpenRead(filePath))
   2.549 -            {
   2.550 -                
   2.551 -                context.Response.ContentLength64 = fs.Length;
   2.552 -                int read;
   2.553 -                while ((read = fs.Read(buffer, 0, buffer.Length)) > 0)
   2.554 -                    context.Response.OutputStream.Write(buffer, 0, read);
   2.555 -            }
   2.556 -
   2.557 -            context.Response.OutputStream.Close();
   2.558 -        }
   2.559 -
   2.560 -        private static string getcontentType(string extension)
   2.561 -        {
   2.562 -            switch (extension)
   2.563 -            {
   2.564 -                case ".avi": return "video/x-msvideo";
   2.565 -                case ".css": return "text/css";
   2.566 -                case ".doc": return "application/msword";
   2.567 -                case ".gif": return "image/gif";
   2.568 -                case ".htm":
   2.569 -                case ".html": return "text/html";
   2.570 -                case ".jpg":
   2.571 -                case ".jpeg": return "image/jpeg";
   2.572 -                case ".js": return "application/x-javascript";
   2.573 -                case ".mp3": return "audio/mpeg";
   2.574 -                case ".png": return "image/png";
   2.575 -                case ".pdf": return "application/pdf";
   2.576 -                case ".ppt": return "application/vnd.ms-powerpoint";
   2.577 -                case ".zip": return "application/zip";
   2.578 -                case ".txt": return "text/plain";
   2.579 -                default: return "application/octet-stream";
   2.580 -            }
   2.581 -        }
   2.582 -
   2.583 -        private static string getHardwareImageFile(HardwareNode hn)
   2.584 -        {
   2.585 -
   2.586 -            switch (hn.Hardware.HardwareType)
   2.587 -            {
   2.588 -                case HardwareType.CPU:
   2.589 -                    return "cpu.png";
   2.590 -                case HardwareType.GpuNvidia:
   2.591 -                    return "nvidia.png";
   2.592 -                case HardwareType.GpuAti:
   2.593 -                    return "ati.png";
   2.594 -                case HardwareType.HDD:
   2.595 -                    return "hdd.png";
   2.596 -                case HardwareType.Heatmaster:
   2.597 -                    return "bigng.png";
   2.598 -                case HardwareType.Mainboard:
   2.599 -                    return "mainboard.png";
   2.600 -                case HardwareType.SuperIO:
   2.601 -                    return "chip.png";
   2.602 -                case HardwareType.TBalancer:
   2.603 -                    return "bigng.png";
   2.604 -                case HardwareType.RAM:
   2.605 -                    return "ram.png";
   2.606 -                default:
   2.607 -                    return "cpu.png";
   2.608 -            }
   2.609 -
   2.610 -        }
   2.611 -
   2.612 -        private static string getTypeImageFile(TypeNode tn)
   2.613 -        {
   2.614 -
   2.615 -            switch (tn.SensorType)
   2.616 -            {
   2.617 -                case SensorType.Voltage:
   2.618 -                    return "voltage.png";
   2.619 -                case SensorType.Clock:
   2.620 -                    return "clock.png";
   2.621 -                case SensorType.Load:
   2.622 -                    return "load.png";
   2.623 -                case SensorType.Temperature:
   2.624 -                    return "temperature.png";
   2.625 -                case SensorType.Fan:
   2.626 -                    return "fan.png";
   2.627 -                case SensorType.Flow:
   2.628 -                    return "flow.png";
   2.629 -                case SensorType.Control:
   2.630 -                    return "control.png";
   2.631 -                case SensorType.Level:
   2.632 -                    return "level.png";
   2.633 -                case SensorType.Power:
   2.634 -                    return "power.png";
   2.635 -                default:
   2.636 -                    return "power.png";
   2.637 -            }
   2.638 -
   2.639 -        }
   2.640 -
   2.641 -        public int ListenerPort
   2.642 -        {
   2.643 -            get { return listenerPort; }
   2.644 -            set { listenerPort = value; }
   2.645 -        }
   2.646 -
   2.647 -        ~HttpServer()
   2.648 -        {
   2.649 -            stopHTTPListener();
   2.650 -            listener.Abort();
   2.651 -        }
   2.652 -
   2.653 -        public void Quit()
   2.654 -        {
   2.655 -            stopHTTPListener();
   2.656 -            listener.Abort();
   2.657 -        }
   2.658 +    public void Quit() {
   2.659 +      StopHTTPListener();
   2.660 +      listener.Abort();
   2.661      }
   2.662 +  }
   2.663  }