2 * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.\
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 namespace Tizen.Applications.Messages
22 /// The RemotePort Class provides functions to get if the remote port is running and to get whether the remote port is registered or unregistered.
24 /// <since_tizen> 4 </since_tizen>
25 public class RemotePort : IDisposable
27 private int _watcherIdForRegistered = -1;
28 private int _watcherIdForUnRegistered = -1;
29 private bool _disposed = false;
31 private Interop.MessagePort.message_port_registration_event_cb _registeredCallBack;
32 private Interop.MessagePort.message_port_registration_event_cb _unregisteredCallBack;
34 private EventHandler<RemotePortStateChangedEventArgs> _remotePortRegistered;
35 private EventHandler<RemotePortStateChangedEventArgs> _remotePortUnregistered;
37 private readonly string _portName = null;
38 private readonly string _appId = null;
39 private readonly bool _trusted = false;
41 private bool _isRunning = false;
44 /// Constructor of the RemotePort class.
46 /// <since_tizen> 4 </since_tizen>
47 /// <param name="appId">The Id of the remote application</param>
48 /// <param name="portName">The name of the remote message port</param>
49 /// <param name="trusted">If true is the trusted message port of application, otherwise false</param>
50 /// <exception cref="System.ArgumentException">Thrown when appId is null or empty, when portName is null or empty</exception>
52 /// RemotePort remotePort = new RemotePort("org.tizen.example.messageport", "SenderPort", false);
54 public RemotePort(String appId, string portName, bool trusted)
56 if (String.IsNullOrEmpty(appId) || String.IsNullOrEmpty(portName))
58 MessagePortErrorFactory.ThrowException((int)MessagePortError.InvalidParameter, "Remote Port", "AppId or PortName");
65 _registeredCallBack = (string remoteAppId, string remotePortName, bool remoteTrusted, IntPtr userData) =>
67 RemotePortStateChangedEventArgs args = new RemotePortStateChangedEventArgs()
69 Status = State.Registered
72 _remotePortRegistered?.Invoke(this, args);
75 _unregisteredCallBack = (string remoteAppId, string remotePortName, bool remoteTrusted, IntPtr userData) =>
77 RemotePortStateChangedEventArgs args = new RemotePortStateChangedEventArgs()
79 Status = State.Unregistered
82 _remotePortUnregistered?.Invoke(this, args);
87 /// Destructor of the RemotePort class.
89 /// <since_tizen> 4 </since_tizen>
96 /// The AppId of the remote port
98 /// <since_tizen> 4 </since_tizen>
99 /// <returns> Return appid of RemotePort </returns>
109 /// The name of the remote message port
111 /// <since_tizen> 4 </since_tizen>
112 /// <returns> Return name of RemotePort </returns>
113 public string PortName
122 /// If true the remote port is a trusted port, otherwise if false it is not
124 /// <since_tizen> 4 </since_tizen>
125 /// <returns> Return true if RemotePort is trusted </returns>
135 /// Check if the remote message port is running.
137 /// <since_tizen> 4 </since_tizen>
138 /// <exception cref="System.InvalidOperationException">Thrown when there is an I/O error</exception>
139 /// <exception cref="System.OutOfMemoryException">Thrown when out of memory.</exception>
141 /// Remote remotePort = new RemotePort("org.tizen.example", "SenderPort", true);
142 /// bool isRunning = remotePort.isRunning();
144 /// <returns> Return true if Remote Port is running </returns>
145 public bool IsRunning()
150 Interop.MessagePort.CheckTrustedRemotePort(_appId, _portName, out _isRunning) :
151 Interop.MessagePort.CheckRemotePort(_appId, _portName, out _isRunning);
153 if (ret != (int)MessagePortError.None)
155 MessagePortErrorFactory.ThrowException(ret);
162 /// Called when the remote port is registered or unregistered.
164 /// <since_tizen> 4 </since_tizen>
165 /// <exception cref="System.InvalidOperationException">Thrown when there is an I/O error</exception>
166 /// <exception cref="System.OutOfMemoryException">Thrown when out of memory.</exception>
168 /// Remote remotePort = new RemotePort("org.tizen.example", "SenderPort", true);
169 /// remotePort.RemotePortStateChanged += RemotePortStateChangedCallback;
170 /// static void RemotePortStateChangedCallback(object sender, MessageReceivedEventArgs e)
172 /// switch (e.Status)
174 /// case State.Registered :
175 /// Console.WriteLine("Remote Port Registered ");
177 /// case State.Unregistered :
178 /// Console.WriteLine("Remote Port Unregistered ");
185 public event EventHandler<RemotePortStateChangedEventArgs> RemotePortStateChanged
189 if (_remotePortRegistered == null)
191 int ret = AddRegistrationCallback();
193 if (ret != (int)MessagePortError.None)
195 MessagePortErrorFactory.ThrowException(ret);
199 _remotePortRegistered += value;
200 _remotePortUnregistered += value;
205 if (_remotePortRegistered?.GetInvocationList()?.Length > 0)
207 _remotePortRegistered -= value;
208 _remotePortUnregistered -= value;
210 if (_remotePortRegistered == null)
212 RemoveRegistrationCallback();
218 private int AddRegistrationCallback()
220 if (_watcherIdForRegistered != -1)
222 Interop.MessagePort.RemoveRegistrationCallback(_watcherIdForRegistered);
223 _watcherIdForRegistered = -1;
226 int ret = Interop.MessagePort.AddRegisteredCallback(_appId, _portName, _trusted, _registeredCallBack, IntPtr.Zero, out _watcherIdForRegistered);
228 if (ret != (int)MessagePortError.None)
233 if (_watcherIdForUnRegistered != -1)
235 Interop.MessagePort.RemoveRegistrationCallback(_watcherIdForUnRegistered);
236 _watcherIdForUnRegistered = -1;
239 ret = Interop.MessagePort.AddUnregisteredCallback(_appId, _portName, _trusted, _unregisteredCallBack, IntPtr.Zero, out _watcherIdForUnRegistered);
241 if (ret != (int)MessagePortError.None)
249 private void RemoveRegistrationCallback()
251 if (_watcherIdForRegistered != -1 && _watcherIdForUnRegistered != -1)
253 int ret = Interop.MessagePort.RemoveRegistrationCallback(_watcherIdForRegistered);
255 if (ret != (int)MessagePortError.None)
257 MessagePortErrorFactory.ThrowException(ret);
260 _watcherIdForRegistered = -1;
262 ret = Interop.MessagePort.RemoveRegistrationCallback(_watcherIdForUnRegistered);
264 if (ret != (int)MessagePortError.None)
266 MessagePortErrorFactory.ThrowException(ret);
269 _watcherIdForUnRegistered = -1;
274 /// Releases the unmanaged resources used by the RemotePort class specifying whether to perform a normal dispose operation.
276 /// <param name="disposing">true for a normal dispose operation; false to finalize the handle.</param>
277 protected virtual void Dispose(bool disposing)
284 RemoveRegistrationCallback();
290 /// Releases all resources used by the RemotePort class.
292 /// <since_tizen> 4 </since_tizen>
293 public void Dispose()
296 GC.SuppressFinalize(this);
301 /// Enumeration for Remote Message Port state type
303 /// <since_tizen> 4 </since_tizen>
304 public enum State : Byte
307 /// Value representing Remote Port state is unregistered
311 /// Value representing Remote Port state is registered