From 90b224fa9d792310b44494127d52072c94e7fa5a Mon Sep 17 00:00:00 2001 From: "gu.martinm@gmail.com" Date: Thu, 27 Feb 2014 02:26:03 +0100 Subject: [PATCH] RemoteAgents Mono GTK implementation. Trying to use log4net Nested using statements --- Mono/RemoteAgents/GTKLinux/GTKLinux.csproj | 8 ++++ Mono/RemoteAgents/GTKLinux/Log4Net.config | 14 ++++++ Mono/RemoteAgents/GTKLinux/MainWindow.cs | 7 ++- .../GTKLinux/Model/CallRemoteProcedure.cs | 51 ++++++++++++++++------ .../GTKLinux/Properties/AssemblyInfo.cs | 2 +- Mono/RemoteAgents/GTKLinux/View/View.cs | 4 +- Mono/RemoteAgents/GTKLinux/ViewModel/ViewModel.cs | 6 +-- 7 files changed, 70 insertions(+), 22 deletions(-) create mode 100644 Mono/RemoteAgents/GTKLinux/Log4Net.config diff --git a/Mono/RemoteAgents/GTKLinux/GTKLinux.csproj b/Mono/RemoteAgents/GTKLinux/GTKLinux.csproj index 712fb3a..67b6699 100644 --- a/Mono/RemoteAgents/GTKLinux/GTKLinux.csproj +++ b/Mono/RemoteAgents/GTKLinux/GTKLinux.csproj @@ -61,6 +61,9 @@ through JSON API. monodevelop True + + ..\..\..\..\..\..\..\usr\mymono\custom\log4net-1.2.13\net\4.0\log4net.dll + @@ -85,4 +88,9 @@ through JSON API. + + + PreserveNewest + + \ No newline at end of file diff --git a/Mono/RemoteAgents/GTKLinux/Log4Net.config b/Mono/RemoteAgents/GTKLinux/Log4Net.config new file mode 100644 index 0000000..7c99490 --- /dev/null +++ b/Mono/RemoteAgents/GTKLinux/Log4Net.config @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + diff --git a/Mono/RemoteAgents/GTKLinux/MainWindow.cs b/Mono/RemoteAgents/GTKLinux/MainWindow.cs index 85a31d4..44acec5 100644 --- a/Mono/RemoteAgents/GTKLinux/MainWindow.cs +++ b/Mono/RemoteAgents/GTKLinux/MainWindow.cs @@ -3,12 +3,15 @@ using Gtk; using System.Net.Http; using System.Threading.Tasks; using Example.RemoteAgents.GTKLinux.View; +using log4net; namespace Example.RemoteAgents.GTKLinux { public partial class MainWindow: Gtk.Window { ViewImpl view; + private static readonly ILog logger = LogManager.GetLogger( + System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); public MainWindow () : base (Gtk.WindowType.Toplevel) { @@ -35,7 +38,7 @@ namespace Example.RemoteAgents.GTKLinux async private void ButtonGetDateClicked(object sender, EventArgs a) { try { - string currentDate = await view.getCurrentDate(); + string currentDate = await view.GetCurrentDateAsync(); if (currentDate != null) { this.RemoteDate.Buffer.Text = currentDate; @@ -43,7 +46,7 @@ namespace Example.RemoteAgents.GTKLinux } catch (Exception e) { - Console.WriteLine("ButtonGetDateClicked. Message: {0} Stacktrace: {1}", e.Message, e.StackTrace); + logger.Error("ButtonGetDateClicked error: ", e); } } } diff --git a/Mono/RemoteAgents/GTKLinux/Model/CallRemoteProcedure.cs b/Mono/RemoteAgents/GTKLinux/Model/CallRemoteProcedure.cs index 548ea92..f444c0d 100644 --- a/Mono/RemoteAgents/GTKLinux/Model/CallRemoteProcedure.cs +++ b/Mono/RemoteAgents/GTKLinux/Model/CallRemoteProcedure.cs @@ -10,13 +10,24 @@ using Gtk; using Newtonsoft.Json; using Newtonsoft.Json.Converters; using Newtonsoft.Json.Serialization; +using System.Threading; +using log4net; namespace Example.RemoteAgents.GTKLinux.Model { public class CallRemoteProcedure { + private static readonly ILog logger = LogManager.GetLogger( + System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); - async public Task callRemoteService(string uri, string method) + async public Task PostRemoteServiceAsync(string uri, string method) + { + POSTResult postResult = await this.PostAsync(uri, method, CancellationToken.None); + + return postResult.result; + } + + async private Task> PostAsync(string uri, string method, CancellationToken cancellation) { var postData = new POST(); postData.id = "2114567586433855105"; @@ -25,34 +36,46 @@ namespace Example.RemoteAgents.GTKLinux.Model var jsonSettings = new JsonSerializerSettings{ Error = delegate(object sender, ErrorEventArgs args) { - Console.WriteLine(args.ErrorContext.Error.Message); + logger.Error(args.ErrorContext.Error.Message); args.ErrorContext.Handled = true; } }; string data = JsonConvert.SerializeObject(postData, jsonSettings); - HttpContent content = new StringContent(data, System.Text.Encoding.UTF8, "application/json-rpc"); - HttpResponseMessage response = await this.issueCall(uri, content); + // 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 (HttpResponseMessage response = await this.PostAsync(uri, content, cancellation)) + { + POSTResult postResult = new POSTResult(); - TResult result = default(TResult); + if (response.StatusCode == HttpStatusCode.OK) { + byte[] responseBytes = await response.Content.ReadAsByteArrayAsync(); + string responseString = System.Text.Encoding.UTF8.GetString(responseBytes); - if (response.StatusCode == HttpStatusCode.OK) { - Task responseBytes = response.Content.ReadAsByteArrayAsync(); - string responseString = System.Text.Encoding.UTF8.GetString(responseBytes.Result); + postResult = JsonConvert.DeserializeObject>(responseString, jsonSettings); + } - POSTResult postResult = JsonConvert.DeserializeObject>(responseString, jsonSettings); - result = postResult.result; + return postResult; } - - return result; } - async private Task issueCall(string uri, HttpContent content) + /// + /// Send a POST request to the specified Uri as an asynchronous operation. + /// + /// The Uri the request is sent to. + /// The HTTP request content sent to the server. + /// Cancellation token. + /// When some error. + /// System.Threading.Tasks.Task]]>.The task object representing the asynchronous operation. + async private Task PostAsync(string uri, HttpContent content, CancellationToken cancellation) { using (HttpClient client = new HttpClient{ Timeout = TimeSpan.FromSeconds(5)}) { - return await client.PostAsync(uri, content); + return await client.PostAsync(uri, content, cancellation); } } diff --git a/Mono/RemoteAgents/GTKLinux/Properties/AssemblyInfo.cs b/Mono/RemoteAgents/GTKLinux/Properties/AssemblyInfo.cs index 7b2678f..9d9a201 100644 --- a/Mono/RemoteAgents/GTKLinux/Properties/AssemblyInfo.cs +++ b/Mono/RemoteAgents/GTKLinux/Properties/AssemblyInfo.cs @@ -19,4 +19,4 @@ using System.Runtime.CompilerServices; // if desired. See the Mono documentation for more information about signing. //[assembly: AssemblyDelaySign(false)] //[assembly: AssemblyKeyFile("")] - +[assembly: log4net.Config.XmlConfigurator(ConfigFile="Log4Net.config", Watch=true)] diff --git a/Mono/RemoteAgents/GTKLinux/View/View.cs b/Mono/RemoteAgents/GTKLinux/View/View.cs index f35dc80..93660f2 100644 --- a/Mono/RemoteAgents/GTKLinux/View/View.cs +++ b/Mono/RemoteAgents/GTKLinux/View/View.cs @@ -9,9 +9,9 @@ namespace Example.RemoteAgents.GTKLinux.View { private static readonly ViewModelImpl vm = new ViewModelImpl(); - async public Task getCurrentDate() + async public Task GetCurrentDateAsync() { - return await vm.getCurrentDate(); + return await vm.GetCurrentDateAsync(); } } } diff --git a/Mono/RemoteAgents/GTKLinux/ViewModel/ViewModel.cs b/Mono/RemoteAgents/GTKLinux/ViewModel/ViewModel.cs index cae493a..e21730c 100644 --- a/Mono/RemoteAgents/GTKLinux/ViewModel/ViewModel.cs +++ b/Mono/RemoteAgents/GTKLinux/ViewModel/ViewModel.cs @@ -7,11 +7,11 @@ namespace Example.RemoteAgents.GTKLinux.ViewModel public class ViewModelImpl { private static readonly string uri = "http://gumartinm.name/spring-mainapp/CurrentDateService.json"; - private static readonly CallRemoteProcedure remoteProcedure = new CallRemoteProcedure(); + private readonly CallRemoteProcedure remoteProcedure = new CallRemoteProcedure(); - async public Task getCurrentDate() + async public Task GetCurrentDateAsync() { - return await remoteProcedure.callRemoteService(uri, "getCurrentDate"); + return await remoteProcedure.PostRemoteServiceAsync(uri, "getCurrentDate"); } } } -- 2.1.4