[smuxi] Common: probe and report remoting connection errors in SingleApplicationInstance



commit fb5e3542d58ae480a1fa44e3fd487f69b9bf6b30
Author: Mirco Bauer <meebey meebey net>
Date:   Thu Apr 23 06:16:03 2015 +0200

    Common: probe and report remoting connection errors in SingleApplicationInstance

 src/Common-Tests/SingleApplicationInstanceTests.cs |    2 +-
 src/Common/SingleApplicationInstance.cs            |   31 ++++++++++++++++++-
 2 files changed, 30 insertions(+), 3 deletions(-)
---
diff --git a/src/Common-Tests/SingleApplicationInstanceTests.cs 
b/src/Common-Tests/SingleApplicationInstanceTests.cs
index ffd9b83..79c5669 100644
--- a/src/Common-Tests/SingleApplicationInstanceTests.cs
+++ b/src/Common-Tests/SingleApplicationInstanceTests.cs
@@ -31,7 +31,7 @@ namespace Smuxi.Common
     {
         SingleApplicationInstance<TestApplication> FirstInstance { get; set; }
 
-        class TestApplication : MarshalByRefObject
+        class TestApplication : SingleApplicationInterface
         {
             public int InvokeCounter { get; private set; }
 
diff --git a/src/Common/SingleApplicationInstance.cs b/src/Common/SingleApplicationInstance.cs
index 935452e..ed9831e 100644
--- a/src/Common/SingleApplicationInstance.cs
+++ b/src/Common/SingleApplicationInstance.cs
@@ -33,7 +33,7 @@ using Mono.Unix.Native;
 
 namespace Smuxi.Common
 {
-    public class SingleApplicationInstance<T> : IDisposable where T : MarshalByRefObject
+    public class SingleApplicationInstance<T> : IDisposable where T : SingleApplicationInterface
     {
         public string Identifier { get; private set; }
         public bool IsFirstInstance { get; private set; }
@@ -285,7 +285,25 @@ namespace Smuxi.Common
         {
             RemotingChannel = new IpcClientChannel();
             ChannelServices.RegisterChannel(RemotingChannel, false);
-            f_FirstInstance = (T) Activator.GetObject(typeof(T), "ipc://" + Identifier + "/" + 
RemotingObjectName);
+            try {
+                f_FirstInstance = (T) Activator.GetObject(typeof(T), "ipc://" + Identifier + "/" + 
RemotingObjectName);
+                // HACK: we have to make a method call so we actually can tell
+                // if the connection works or not
+                var isAlive = f_FirstInstance.IsAlive;
+            } catch (RemotingException ex) {
+#if MONO_UNIX
+                if (FirstInstanceFileInfo == null) {
+                    // no idea what happened
+                    throw;
+                } else {
+                    throw new RemotingException(
+                        "Remoting communication error with existing " +
+                        "application instance. Stalled file lock? (" +
+                        FirstInstanceFileInfo.FullName + ")", ex
+                    );
+                }
+#endif
+            }
         }
 
         static bool IsRunningOnMono()
@@ -293,4 +311,13 @@ namespace Smuxi.Common
             return Type.GetType("Mono.Runtime") != null;
         }
     }
+
+    public class SingleApplicationInterface : MarshalByRefObject
+    {
+        public bool IsAlive {
+            get {
+                return true;
+            }
+        }
+    }
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]