sync with tizen_2.0
[platform/framework/native/appfw.git] / src / io / FIoClientChannel.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        FIoClientChannel.cpp
20  * @brief       This is the implementation file for the ClientChannel class.
21  *
22  */
23
24 #include <cstdlib>
25 #include <new>
26 #include <pthread.h>
27
28 #include <FBaseSysLog.h>
29 #include <FIoClientChannel.h>
30 #include <FAppPkg_PackageManagerImpl.h>
31 #include "FIo_ClientChannelImpl.h"
32
33
34 using namespace Tizen::App;
35 using namespace Tizen::App::Package;
36 using namespace Tizen::Base;
37 using namespace Tizen::Base::Collection;
38
39 namespace Tizen { namespace Io
40 {
41
42 ClientChannel* ClientChannel::__pClientChannelInstance = null;
43
44 ClientChannel::ClientChannel(void)
45         : __pClientChannelImpl(null)
46 {
47 }
48
49 ClientChannel::~ClientChannel(void)
50 {
51         delete __pClientChannelImpl;
52 }
53
54 void
55 ClientChannel::SetChannelResponseEventListener(IChannelResponseEventListener* pResponseListener)
56 {
57         SysAssertf(__pClientChannelImpl != null, "Not yet constructed. Construct() should be called before use.\n");
58
59         __pClientChannelImpl->SetChannelResponseEventListener(pResponseListener);
60 }
61
62 result
63 ClientChannel::SendRequest(const String& serverChannelId, const IList* pArgs, RequestId& reqId)
64 {
65         SysAssertf(__pClientChannelImpl != null, "Not yet constructed. Construct() should be called before use.\n");
66
67         SysTryReturnResult(NID_IO, !serverChannelId.IsEmpty(), E_OBJ_NOT_FOUND, "The server channel is not found.");
68
69         return __pClientChannelImpl->SendRequest(serverChannelId, pArgs, reqId);
70 }
71
72 ClientChannel*
73 ClientChannel::GetInstance(void)
74 {
75         static pthread_once_t onceBlock = PTHREAD_ONCE_INIT;
76
77         if (__pClientChannelInstance == null)
78         {
79                 ClearLastResult();
80
81                 pthread_once(&onceBlock, InitSingleton);
82                 result r = GetLastResult();
83                 if (IsFailed(r))
84                 {
85                         onceBlock = PTHREAD_ONCE_INIT;
86                         SysPropagate(NID_IO, r);
87                 }
88         }
89
90         return __pClientChannelInstance;
91 }
92
93 ClientChannel*
94 ClientChannel::GetInstance(const String& channelName)
95 {
96         ClientChannel* pClientChannel = _ClientChannelImpl::GetClientChannel(channelName);
97         SysTryReturn(NID_IO, pClientChannel != null, null, E_SYSTEM, "[E_SYSTEM] Failed to create a client channel.");
98
99         return pClientChannel;
100 }
101
102 void
103 ClientChannel::InitSingleton(void)
104 {
105         ClientChannel* pInst = new (std::nothrow) ClientChannel();
106         SysTryReturnVoidResult(NID_IO, pInst, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
107
108         pInst->__pClientChannelImpl = _ClientChannelImpl::GetInstance(pInst);
109         SysTryCatch(NID_IO, pInst->__pClientChannelImpl != null, , E_SYSTEM,
110                         "[E_SYSTEM] Failed to initialize the client channel.");
111
112         __pClientChannelInstance = pInst;
113         std::atexit(DestroySingleton);
114         return;
115
116 CATCH:
117         delete pInst;
118 }
119
120 void
121 ClientChannel::DestroySingleton(void)
122 {
123         delete __pClientChannelInstance;
124 }
125
126 } } // Tizen::Io