19a72f99713b0197f2db071043e472ecc2b54d30
[platform/framework/native/channel-service.git] / src / FIo_ChannelService.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file        FIo_ChannelService.cpp
20  * @brief       This is the implementation file for the _ChannelService class.
21  *
22  */
23
24 #include <unique_ptr.h>
25
26 #include <FBaseStringComparer.h>
27 #include <FBaseSysLog.h>
28 #include <FBase_StringConverter.h>
29 #include <FBaseRt_EventDispatcher.h>
30 #include "FIo_ChannelService.h"
31 #include "FIo_ChannelMessages.h"
32 #include "FIo_IChannelServiceStub.h"
33 #include "FIo_ChannelCAppStub.h"
34
35 using namespace std;
36 using namespace Tizen::Base;
37 using namespace Tizen::Base::Collection;
38 using namespace Tizen::Base::Runtime;
39 using namespace Tizen::Io;
40
41
42 namespace Tizen { namespace Io
43 {
44
45 _ChannelService::_ChannelService(void)
46         : __pIChannelServiceStub(null)
47 {
48
49 }
50
51 _ChannelService::~_ChannelService(void)
52 {
53
54 }
55
56 result
57 _ChannelService::Construct(_IChannelServiceStub& stub)
58 {
59         static _StringHashProvider hashProvider;
60         static _StringComparer stringComparer;
61         result r = E_SUCCESS;
62
63         __channels.Construct(0, 0, hashProvider, stringComparer);
64
65         __pIChannelServiceStub = &stub;
66
67         __pIChannelServiceStub->SetChannelService(*this);
68
69         // Create a CAppStub
70         unique_ptr<_ChannelCAppStub> pCStub(new (std::nothrow) _ChannelCAppStub());
71         SysTryReturnResult(NID_IO, pCStub != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
72
73         r = pCStub->Construct();
74         SysTryReturnResult(NID_IO, r == E_SUCCESS, E_SYSTEM, "Failed to create a CAppStub.");
75
76         pCStub->SetChannelService(*this);
77
78         pCStub.release();
79
80         return E_SUCCESS;
81 }
82
83 result
84 _ChannelService::RegisterChannel(const String& channelId, const _IChannelServiceEventListener& listener)
85 {
86         return E_SUCCESS;
87 }
88
89 result
90 _ChannelService::RegisterChannel(const String& channelId, int clientId, unsigned int type)
91 {
92         result r = E_SUCCESS;
93         _ChannelInfo* pChannelInfo = null;
94
95         SysLog(NID_IO, "Register a channel : %ls", channelId.GetPointer());
96
97         r = __channels.GetValue(channelId, pChannelInfo);
98         if (pChannelInfo != null)
99         {
100                 SysTryReturnResult(NID_IO, pChannelInfo->clientId != clientId, E_SYSTEM,  "Channel has already been registered.");
101
102                 SysLog(NID_IO, "Remove garbage values : %ls", channelId.GetPointer());
103                 __channels.Remove(channelId);
104         }
105
106         pChannelInfo = new (std::nothrow) _ChannelInfo;
107         SysTryReturnResult(NID_IO, pChannelInfo != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
108
109         pChannelInfo->channelId = channelId;
110         pChannelInfo->clientId = clientId;
111         pChannelInfo->type = type;
112         pChannelInfo->pGIOChannel = null;
113
114         __channels.Add(channelId, pChannelInfo);
115
116         return E_SUCCESS;
117 }
118
119 result
120 _ChannelService::RegisterChannel(const String& channelId, int clientId, void* pGIOChannel)
121 {
122         result r = E_SUCCESS;
123         _ChannelInfo* pChannelInfo = null;
124
125         SysLog(NID_IO, "Register a channel : [%ls]", channelId.GetPointer());
126
127         r = __channels.GetValue(channelId, pChannelInfo);
128         if (pChannelInfo != null)
129         {
130                 SysTryReturnResult(NID_IO, pChannelInfo->clientId != clientId, E_SYSTEM,  "Channel has already been registered.");
131
132                 SysLog(NID_IO, "Remove garbage values : %ls", channelId.GetPointer());
133                 __channels.Remove(channelId);
134         }
135
136         pChannelInfo = new (std::nothrow) _ChannelInfo;
137         SysTryReturnResult(NID_IO, pChannelInfo != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
138
139         pChannelInfo->channelId = channelId;
140         pChannelInfo->clientId = clientId;
141         pChannelInfo->type = CAPP_CHANNEL;
142         pChannelInfo->pGIOChannel = pGIOChannel;
143
144         __channels.Add(channelId, pChannelInfo);
145
146         return E_SUCCESS;
147 }
148
149 result
150 _ChannelService::UnregisterChannel(const String& channelId)
151 {
152         return E_SUCCESS;
153 }
154
155 result
156 _ChannelService::UnregisterChannel(int clientId)
157 {
158         SysLog(NID_IO, "Unregister - client =  %d", clientId);
159
160         result r = E_OBJ_NOT_FOUND;
161
162         String key;
163         _ChannelInfo* pValue = null;
164         IListT<String>* pKeys = __channels.GetKeysN();
165         SysTryReturnResult(NID_IO, pKeys != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
166
167         int count = __channels.GetCount();
168
169         for (int i = 0; i < count; i++)
170         {
171                 pKeys->GetAt(i, key);
172                 __channels.GetValue(key, pValue);
173                 if (pValue != null && pValue->clientId == clientId)
174                 {
175                         SysLog(NID_IO, "Unregister - Channel = %ls", key.GetPointer());
176                         __channels.Remove(key);
177                         delete pValue;
178
179                         r = E_SUCCESS;
180                 }
181         }
182
183         delete pKeys;
184
185         return r;
186 }
187
188 result
189 _ChannelService::SendRequest(const String& src,
190                                                          const String& dest,
191                                                          const ArrayList& args,
192                                                          int requestId)
193 {
194         SysLog(NID_IO, "[%ls] ---> [%ls], Request = %d", src.GetPointer(), dest.GetPointer(), requestId);
195
196         _ChannelInfo* pChannelInfo = null;
197
198         __channels.GetValue(dest, pChannelInfo);
199         SysTryReturnResult(NID_IO, pChannelInfo != null, E_OBJ_NOT_FOUND,
200                                 "Destination channel is not found.");
201
202         result r = __pIChannelServiceStub->SendRequest(pChannelInfo->clientId, src, dest, args, requestId);
203         SysTryReturnResult(NID_IO, r == E_SUCCESS, E_SYSTEM, "Failed to send the request data.");
204
205         return E_SUCCESS;
206 }
207
208 result
209 _ChannelService::SendNullRequest(const String& src,
210                                                          const String& dest,
211                                                          int requestId)
212 {
213         SysLog(NID_IO, "[%ls] ---> [%ls], Request = %d", src.GetPointer(), dest.GetPointer(), requestId);
214
215         _ChannelInfo* pChannelInfo = null;
216
217         __channels.GetValue(dest, pChannelInfo);
218         SysTryReturnResult(NID_IO, pChannelInfo != null, E_OBJ_NOT_FOUND,
219                                 "Destination channel is not found.");
220
221         __pIChannelServiceStub->SendNullRequest(pChannelInfo->clientId, src, dest, requestId);
222
223         return E_SUCCESS;
224 }
225
226 result
227 _ChannelService::SendResponse(const String& src,
228                                                           const String& dest,
229                                                           const ArrayList& args,
230                                                           int requestId)
231 {
232         SysLog(NID_IO, "[%ls] ---> [%ls], Request = %d", src.GetPointer(), dest.GetPointer(), requestId);
233
234         _ChannelInfo* pChannelInfo = null;
235
236         __channels.GetValue(dest, pChannelInfo);
237         SysTryReturnResult(NID_IO, pChannelInfo != null, E_OBJ_NOT_FOUND,
238                                 "Destination channel not found.");
239
240         // Channel for CApp
241         if (pChannelInfo->type == CAPP_CHANNEL)
242         {
243                 bool ret = _ChannelCAppStub::SendResponse(requestId, pChannelInfo->pGIOChannel, args);
244                 SysTryReturnResult(NID_IO, ret, E_SYSTEM, "Failed to send the data to a CApp.");
245         }
246         // Channel for OspApp
247         else
248         {
249                 result r = __pIChannelServiceStub->SendResponse(pChannelInfo->clientId, src, dest, args, requestId);
250                 SysTryReturnResult(NID_IO, r == E_SUCCESS, E_SYSTEM, "Failed to send the response data");
251         }
252
253         return E_SUCCESS;
254 }
255
256 result
257 _ChannelService::SendNullResponse(const String& src,
258                                                           const String& dest,
259                                                           int requestId)
260 {
261         SysLog(NID_IO, "[%ls] ---> [%ls], Request = %d", src.GetPointer(), dest.GetPointer(), requestId);
262
263         _ChannelInfo* pChannelInfo = null;
264
265         __channels.GetValue(dest, pChannelInfo);
266         SysTryReturnResult(NID_IO, pChannelInfo != null, E_OBJ_NOT_FOUND,
267                                 "Destination channel not found.");
268
269         __pIChannelServiceStub->SendNullResponse(pChannelInfo->clientId, src, dest, requestId);
270
271         return E_SUCCESS;
272 }
273
274 bool
275 _ChannelService::IsChannelRegistered(const String& channelId)
276 {
277         bool out = false;
278
279         __channels.ContainsKey(channelId, out);
280
281         return out;
282 }
283
284 }}