From 5c36ad9e70d38bae5995ae0b679c331d9001db5c Mon Sep 17 00:00:00 2001 From: "gu.martinm@gmail.com" Date: Fri, 7 Mar 2014 03:26:55 +0100 Subject: [PATCH] JSONRPC4NET: methods with parameters and returning or not values. --- Mono/RemoteAgents/GTKLinux/MainWindow.cs | 19 +++++- Mono/RemoteAgents/GTKLinux/NLog.config | 8 ++- Mono/RemoteAgents/GTKLinux/View/View.cs | 5 ++ Mono/RemoteAgents/GTKLinux/ViewModel/ViewModel.cs | 10 ++- .../Example.RemoteAgents.GTKLinux.MainWindow.cs | 73 ++++++++++++++++------ Mono/RemoteAgents/GTKLinux/gtk-gui/gui.stetic | 50 +++++++++++++-- jsonrpc4net/jsonrpc4net/JsonRpcHttpAsyncClient.cs | 72 ++++++++++++++++++--- 7 files changed, 199 insertions(+), 38 deletions(-) diff --git a/Mono/RemoteAgents/GTKLinux/MainWindow.cs b/Mono/RemoteAgents/GTKLinux/MainWindow.cs index 04c7d98..bc9d341 100644 --- a/Mono/RemoteAgents/GTKLinux/MainWindow.cs +++ b/Mono/RemoteAgents/GTKLinux/MainWindow.cs @@ -16,6 +16,7 @@ namespace Example.RemoteAgents.GTKLinux _view = new ViewImpl(); Build (); this.ButtonGetDate.Clicked += this.ButtonGetDateClicked; + this.SendDataButton.Clicked += this.SendDataButtonClicked; } protected void OnDeleteEvent (object sender, DeleteEventArgs a) @@ -24,14 +25,26 @@ namespace Example.RemoteAgents.GTKLinux a.RetVal = true; } - async private void ButtonGetDateClicked(object sender, EventArgs a) + async private void ButtonGetDateClicked(object sender, EventArgs e) { try { this.RemoteDate.Buffer.Text = await _view.GetCurrentDateAsync(); } - catch (Exception e) + catch (Exception exception) { - logger.Error("ButtonGetDateClicked error: ", e); + logger.ErrorException("ButtonGetDateClicked error: ", exception); + } + } + + async private void SendDataButtonClicked(object sender, EventArgs e) + { + try { + await _view.SetWriteTextAsync(this.TextToSend.Buffer.Text, + this.SpinButtonNumbers.Value); + } + catch (Exception exception) + { + logger.ErrorException("SendDataButtonClicked error: ", exception); } } } diff --git a/Mono/RemoteAgents/GTKLinux/NLog.config b/Mono/RemoteAgents/GTKLinux/NLog.config index ba49fd2..93a4c30 100644 --- a/Mono/RemoteAgents/GTKLinux/NLog.config +++ b/Mono/RemoteAgents/GTKLinux/NLog.config @@ -11,9 +11,12 @@ + layout="${longdate} ${uppercase:${level}} ${callsite} ${message} ${exception:format=ToString,StackTrace} ${newline}" /> + + --> - + @@ -31,6 +34,7 @@ diff --git a/Mono/RemoteAgents/GTKLinux/View/View.cs b/Mono/RemoteAgents/GTKLinux/View/View.cs index e423c2d..dcfcc09 100644 --- a/Mono/RemoteAgents/GTKLinux/View/View.cs +++ b/Mono/RemoteAgents/GTKLinux/View/View.cs @@ -13,6 +13,11 @@ namespace Example.RemoteAgents.GTKLinux.View { return await _vm.GetCurrentDateAsync(); } + + async public Task SetWriteTextAsync(params object[] parameters) + { + await _vm.SetWriteTextAsync(parameters); + } } } diff --git a/Mono/RemoteAgents/GTKLinux/ViewModel/ViewModel.cs b/Mono/RemoteAgents/GTKLinux/ViewModel/ViewModel.cs index e8dda29..8178d35 100644 --- a/Mono/RemoteAgents/GTKLinux/ViewModel/ViewModel.cs +++ b/Mono/RemoteAgents/GTKLinux/ViewModel/ViewModel.cs @@ -7,12 +7,18 @@ namespace Example.RemoteAgents.GTKLinux.ViewModel { public class ViewModelImpl { - private static readonly string uri = "http://127.0.0.1:8080/spring-mainapp/CurrentDateService.json"; + private static readonly string uriGetCurrentDate = "http://127.0.0.1:8080/spring-mainapp/CurrentDateService.json"; + private static readonly string uriSetWriteText = "http://127.0.0.1:8080/spring-mainapp/WriteTextService.json"; private readonly JsonRpcHttpAsyncClient _remoteClient = new JsonRpcHttpAsyncClient(); async public Task GetCurrentDateAsync() { - return await _remoteClient.PostRemoteServiceAsync(uri, "getCurrentDate"); + return await _remoteClient.PostRemoteServiceAsync(uriGetCurrentDate, "getCurrentDate"); + } + + async public Task SetWriteTextAsync(params object[] parameters) + { + await _remoteClient.PostWithParametersRemoteServiceAsync(uriSetWriteText, "setWriteText", parameters); } } } diff --git a/Mono/RemoteAgents/GTKLinux/gtk-gui/Example.RemoteAgents.GTKLinux.MainWindow.cs b/Mono/RemoteAgents/GTKLinux/gtk-gui/Example.RemoteAgents.GTKLinux.MainWindow.cs index 12e792d..6d358d3 100644 --- a/Mono/RemoteAgents/GTKLinux/gtk-gui/Example.RemoteAgents.GTKLinux.MainWindow.cs +++ b/Mono/RemoteAgents/GTKLinux/gtk-gui/Example.RemoteAgents.GTKLinux.MainWindow.cs @@ -6,9 +6,13 @@ namespace Example.RemoteAgents.GTKLinux { private global::Gtk.VBox vbox3; private global::Gtk.TextView RemoteDate; + private global::Gtk.HBox hbox1; + private global::Gtk.ScrolledWindow GtkScrolledWindow; + private global::Gtk.TextView TextToSend; + private global::Gtk.SpinButton SpinButtonNumbers; private global::Gtk.HBox hbox2; private global::Gtk.Button ButtonGetDate; - private global::Gtk.Button RetrieveRemoteDateButton; + private global::Gtk.Button SendDataButton; protected virtual void Build () { @@ -31,6 +35,37 @@ namespace Example.RemoteAgents.GTKLinux global::Gtk.Box.BoxChild w1 = ((global::Gtk.Box.BoxChild)(this.vbox3 [this.RemoteDate])); w1.Position = 0; // Container child vbox3.Gtk.Box+BoxChild + this.hbox1 = new global::Gtk.HBox (); + this.hbox1.Name = "hbox1"; + this.hbox1.Spacing = 6; + // Container child hbox1.Gtk.Box+BoxChild + this.GtkScrolledWindow = new global::Gtk.ScrolledWindow (); + this.GtkScrolledWindow.Name = "GtkScrolledWindow"; + this.GtkScrolledWindow.ShadowType = ((global::Gtk.ShadowType)(1)); + // Container child GtkScrolledWindow.Gtk.Container+ContainerChild + this.TextToSend = new global::Gtk.TextView (); + this.TextToSend.CanFocus = true; + this.TextToSend.Name = "TextToSend"; + this.GtkScrolledWindow.Add (this.TextToSend); + this.hbox1.Add (this.GtkScrolledWindow); + global::Gtk.Box.BoxChild w3 = ((global::Gtk.Box.BoxChild)(this.hbox1 [this.GtkScrolledWindow])); + w3.Position = 0; + // Container child hbox1.Gtk.Box+BoxChild + this.SpinButtonNumbers = new global::Gtk.SpinButton (0, 1000, 10); + this.SpinButtonNumbers.CanFocus = true; + this.SpinButtonNumbers.Name = "SpinButtonNumbers"; + this.SpinButtonNumbers.Adjustment.PageIncrement = 10; + this.SpinButtonNumbers.ClimbRate = 1; + this.SpinButtonNumbers.Numeric = true; + this.hbox1.Add (this.SpinButtonNumbers); + global::Gtk.Box.BoxChild w4 = ((global::Gtk.Box.BoxChild)(this.hbox1 [this.SpinButtonNumbers])); + w4.Position = 1; + w4.Fill = false; + this.vbox3.Add (this.hbox1); + global::Gtk.Box.BoxChild w5 = ((global::Gtk.Box.BoxChild)(this.vbox3 [this.hbox1])); + w5.Position = 1; + w5.Fill = false; + // Container child vbox3.Gtk.Box+BoxChild this.hbox2 = new global::Gtk.HBox (); this.hbox2.Name = "hbox2"; this.hbox2.Spacing = 6; @@ -41,26 +76,26 @@ namespace Example.RemoteAgents.GTKLinux this.ButtonGetDate.UseUnderline = true; this.ButtonGetDate.Label = global::Mono.Unix.Catalog.GetString ("GetRemoteDate"); this.hbox2.Add (this.ButtonGetDate); - global::Gtk.Box.BoxChild w2 = ((global::Gtk.Box.BoxChild)(this.hbox2 [this.ButtonGetDate])); - w2.Position = 0; - w2.Expand = false; - w2.Fill = false; + global::Gtk.Box.BoxChild w6 = ((global::Gtk.Box.BoxChild)(this.hbox2 [this.ButtonGetDate])); + w6.Position = 0; + w6.Expand = false; + w6.Fill = false; // Container child hbox2.Gtk.Box+BoxChild - this.RetrieveRemoteDateButton = new global::Gtk.Button (); - this.RetrieveRemoteDateButton.CanFocus = true; - this.RetrieveRemoteDateButton.Name = "RetrieveRemoteDateButton"; - this.RetrieveRemoteDateButton.UseUnderline = true; - this.RetrieveRemoteDateButton.Label = global::Mono.Unix.Catalog.GetString ("RemoteDate"); - this.hbox2.Add (this.RetrieveRemoteDateButton); - global::Gtk.Box.BoxChild w3 = ((global::Gtk.Box.BoxChild)(this.hbox2 [this.RetrieveRemoteDateButton])); - w3.Position = 2; - w3.Expand = false; - w3.Fill = false; + this.SendDataButton = new global::Gtk.Button (); + this.SendDataButton.CanFocus = true; + this.SendDataButton.Name = "SendDataButton"; + this.SendDataButton.UseUnderline = true; + this.SendDataButton.Label = global::Mono.Unix.Catalog.GetString ("SendData"); + this.hbox2.Add (this.SendDataButton); + global::Gtk.Box.BoxChild w7 = ((global::Gtk.Box.BoxChild)(this.hbox2 [this.SendDataButton])); + w7.Position = 2; + w7.Expand = false; + w7.Fill = false; this.vbox3.Add (this.hbox2); - global::Gtk.Box.BoxChild w4 = ((global::Gtk.Box.BoxChild)(this.vbox3 [this.hbox2])); - w4.Position = 2; - w4.Expand = false; - w4.Fill = false; + global::Gtk.Box.BoxChild w8 = ((global::Gtk.Box.BoxChild)(this.vbox3 [this.hbox2])); + w8.Position = 2; + w8.Expand = false; + w8.Fill = false; this.Add (this.vbox3); if ((this.Child != null)) { this.Child.ShowAll (); diff --git a/Mono/RemoteAgents/GTKLinux/gtk-gui/gui.stetic b/Mono/RemoteAgents/GTKLinux/gtk-gui/gui.stetic index 6fcea56..bf8a279 100644 --- a/Mono/RemoteAgents/GTKLinux/gtk-gui/gui.stetic +++ b/Mono/RemoteAgents/GTKLinux/gtk-gui/gui.stetic @@ -26,11 +26,53 @@ 0 - False + True - + + + 6 + + + + In + + + + True + True + + + + + + 0 + True + + + + + + True + 1000 + 10 + 10 + 1 + True + + + 1 + False + False + + + + + 1 + False + False + @@ -55,11 +97,11 @@ - + True TextOnly - RemoteDate + SendData True diff --git a/jsonrpc4net/jsonrpc4net/JsonRpcHttpAsyncClient.cs b/jsonrpc4net/jsonrpc4net/JsonRpcHttpAsyncClient.cs index 50ad4fb..856115d 100644 --- a/jsonrpc4net/jsonrpc4net/JsonRpcHttpAsyncClient.cs +++ b/jsonrpc4net/jsonrpc4net/JsonRpcHttpAsyncClient.cs @@ -53,33 +53,81 @@ namespace GumartinM.JsonRPC4NET /// The 1st type parameter. async public Task PostRemoteServiceAsync(string uri, string method) { - POSTResult postResult = await this.PostAsync(uri, method, CancellationToken.None); + var postData = new POST(); + postData.id = Interlocked.Increment(ref _nextId).ToString(); + postData.jsonrpc = "2.0"; + postData.method = method; + + string jsonData = JsonConvert.SerializeObject(postData, _jsonSettings); + + POSTResult postResult = await this.PostAsync(uri, method, jsonData, CancellationToken.None); return postResult.result; } /// - /// Posts the async. + /// Posts the remote service async. /// - /// The async. + /// The remote service async. /// URI. /// Method. - /// Cancellation. /// The 1st type parameter. - async private Task> PostAsync(string uri, string method, CancellationToken cancellation) + async public Task PostRemoteServiceAsync(string uri, string method) { - var postData = new POST(); + await this.PostRemoteServiceAsync(uri, method); + } + + /// + /// Posts the with parameters remote service async. + /// + /// The with parameters remote service async. + /// URI. + /// Method. + /// Parameters. + /// The 1st type parameter. + async public Task PostWithParametersRemoteServiceAsync(string uri, string method, params object[] parameters) + { + var inputParameters = new List(parameters); + var postData = new POSTParameters(); postData.id = Interlocked.Increment(ref _nextId).ToString(); postData.jsonrpc = "2.0"; postData.method = method; + postData.@params = inputParameters; + + string jsonData = JsonConvert.SerializeObject(postData, _jsonSettings); + + POSTResult postResult = await this.PostAsync(uri, method, jsonData, CancellationToken.None); + + return postResult.result; + } - string data = JsonConvert.SerializeObject(postData, _jsonSettings); + /// + /// Posts the with parameters remote service async. + /// + /// The with parameters remote service async. + /// URI. + /// Method. + /// Parameters. + async public Task PostWithParametersRemoteServiceAsync(string uri, string method, params object[] parameters) + { + await this.PostWithParametersRemoteServiceAsync(uri, method, parameters); + } + /// + /// Posts the async. + /// + /// The async. + /// URI. + /// Method. + /// Cancellation. + /// The 1st type parameter. + async private Task> PostAsync(string uri, string method, string jsonData, CancellationToken cancellation) + { // see: http://stackoverflow.com/questions/1329739/nested-using-statements-in-c-sharp // see: http://stackoverflow.com/questions/5895879/when-do-we-need-to-call-dispose-in-dot-net-c //TODO: Am I really sure I have to call the Dispose method of HttpContent content? In this case, shouldn't it be stupid? // For HttpResponseMessage response I am sure I have to do it but I am not for HttpContent content. - using (HttpContent content = new StringContent(data, System.Text.Encoding.UTF8, "application/json-rpc")) + using (HttpContent content = new StringContent(jsonData, System.Text.Encoding.UTF8, "application/json-rpc")) using (HttpResponseMessage response = await this.PostAsync(uri, content, cancellation)) { @@ -178,5 +226,13 @@ namespace GumartinM.JsonRPC4NET public string jsonrpc { get; set; } public TResult result { get; set; } } + + private class POSTParameters + { + public string id { get; set; } + public string jsonrpc { get; set; } + public string method { get; set; } + public List @params { get; set; } + } } } -- 2.1.4