[Multimedia] Modified MediaControlServer to check the running state by querying. 94/147994/4
authorcoderhyme <jhyo.kim@samsung.com>
Wed, 6 Sep 2017 08:01:35 +0000 (17:01 +0900)
committerhj kim <backto.kim@samsung.com>
Tue, 12 Sep 2017 02:07:00 +0000 (02:07 +0000)
The reason is when the application crashes, the server is alive in the database,
which means the server can work continously after the application is restarted.

Change-Id: I8819e0e4a7caca828aa8e493043461203364aa98
Signed-off-by: coderhyme <jhyo.kim@samsung.com>
src/Tizen.Multimedia.Remoting/Interop/Interop.MediaControllerServer.cs
src/Tizen.Multimedia.Remoting/MediaController/MediaControlServer.cs

index 6fe514d..abd6e58 100644 (file)
@@ -62,5 +62,14 @@ internal static partial class Interop
 
         [DllImport(Libraries.MediaController, EntryPoint = "mc_server_unset_playback_state_command_received_cb")]
         internal static extern MediaControllerError UnsetPlaybackStateCmdRecvCb(IntPtr handle);
+
+        [DllImport(Libraries.MediaController, EntryPoint = "mc_db_connect")]
+        internal static extern MediaControllerError ConnectDb(out IntPtr dbHandle);
+
+        [DllImport(Libraries.MediaController, EntryPoint = "mc_db_disconnect")]
+        internal static extern MediaControllerError DisconnectDb(IntPtr dbHandle);
+
+        [DllImport(Libraries.MediaController, EntryPoint = "mc_db_check_server_table_exist")]
+        internal static extern MediaControllerError CheckServerExist(IntPtr dbHandle, string appId, out bool value);
     }
 }
index f4fcdf2..c4909bb 100644 (file)
@@ -27,6 +27,7 @@ namespace Tizen.Multimedia.Remoting
     public static class MediaControlServer
     {
         private static IntPtr _handle = IntPtr.Zero;
+        private static bool? _isRunning;
 
         /// <summary>
         /// Gets a value indicating whether the server is running.
@@ -36,53 +37,71 @@ namespace Tizen.Multimedia.Remoting
         /// <seealso cref="Stop"/>
         public static bool IsRunning
         {
-            get => _handle != IntPtr.Zero;
+            get
+            {
+                if (_isRunning.HasValue == false)
+                {
+                    _isRunning = GetRunningState();
+                }
+
+                return _isRunning.Value;
+            }
+        }
+
+        private static bool GetRunningState()
+        {
+            IntPtr handle = IntPtr.Zero;
+            try
+            {
+                Native.ConnectDb(out handle).ThrowIfError("Failed to retrieve the running state.");
+
+                Native.CheckServerExist(handle, Applications.Application.Current.ApplicationInfo.ApplicationId,
+                    out var value).ThrowIfError("Failed to retrieve the running state.");
+
+                return value;
+            }
+            finally
+            {
+                if (handle != IntPtr.Zero)
+                {
+                    Native.DisconnectDb(handle);
+                }
+            }
         }
 
-        private static void ThrowIfNotRunning()
+        private static void EnsureInitializedIfRunning()
         {
+            if (_handle != IntPtr.Zero)
+            {
+                return;
+            }
+
             if (IsRunning == false)
             {
                 throw new InvalidOperationException("The server is not running.");
             }
+
+            Initialize();
         }
 
         private static IntPtr Handle
         {
             get
             {
-                ThrowIfNotRunning();
+                EnsureInitializedIfRunning();
 
                 return _handle;
             }
         }
 
-        /// <summary>
-        /// Starts the media control server.
-        /// </summary>
-        /// <remarks>
-        /// When the server starts, <see cref="MediaControllerManager.ServerStarted"/> will be raised.
-        /// </remarks>
-        /// <privilege>http://tizen.org/privilege/mediacontroller.server</privilege>
-        /// <exception cref="InvalidOperationException">
-        ///     The server has already started.\n
-        ///     -or-\n
-        ///     An internal error occurs.
-        /// </exception>
-        /// <exception cref="UnauthorizedAccessException">Caller does not have required privilege.</exception>
-        /// <seealso cref="MediaControllerManager.ServerStarted"/>
-        public static void Start()
+        private static void Initialize()
         {
-            if (IsRunning)
-            {
-                throw new InvalidOperationException("The server is already running.");
-            }
-
             Native.Create(out _handle).ThrowIfError("Failed to create media controller server.");
 
             try
             {
                 RegisterPlaybackCommandReceivedEvent();
+                _isRunning = true;
             }
             catch
             {
@@ -94,6 +113,21 @@ namespace Tizen.Multimedia.Remoting
         }
 
         /// <summary>
+        /// Starts the media control server.
+        /// </summary>
+        /// <remarks>
+        /// When the server starts, <see cref="MediaControllerManager.ServerStarted"/> will be raised.
+        /// </remarks>
+        /// <privilege>http://tizen.org/privilege/mediacontroller.server</privilege>
+        /// <exception cref="InvalidOperationException">An internal error occurs.</exception>
+        /// <exception cref="UnauthorizedAccessException">Caller does not have required privilege.</exception>
+        /// <seealso cref="MediaControllerManager.ServerStarted"/>
+        public static void Start()
+        {
+            Initialize();
+        }
+
+        /// <summary>
         /// Stops the media control server.
         /// </summary>
         /// <remarks>
@@ -107,12 +141,13 @@ namespace Tizen.Multimedia.Remoting
         /// <seealso cref="MediaControllerManager.ServerStopped"/>
         public static void Stop()
         {
-            ThrowIfNotRunning();
+            EnsureInitializedIfRunning();
 
             Native.Destroy(_handle).ThrowIfError("Failed to stop the server.");
 
             _handle = IntPtr.Zero;
             _playbackCommandCallback = null;
+            _isRunning = false;
         }
 
         /// <summary>