Merge branch 'temp' into nkw
[platform/framework/native/appfw.git] / src / io / FIoServerChannel.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        FIoServerChannel.cpp
19  * @brief       This is the implementation file for the ServerChannel class.
20  *
21  */
22  
23 #include <cstdlib>
24 #include <pthread.h>
25
26 #include <FBaseSysLog.h>
27 #include <FIoServerChannel.h>
28 #include <FAppPkg_PackageManagerImpl.h>
29 #include "FIo_ServerChannelImpl.h"
30
31 using namespace Tizen::App;
32 using namespace Tizen::App::Package;
33 using namespace Tizen::Base;
34 using namespace Tizen::Base::Collection;
35
36 namespace Tizen { namespace Io
37 {
38
39 ServerChannel* ServerChannel::__pServerChannelInstance = null;
40
41 ////////////////////////////////////////////////////////////////////////////////
42 /// Lifecycle
43
44 ServerChannel::ServerChannel(void)
45         : __pServerChannelImpl(null)
46 {
47 }
48
49 ServerChannel::~ServerChannel(void)
50 {
51         delete __pServerChannelImpl;
52 }
53
54 void
55 ServerChannel::SetChannelRequestEventListener(IChannelRequestEventListener* pRequestListener)
56 {
57         SysAssertf(__pServerChannelImpl != null, "Not yet constructed. Construct() should be called before use.\n");
58         __pServerChannelImpl->SetChannelRequestEventListener(pRequestListener);
59 }
60
61 result
62 ServerChannel::SendResponse(const String& clientChannelId, RequestId reqId, const IList* pArgs)
63 {
64         SysAssertf(__pServerChannelImpl != null, "Not yet constructed. Construct() should be called before use.\n");
65
66         SysTryReturnResult(NID_IO, !clientChannelId.IsEmpty(), E_OBJ_NOT_FOUND, "The client channel is not found.");
67
68         return __pServerChannelImpl->SendResponse(clientChannelId, pArgs, reqId);
69 }
70
71 ServerChannel*
72 ServerChannel::GetInstance(void)
73 {
74         static pthread_once_t onceBlock = PTHREAD_ONCE_INIT;
75
76         if (__pServerChannelInstance == 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 __pServerChannelInstance;
90 }
91
92 ServerChannel*
93 ServerChannel::GetInstance(const String& channelName)
94 {
95         ServerChannel* pServerChannel = _ServerChannelImpl::GetServerChannel(channelName);
96         SysTryReturn(NID_IO, pServerChannel != null, null, E_SYSTEM, "[E_SYSTEM] Failed to create a server channel.");
97
98         return pServerChannel;
99 }
100
101 void
102 ServerChannel::InitSingleton(void)
103 {
104         ServerChannel* pInst = new (std::nothrow) ServerChannel();
105         SysTryReturnVoidResult(NID_IO, pInst != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
106
107         pInst->__pServerChannelImpl = _ServerChannelImpl::GetInstance(pInst);   
108         SysTryCatch(NID_IO, pInst->__pServerChannelImpl != null, ,E_SYSTEM,
109                         "[E_SYSTEM] Failed to initialize the server channel.");
110
111         __pServerChannelInstance = pInst;
112         std::atexit(DestroySingleton);
113         return;
114
115 CATCH:
116         delete pInst;
117 }
118
119 void
120 ServerChannel::DestroySingleton(void)
121 {
122         delete __pServerChannelInstance;
123 }
124
125 } } // Tizen::Io