2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @file FIo_ChannelManager.cpp
19 * @brief This is the implementation file for the _ChannelManager class.
27 #include <FBaseSysLog.h>
28 #include <FIoClientChannel.h>
29 #include <FIoServerChannel.h>
30 #include "FIo_Channel.h"
31 #include "FIo_ChannelManager.h"
32 #include "FIo_ChannelServiceManager.h"
33 #include "FIo_IChannelService.h"
35 using namespace Tizen::Base;
37 namespace Tizen { namespace Io
40 _ChannelManager* _ChannelManager::__pChannelManagerInst = null;
42 _ChannelManager::_ChannelManager(void)
43 : __pIChannelService(null)
48 _ChannelManager::~_ChannelManager(void)
54 _ChannelManager::Construct(void)
56 static _StringHashProvider hashProvider;
57 static _StringComparer stringComparer;
58 __channels.Construct(0, 0, hashProvider, stringComparer);
59 __clientChannels.Construct(0, 0, hashProvider, stringComparer);
60 __serverChannels.Construct(0, 0, hashProvider, stringComparer);
62 _ChannelServiceManager* pManager = _ChannelServiceManager::GetInstance();
63 SysTryReturnResult(NID_IO, pManager != null, E_SYSTEM, "Failed to create a channel manager.");
65 __pIChannelService = pManager->GetChannelService();
66 SysTryReturnResult(NID_IO, __pIChannelService != null, E_SYSTEM, "Failed to get channel service.");
72 _ChannelManager::InitSingleton(void)
74 _ChannelManager* pInst = new (std::nothrow) _ChannelManager();
75 SysTryReturnVoidResult(NID_IO, pInst != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
77 result r = pInst->Construct();
78 SysTryCatch(NID_IO, !IsFailed(r), ,E_SYSTEM,
79 "[E_SYSTEM] Failed to initialize the channel manager.");
81 __pChannelManagerInst = pInst;
82 std::atexit(DestroySingleton);
90 _ChannelManager::DestroySingleton(void)
92 delete __pChannelManagerInst;
96 _ChannelManager::GetInstance(void)
98 static pthread_once_t onceBlock = PTHREAD_ONCE_INIT;
100 if (__pChannelManagerInst == null)
104 pthread_once(&onceBlock, InitSingleton);
105 result r = GetLastResult();
108 onceBlock = PTHREAD_ONCE_INIT;
109 SysPropagate(NID_IO, r);
113 return __pChannelManagerInst;
117 _ChannelManager::GetChannel(const String& channelId)
119 result r = E_SUCCESS;
120 _Channel* pChannel = null;
122 __channels.GetValue(channelId, pChannel);
128 pChannel = new (std::nothrow) _Channel;
129 SysTryReturn(NID_IO, pChannel != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Not enough memory.");
131 r = pChannel->Construct(channelId, *__pIChannelService);
132 SysTryCatch(NID_IO, !IsFailed(r), , r, "[%s] Failed to create _Channel instance", GetErrorMessage(r));
134 __channels.Add(channelId, pChannel);
145 _ChannelManager::GetClientChannel(const Tizen::Base::String& channelName)
147 ClientChannel* pChannel = null;
149 __clientChannels.GetValue(channelName, pChannel);
159 _ChannelManager::GetServerChannel(const Tizen::Base::String& channelName)
161 ServerChannel* pChannel = null;
163 __serverChannels.GetValue(channelName, pChannel);
173 _ChannelManager::AddClientChannel(const Tizen::Base::String& channelName, ClientChannel* pChannel)
175 __clientChannels.Add(channelName, pChannel);
179 _ChannelManager::AddServerChannel(const Tizen::Base::String& channelName, ServerChannel* pChannel)
181 __serverChannels.Add(channelName, pChannel);