Merge branch 'tizen_2.2' into tizen
[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
34 using namespace Tizen::Base;
35 using namespace Tizen::Base::Collection;
36 using namespace Tizen::Base::Runtime;
37 using namespace Tizen::Io;
38
39
40 namespace Tizen { namespace Io
41 {
42
43 _ChannelService::_ChannelService(void)
44         : __pIChannelServiceStub(null)
45 {
46
47 }
48
49 _ChannelService::~_ChannelService(void)
50 {
51
52 }
53
54 result
55 _ChannelService::Construct(_IChannelServiceStub& stub)
56 {
57         static _StringHashProvider hashProvider;
58         static _StringComparer stringComparer;
59
60         __channels.Construct(0, 0, hashProvider, stringComparer);
61
62         __pIChannelServiceStub = &stub;
63
64         __pIChannelServiceStub->SetChannelService(*this);
65
66         return E_SUCCESS;
67 }
68
69 result
70 _ChannelService::RegisterChannel(const String& channelId, const _IChannelServiceEventListener& listener)
71 {
72         return E_SUCCESS;
73 }
74
75 result
76 _ChannelService::RegisterChannel(const String& channelId, int clientId)
77 {
78         result r = E_SUCCESS;
79         _ChannelInfo* pChannelInfo = null;
80
81         SysLog(NID_IO, "Register a channel : %ls", channelId.GetPointer());
82
83         r = __channels.GetValue(channelId, pChannelInfo);
84         if (pChannelInfo != null)
85         {
86                 SysTryReturnResult(NID_IO, pChannelInfo->clientId != clientId, E_SYSTEM,  "Channel has already been registered.");
87
88                 SysLog(NID_IO, "Remove garbage values : %ls", channelId.GetPointer());
89                 __channels.Remove(channelId);
90         }
91
92         pChannelInfo = new (std::nothrow) _ChannelInfo;
93         SysTryReturnResult(NID_IO, pChannelInfo != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
94
95         pChannelInfo->channelId = channelId;
96         pChannelInfo->clientId = clientId;
97         pChannelInfo->pGIOChannel = null;
98
99         __channels.Add(channelId, pChannelInfo);
100
101         return E_SUCCESS;
102 }
103
104 result
105 _ChannelService::UnregisterChannel(const String& channelId)
106 {
107         return E_SUCCESS;
108 }
109
110 result
111 _ChannelService::UnregisterChannel(int clientId)
112 {
113         SysLog(NID_IO, "Unregister - client =  %d", clientId);
114
115         result r = E_OBJ_NOT_FOUND;
116
117         String key;
118         _ChannelInfo* pValue = null;
119         IListT<String>* pKeys = __channels.GetKeysN();
120         SysTryReturnResult(NID_IO, pKeys != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
121
122         int count = __channels.GetCount();
123
124         for (int i = 0; i < count; i++)
125         {
126                 pKeys->GetAt(i, key);
127                 __channels.GetValue(key, pValue);
128                 if (pValue != null && pValue->clientId == clientId)
129                 {
130                         SysLog(NID_IO, "Unregister - Channel = %ls", key.GetPointer());
131                         __channels.Remove(key);
132                         delete pValue;
133
134                         r = E_SUCCESS;
135                 }
136         }
137
138         delete pKeys;
139
140         return r;
141 }
142
143 result
144 _ChannelService::SendRequest(const String& src,
145                                                          const String& dest,
146                                                          const ArrayList& args,
147                                                          int requestId)
148 {
149         SysLog(NID_IO, "[%ls] ---> [%ls], Request = %d", src.GetPointer(), dest.GetPointer(), requestId);
150
151         _ChannelInfo* pChannelInfo = null;
152
153         __channels.GetValue(dest, pChannelInfo);
154         SysTryReturnResult(NID_IO, pChannelInfo != null, E_OBJ_NOT_FOUND,
155                                 "Destination channel is not found.");
156
157         result r = __pIChannelServiceStub->SendRequest(pChannelInfo->clientId, src, dest, args, requestId);
158         SysTryReturnResult(NID_IO, r == E_SUCCESS, E_SYSTEM, "Failed to send the request data.");
159
160         return E_SUCCESS;
161 }
162
163 result
164 _ChannelService::SendNullRequest(const String& src,
165                                                          const String& dest,
166                                                          int requestId)
167 {
168         SysLog(NID_IO, "[%ls] ---> [%ls], Request = %d", src.GetPointer(), dest.GetPointer(), requestId);
169
170         _ChannelInfo* pChannelInfo = null;
171
172         __channels.GetValue(dest, pChannelInfo);
173         SysTryReturnResult(NID_IO, pChannelInfo != null, E_OBJ_NOT_FOUND,
174                                 "Destination channel is not found.");
175
176         __pIChannelServiceStub->SendNullRequest(pChannelInfo->clientId, src, dest, requestId);
177
178         return E_SUCCESS;
179 }
180
181 result
182 _ChannelService::SendResponse(const String& src,
183                                                           const String& dest,
184                                                           const ArrayList& args,
185                                                           int requestId)
186 {
187         SysLog(NID_IO, "[%ls] ---> [%ls], Request = %d", src.GetPointer(), dest.GetPointer(), requestId);
188
189         _ChannelInfo* pChannelInfo = null;
190
191         __channels.GetValue(dest, pChannelInfo);
192         SysTryReturnResult(NID_IO, pChannelInfo != null, E_OBJ_NOT_FOUND,
193                                 "Destination channel not found.");
194
195         result r = __pIChannelServiceStub->SendResponse(pChannelInfo->clientId, src, dest, args, requestId);
196         SysTryReturnResult(NID_IO, r == E_SUCCESS, E_SYSTEM, "Failed to send the response data");
197
198         return E_SUCCESS;
199 }
200
201 result
202 _ChannelService::SendNullResponse(const String& src,
203                                                           const String& dest,
204                                                           int requestId)
205 {
206         SysLog(NID_IO, "[%ls] ---> [%ls], Request = %d", src.GetPointer(), dest.GetPointer(), requestId);
207
208         _ChannelInfo* pChannelInfo = null;
209
210         __channels.GetValue(dest, pChannelInfo);
211         SysTryReturnResult(NID_IO, pChannelInfo != null, E_OBJ_NOT_FOUND,
212                                 "Destination channel not found.");
213
214         __pIChannelServiceStub->SendNullResponse(pChannelInfo->clientId, src, dest, requestId);
215
216         return E_SUCCESS;
217 }
218
219 bool
220 _ChannelService::IsChannelRegistered(const String& channelId)
221 {
222         bool out = false;
223
224         __channels.ContainsKey(channelId, out);
225
226         return out;
227 }
228
229 }}