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