2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @file FIo_Channel.cpp
19 * @brief This is the implementation file for the _Channel class.
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"
34 using namespace Tizen::Base;
35 using namespace Tizen::Base::Collection;
37 namespace Tizen { namespace Io
40 _Channel::_Channel(void)
41 : __pIChannelService(null)
42 , __pResponseEventListener(null)
43 , __pRequestEventListener(null)
47 _Channel::~_Channel(void)
52 _Channel::Construct(const String& channelId, _IChannelService& channelService)
54 SysAssertf(__pIChannelService == null, "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
57 __channelId = channelId;
58 __pIChannelService = &channelService;
60 SysLog(NID_IO, "Channel created : [%ls]", channelId.GetPointer());
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());
70 _Channel::SendRequest(const String& channelId, const Tizen::Base::Collection::ArrayList& args, RequestId& reqId)
72 SysAssertf(__pIChannelService != null, "Channel service has not been initialized.\n");
74 //SysLog(NID_IO, "Sent a request: [%ls] ----> [%ls]", __channelId.GetPointer(), channelId.GetPointer());
76 reqId = GetRequestId();
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));
85 _Channel::SendNullRequest(const String& channelId, RequestId& reqId)
87 SysAssertf(__pIChannelService != null, "Channel service has not been initialized.\n");
89 //SysLog(NID_IO, "Sent a null request: [%ls] ----> [%ls]", __channelId.GetPointer(), channelId.GetPointer());
91 reqId = GetRequestId();
93 result r = __pIChannelService->SendNullRequest(__channelId, channelId, reqId);
94 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to send a request.", GetErrorMessage(r));
100 _Channel::SendResponse(const String& channelId, const Tizen::Base::Collection::ArrayList& args, RequestId reqId)
102 SysAssertf(__pIChannelService != null, "Channel service has not been initialized.\n");
104 //SysLog(NID_IO, "Sent a response: [%ls] ----> [%ls]", __channelId.GetPointer(), channelId.GetPointer());
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));
113 _Channel::SendNullResponse(const String& channelId, RequestId reqId)
115 SysAssertf(__pIChannelService != null, "Channel service has not been initialized.\n");
117 //SysLog(NID_IO, "Sent a null response: [%ls] ----> [%ls]", __channelId.GetPointer(), channelId.GetPointer());
119 result r = __pIChannelService->SendNullResponse(__channelId, channelId, reqId);
120 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to send a response.", GetErrorMessage(r));
126 _Channel::SetChannelRequestEventListener(const _IChannelRequestEventListener& listener)
128 __pRequestEventListener = const_cast <_IChannelRequestEventListener*>(&listener);
132 _Channel::SetChannelResponseEventListener(const _IChannelResponseEventListener& listener)
134 __pResponseEventListener = const_cast <_IChannelResponseEventListener*>(&listener);
139 _Channel::OnChannelRequestReceivedN(const String& src, const String& dest, int requestId, Collection::ArrayList* pArgs)
141 SysLog(NID_IO, "Request received: [%ls] ----> [%ls]", src.GetPointer(), dest.GetPointer());
143 if (__pRequestEventListener)
145 __pRequestEventListener->OnChannelRequestReceivedN(requestId, src, pArgs);
155 _Channel::OnChannelResponseReceivedN(const String& src, const String& dest, int requestId, Collection::ArrayList* pArgs)
157 SysLog(NID_IO, "Response received: [%ls] ----> [%ls]", src.GetPointer(), dest.GetPointer());
159 if (__pResponseEventListener)
161 __pResponseEventListener->OnChannelResponseReceivedN(requestId, src, pArgs);
170 _Channel::GetRequestId(void)
172 static int count = 0;