sync with tizen_2.0
[platform/framework/native/appfw.git] / src / io / FIo_Channel.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_Channel.cpp
20  * @brief       This is the implementation file for the _Channel class.
21  *
22  */
23
24 #include <new>
25
26 #include <FBaseString.h>
27 #include <FBaseColArrayList.h>
28 #include <FBaseSysLog.h>
29 #include "FIo_Channel.h"
30 #include "FIo_IChannelService.h"
31 #include "FIo_IChannelResponseEventListener.h"
32 #include "FIo_IChannelRequestEventListener.h"
33 #include "FIo_ChannelServiceManager.h"
34
35 using namespace Tizen::Base;
36 using namespace Tizen::Base::Collection;
37
38 namespace Tizen { namespace Io
39 {
40
41 _Channel::_Channel(void)
42         : __pIChannelService(null)
43         , __pResponseEventListener(null)
44         , __pRequestEventListener(null)
45 {
46 }
47
48 _Channel::~_Channel(void)
49 {
50 }
51
52 result
53 _Channel::Construct(const String& channelId, _IChannelService& channelService)
54 {
55         SysAssertf(__pIChannelService == null, "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
56
57         result r = E_SUCCESS;
58         __channelId = channelId;
59         __pIChannelService = &channelService;
60         
61         SysLog(NID_IO, "Channel created : [%ls]", channelId.GetPointer());
62
63         r = __pIChannelService->RegisterChannel(channelId, *this);
64         SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to register a channel with channelId(%ls)."
65                                           , GetErrorMessage(r), channelId.GetPointer());
66
67         return E_SUCCESS;
68 }
69
70 result
71 _Channel::SendRequest(const String& channelId, const Tizen::Base::Collection::ArrayList& args, RequestId& reqId)
72 {
73         SysAssertf(__pIChannelService != null, "Channel service has not been initialized.\n");
74
75         //SysLog(NID_IO, "Sent a request: [%ls] ----> [%ls]", __channelId.GetPointer(), channelId.GetPointer());
76
77         reqId = GetRequestId();
78
79         result r = __pIChannelService->SendRequest(__channelId, channelId, args, reqId);
80         SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to send a request.", GetErrorMessage(r));
81
82         return E_SUCCESS;
83 }
84
85 result
86 _Channel::SendNullRequest(const String& channelId, RequestId& reqId)
87 {
88         SysAssertf(__pIChannelService != null, "Channel service has not been initialized.\n");
89
90         //SysLog(NID_IO, "Sent a null request: [%ls] ----> [%ls]", __channelId.GetPointer(), channelId.GetPointer());
91
92         reqId = GetRequestId();
93
94         result r = __pIChannelService->SendNullRequest(__channelId, channelId, reqId);
95         SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to send a request.", GetErrorMessage(r));
96
97         return E_SUCCESS;
98 }
99
100 result
101 _Channel::SendResponse(const String& channelId, const Tizen::Base::Collection::ArrayList& args, RequestId reqId)
102 {
103         SysAssertf(__pIChannelService != null, "Channel service has not been initialized.\n");
104
105         //SysLog(NID_IO, "Sent a response: [%ls] ----> [%ls]", __channelId.GetPointer(), channelId.GetPointer());
106
107         result r = __pIChannelService->SendResponse(__channelId, channelId, args, reqId);
108         SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to send a response.", GetErrorMessage(r));
109
110         return E_SUCCESS;
111 }
112
113 result
114 _Channel::SendNullResponse(const String& channelId, RequestId reqId)
115 {
116         SysAssertf(__pIChannelService != null, "Channel service has not been initialized.\n");
117
118         //SysLog(NID_IO, "Sent a null response: [%ls] ----> [%ls]", __channelId.GetPointer(), channelId.GetPointer());
119
120         result r = __pIChannelService->SendNullResponse(__channelId, channelId, reqId);
121         SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to send a response.", GetErrorMessage(r));
122
123         return E_SUCCESS;
124 }
125
126 void
127 _Channel::SetChannelRequestEventListener(const _IChannelRequestEventListener& listener)
128 {
129         __pRequestEventListener = const_cast <_IChannelRequestEventListener*>(&listener);
130 }
131
132 void
133 _Channel::SetChannelResponseEventListener(const _IChannelResponseEventListener& listener)
134 {
135         __pResponseEventListener = const_cast <_IChannelResponseEventListener*>(&listener);
136 }
137
138
139 bool
140 _Channel::OnChannelRequestReceivedN(const String& src, const String& dest, int requestId, Collection::ArrayList* pArgs)
141 {
142         SysLog(NID_IO, "Request received: [%ls] ----> [%ls]", src.GetPointer(), dest.GetPointer());
143
144         if (__pRequestEventListener)
145         {
146                 __pRequestEventListener->OnChannelRequestReceivedN(requestId, src, pArgs);
147
148                 return true;
149         }
150
151         return false;
152 }
153
154
155 bool
156 _Channel::OnChannelResponseReceivedN(const String& src, const String& dest,  int requestId, Collection::ArrayList* pArgs)
157 {
158         SysLog(NID_IO, "Response received: [%ls] ----> [%ls]", src.GetPointer(), dest.GetPointer());
159
160         if (__pResponseEventListener)
161         {
162                 __pResponseEventListener->OnChannelResponseReceivedN(requestId, src, pArgs);
163
164                 return true;
165         }
166
167         return false;
168 }
169
170 int
171 _Channel::GetRequestId(void)
172 {
173         static int count = 0;
174
175         return count++;
176 }
177
178 } } // Tizen::Io