|
3 | 3 | using System.Collections.Generic;
|
4 | 4 | using System.Collections.Specialized;
|
5 | 5 | using System.Configuration;
|
| 6 | +using System.Diagnostics; |
6 | 7 | using System.Linq;
|
7 | 8 | using System.Reflection;
|
8 | 9 | using System.Text.RegularExpressions;
|
| 10 | +using System.Threading; |
9 | 11 | using System.Web;
|
10 | 12 | using System.Web.SessionState;
|
11 | 13 |
|
@@ -45,76 +47,103 @@ public class JSONRPC : Common, IHttpHandler, IRequiresSessionState
|
45 | 47 | /// <param name="context"></param>
|
46 | 48 | public void ProcessRequest(HttpContext context)
|
47 | 49 | {
|
48 |
| - Log.Instance.Info("API Interface Opened"); |
| 50 | + // Initiate Stopwatch |
| 51 | + Stopwatch sw = new Stopwatch(); |
| 52 | + // Start Stopwatch |
| 53 | + sw.Start(); |
49 | 54 |
|
50 |
| - // Set HTTP Requests |
51 |
| - httpGET = GetHttpGET(); |
52 |
| - httpPOST = GetHttpPOST(); |
| 55 | + // Thread a PerfomanceCollector |
| 56 | + PerfomanceCollector performanceCollector = new PerfomanceCollector(); |
| 57 | + Thread performanceThread = new Thread(new ThreadStart(performanceCollector.CollectData)); |
| 58 | + performanceThread.Start(); |
53 | 59 |
|
54 |
| - // Set Mime-Type for the Content Type and override the Charset |
55 |
| - context.Response.ContentType = JSONRPC_MimeType; |
56 |
| - context.Response.Charset = null; |
57 |
| - // Set CacheControl to no-cache |
58 |
| - context.Response.CacheControl = "no-cache"; |
| 60 | + try |
| 61 | + { |
| 62 | + Log.Instance.Info("API Interface Opened"); |
59 | 63 |
|
60 |
| - // Deserialize and parse the JSON request into an Object dynamically |
61 |
| - JSONRPC_Request JSONRPC_Request = ParseRequest(ref context); |
| 64 | + // Set HTTP Requests |
| 65 | + httpGET = GetHttpGET(); |
| 66 | + httpPOST = GetHttpPOST(); |
62 | 67 |
|
63 |
| - // Check for the maintenance flag |
64 |
| - if (Maintenance) |
65 |
| - { |
66 |
| - JSONRPC_Error error = new JSONRPC_Error { code = -32001, data = "The system is currently under maintenance." }; |
67 |
| - ParseError(ref context, JSONRPC_Request.id, error); |
68 |
| - } |
| 68 | + // Set Mime-Type for the Content Type and override the Charset |
| 69 | + context.Response.ContentType = JSONRPC_MimeType; |
| 70 | + context.Response.Charset = null; |
| 71 | + // Set CacheControl to no-cache |
| 72 | + context.Response.CacheControl = "no-cache"; |
69 | 73 |
|
70 |
| - // Authenticate and append credentials |
71 |
| - if (Authenticate(ref context) == false) |
72 |
| - { |
73 |
| - JSONRPC_Error error = new JSONRPC_Error { code = -32002 }; |
74 |
| - ParseError(ref context, JSONRPC_Request.id, error); |
75 |
| - } |
| 74 | + // Deserialize and parse the JSON request into an Object dynamically |
| 75 | + JSONRPC_Request JSONRPC_Request = ParseRequest(ref context); |
76 | 76 |
|
77 |
| - // Get results from the relevant method with the params |
78 |
| - JSONRPC_Output result = GetResult(ref context, JSONRPC_Request); |
79 |
| - if (result == null) |
80 |
| - { |
81 |
| - JSONRPC_Error error = new JSONRPC_Error { code = -32603 }; |
82 |
| - ParseError(ref context, JSONRPC_Request.id, error); |
83 |
| - } |
84 |
| - else if (result.error != null) |
85 |
| - { |
86 |
| - JSONRPC_Error error = new JSONRPC_Error { code = -32099, data = result.error }; |
87 |
| - ParseError(ref context, JSONRPC_Request.id, error); |
88 |
| - } |
89 |
| - else |
90 |
| - { |
91 |
| - // Check if the result.data is already a JSON type casted as: new JRaw(data); |
92 |
| - var jsonRaw = result.data as JRaw; |
93 |
| - if (jsonRaw != null) |
| 77 | + // Check for the maintenance flag |
| 78 | + if (Maintenance) |
94 | 79 | {
|
95 |
| - JSONRPC_API_ResponseDataJRaw output = new JSONRPC_API_ResponseDataJRaw |
96 |
| - { |
97 |
| - jsonrpc = JSONRPC_Request.jsonrpc, |
98 |
| - result = jsonRaw, |
99 |
| - id = JSONRPC_Request.id |
100 |
| - }; |
101 |
| - // Output the JSON-RPC repsonse with JRaw data |
102 |
| - context.Response.Write(Utility.JsonSerialize_IgnoreLoopingReference(output)); |
| 80 | + JSONRPC_Error error = new JSONRPC_Error { code = -32001, data = "The system is currently under maintenance." }; |
| 81 | + ParseError(ref context, JSONRPC_Request.id, error); |
| 82 | + } |
| 83 | + |
| 84 | + // Authenticate and append credentials |
| 85 | + if (Authenticate(ref context) == false) |
| 86 | + { |
| 87 | + JSONRPC_Error error = new JSONRPC_Error { code = -32002 }; |
| 88 | + ParseError(ref context, JSONRPC_Request.id, error); |
| 89 | + } |
| 90 | + |
| 91 | + // Get results from the relevant method with the params |
| 92 | + JSONRPC_Output result = GetResult(ref context, JSONRPC_Request); |
| 93 | + if (result == null) |
| 94 | + { |
| 95 | + JSONRPC_Error error = new JSONRPC_Error { code = -32603 }; |
| 96 | + ParseError(ref context, JSONRPC_Request.id, error); |
| 97 | + } |
| 98 | + else if (result.error != null) |
| 99 | + { |
| 100 | + JSONRPC_Error error = new JSONRPC_Error { code = -32099, data = result.error }; |
| 101 | + ParseError(ref context, JSONRPC_Request.id, error); |
103 | 102 | }
|
104 | 103 | else
|
105 | 104 | {
|
106 |
| - JSONRPC_API_ResponseData output = new JSONRPC_API_ResponseData |
| 105 | + // Check if the result.data is already a JSON type casted as: new JRaw(data); |
| 106 | + var jsonRaw = result.data as JRaw; |
| 107 | + if (jsonRaw != null) |
107 | 108 | {
|
108 |
| - jsonrpc = JSONRPC_Request.jsonrpc, |
109 |
| - result = result.data, |
110 |
| - id = JSONRPC_Request.id |
111 |
| - }; |
112 |
| - // Output the JSON-RPC repsonse |
113 |
| - context.Response.Write(Utility.JsonSerialize_IgnoreLoopingReference(output)); |
| 109 | + JSONRPC_API_ResponseDataJRaw output = new JSONRPC_API_ResponseDataJRaw |
| 110 | + { |
| 111 | + jsonrpc = JSONRPC_Request.jsonrpc, |
| 112 | + result = jsonRaw, |
| 113 | + id = JSONRPC_Request.id |
| 114 | + }; |
| 115 | + // Output the JSON-RPC repsonse with JRaw data |
| 116 | + context.Response.Write(Utility.JsonSerialize_IgnoreLoopingReference(output)); |
| 117 | + } |
| 118 | + else |
| 119 | + { |
| 120 | + JSONRPC_API_ResponseData output = new JSONRPC_API_ResponseData |
| 121 | + { |
| 122 | + jsonrpc = JSONRPC_Request.jsonrpc, |
| 123 | + result = result.data, |
| 124 | + id = JSONRPC_Request.id |
| 125 | + }; |
| 126 | + // Output the JSON-RPC repsonse |
| 127 | + context.Response.Write(Utility.JsonSerialize_IgnoreLoopingReference(output)); |
| 128 | + } |
114 | 129 | }
|
115 | 130 | }
|
| 131 | + catch (Exception e) |
| 132 | + { |
| 133 | + Log.Instance.Fatal(e); |
| 134 | + throw; |
| 135 | + } |
| 136 | + finally |
| 137 | + { |
| 138 | + // Terminate Perfomance collection |
| 139 | + performanceThread.Abort(); |
116 | 140 |
|
117 |
| - Log.Instance.Info("API Interface Closed"); |
| 141 | + // Stop Stopwatch |
| 142 | + sw.Stop(); |
| 143 | + |
| 144 | + Log.Instance.Info("API Execution Time (s): " + ((float)Math.Round(sw.Elapsed.TotalMilliseconds / 1000, 3)).ToString()); |
| 145 | + Log.Instance.Info("API Interface Closed"); |
| 146 | + } |
118 | 147 | }
|
119 | 148 |
|
120 | 149 | /// <summary>
|
|
0 commit comments