Merge "Add the exception handling when using manual cert mode" into tizen_2.1
[platform/framework/native/net.git] / src / FNet_LocalDhcpServerEvent.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                FNet_DhcpServerEvent.cpp
19  * @brief               This is the implementation file for the _LocalDhcpServerEvent Class.
20  *
21  * This file contains the implementation of the _LocalDhcpServerEvent Class.
22  */
23
24 #include <FNetNetTypes.h>
25 #include <FNetDhcpClientInfo.h>
26 #include <FNetILocalDhcpServerEventListener.h>
27 #include <FBaseSysLog.h>
28 #include "FNet_NetTypes.h"
29 #include "FNet_LocalDhcpServerImpl.h"
30 #include "FNet_LocalDhcpServerEvent.h"
31 #include "FNet_LocalDhcpServerEventArg.h"
32 #include "FNet_DhcpClientInfoImpl.h"
33
34 using namespace Tizen::Base;
35 using namespace Tizen::Base::Runtime;
36 using namespace Tizen::Base::Collection;
37
38 namespace Tizen { namespace Net
39 {
40
41 _LocalDhcpServerEvent::_LocalDhcpServerEvent()
42         : __pLocalDhcpServer(null)
43         , __pLocalDhcpServerEventListener(null)
44 {
45 }
46
47 _LocalDhcpServerEvent::~_LocalDhcpServerEvent(void)
48 {       
49 }
50
51 result
52 _LocalDhcpServerEvent::Construct(const LocalDhcpServer& localDhcpServer)
53 {
54         result r = E_SUCCESS;
55
56         SysAssertf(__pLocalDhcpServer == null,
57                         "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
58
59         r =  _Event::Initialize();
60         SysTryReturnResult(NID_NET, r == E_SUCCESS, E_SYSTEM,
61                         "A system error has been occurred. Failed to initialize the event.");
62
63         __pLocalDhcpServer = const_cast <LocalDhcpServer*>(&localDhcpServer);
64
65         return r;
66 }
67
68 result
69 _LocalDhcpServerEvent::SetEventListener(ILocalDhcpServerEventListener* pListener)
70 {
71         result r = E_SUCCESS;
72
73         SysAssertf(__pLocalDhcpServer != null, "Not yet constructed. Construct() should be called before use.");
74
75         if (__pLocalDhcpServerEventListener == null)
76         {
77                 if (pListener != null)
78                 {
79                         __pLocalDhcpServerEventListener = pListener;
80                         r = this->AddListener(*pListener, true);
81                 }
82                 else
83                 {
84                         __pLocalDhcpServerEventListener = null;
85                 }
86         }
87         else
88         {
89                 if (pListener != null)
90                 {
91                         this->RemoveListener(*__pLocalDhcpServerEventListener);
92                         __pLocalDhcpServerEventListener = pListener;
93                         r = this->AddListener(*pListener, true);
94                 }
95                 else
96                 {
97                         this->RemoveListener(*__pLocalDhcpServerEventListener);
98                         __pLocalDhcpServerEventListener = null;
99                 }
100         }
101
102         return r;
103 }
104
105 void
106 _LocalDhcpServerEvent::FireImpl(IEventListener& listener, const IEventArg& arg)
107 {
108         result r = E_SUCCESS;
109
110         ILocalDhcpServerEventListener* pLocalDhcpServerEventListener = null;
111         pLocalDhcpServerEventListener = dynamic_cast < ILocalDhcpServerEventListener* >(&listener);
112         SysTryReturnVoidResult(NID_NET, pLocalDhcpServerEventListener != null, E_SYSTEM,
113                         "A system error has been occurred. Listener argument is invalid.");
114
115         IEventArg* pEventArg = const_cast < IEventArg* >(&arg);
116         _LocalDhcpServerEventArg* pLocalDhcpServerEventArg = dynamic_cast < _LocalDhcpServerEventArg* >(pEventArg);
117         SysTryReturnVoidResult(NID_NET, pLocalDhcpServerEventArg != null, E_SYSTEM,
118                         "A system error has been occurred. Event argument is invalid type.");
119
120         // Fire callback
121         DhcpClientInfo* pDhcpClientInfo = pLocalDhcpServerEventArg->GetDhcpClientInfo();
122         SysTryReturnVoidResult(NID_NET, pDhcpClientInfo != null, E_SYSTEM,
123                         "A system error has been occurred. Failed to get dhcp client information.");
124
125         // copy info and send to app.
126         DhcpClientInfo* pCallbackDhcpClientInfo = null;
127         pCallbackDhcpClientInfo = _DhcpClientInfoImpl::CloneDhcpClientInfoN(*pDhcpClientInfo);
128         SysTryReturnVoidResult(NID_NET, pCallbackDhcpClientInfo != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
129
130         switch (pLocalDhcpServerEventArg->GetEventType())
131         {
132         case NET_DHCP_SERVER_EVENT_CONNECT:
133                 SysSecureLog(NID_NET, "Calling OnDhcpClientConnectedN() with Client name:%ls, Mac address:%ls",
134                     (pCallbackDhcpClientInfo->GetName()).GetPointer(),
135                     (pCallbackDhcpClientInfo->GetMacAddress()).GetPointer());
136
137                 pLocalDhcpServerEventListener->OnDhcpClientConnectedN(*GetLocalDhcpServer(),
138                     *pCallbackDhcpClientInfo);
139
140                 break;
141
142         case NET_DHCP_SERVER_EVENT_DISCONNECT:
143                 SysSecureLog(NID_NET, "Calling OnDhcpClientDisconnectedN() with Client name:%ls, Mac address:%ls",
144                                         (pCallbackDhcpClientInfo->GetName()).GetPointer(),
145                                         (pCallbackDhcpClientInfo->GetMacAddress()).GetPointer());
146
147                 pLocalDhcpServerEventListener->OnDhcpClientDisconnectedN(*GetLocalDhcpServer(),
148                     *pCallbackDhcpClientInfo);
149
150                 break;
151
152         default:
153                 delete pCallbackDhcpClientInfo;
154
155                 r = E_SYSTEM;
156                 SysLogException(NID_NET, r, "Event[%d] is invalid.", pLocalDhcpServerEventArg->GetEventType());
157                 
158                 break;
159         }
160
161         return;
162 }
163
164 LocalDhcpServer*
165 _LocalDhcpServerEvent::GetLocalDhcpServer(void) const
166 {
167         ClearLastResult();
168
169         SysTryReturn(NID_NET, __pLocalDhcpServer != null, null, E_INVALID_STATE, "__pLocalDhcpServer is null.");
170
171         return __pLocalDhcpServer;
172 }
173
174 }} // Tizen::Net