2 // Open Service Platform
3 // Copyright (c) 2012-2013 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.
18 * @file FNetLocalDhcpServer.cpp
19 * @brief This is the implementation file for the LocalDhcpServer Class.
21 * This header file contains implementation of the LocalDhcpServer Class.
24 #include <FBaseObject.h>
25 #include <FBaseString.h>
26 #include <FBaseResult.h>
27 #include <FBaseColIList.h>
28 #include <FNetNetTypes.h>
29 #include <FNetNetConnection.h>
30 #include <FNetLocalDhcpServer.h>
31 #include <FBaseSysLog.h>
32 #include "FNet_NetTypes.h"
33 #include "FNet_LocalDhcpServerEvent.h"
34 #include "FNet_LocalDhcpServerImpl.h"
36 using namespace Tizen::Base;
37 using namespace Tizen::Base::Collection;
39 namespace Tizen { namespace Net
42 static const int _MAX_ACTIVE_NETWORK_COUNT = 32;
44 static ArrayList* gpLocalDhcpServerList = null;
46 LocalDhcpServer::LocalDhcpServer(void)
47 : __pLocalDhcpServerImpl(null)
51 LocalDhcpServer::~LocalDhcpServer(void)
53 if (__pLocalDhcpServerImpl != null)
55 delete __pLocalDhcpServerImpl;
56 __pLocalDhcpServerImpl = null;
58 //remove from list also
59 if (gpLocalDhcpServerList != null)
61 gpLocalDhcpServerList->Remove(*this);
65 // when count is zero, cleanup list
66 if ((gpLocalDhcpServerList != null) && (gpLocalDhcpServerList->GetCount() == 0))
68 gpLocalDhcpServerList->RemoveAll(true);
69 delete gpLocalDhcpServerList;
70 gpLocalDhcpServerList = null;
75 LocalDhcpServer::GetInstance(const NetConnection& netConnection)
78 LocalDhcpServer* pLocalDhcpServer = null;
82 // create list,if list is not created
83 if (gpLocalDhcpServerList == null)
85 gpLocalDhcpServerList = new (std::nothrow) ArrayList();
86 SysTryReturn(NID_NET, gpLocalDhcpServerList != null, null, E_OUT_OF_MEMORY,
87 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
89 r = gpLocalDhcpServerList->Construct(_MAX_ACTIVE_NETWORK_COUNT);
90 SysTryCatch(NID_NET, r == E_SUCCESS, r = E_OUT_OF_MEMORY , E_OUT_OF_MEMORY,
91 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
94 // check if we already have the server created
95 // if yes, search the list and return that, else create a new and add to the list
96 if (gpLocalDhcpServerList->GetCount() == 0)
98 pLocalDhcpServer = new (std::nothrow) LocalDhcpServer();
99 SysTryCatch(NID_NET, pLocalDhcpServer != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
100 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
102 r = gpLocalDhcpServerList->Add(*pLocalDhcpServer);
103 SysTryCatch(NID_NET, r == E_SUCCESS, r = E_SYSTEM , E_SYSTEM,
104 "[%s] A system error has been occurred. Failed to add local dhcp server to the list.", GetErrorMessage(E_SYSTEM));
106 pLocalDhcpServer->__pLocalDhcpServerImpl = new (std::nothrow) _LocalDhcpServerImpl();
107 SysTryCatch(NID_NET, pLocalDhcpServer->__pLocalDhcpServerImpl != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
108 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
110 r = pLocalDhcpServer->__pLocalDhcpServerImpl->Construct(pLocalDhcpServer, netConnection);
111 SysTryCatch(NID_NET, r == E_SUCCESS, , r, "%s", GetErrorMessage(r));
116 // get LocalDhcpServer based on netConnection
117 pLocalDhcpServer = _LocalDhcpServerImpl::GetLocalDhcpServer(gpLocalDhcpServerList, netConnection);
119 // if not found, create new
120 if (pLocalDhcpServer == null)
122 pLocalDhcpServer = new (std::nothrow) LocalDhcpServer();
123 SysTryCatch(NID_NET, pLocalDhcpServer != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
124 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
126 r = gpLocalDhcpServerList->Add(*pLocalDhcpServer);
127 SysTryCatch(NID_NET, r == E_SUCCESS, r = E_SYSTEM , E_SYSTEM,
128 "[%s] A system error has been occurred. Failed to add local dhcp server to the list.", GetErrorMessage(E_SYSTEM));
130 pLocalDhcpServer->__pLocalDhcpServerImpl = new (std::nothrow) _LocalDhcpServerImpl();
131 SysTryCatch(NID_NET, pLocalDhcpServer->__pLocalDhcpServerImpl != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
132 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
134 r = pLocalDhcpServer->__pLocalDhcpServerImpl->Construct(pLocalDhcpServer, netConnection);
135 SysTryCatch(NID_NET, r == E_SUCCESS, , r, "%s", GetErrorMessage(r));
137 SysLog(NID_NET, "New instance added \n");
141 return pLocalDhcpServer;
144 if (pLocalDhcpServer != null)
146 gpLocalDhcpServerList->Remove(*pLocalDhcpServer);
147 delete pLocalDhcpServer;
148 pLocalDhcpServer = null;
150 if ((gpLocalDhcpServerList != null) && (gpLocalDhcpServerList->GetCount() == 0))
152 gpLocalDhcpServerList->RemoveAll(true);
153 delete gpLocalDhcpServerList;
154 gpLocalDhcpServerList = null;
161 LocalDhcpServer::SetLocalDhcpServerEventListener(ILocalDhcpServerEventListener* pListener)
163 result r = E_SUCCESS;
165 SysTryReturnResult(NID_NET, gpLocalDhcpServerList != null, E_INVALID_STATE,
166 "This instance has not been constructed or destroyed.");
168 r = __pLocalDhcpServerImpl->SetLocalDhcpServerEventListener(pListener);
169 SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
175 LocalDhcpServer::GetDhcpClientInfoListN(void) const
179 SysTryReturn(NID_NET, gpLocalDhcpServerList != null, null, E_INVALID_STATE,
180 "[%s] LocalDhcpServerList is in an invalid state. This instance has not been constructed or destroyed.",
181 GetErrorMessage(E_INVALID_STATE));
183 return __pLocalDhcpServerImpl->GetDhcpClientInfoListN();