2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @file FIo_ChannelManager.cpp
20 * @brief This is the implementation file for the _ChannelManager class.
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"
36 using namespace Tizen::Base;
38 namespace Tizen { namespace Io
41 _ChannelManager* _ChannelManager::__pChannelManagerInst = null;
43 _ChannelManager::_ChannelManager(void)
44 : __pIChannelService(null)
49 _ChannelManager::~_ChannelManager(void)
55 _ChannelManager::Construct(void)
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);
63 _ChannelServiceManager* pManager = _ChannelServiceManager::GetInstance();
64 SysTryReturnResult(NID_IO, pManager != null, E_SYSTEM, "Failed to create a channel manager.");
66 __pIChannelService = pManager->GetChannelService();
67 SysTryReturnResult(NID_IO, __pIChannelService != null, E_SYSTEM, "Failed to get channel service.");
73 _ChannelManager::InitSingleton(void)
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.");
78 result r = pInst->Construct();
79 SysTryCatch(NID_IO, !IsFailed(r), ,E_SYSTEM,
80 "[E_SYSTEM] Failed to initialize the channel manager.");
82 __pChannelManagerInst = pInst;
83 std::atexit(DestroySingleton);
91 _ChannelManager::DestroySingleton(void)
93 delete __pChannelManagerInst;
97 _ChannelManager::GetInstance(void)
99 static pthread_once_t onceBlock = PTHREAD_ONCE_INIT;
101 if (__pChannelManagerInst == null)
105 pthread_once(&onceBlock, InitSingleton);
106 result r = GetLastResult();
109 onceBlock = PTHREAD_ONCE_INIT;
110 SysPropagate(NID_IO, r);
114 return __pChannelManagerInst;
118 _ChannelManager::GetChannel(const String& channelId)
120 result r = E_SUCCESS;
121 _Channel* pChannel = null;
123 __channels.GetValue(channelId, pChannel);
129 pChannel = new (std::nothrow) _Channel;
130 SysTryReturn(NID_IO, pChannel != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Not enough memory.");
132 r = pChannel->Construct(channelId, *__pIChannelService);
133 SysTryCatch(NID_IO, !IsFailed(r), , r, "[%s] Failed to create _Channel instance", GetErrorMessage(r));
135 __channels.Add(channelId, pChannel);
146 _ChannelManager::GetClientChannel(const Tizen::Base::String& channelName)
148 ClientChannel* pChannel = null;
150 __clientChannels.GetValue(channelName, pChannel);
160 _ChannelManager::GetServerChannel(const Tizen::Base::String& channelName)
162 ServerChannel* pChannel = null;
164 __serverChannels.GetValue(channelName, pChannel);
174 _ChannelManager::AddClientChannel(const Tizen::Base::String& channelName, ClientChannel* pChannel)
176 __clientChannels.Add(channelName, pChannel);
180 _ChannelManager::AddServerChannel(const Tizen::Base::String& channelName, ServerChannel* pChannel)
182 __serverChannels.Add(channelName, pChannel);