2 * Copyright (c) 2021 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.
19 namespace Tizen.Applications
22 /// An abstract class to represent cion group.
24 /// <since_tizen> 9 </since_tizen>
25 public abstract class GroupBase : IDisposable
27 private readonly GroupSafeHandle _handle;
29 private Interop.CionGroup.CionGroupPayloadReceivedCb _payloadReceivedCb;
30 private Interop.CionGroup.CionGroupLeftCb _leftCb;
31 private Interop.CionGroup.CionGroupJoinedCb _joinedCb;
34 /// Gets the topic of current cion group.
36 /// <since_tizen> 9 </since_tizen>
37 public string Topic { get; }
40 /// The constructor of GroupBase class.
42 /// <param name="topicName">The topic of group.</param>
43 /// <since_tizen> 9 </since_tizen>
44 public GroupBase(string topicName) : this(topicName, null) { }
47 /// The constructor of GroupBase class.
49 /// <param name="topicName">The topic of group.</param>
50 /// <param name="security">The security configuration.</param>
51 /// <exception cref="OutOfMemoryException">Thrown when there is not enough memory to continue the execution of the method.</exception>
52 /// <since_tizen> 9 </since_tizen>
53 public GroupBase(string topicName, Cion.SecurityInfo security)
57 Cion.SecuritySafeHandle handle = security?._handle;
58 Interop.Cion.ErrorCode ret = Interop.CionGroup.CionGroupCreate(out _handle, topicName, handle?.DangerousGetHandle() ?? IntPtr.Zero);
59 if (ret != Interop.Cion.ErrorCode.None)
61 throw CionErrorFactory.GetException(ret, "Failed to create group.");
64 _payloadReceivedCb = new Interop.CionGroup.CionGroupPayloadReceivedCb(
65 (IntPtr group, IntPtr peerInfo, IntPtr payload, IntPtr userData) =>
67 Payload receivedPayload;
68 Interop.CionPayload.CionPayloadGetType(payload, out Interop.CionPayload.PayloadType type);
71 case Interop.CionPayload.PayloadType.Data:
72 receivedPayload = new DataPayload(new PayloadSafeHandle(payload, false));
74 case Interop.CionPayload.PayloadType.File:
75 receivedPayload = new FilePayload(new PayloadSafeHandle(payload, false));
78 throw new ArgumentException("Invalid payload type received.");
80 OnPayloadReceived(receivedPayload, new PeerInfo(new PeerInfoSafeHandle(peerInfo, false)));
82 ret = Interop.CionGroup.CionGroupAddPayloadReceivedCb(_handle, _payloadReceivedCb, IntPtr.Zero);
83 if (ret != Interop.Cion.ErrorCode.None)
86 throw CionErrorFactory.GetException(ret, "Failed to add payload received callback.");
89 _joinedCb = new Interop.CionGroup.CionGroupJoinedCb(
90 (string name, IntPtr peerInfo, IntPtr userData) =>
92 Interop.Cion.ErrorCode clone_ret = Interop.CionPeerInfo.CionPeerInfoClone(peerInfo, out PeerInfoSafeHandle clone);
93 if (clone_ret != Interop.Cion.ErrorCode.None)
97 OnJoined(new PeerInfo(clone));
99 ret = Interop.CionGroup.CionGroupAddJoinedCb(_handle, _joinedCb, IntPtr.Zero);
100 if (ret != Interop.Cion.ErrorCode.None)
103 throw CionErrorFactory.GetException(ret, "Failed to add joined callback.");
106 _leftCb = new Interop.CionGroup.CionGroupLeftCb(
107 (string name, IntPtr peerInfo, IntPtr userData) =>
109 Interop.Cion.ErrorCode clone_ret = Interop.CionPeerInfo.CionPeerInfoClone(peerInfo, out PeerInfoSafeHandle clone);
110 if (clone_ret != Interop.Cion.ErrorCode.None)
114 OnLeft(new PeerInfo(clone));
116 ret = Interop.CionGroup.CionGroupAddLeftCb(_handle, _leftCb, IntPtr.Zero);
117 if (ret != Interop.Cion.ErrorCode.None)
120 throw CionErrorFactory.GetException(ret, "Failed to add joined callback.");
125 /// Subscribes the topic.
127 /// <privilege>http://tizen.org/privilege/d2d.datasharing</privilege>
128 /// <since_tizen> 9 </since_tizen>
129 public void Subscribe()
131 Interop.Cion.ErrorCode ret = Interop.CionGroup.CionGroupSubscribe(_handle);
132 if (ret != Interop.Cion.ErrorCode.None)
134 throw CionErrorFactory.GetException(ret, "Failed to subscribe.");
139 /// Unsubscribes the topic.
141 /// <since_tizen> 9 </since_tizen>
142 public void Unsubscribe()
144 Interop.Cion.ErrorCode ret = Interop.CionGroup.CionGroupUnsubscribe(_handle);
145 if (ret != Interop.Cion.ErrorCode.None)
147 throw CionErrorFactory.GetException(ret, "Failed to unsubscribe.");
152 /// Publishes payload to current group.
154 /// <param name="payload">The payload to publish.</param>
155 /// <exception cref="ArgumentException">Thrown when the payload is invalid.</exception>
156 /// <since_tizen> 9 </since_tizen>
157 public void Publish(Payload payload)
159 Interop.Cion.ErrorCode ret = Interop.CionGroup.CionGroupPublish(_handle, payload?._handle);
160 if (ret != Interop.Cion.ErrorCode.None)
162 throw CionErrorFactory.GetException(ret, "Failed to publish payload.");
167 /// The callback invoked when payload received.
169 /// <since_tizen> 9 </since_tizen>
170 protected abstract void OnPayloadReceived(Payload payload, PeerInfo peer);
173 /// The callback invoked when another peer joined in the current group.
175 /// <param name="peerInfo">The peer info of joined in the current group.</param>
176 /// <since_tizen> 9 </since_tizen>
177 protected abstract void OnJoined(PeerInfo peerInfo);
180 /// The callback invoked when another peer left from the current group.
182 /// <param name="peerInfo">The peer info of left from the current group.</param>
183 /// <since_tizen> 9 </since_tizen>
184 protected abstract void OnLeft(PeerInfo peerInfo);
186 #region IDisposable Support
187 private bool disposedValue = false;
190 /// Releases any unmanaged resources used by this object. Can also dispose any other disposable objects.
192 /// <param name="disposing">If true, disposes any disposable objects. If false, does not dispose disposable objects.</param>
193 /// <since_tizen> 9 </since_tizen>
194 protected virtual void Dispose(bool disposing)
202 disposedValue = true;
207 /// Releases all resources used by the GroupBase class.
209 /// <since_tizen> 9 </since_tizen>
210 public void Dispose()
213 GC.SuppressFinalize(this);