Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Convergence / Tizen.Convergence / InternalChannel.cs
1 /*
2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 using System;
18 using System.Runtime.InteropServices;
19 using System.ComponentModel;
20
21 namespace Tizen.Convergence
22 {
23     /// <summary>
24     /// The Channel class provies a logical session for communicating with AppCommunicationService.
25     /// </summary>
26     [EditorBrowsable(EditorBrowsableState.Never)]
27     public class InternalChannel : IDisposable
28     {
29         internal Interop.Internal.ConvChannelHandle _channelHandle;
30         internal const string ChannelUriKey = "uri";
31         internal const string ChannelIdKey = "channel_id";
32
33         /// <summary>
34         /// The constructor
35         /// </summary>
36         /// <param name="uri">Uri of the server side app</param>
37         /// <param name="id">Unique identifier</param>
38         /// <feature>http://tizen.org/feature/convergence.d2d</feature>
39         /// <exception cref="NotSupportedException">Thrown if the required feature is not supported.</exception>
40         /// <exception cref="ArgumentNullException">Throws when the arguments passed are null</exception>
41         public InternalChannel(string uri, string id)
42         {
43             if (uri == null || id == null)
44             {
45                 throw new ArgumentNullException();
46             }
47
48             int ret = Interop.Internal.ConvChannel.Create(out _channelHandle);
49             if (ret != (int)ConvErrorCode.None)
50             {
51                 Log.Error(ErrorFactory.LogTag, "Failed to create channel");
52                 throw ErrorFactory.GetException(ret);
53             }
54
55             ret = Interop.Internal.ConvChannel.SetString(_channelHandle, ChannelUriKey, uri);
56             if (ret != (int)ConvErrorCode.None)
57             {
58                 Log.Error(ErrorFactory.LogTag, "Failed to create channel");
59                 throw ErrorFactory.GetException(ret);
60             }
61
62             ret = Interop.Internal.ConvChannel.SetString(_channelHandle, ChannelIdKey, id);
63             if (ret != (int)ConvErrorCode.None)
64             {
65                 Log.Error(ErrorFactory.LogTag, "Failed to create channel");
66                 throw ErrorFactory.GetException(ret);
67             }
68
69             Uri = uri;
70             Id = id;
71         }
72
73         internal InternalChannel(IntPtr channelHandle)
74         {
75             _channelHandle = new Interop.Internal.ConvChannelHandle(channelHandle, false);
76
77             IntPtr stringPtr = IntPtr.Zero;
78             int ret = Interop.Internal.ConvChannel.GetString(_channelHandle, ChannelUriKey, out stringPtr);
79             if (ret != (int)ConvErrorCode.None)
80             {
81                 Log.Error(ErrorFactory.LogTag, "Failed to create channel");
82                 throw ErrorFactory.GetException(ret);
83             }
84             Uri = Marshal.PtrToStringAnsi(stringPtr);
85             Interop.Libc.Free(stringPtr);
86
87             ret = Interop.Internal.ConvChannel.GetString(_channelHandle, ChannelIdKey, out stringPtr);
88             if (ret != (int)ConvErrorCode.None)
89             {
90                 Log.Error(ErrorFactory.LogTag, "Failed to create channel");
91                 throw ErrorFactory.GetException(ret);
92             }
93
94             Id = Marshal.PtrToStringAnsi(stringPtr);
95             Interop.Libc.Free(stringPtr);
96         }
97
98         /// <summary>
99         /// Uri of the server side app
100         /// </summary>
101         public string Uri { get; }
102
103         /// <summary>
104         /// Unique identifier
105         /// </summary>
106         public string Id { get; }
107
108         /// <summary>
109         /// The dispose method
110         /// </summary>
111         public void Dispose()
112         {
113             _channelHandle.Dispose();
114         }
115     }
116 }