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