From: CryptoManiac Date: Wed, 5 Aug 2015 17:42:54 +0000 (+0300) Subject: Notifications support X-Git-Url: https://git.novaco.in/?p=StratumLibrary.git;a=commitdiff_plain;h=5ac1ea8b2c1c4eca105471979abafa6c637b67a9 Notifications support --- diff --git a/Stratum.sln b/Stratum.sln index 362d3c4..95d9e47 100644 --- a/Stratum.sln +++ b/Stratum.sln @@ -1,6 +1,6 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 2012 +# Visual Studio Express 2012 for Windows Desktop Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Stratum", "StratumLibrary\Stratum.csproj", "{27F0C6C3-DCD6-4588-AFBB-DCE22306F5F8}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StratumTest", "StratumTest\StratumTest.csproj", "{FB61065F-93BA-497B-A2D5-3574D23B0B62}" @@ -20,9 +20,6 @@ Global {FB61065F-93BA-497B-A2D5-3574D23B0B62}.Release|Any CPU.ActiveCfg = Release|Any CPU {FB61065F-93BA-497B-A2D5-3574D23B0B62}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection - GlobalSection(MonoDevelopProperties) = preSolution - StartupItem = StratumTest\StratumTest.csproj - EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection diff --git a/StratumLibrary/Stratum.cs b/StratumLibrary/Stratum.cs index 30ca0bc..d023f6a 100644 --- a/StratumLibrary/Stratum.cs +++ b/StratumLibrary/Stratum.cs @@ -2,6 +2,7 @@ using System.Net; using System.Threading; using System.Net.Sockets; +using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System.Text; @@ -9,8 +10,6 @@ using System.Collections.Generic; namespace Stratum { - delegate void NotificationCallback(out object[] notificationData); - public class Stratum { private Socket client; @@ -146,11 +145,7 @@ namespace Stratum private StratumResponse Invoke(StratumRequest stratumReq) { // Serialize stratumReq into JSON string - var reqJSON = Newtonsoft.Json.JsonConvert.SerializeObject(stratumReq) + '\n'; - var reqId = (string) stratumReq.Id; - - string strResponse = ""; - StratumResponse responseObj = null; + var reqJSON = JsonConvert.SerializeObject(stratumReq) + '\n'; // Send JSON data to the remote device. Send(client, reqJSON); @@ -158,13 +153,16 @@ namespace Stratum // Wait for response gotResponse.WaitOne(); + var strResponse = string.Empty; lock (responsesLock) { // Deserialize the response - strResponse = responses[reqId]; - responses.Remove(reqId); + strResponse = responses[stratumReq.Id]; + responses.Remove(stratumReq.Id); } - responseObj = Newtonsoft.Json.JsonConvert.DeserializeObject>(strResponse); + + // Deserialize response into new instance of StratumResponse + StratumResponse responseObj = JsonConvert.DeserializeObject>(strResponse); // Reset the state gotResponse.Reset(); @@ -173,10 +171,10 @@ namespace Stratum { try { - JObject jo = Newtonsoft.Json.JsonConvert.DeserializeObject(strResponse) as JObject; - throw new Exception(jo["Error"].ToString()); + JObject jResponseObj = JsonConvert.DeserializeObject(strResponse) as JObject; + throw new Exception(jResponseObj["Error"].ToString()); } - catch (Newtonsoft.Json.JsonSerializationException) + catch (JsonSerializationException) { throw new Exception("Inconsistent or empty response"); } @@ -211,30 +209,32 @@ namespace Stratum if (arStatus.buffer[bytesRead - 1] == '\n') { - string strMessage = arStatus.sb.ToString(); + var strMessage = arStatus.sb.ToString(); arStatus.sb.Clear(); try { - JObject jo = Newtonsoft.Json.JsonConvert.DeserializeObject(strMessage) as JObject; - string requestId = (string)jo["id"]; + JObject jResponse = JsonConvert.DeserializeObject(strMessage) as JObject; + var reqId = (string)jResponse["id"]; - if (!String.IsNullOrEmpty(requestId)) + if (!String.IsNullOrEmpty(reqId)) { lock (responsesLock) { - responses.Add(requestId, strMessage); + responses.Add(reqId, strMessage); } gotResponse.Set(); } else { - // TODO: notifications handling - Console.WriteLine("Notification: {0}", strMessage); + StratumNotification jNotification = JsonConvert.DeserializeObject(strMessage); + + var NotifyProcessThread = new Thread(() => NotificationHandler(jNotification.Method, jNotification.Params)); + NotifyProcessThread.Start(); } } - catch (Newtonsoft.Json.JsonSerializationException e) + catch (JsonSerializationException e) { // TODO: handle parse error } @@ -246,6 +246,16 @@ namespace Stratum client.BeginReceive(state.buffer, 0, StratumReadState.BufferSize, SocketFlags.None, new AsyncCallback(ReceiveCallback), state); } + + /// + /// Notifications stub which is run in a separate thread. If you wish to implement real notification processing then just override this method in the derived class. + /// + /// Method name + /// Array of values + private static void NotificationHandler(string NotificationMethod, JArray NotificationData) + { + Console.WriteLine("\nNotification: Method={0}, data={1}", NotificationMethod, NotificationData.ToString()); + } } } \ No newline at end of file diff --git a/StratumLibrary/StratumNotification.cs b/StratumLibrary/StratumNotification.cs new file mode 100644 index 0000000..9be11f4 --- /dev/null +++ b/StratumLibrary/StratumNotification.cs @@ -0,0 +1,31 @@ +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; + + +namespace Stratum +{ + /// + /// Represents a Stratum notification + /// + [JsonObject(MemberSerialization.OptIn)] + public class StratumNotification + { + /// + /// Response id, should be null + /// + [JsonProperty(PropertyName = "id")] + public object Id { get; set; } + + /// + /// Method name + /// + [JsonProperty(PropertyName = "method")] + public string Method { get; set; } + + /// + /// Notification data + /// + [JsonProperty(PropertyName = "params")] + public JArray Params { get; set; } + } +} diff --git a/StratumLibrary/StratumReadState.cs b/StratumLibrary/StratumReadState.cs index 5d0ffa9..a53d61b 100644 --- a/StratumLibrary/StratumReadState.cs +++ b/StratumLibrary/StratumReadState.cs @@ -1,5 +1,4 @@ using System.Text; - using System.Net.Sockets; namespace Stratum diff --git a/StratumLibrary/StratumRequest.cs b/StratumLibrary/StratumRequest.cs index 1898096..ccb0cc9 100644 --- a/StratumLibrary/StratumRequest.cs +++ b/StratumLibrary/StratumRequest.cs @@ -18,7 +18,7 @@ namespace Stratum /// Unique request id /// [JsonProperty("id")] - public object Id { get; private set; } + public string Id { get; private set; } /// /// Stratum method name diff --git a/StratumLibrary/StratumResponse.cs b/StratumLibrary/StratumResponse.cs index 1e0df84..e5cb6ae 100644 --- a/StratumLibrary/StratumResponse.cs +++ b/StratumLibrary/StratumResponse.cs @@ -12,7 +12,7 @@ namespace Stratum /// Response id, should be null or identical to request id /// [JsonProperty(PropertyName = "id")] - public object Id { get; set; } + public string Id { get; set; } /// /// Result object @@ -37,7 +37,7 @@ namespace Stratum /// Response id, should be null or identical to request id /// [JsonProperty(PropertyName = "id")] - public object Id { get; set; } + public string Id { get; set; } /// /// Result object