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