2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @file FIo_Channel.cpp
20 * @brief This is the implementation file for the _Channel class.
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"
35 using namespace Tizen::Base;
36 using namespace Tizen::Base::Collection;
38 namespace Tizen { namespace Io
41 _Channel::_Channel(void)
42 : __pIChannelService(null)
43 , __pResponseEventListener(null)
44 , __pRequestEventListener(null)
48 _Channel::~_Channel(void)
53 _Channel::Construct(const String& channelId, _IChannelService& channelService)
55 SysAssertf(__pIChannelService == null, "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
58 __channelId = channelId;
59 __pIChannelService = &channelService;
61 SysLog(NID_IO, "Channel created : [%ls]", channelId.GetPointer());
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());
71 _Channel::SendRequest(const String& channelId, const Tizen::Base::Collection::ArrayList& args, RequestId& reqId)
73 SysAssertf(__pIChannelService != null, "Channel service has not been initialized.\n");
75 //SysLog(NID_IO, "Sent a request: [%ls] ----> [%ls]", __channelId.GetPointer(), channelId.GetPointer());
77 reqId = GetRequestId();
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));
86 _Channel::SendNullRequest(const String& channelId, RequestId& reqId)
88 SysAssertf(__pIChannelService != null, "Channel service has not been initialized.\n");
90 //SysLog(NID_IO, "Sent a null request: [%ls] ----> [%ls]", __channelId.GetPointer(), channelId.GetPointer());
92 reqId = GetRequestId();
94 result r = __pIChannelService->SendNullRequest(__channelId, channelId, reqId);
95 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to send a request.", GetErrorMessage(r));
101 _Channel::SendResponse(const String& channelId, const Tizen::Base::Collection::ArrayList& args, RequestId reqId)
103 SysAssertf(__pIChannelService != null, "Channel service has not been initialized.\n");
105 //SysLog(NID_IO, "Sent a response: [%ls] ----> [%ls]", __channelId.GetPointer(), channelId.GetPointer());
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));
114 _Channel::SendNullResponse(const String& channelId, RequestId reqId)
116 SysAssertf(__pIChannelService != null, "Channel service has not been initialized.\n");
118 //SysLog(NID_IO, "Sent a null response: [%ls] ----> [%ls]", __channelId.GetPointer(), channelId.GetPointer());
120 result r = __pIChannelService->SendNullResponse(__channelId, channelId, reqId);
121 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to send a response.", GetErrorMessage(r));
127 _Channel::SetChannelRequestEventListener(const _IChannelRequestEventListener& listener)
129 __pRequestEventListener = const_cast <_IChannelRequestEventListener*>(&listener);
133 _Channel::SetChannelResponseEventListener(const _IChannelResponseEventListener& listener)
135 __pResponseEventListener = const_cast <_IChannelResponseEventListener*>(&listener);
140 _Channel::OnChannelRequestReceivedN(const String& src, const String& dest, int requestId, Collection::ArrayList* pArgs)
142 SysLog(NID_IO, "Request received: [%ls] ----> [%ls]", src.GetPointer(), dest.GetPointer());
144 if (__pRequestEventListener)
146 __pRequestEventListener->OnChannelRequestReceivedN(requestId, src, pArgs);
156 _Channel::OnChannelResponseReceivedN(const String& src, const String& dest, int requestId, Collection::ArrayList* pArgs)
158 SysLog(NID_IO, "Response received: [%ls] ----> [%ls]", src.GetPointer(), dest.GetPointer());
160 if (__pResponseEventListener)
162 __pResponseEventListener->OnChannelResponseReceivedN(requestId, src, pArgs);
171 _Channel::GetRequestId(void)
173 static int count = 0;