change license
[platform/core/csapi/tizenfx.git] / src / Tizen.Network.IoTConnectivity / Tizen.Network.IoTConnectivity / Response.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
18 using System;
19
20 namespace Tizen.Network.IoTConnectivity
21 {
22     /// <summary>
23     /// This class represents response from a resource.
24     /// It provides APIs to manage response.
25     /// </summary>
26     public class Response : IDisposable
27     {
28         private bool _disposed = false;
29
30         /// <summary>
31         /// Constructor of Response
32         /// </summary>
33         /// <code>
34         /// Response response = new Response();
35         /// </code>
36         public Response() { }
37
38         /// <summary>
39         /// Destructor of the Response class.
40         /// </summary>
41         ~Response()
42         {
43             Dispose(false);
44         }
45
46         /// <summary>
47         /// Gets or sets the result from/into the reponse
48         /// </summary>
49         public ResponseCode Result { get; set; }
50
51         /// <summary>
52         /// Gets or sets the representation from/into the reponse
53         /// </summary>
54         public Representation Representation { get; set; }
55
56         /// <summary>
57         /// Gets or sets the options from/into the reponse
58         /// </summary>
59         public ResourceOptions Options { get; set; }
60
61         /// <summary>
62         /// Releases any unmanaged resources used by this object.
63         /// </summary>
64         public void Dispose()
65         {
66             Dispose(true);
67             GC.SuppressFinalize(this);
68         }
69
70         internal bool Send(IntPtr requestHandle)
71         {
72             IntPtr responseHandle = IntPtr.Zero;
73             int ret = Interop.IoTConnectivity.Server.Response.Create(requestHandle, out responseHandle);
74             if (ret != (int)IoTConnectivityError.None)
75             {
76                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to send response");
77                 return false;
78             }
79
80             ret = Interop.IoTConnectivity.Server.Response.SetResult(responseHandle, (int)Result);
81             if (ret != (int)IoTConnectivityError.None)
82             {
83                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to send response");
84                 Interop.IoTConnectivity.Server.Response.Destroy(responseHandle);
85                 return false;
86             }
87
88             if (Representation != null)
89             {
90                 ret = Interop.IoTConnectivity.Server.Response.SetRepresentation(responseHandle, Representation._representationHandle);
91                 if (ret != (int)IoTConnectivityError.None)
92                 {
93                     Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to send response");
94                     Interop.IoTConnectivity.Server.Response.Destroy(responseHandle);
95                     return false;
96                 }
97             }
98
99             if (Options != null)
100             {
101                 ret = Interop.IoTConnectivity.Server.Response.SetOptions(responseHandle, Options._resourceOptionsHandle);
102                 if (ret != (int)IoTConnectivityError.None)
103                 {
104                     Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to send response");
105                     Interop.IoTConnectivity.Server.Response.Destroy(responseHandle);
106                     return false;
107                 }
108             }
109
110             ret = Interop.IoTConnectivity.Server.Response.Send(responseHandle);
111             if (ret != (int)IoTConnectivityError.None)
112             {
113                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to send response");
114                 Interop.IoTConnectivity.Server.Response.Destroy(responseHandle);
115                 return false;
116             }
117
118             Interop.IoTConnectivity.Server.Response.Destroy(responseHandle);
119             return true;
120         }
121
122         /// <summary>
123         /// Releases any unmanaged resources used by this object. Can also dispose any other disposable objects.
124         /// </summary>
125         /// <param name="disposing">If true, disposes any disposable objects. If false, does not dispose disposable objects.</param>
126         protected virtual void Dispose(bool disposing)
127         {
128             if (_disposed)
129                 return;
130
131             if (disposing)
132             {
133             }
134
135             _disposed = true;
136         }
137     }
138 }