From 40bcedea4800e8beaecfe627283c4142738eac6e Mon Sep 17 00:00:00 2001 From: Inkyun Kil Date: Wed, 16 Aug 2017 20:15:38 +0900 Subject: [PATCH] Add new apis for getting information of remoteport Change-Id: Ieaed48ac0e39db7d70ab8424b44c059a59a353b7 Signed-off-by: Inkyun Kil --- .../Interop/Interop.MessagePort.cs | 18 ++ .../Tizen.Applications.Messages/MessagePort.cs | 4 +- .../Tizen.Applications.Messages/RemotePort.cs | 313 +++++++++++++++++++++ .../RemotePortStateChangedEventArgs.cs | 33 +++ 4 files changed, 366 insertions(+), 2 deletions(-) create mode 100755 src/Tizen.Applications.MessagePort/Tizen.Applications.Messages/RemotePort.cs create mode 100755 src/Tizen.Applications.MessagePort/Tizen.Applications.Messages/RemotePortStateChangedEventArgs.cs diff --git a/src/Tizen.Applications.MessagePort/Interop/Interop.MessagePort.cs b/src/Tizen.Applications.MessagePort/Interop/Interop.MessagePort.cs index bcbbc63..564e7ba 100755 --- a/src/Tizen.Applications.MessagePort/Interop/Interop.MessagePort.cs +++ b/src/Tizen.Applications.MessagePort/Interop/Interop.MessagePort.cs @@ -41,7 +41,25 @@ internal static partial class Interop [DllImport(Libraries.MessagePort, EntryPoint = "message_port_send_trusted_message_with_local_port", CallingConvention = CallingConvention.Cdecl)] internal static extern int SendTrustedMessageWithLocalPort(string remote_app_id, string remote_port, SafeBundleHandle message, int local_port_id); + [DllImport(Libraries.MessagePort, EntryPoint = "message_port_check_remote_port", CallingConvention = CallingConvention.Cdecl)] + internal static extern int CheckRemotePort(string remote_app_id, string remote_port, out bool exist); + + [DllImport(Libraries.MessagePort, EntryPoint = "message_port_check_trusted_remote_port", CallingConvention = CallingConvention.Cdecl)] + internal static extern int CheckTrustedRemotePort(string remote_app_id, string remote_port, out bool exist); + + [DllImport(Libraries.MessagePort, EntryPoint = "message_port_add_registered_cb", CallingConvention = CallingConvention.Cdecl)] + internal static extern int AddRegisteredCallback(string remote_app_id, string remote_port, bool trusted_remote_port, message_port_registration_event_cb callback, IntPtr userData, out int watcher_id); + + [DllImport(Libraries.MessagePort, EntryPoint = "message_port_add_unregistered_cb", CallingConvention = CallingConvention.Cdecl)] + internal static extern int AddUnregisteredCallback(string remote_app_id, string remote_port, bool trusted_remote_port, message_port_registration_event_cb callback, IntPtr userData, out int watcher_id); + + [DllImport(Libraries.MessagePort, EntryPoint = "message_port_remove_registration_event_cb", CallingConvention = CallingConvention.Cdecl)] + internal static extern int RemoveRegistrationCallback(int watcher_id); + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] internal delegate void message_port_message_cb(int local_port_id, string remote_app_id, string remote_port, bool trusted_remote_port, IntPtr message, IntPtr userData); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void message_port_registration_event_cb(string remote_app_id, string remote_port, bool trusted_remote_port, IntPtr userData); } } diff --git a/src/Tizen.Applications.MessagePort/Tizen.Applications.Messages/MessagePort.cs b/src/Tizen.Applications.MessagePort/Tizen.Applications.Messages/MessagePort.cs index c92362a..b429a88 100755 --- a/src/Tizen.Applications.MessagePort/Tizen.Applications.Messages/MessagePort.cs +++ b/src/Tizen.Applications.MessagePort/Tizen.Applications.Messages/MessagePort.cs @@ -183,7 +183,7 @@ namespace Tizen.Applications.Messages /// message.AddItem("message", "a_string"); /// messagePort.Send(message, "ReceiverAppID", "ReceiverPort"); /// } - /// messageProt.StopListening(); + /// messagePort.StopListening(); /// public void StopListening() { @@ -276,7 +276,7 @@ namespace Tizen.Applications.Messages } /// - /// Releases the unmanaged resource used by the MessagePort class specifying whether to perform a normal dispose operation. + /// Releases the unmanaged resources used by the MessagePort class specifying whether to perform a normal dispose operation. /// /// true for a normal dispose operation; false to finalize the handle. protected virtual void Dispose(bool disposing) diff --git a/src/Tizen.Applications.MessagePort/Tizen.Applications.Messages/RemotePort.cs b/src/Tizen.Applications.MessagePort/Tizen.Applications.Messages/RemotePort.cs new file mode 100755 index 0000000..1d6d0d4 --- /dev/null +++ b/src/Tizen.Applications.MessagePort/Tizen.Applications.Messages/RemotePort.cs @@ -0,0 +1,313 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License.\ + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +namespace Tizen.Applications.Messages +{ + using System; + + /// + /// The RemotePort Class provides functions to get if the remote port is running and to get whether the remote port is registered or unregistered. + /// + /// 4 + public class RemotePort : IDisposable + { + private int _watcherIdForRegistered = -1; + private int _watcherIdForUnRegistered = -1; + private bool _disposed = false; + + private Interop.MessagePort.message_port_registration_event_cb _registeredCallBack; + private Interop.MessagePort.message_port_registration_event_cb _unregisteredCallBack; + + private EventHandler _remotePortRegistered; + private EventHandler _remotePortUnregistered; + + private readonly string _portName = null; + private readonly string _appId = null; + private readonly bool _trusted = false; + + private bool _isRunning = false; + + /// + /// Constructor of the RemotePort class. + /// + /// 4 + /// The Id of the remote application + /// The name of the remote message port + /// If true is the trusted message port of application, otherwise false + /// Thrown when appId is null or empty, when portName is null or empty + /// + /// RemotePort remotePort = new RemotePort("org.tizen.example.messageport", "SenderPort", false); + /// + public RemotePort(String appId, string portName, bool trusted) + { + if (String.IsNullOrEmpty(appId) || String.IsNullOrEmpty(portName)) + { + MessagePortErrorFactory.ThrowException((int)MessagePortError.InvalidParameter, "Remote Port", "AppId or PortName"); + } + + _appId = appId; + _portName = portName; + _trusted = trusted; + + _registeredCallBack = (string remoteAppId, string remotePortName, bool remoteTrusted, IntPtr userData) => + { + RemotePortStateChangedEventArgs args = new RemotePortStateChangedEventArgs() + { + Status = State.Registered + }; + + _remotePortRegistered?.Invoke(this, args); + }; + + _unregisteredCallBack = (string remoteAppId, string remotePortName, bool remoteTrusted, IntPtr userData) => + { + RemotePortStateChangedEventArgs args = new RemotePortStateChangedEventArgs() + { + Status = State.Unregistered + }; + + _remotePortUnregistered?.Invoke(this, args); + }; + } + + /// + /// Destructor of the RemotePort class. + /// + /// 4 + ~RemotePort() + { + Dispose(false); + } + + /// + /// The AppId of the remote port + /// + /// 4 + /// Return appid of RemotePort + public string AppId + { + get + { + return _appId; + } + } + + /// + /// The name of the remote message port + /// + /// 4 + /// Return name of RemotePort + public string PortName + { + get + { + return _portName; + } + } + + /// + /// If true the remote port is a trusted port, otherwise if false it is not + /// + /// 4 + /// Return true if RemotePort is trusted + public bool Trusted + { + get + { + return _trusted; + } + } + + /// + /// Check if the remote message port is running. + /// + /// 4 + /// Thrown when out of memory, when there is an I/O error + /// + /// Remote remotePort = new RemotePort("org.tizen.example", "SenderPort", true); + /// bool isRunning = remotePort.isRunning(); + /// + /// Return true if Remote Port is running + public bool IsRunning() + { + int ret; + + ret = _trusted ? + Interop.MessagePort.CheckTrustedRemotePort(_appId, _portName, out _isRunning) : + Interop.MessagePort.CheckRemotePort(_appId, _portName, out _isRunning); + + if (ret != (int)MessagePortError.None) + { + MessagePortErrorFactory.ThrowException(ret); + } + + return _isRunning; + } + + /// + /// Called when the remote port is registered or unregistered. + /// + /// 4 + /// Thrown when out of memory, when there is an I/O error + /// + /// Remote remotePort = new RemotePort("org.tizen.example", "SenderPort", true); + /// remotePort.RemotePortStateChanged += RemotePortStateChangedCallback; + /// static void RemotePortStateChangedCallback(object sender, MessageReceivedEventArgs e) + /// { + /// switch (e.Status) + /// { + /// case State.Registered : + /// Console.WriteLine("Remote Port Registered "); + /// break; + /// case State.Unregistered : + /// Console.WriteLine("Remote Port Unregistered "); + /// break; + /// default : + /// break; + /// } + /// } + /// + public event EventHandler RemotePortStateChanged + { + add + { + if (_remotePortRegistered == null) + { + int ret = AddRegistrationCallback(); + + if (ret != (int)MessagePortError.None) + { + MessagePortErrorFactory.ThrowException(ret); + } + } + + _remotePortRegistered += value; + _remotePortUnregistered += value; + } + + remove + { + if (_remotePortRegistered?.GetInvocationList()?.Length > 0) + { + _remotePortRegistered -= value; + _remotePortUnregistered -= value; + + if (_remotePortRegistered == null) + { + RemoveRegistrationCallback(); + } + } + } + } + + private int AddRegistrationCallback() + { + if (_watcherIdForRegistered != -1) + { + Interop.MessagePort.RemoveRegistrationCallback(_watcherIdForRegistered); + _watcherIdForRegistered = -1; + } + + int ret = Interop.MessagePort.AddRegisteredCallback(_appId, _portName, _trusted, _registeredCallBack, IntPtr.Zero, out _watcherIdForRegistered); + + if (ret != (int)MessagePortError.None) + { + return ret; + } + + if (_watcherIdForUnRegistered != -1) + { + Interop.MessagePort.RemoveRegistrationCallback(_watcherIdForUnRegistered); + _watcherIdForUnRegistered = -1; + } + + ret = Interop.MessagePort.AddUnregisteredCallback(_appId, _portName, _trusted, _unregisteredCallBack, IntPtr.Zero, out _watcherIdForUnRegistered); + + if (ret != (int)MessagePortError.None) + { + return ret; + } + + return ret; + } + + private void RemoveRegistrationCallback() + { + if (_watcherIdForRegistered != -1 && _watcherIdForUnRegistered != -1) + { + int ret = Interop.MessagePort.RemoveRegistrationCallback(_watcherIdForRegistered); + + if (ret != (int)MessagePortError.None) + { + MessagePortErrorFactory.ThrowException(ret); + } + + _watcherIdForRegistered = -1; + + ret = Interop.MessagePort.RemoveRegistrationCallback(_watcherIdForUnRegistered); + + if (ret != (int)MessagePortError.None) + { + MessagePortErrorFactory.ThrowException(ret); + } + + _watcherIdForUnRegistered = -1; + } + } + + /// + /// Releases the unmanaged resources used by the RemotePort class specifying whether to perform a normal dispose operation. + /// + /// true for a normal dispose operation; false to finalize the handle. + protected virtual void Dispose(bool disposing) + { + if (_disposed) + { + return; + } + + RemoveRegistrationCallback(); + + _disposed = true; + } + + /// + /// Releases all resources used by the RemotePort class. + /// + /// 4 + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + } + + /// + /// Enumeration for Remote Message Port state type + /// + /// 4 + public enum State : Byte + { + /// + /// Value representing Remote Port state is unregistered + /// + Unregistered = 0, + /// + /// Value representing Remote Port state is registered + /// + Registered = 1 + } +} diff --git a/src/Tizen.Applications.MessagePort/Tizen.Applications.Messages/RemotePortStateChangedEventArgs.cs b/src/Tizen.Applications.MessagePort/Tizen.Applications.Messages/RemotePortStateChangedEventArgs.cs new file mode 100755 index 0000000..b679eb8 --- /dev/null +++ b/src/Tizen.Applications.MessagePort/Tizen.Applications.Messages/RemotePortStateChangedEventArgs.cs @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License.\ + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +namespace Tizen.Applications.Messages +{ + using System; + + /// + /// An extended EventArgs class which contains state of remote message port + /// + /// 4 + public class RemotePortStateChangedEventArgs : EventArgs + { + /// + /// The State of remote port + /// + /// 4 + public State Status { get; internal set; } + } +} -- 2.7.4