Changed the retry function in PithosClient to use the TPL
authorPanagiotis Kanavos <pkanavos@gmail.com>
Thu, 15 Sep 2011 20:05:28 +0000 (23:05 +0300)
committerPanagiotis Kanavos <pkanavos@gmail.com>
Thu, 15 Sep 2011 20:05:28 +0000 (23:05 +0300)
Deactivated the EnsureHost() function in PithosHost.cs
Modified the GotoPithos menu command in FileContextMenu.cs to open a URL using the user's account and token

12 files changed:
trunk/Pithos.Client.WPF/Properties/Settings.Designer.cs
trunk/Pithos.Client.WPF/Properties/Settings.settings
trunk/Pithos.Client.WPF/TaskbarViewModel.cs
trunk/Pithos.Client.WPF/app.config
trunk/Pithos.Core/StatusKeeper.cs
trunk/Pithos.Network/PithosClient.cs
trunk/Pithos.ShellExtensions/Menus/FileContextMenu.cs
trunk/Pithos.ShellExtensions/Pithos.ShellExtensions.csproj
trunk/Pithos.ShellExtensions/PithosHost.cs
trunk/Pithos.ShellExtensions/Properties/Resources.Designer.cs
trunk/Pithos.ShellExtensions/Properties/Resources.resx
trunk/Pithos.ShellExtensions/Resources/MenuProperties.bmp [new file with mode: 0644]

index 0f59544..abd9df9 100644 (file)
@@ -205,7 +205,7 @@ namespace Pithos.Client.WPF.Properties {
         
         [global::System.Configuration.ApplicationScopedSettingAttribute()]
         [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
-        [global::System.Configuration.DefaultSettingValueAttribute("http://pithos.dev.grnet.gr/pithos.html")]
+        [global::System.Configuration.DefaultSettingValueAttribute("http://pithos.dev.grnet.gr")]
         public string PithosSite {
             get {
                 return ((string)(this["PithosSite"]));
index 1a83a10..1c64814 100644 (file)
@@ -48,7 +48,7 @@
       <Value Profile="(Default)">False</Value>
     </Setting>
     <Setting Name="PithosSite" Type="System.String" Scope="Application">
-      <Value Profile="(Default)">http://pithos.dev.grnet.gr/pithos.html</Value>
+      <Value Profile="(Default)">http://pithos.dev.grnet.gr</Value>
     </Setting>
     <Setting Name="Accounts" Type="Pithos.Interfaces.AccountsCollection" Scope="User">
       <Value Profile="(Default)">
index cacc37a..14af781 100644 (file)
@@ -169,7 +169,14 @@ namespace Pithos.Client.WPF
             }
             if (!String.IsNullOrWhiteSpace(Monitor.UserName) &&
                 !String.IsNullOrWhiteSpace(Monitor.ApiKey))
-                Monitor.Start();
+                try
+                {
+                    Monitor.Start();
+                }
+                catch (Exception exc)
+                {
+                    MessageBox.Show("An exception occured. Can't start monitoring");
+                }
         }
     }
 }
index e72c8a7..446a37c 100644 (file)
       <setting name="UseManualProxy" serializeAs="String">
         <value>False</value>
       </setting>
+      <setting name="Accounts" serializeAs="Xml">
+        <value>
+          <ArrayOfAccountSettings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+            xmlns:xsd="http://www.w3.org/2001/XMLSchema">
+            <AccountSettings>
+            </AccountSettings>
+          </ArrayOfAccountSettings>
+        </value>
+      </setting>
     </Pithos.Client.WPF.Properties.Settings>
   </userSettings>
   <connectionStrings>
@@ -88,7 +97,7 @@
   <applicationSettings>
     <Pithos.Client.WPF.Properties.Settings>
       <setting name="PithosSite" serializeAs="String">
-        <value>http://pithos.dev.grnet.gr/pithos.html</value>
+        <value>http://pithos.dev.grnet.gr</value>
       </setting>
       <setting name="PithosAuthenticationUrl" serializeAs="String">
         <value>https://pithos.dev.grnet.gr</value>
index 4dc51a6..5e7438c 100644 (file)
@@ -24,7 +24,7 @@ namespace Pithos.Core
         [System.ComponentModel.Composition.Import]
         public IPithosSettings Settings { get; set; }
 
-        private JobQueue _statusUpdateQueue;
+        private JobQueue _statusUpdateQueue=new JobQueue();
 
         public StatusKeeper()
         {            
index 2942fce..3bdd740 100644 (file)
@@ -7,8 +7,10 @@
 using System.Collections.Specialized;
 using System.Diagnostics;
 using System.Diagnostics.Contracts;
+using System.IO;
 using System.Net;
 using System.Runtime.Serialization;
+using System.Threading.Tasks;
 
 namespace Pithos.Network
 {
@@ -84,10 +86,35 @@ namespace Pithos.Network
 
         protected override WebResponse GetWebResponse(WebRequest request)
         {
+            try
+            {
+
             var response = (HttpWebResponse)base.GetWebResponse(request);
             StatusCode = response.StatusCode;
             StatusDescription = response.StatusDescription;
             return response;
+            }
+            catch (WebException exc)
+            {
+                if (exc.Response.ContentLength > 0)
+                {
+                    string content;
+                    content = GetContent(exc.Response);
+                    Trace.TraceError(content);
+                }
+                throw;
+            }
+        }
+
+        private static string GetContent(WebResponse webResponse)
+        {
+            string content;
+            using (var stream = webResponse.GetResponseStream())
+            using (var reader = new StreamReader(stream))
+            {
+                content = reader.ReadToEnd();
+            }
+            return content;
         }
 
         public string DownloadStringWithRetry(string address,int retries=0)
@@ -100,7 +127,8 @@ namespace Pithos.Network
             var actualRetries = (retries == 0) ? Retries : retries;
             
 
-            var func = Retry(() =>
+            
+            var task = Retry(() =>
             {
                 var uriString = String.Join("/", BaseAddress, actualAddress);
                 var content = base.DownloadString(uriString);
@@ -111,7 +139,7 @@ namespace Pithos.Network
 
             }, actualRetries);
 
-            var result = func();
+            var result = task.Result;
             return result;
         }
 
@@ -147,7 +175,7 @@ namespace Pithos.Network
             var actualAddress = GetActualAddress(address);
 
             var actualRetries = (retries == 0) ? Retries : retries;
-            var func = Retry(() =>
+            var task = Retry(() =>
             {
                 var uriString = String.Join("/",BaseAddress ,actualAddress);
                 var uri = new Uri(uriString);
@@ -164,7 +192,7 @@ namespace Pithos.Network
                 return 0;
             }, actualRetries);
 
-            func();
+            task.Wait();
         }
 
         private string GetActualAddress(string address)
@@ -191,7 +219,7 @@ namespace Pithos.Network
                 throw new ArgumentNullException("address");
 
             var actualRetries = (retries == 0) ? Retries : retries;            
-            var func = Retry(() =>
+            var task = Retry(() =>
             {
                 var content = base.DownloadString(address);
 
@@ -201,7 +229,7 @@ namespace Pithos.Network
 
             }, actualRetries);
 
-            var result = func();
+            var result = task.Result;
             return result;
         }
 
@@ -243,7 +271,7 @@ namespace Pithos.Network
                 throw new WebException(String.Format("{0} with code {1} - {2}", message, StatusCode, StatusDescription));
         }
 
-        private Func<T> Retry<T>(Func< T> original, int retryCount)
+        /*private Func<T> Retry<T>(Func< T> original, int retryCount)
         {
             return () =>
             {
@@ -305,6 +333,44 @@ namespace Pithos.Network
                     }
                 }
             };
+        }*/
+        
+        private Task<T> Retry<T>(Func< T> original, int retryCount)
+        {
+            return Task.Factory.StartNew(() => original()).ContinueWith(_original =>
+                {
+                    if(_original.IsFaulted )
+                    {
+                        var e = _original.Exception.InnerException;
+                        if (e is WebException)
+                        {
+                            var we = (e as WebException);
+                            var statusCode = ((HttpWebResponse) we.Response).StatusCode;
+                            this.StatusCode = statusCode;
+
+                            if (we.Status==WebExceptionStatus.Timeout || 
+                                (we.Status==WebExceptionStatus.ProtocolError && statusCode==HttpStatusCode.ServiceUnavailable))
+                            {
+                                TimedOut = true;
+                                if (retryCount == 0)
+                                {
+                                    Trace.TraceError("[ERROR] Timed out too many times. {0}\n", e);
+                                    throw new RetryException("Timed out too many times.", e);
+                                }
+                                Trace.TraceError(
+                                    "[RETRY] Timed out after {0} ms. Will retry {1} more times\n{2}", Timeout,
+                                    retryCount, e);
+                                return Retry(original, retryCount - 1);
+                            }
+
+                            if (statusCode==HttpStatusCode.NotFound)
+                                return new Task<T>(() => default(T));                            
+                        }
+                        throw e;
+                    }
+                    else                    
+                        return Task<T>.Factory.StartNew(() => _original.Result);
+                }).Unwrap();
         }
 
        
index bc0ee04..a7f2396 100644 (file)
@@ -27,11 +27,13 @@ namespace Pithos.ShellExtensions.Menus
 
         private IntPtr _gotoBitmap=IntPtr.Zero;
         private IntPtr _versionBitmap = IntPtr.Zero;
+        private IntPtr _propertiesBitmap = IntPtr.Zero;
 
         public FileContextMenu()
         {                        
             _gotoBitmap = GetBitmapPtr(Resources.MenuGoToPithos);
             _versionBitmap = GetBitmapPtr(Resources.MenuHistory);
+            _propertiesBitmap = GetBitmapPtr(Resources.MenuProperties);
 
 
             
@@ -47,12 +49,22 @@ namespace Pithos.ShellExtensions.Menus
                                                DisplayFlags=DisplayFlags.All,
                                                MenuBitmap = _gotoBitmap
                                            }},
+                /*{"showProperties",new MenuItem{
+                                           MenuText = "&Pithos File Properties",
+                                            Verb = "showProperties",
+                                             VerbCanonicalName = "PITHOSProperties",
+                                              VerbHelpText = "Pithos File Properties",
+                                               MenuDisplayId = 2,
+                                               MenuCommand=OnShowProperties,
+                                               DisplayFlags=DisplayFlags.File,
+                                               MenuBitmap = _propertiesBitmap
+                                           }},*/
                 {"prevVersions",new MenuItem{
                                            MenuText = "&Show Previous Versions",
                                             Verb = "prevVersions",
                                              VerbCanonicalName = "PITHOSPrevVersions",
                                               VerbHelpText = "Go to Pithos and display previous versions",
-                                               MenuDisplayId = 2,
+                                               MenuDisplayId = 3,
                                                MenuCommand=OnVerbDisplayFileName,
                                                DisplayFlags=DisplayFlags.File,
                                                MenuBitmap=_versionBitmap
@@ -77,6 +89,11 @@ namespace Pithos.ShellExtensions.Menus
                 NativeMethods.DeleteObject(_versionBitmap);
                 _versionBitmap = IntPtr.Zero;
             }
+            if (_propertiesBitmap != IntPtr.Zero)
+            {
+                NativeMethods.DeleteObject(_propertiesBitmap);
+                _propertiesBitmap = IntPtr.Zero;
+            }
 
         }
         private static IntPtr GetBitmapPtr(Bitmap gotoBitmap)
@@ -86,6 +103,10 @@ namespace Pithos.ShellExtensions.Menus
             return hbitmap;
         }
 
+        void OnShowProperties(IntPtr hWnd)
+        {
+            
+        }
 
         void OnVerbDisplayFileName(IntPtr hWnd)
         {
@@ -102,8 +123,15 @@ namespace Pithos.ShellExtensions.Menus
 
         void OnGotoPithos(IntPtr hWnd)
         {
-            Context.Settings.Reload();
-            Process.Start(Context.Settings.PithosSite);
+            var settings = Context.Settings;
+            var activeAccount = settings.Accounts.FirstOrDefault(acc => acc.IsActive);
+            var address = String.Format("{0}/ui/?token={1}&user={2}",
+                                        settings.PithosSite,
+                                        activeAccount.ApiKey,
+                                        Uri.EscapeUriString(activeAccount.AccountName));
+
+            settings.Reload();
+            Process.Start(address);
         }
         
 
index 5e0ae38..1620007 100644 (file)
       <LastGenOutput>Reference.cs</LastGenOutput>
     </None>
   </ItemGroup>
+  <ItemGroup>
+    <None Include="Resources\MenuProperties.bmp" />
+  </ItemGroup>
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
   <PropertyGroup>
     <PreBuildEvent>if  exist "$(TargetPath).locked" del "$(TargetPath).locked"
index d87d0c8..bf8db73 100644 (file)
@@ -24,6 +24,7 @@ namespace Pithos.ShellExtensions
     {
         public static void EnsureHost()
         {
+            return;
             if (Process.GetProcessesByName("Pithos.Client.WPF").Length == 0 &&
                 Process.GetProcessesByName("Pithos.Client.WPF.vshost").Length == 0)
             {
index f3fc96c..02d3ad0 100644 (file)
@@ -73,5 +73,12 @@ namespace Pithos.ShellExtensions.Properties {
                 return ((System.Drawing.Bitmap)(obj));
             }
         }
+        
+        internal static System.Drawing.Bitmap MenuProperties {
+            get {
+                object obj = ResourceManager.GetObject("MenuProperties", resourceCulture);
+                return ((System.Drawing.Bitmap)(obj));
+            }
+        }
     }
 }
index a38fe9a..8778c26 100644 (file)
   <data name="MenuHistory" type="System.Resources.ResXFileRef, System.Windows.Forms">
     <value>..\Resources\MenuHistory.bmp;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
   </data>
+  <data name="MenuProperties" type="System.Resources.ResXFileRef, System.Windows.Forms">
+    <value>..\Resources\MenuProperties.bmp;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
+  </data>
 </root>
\ No newline at end of file
diff --git a/trunk/Pithos.ShellExtensions/Resources/MenuProperties.bmp b/trunk/Pithos.ShellExtensions/Resources/MenuProperties.bmp
new file mode 100644 (file)
index 0000000..29b4985
Binary files /dev/null and b/trunk/Pithos.ShellExtensions/Resources/MenuProperties.bmp differ