change version to 1.2.2.0
[platform/framework/native/net.git] / src / FNetLocalDhcpServer.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 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  * @file                FNetLocalDhcpServer.cpp
19  * @brief               This is the implementation file for the LocalDhcpServer Class.
20  *
21  * This header file contains implementation of the LocalDhcpServer Class.
22  */
23
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"
35
36 using namespace Tizen::Base;
37 using namespace Tizen::Base::Collection;
38
39 namespace Tizen { namespace Net
40 {
41
42 static const int  _MAX_ACTIVE_NETWORK_COUNT = 32;
43
44 static ArrayList* gpLocalDhcpServerList = null;
45
46 LocalDhcpServer::LocalDhcpServer(void)
47         : __pLocalDhcpServerImpl(null)
48 {
49 }
50
51 LocalDhcpServer::~LocalDhcpServer(void)
52 {
53         if (__pLocalDhcpServerImpl != null)
54         {
55                 delete __pLocalDhcpServerImpl;
56                 __pLocalDhcpServerImpl = null;
57
58                 //remove from list also
59                 if (gpLocalDhcpServerList != null)
60                 {
61                         gpLocalDhcpServerList->Remove(*this);
62                 }
63         }
64
65         // when count is zero, cleanup list
66         if ((gpLocalDhcpServerList != null) && (gpLocalDhcpServerList->GetCount() == 0))
67         {
68                 gpLocalDhcpServerList->RemoveAll(true);
69                 delete gpLocalDhcpServerList;
70                 gpLocalDhcpServerList = null;
71         }
72 }
73
74 LocalDhcpServer*
75 LocalDhcpServer::GetInstance(const NetConnection& netConnection)
76 {
77         result r = E_SUCCESS;
78         LocalDhcpServer* pLocalDhcpServer = null;
79
80         ClearLastResult();
81
82         // create list,if list is not created
83         if (gpLocalDhcpServerList == null)
84         {
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));
88
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));
92         }
93
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)
97         {
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));
101
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));
105
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));
109
110                 r = pLocalDhcpServer->__pLocalDhcpServerImpl->Construct(pLocalDhcpServer, netConnection);
111                 SysTryCatch(NID_NET, r == E_SUCCESS, , r, "%s", GetErrorMessage(r));
112
113         }
114         else
115         {
116                 // get LocalDhcpServer based on netConnection
117                 pLocalDhcpServer = _LocalDhcpServerImpl::GetLocalDhcpServer(gpLocalDhcpServerList, netConnection);
118
119                 // if not found, create new
120                 if (pLocalDhcpServer == null)
121                 {
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));
125
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));
129
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));
133
134                         r = pLocalDhcpServer->__pLocalDhcpServerImpl->Construct(pLocalDhcpServer, netConnection);
135                         SysTryCatch(NID_NET, r == E_SUCCESS, , r, "%s", GetErrorMessage(r));
136
137                         SysLog(NID_NET, "New instance added \n");
138                 }
139         }
140
141         return pLocalDhcpServer;
142
143 CATCH:
144         if (pLocalDhcpServer != null)
145         {
146                 gpLocalDhcpServerList->Remove(*pLocalDhcpServer);
147                 delete pLocalDhcpServer;
148                 pLocalDhcpServer = null;
149         }
150         if ((gpLocalDhcpServerList != null) && (gpLocalDhcpServerList->GetCount() == 0))
151         {
152                 gpLocalDhcpServerList->RemoveAll(true);
153                 delete gpLocalDhcpServerList;
154                 gpLocalDhcpServerList = null;
155         }
156
157         return null;
158 }
159
160 result
161 LocalDhcpServer::SetLocalDhcpServerEventListener(ILocalDhcpServerEventListener* pListener)
162 {
163         result r = E_SUCCESS;
164
165         SysTryReturnResult(NID_NET, gpLocalDhcpServerList != null, E_INVALID_STATE,
166                         "This instance has not been constructed or destroyed.");
167
168         r = __pLocalDhcpServerImpl->SetLocalDhcpServerEventListener(pListener);
169         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
170
171         return r;
172 }
173
174 IList*
175 LocalDhcpServer::GetDhcpClientInfoListN(void) const
176 {
177         ClearLastResult();
178
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));
182
183         return __pLocalDhcpServerImpl->GetDhcpClientInfoListN();
184 }
185
186 }} // Tizen::Net