sync with tizen_2.0
[platform/framework/native/appfw.git] / src / io / FIo_ChannelManager.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        FIo_ChannelManager.cpp
20  * @brief       This is the implementation file for the _ChannelManager class.
21  *
22  */
23
24 #include <cstdlib>
25 #include <pthread.h>
26 #include <iostream>
27
28 #include <FBaseSysLog.h>
29 #include <FIoClientChannel.h>
30 #include <FIoServerChannel.h>
31 #include "FIo_Channel.h"
32 #include "FIo_ChannelManager.h"
33 #include "FIo_ChannelServiceManager.h"
34 #include "FIo_IChannelService.h"
35
36 using namespace Tizen::Base;
37
38 namespace Tizen { namespace Io
39 {
40
41 _ChannelManager* _ChannelManager::__pChannelManagerInst = null;
42
43 _ChannelManager::_ChannelManager(void)
44         : __pIChannelService(null)
45 {
46
47 }
48
49 _ChannelManager::~_ChannelManager(void)
50 {
51
52 }
53
54 result
55 _ChannelManager::Construct(void)
56 {
57         static _StringHashProvider hashProvider;
58         static _StringComparer stringComparer;
59         __channels.Construct(0, 0, hashProvider, stringComparer);
60         __clientChannels.Construct(0, 0, hashProvider, stringComparer);
61         __serverChannels.Construct(0, 0, hashProvider, stringComparer);
62
63         _ChannelServiceManager* pManager = _ChannelServiceManager::GetInstance();
64         SysTryReturnResult(NID_IO, pManager != null, E_SYSTEM, "Failed to create a channel manager.");
65
66         __pIChannelService = pManager->GetChannelService();
67         SysTryReturnResult(NID_IO, __pIChannelService != null, E_SYSTEM, "Failed to get channel service.");
68
69         return E_SUCCESS;
70 }
71
72 void
73 _ChannelManager::InitSingleton(void)
74 {
75         _ChannelManager* pInst = new (std::nothrow) _ChannelManager();
76         SysTryReturnVoidResult(NID_IO, pInst != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
77
78         result r = pInst->Construct();
79         SysTryCatch(NID_IO, !IsFailed(r), ,E_SYSTEM,
80                         "[E_SYSTEM] Failed to initialize the channel manager.");
81
82         __pChannelManagerInst = pInst;
83         std::atexit(DestroySingleton);
84         return;
85
86 CATCH:
87         delete pInst;
88 }
89
90 void
91 _ChannelManager::DestroySingleton(void)
92 {
93         delete __pChannelManagerInst;
94 }
95
96 _ChannelManager*
97 _ChannelManager::GetInstance(void)
98 {
99         static pthread_once_t onceBlock = PTHREAD_ONCE_INIT;
100
101         if (__pChannelManagerInst == null)
102         {
103                 ClearLastResult();
104
105                 pthread_once(&onceBlock, InitSingleton);
106                 result r = GetLastResult();
107                 if (IsFailed(r))
108                 {
109                         onceBlock = PTHREAD_ONCE_INIT;
110                         SysPropagate(NID_IO, r);
111                 }
112         }
113
114         return __pChannelManagerInst;
115 }
116
117 _Channel*
118 _ChannelManager::GetChannel(const String& channelId)
119 {
120         result r = E_SUCCESS;
121         _Channel* pChannel = null;
122
123         __channels.GetValue(channelId, pChannel);
124         if (pChannel)
125         {
126                 return pChannel;
127         }
128
129         pChannel = new (std::nothrow) _Channel;
130         SysTryReturn(NID_IO, pChannel != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Not enough memory.");
131
132         r = pChannel->Construct(channelId, *__pIChannelService);
133         SysTryCatch(NID_IO, !IsFailed(r), , r, "[%s] Failed to create _Channel instance", GetErrorMessage(r));
134
135         __channels.Add(channelId, pChannel);
136
137         return pChannel;
138
139 CATCH:
140         delete pChannel;
141
142         return null;
143 }
144
145 ClientChannel*
146 _ChannelManager::GetClientChannel(const Tizen::Base::String& channelName)
147 {
148         ClientChannel* pChannel = null;
149
150         __clientChannels.GetValue(channelName, pChannel);
151         if (pChannel)
152         {
153                 return pChannel;
154         }
155
156         return null;
157 }
158
159 ServerChannel*
160 _ChannelManager::GetServerChannel(const Tizen::Base::String& channelName)
161 {
162         ServerChannel* pChannel = null;
163
164         __serverChannels.GetValue(channelName, pChannel);
165         if (pChannel)
166         {
167                 return pChannel;
168         }
169
170         return null;
171 }
172
173 void
174 _ChannelManager::AddClientChannel(const Tizen::Base::String& channelName, ClientChannel* pChannel)
175 {
176         __clientChannels.Add(channelName, pChannel);
177 }
178
179 void
180 _ChannelManager::AddServerChannel(const Tizen::Base::String& channelName, ServerChannel* pChannel)
181 {
182         __serverChannels.Add(channelName, pChannel);
183 }
184
185 }}