Refactoring MessagePort
authorSeungkeun Lee <sngn.lee@samsung.com>
Tue, 5 Apr 2016 00:56:16 +0000 (09:56 +0900)
committerSeungkeun Lee <sngn.lee@samsung.com>
Tue, 5 Apr 2016 05:04:33 +0000 (14:04 +0900)
Change-Id: Iee540aeee3ae2090029294c2cfc02bbbfc6c2034

Tizen.Applications/Tizen.Applications.Messages/MessagePort.cs
Tizen.Applications/Tizen.Applications.Messages/MessagePortErrorFactory.cs [changed mode: 0644->0755]
Tizen.Applications/Tizen.Applications.Messages/MessageReceivedEventArgs.cs
Tizen.Applications/Tizen.Applications.Messages/RemoteValues.cs

index 223714d..ac820fc 100755 (executable)
@@ -17,9 +17,10 @@ namespace Tizen.Applications.Messages
     /// If a remote application sends a message, the registered callback function of the local port is called.
     /// The trusted message-port API allows communications between applications that are signed by the same developer(author) certificate.
     /// </remarks>
-    public class MessagePort
+    public class MessagePort : IDisposable
     {
-        private static Dictionary<MessagePort, int> s_portMap = new Dictionary<MessagePort, int>();
+        private static object s_lock = new object();
+        private static HashSet<string> s_portMap = new HashSet<string>();
 
         /// <summary>
         /// Constructor
@@ -42,6 +43,7 @@ namespace Tizen.Applications.Messages
             {
                 StopListening();
             }
+            Dispose(false);
         }
 
         /// <summary>
@@ -108,47 +110,37 @@ namespace Tizen.Applications.Messages
         /// </summary>
         public void Listen()
         {
-            if (!s_portMap.ContainsKey(this))
+            lock(s_lock)
             {
-                _messageCallBack = (int localPortId, string remoteAppId, string remotePortName, bool trusted, IntPtr message, IntPtr userData) =>
+                if (s_portMap.Contains(_portName))
                 {
-                    Bundle bundle = new Bundle(message);
-                    MessageReceivedEventArgs args;
+                    throw new InvalidOperationException(_portName + " is already used");
+                }
+                _messageCallBack = (int localPortId, string remoteAppId, string remotePortName, bool trusted, IntPtr message, IntPtr userData) => {
+                    MessageReceivedEventArgs args = new MessageReceivedEventArgs() {
+                        Message = new Bundle(message)
+                    };
 
-                    if (!String.IsNullOrEmpty(remotePortName))
-                    {
-                        args = new MessageReceivedEventArgs(bundle, remoteAppId, remotePortName, trusted);
-                    }
-                    else
+                    if (!String.IsNullOrEmpty(remotePortName) && !String.IsNullOrEmpty(remoteAppId))
                     {
-                        args = new MessageReceivedEventArgs(bundle);
+                        args.Remote = new RemoteValues() {
+                            AppId = remoteAppId,
+                            PortName = remotePortName,
+                            Trusted = trusted
+                        };
                     }
-
                     RaiseMessageReceivedEvent(MessageReceived, args);
                 };
 
-                if (_trusted)
-                {
-                    _portId = Interop.MessagePort.RegisterTrustedPort(_portName, _messageCallBack, IntPtr.Zero);
-                }
-                else
-                {
-                    _portId = Interop.MessagePort.RegisterPort(_portName, _messageCallBack, IntPtr.Zero);
-                }
+                _portId = _trusted ?
+                            Interop.MessagePort.RegisterTrustedPort(_portName, _messageCallBack, IntPtr.Zero) :
+                            Interop.MessagePort.RegisterPort(_portName, _messageCallBack, IntPtr.Zero);
 
-                if (_portId > 0)
-                {
-                    s_portMap.Add(this, 1);
-                    _listening = true;
-                }
-                else
-                {
-                    MessagePortErrorFactory.ThrowException(_portId);
-                }
-            }
-            else
-            {
-                MessagePortErrorFactory.ThrowException((int)MessagePortError.InvalidOperation, "Already listening");
+                if (_portId <= 0)
+                    throw new InvalidOperationException("Can't Listening with " + _portName);
+
+                s_portMap.Add(_portName);
+                _listening = true;
             }
         }
 
@@ -157,33 +149,26 @@ namespace Tizen.Applications.Messages
         /// </summary>
         public void StopListening()
         {
-            if (_listening)
+            if (!_listening)
             {
-                int ret;
-                if (_trusted)
-                {
-                    ret = Interop.MessagePort.UnregisterTrustedPort(_portId);
-                }
-                else
-                {
-                    ret = Interop.MessagePort.UnregisterPort(_portId);
-                }
+                throw new InvalidOperationException("Already stopped");
+            }
 
-                if (ret == (int)MessagePortError.None)
-                {
-                    s_portMap.Remove(this);
-                    _portId = 0;
-                    _listening = false;
-                }
-                else
-                {
-                    MessagePortErrorFactory.ThrowException(ret);
-                }
+            int ret = _trusted ?
+                        Interop.MessagePort.UnregisterTrustedPort(_portId) :
+                        Interop.MessagePort.UnregisterPort(_portId);
+
+            if (ret != (int)MessagePortError.None)
+            {
+                MessagePortErrorFactory.ThrowException(ret);
             }
-            else
+
+            lock(s_lock)
             {
-                MessagePortErrorFactory.ThrowException((int)MessagePortError.InvalidOperation, "Already stopped");
+                s_portMap.Remove(_portName);
             }
+            _portId = 0;
+            _listening = false;
         }
 
         /// <summary>
@@ -195,72 +180,45 @@ namespace Tizen.Applications.Messages
         /// <param name="trusted">If true the trusted message port of remote application otherwise false</param>
         public void Send(Bundle message, string remoteAppId, string remotePortName, bool trusted = false)
         {
-            if (_listening)
+            if (!_listening)
             {
-                int ret;
-                if (trusted)
-                {
-                    ret = Interop.MessagePort.SendTrustedMessageWithLocalPort(remoteAppId, remotePortName, message.Handle, _portId);
-                }
-                else
-                {
-                    ret = Interop.MessagePort.SendMessageWithLocalPort(remoteAppId, remotePortName, message.Handle, _portId);
-                }
+                throw new InvalidOperationException("Sould start listen before send");
+            }
+            int ret = trusted ?
+                        Interop.MessagePort.SendTrustedMessageWithLocalPort(remoteAppId, remotePortName, message.Handle, _portId) :
+                        Interop.MessagePort.SendMessageWithLocalPort(remoteAppId, remotePortName, message.Handle, _portId);
 
-                if (ret != (int)MessagePortError.None)
+            if (ret != (int)MessagePortError.None)
+            {
+                if (ret== (int)MessagePortError.MaxExceeded)
                 {
-                    if (ret== (int)MessagePortError.MaxExceeded)
-                    {
-                        MessagePortErrorFactory.ThrowException(ret, "Message has exceeded the maximum limit(4KB)", "Message");
-                    }
-                    else
-                    {
-                        MessagePortErrorFactory.ThrowException(ret);
-                    }
+                    MessagePortErrorFactory.ThrowException(ret, "Message has exceeded the maximum limit(4KB)", "Message");
                 }
-            }
-            else
-            {
-                MessagePortErrorFactory.ThrowException((int)MessagePortError.InvalidOperation, "Need listening");
+                MessagePortErrorFactory.ThrowException(ret, "Can't send message");
             }
         }
 
-        /// <summary>
-        /// Override GetHashCode
-        /// </summary>
-        /// <returns></returns>
-        public override int GetHashCode()
+        private void RaiseMessageReceivedEvent(EventHandler<MessageReceivedEventArgs> evt, MessageReceivedEventArgs args)
         {
-            int hash = 0;
-            if (!String.IsNullOrEmpty(_portName))
+            if (evt != null)
             {
-                hash ^= _portName.GetHashCode();
+                evt(this, args);
             }
-            hash ^= _trusted.GetHashCode();
-            return hash;
         }
 
-        /// <summary>
-        /// Override Equals
-        /// </summary>
-        /// <param name="obj"></param>
-        /// <returns></returns>
-        public override bool Equals(object obj)
+        protected virtual void Dispose(bool disposing)
         {
-            MessagePort p = obj as MessagePort;
-            if (p == null)
+            if (_listening)
             {
-                return false;
+                StopListening();
             }
-            return (_portName == p._portName) & (_trusted == p._trusted);
         }
 
-        private void RaiseMessageReceivedEvent(EventHandler<MessageReceivedEventArgs> evt, MessageReceivedEventArgs args)
+        public void Dispose()
         {
-            if (evt != null)
-            {
-                evt(this, args);
-            }
+            Dispose(true);
+            GC.SuppressFinalize(this);
         }
+
     }
 }
\ No newline at end of file
index 675dc61..d3f9121 100755 (executable)
@@ -11,39 +11,13 @@ namespace Tizen.Applications.Messages
     /// </summary>
     public class MessageReceivedEventArgs : EventArgs
     {
-        private RemoteValues _remote;
-        private Bundle _message;
-
-        /// <summary>
-        /// Constructor
-        /// </summary>
-        public MessageReceivedEventArgs(Bundle message, string appId, string portName, bool trusted)
-        {
-            _message = message;
-
-            _remote = new RemoteValues();
-            _remote.AppId = appId;
-            _remote.PortName = portName;
-            _remote.Trusted = trusted;
-        }
-
-        /// <summary>
-        /// Constructor
-        /// </summary>
-        public MessageReceivedEventArgs(Bundle message)
-        {
-            _message = message;
-        }
-
         /// <summary>
         /// Contains AppId, Port Name, Trusted
         /// </summary>
         public RemoteValues Remote
         {
-            get
-            {
-                return _remote;
-            }
+            get;
+            internal set;
         }
 
         /// <summary>
@@ -51,10 +25,8 @@ namespace Tizen.Applications.Messages
         /// </summary>
         public Bundle Message
         {
-            get
-            {
-                return _message;
-            }
+            get;
+            internal set;
         }
     }
 }
index ce43996..191f69b 100755 (executable)
@@ -9,19 +9,29 @@ namespace Tizen.Applications.Messages
     /// <summary>
     /// Contains AppId, Port Name, Trusted
     /// </summary>
-    public struct RemoteValues
+    public class RemoteValues
     {
         /// <summary>
         /// The ID of the remote application that sent this message
         /// </summary>
-        public string AppId;
+        public string AppId {
+            get; set;
+        }
         /// <summary>
         /// The name of the remote message port
         /// </summary>
-        public string PortName;
+        public string PortName
+        {
+            get;
+            set;
+        }
         /// <summary>
         /// If true the remote port is a trusted port, otherwise if false it is not
         /// </summary>
-        public bool Trusted;
+        public bool Trusted
+        {
+            get;
+            set;
+        }
     }
 }