VirtualBox

source: vbox/trunk/src/VBox/Main/src-server/HostDnsService.cpp@ 49644

Last change on this file since 49644 was 49409, checked in by vboxsync, 11 years ago

Main/HostDnsService: fixed object deletion and coding style

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.9 KB
Line 
1/* $Id: HostDnsService.cpp 49409 2013-11-07 13:36:15Z vboxsync $ */
2/** @file
3 * Base class fo Host DNS & Co services.
4 */
5
6/*
7 * Copyright (C) 2013 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18#include <VBox/com/array.h>
19#include <VBox/com/ptr.h>
20#include <VBox/com/string.h>
21
22#include <iprt/cpp/utils.h>
23
24#include "VirtualBoxImpl.h"
25#include <iprt/thread.h>
26#include <iprt/semaphore.h>
27#include <iprt/critsect.h>
28
29#include <algorithm>
30#include <string>
31#include "HostDnsService.h"
32
33
34static HostDnsMonitor *g_monitor;
35
36
37/* Lockee */
38Lockee::Lockee()
39{
40 RTCritSectInit(&mLock);
41}
42
43Lockee::~Lockee()
44{
45 RTCritSectDelete(&mLock);
46}
47
48const RTCRITSECT* Lockee::lock() const
49{
50 return &mLock;
51}
52
53/* ALock */
54ALock::ALock(const Lockee *aLockee)
55 : lockee(aLockee)
56{
57 RTCritSectEnter(const_cast<PRTCRITSECT>(lockee->lock()));
58}
59
60ALock::~ALock()
61{
62 RTCritSectLeave(const_cast<PRTCRITSECT>(lockee->lock()));
63}
64
65inline static void detachVectorOfString(const std::vector<std::string>& v,
66 ComSafeArrayOut(BSTR, aBstrArray))
67{
68 com::SafeArray<BSTR> aBstr(v.size());
69
70 std::vector<std::string>::const_iterator it;
71
72 int i = 0;
73 it = v.begin();
74 for (; it != v.end(); ++it, ++i)
75 Utf8Str(it->c_str()).cloneTo(&aBstr[i]);
76
77 aBstr.detachTo(ComSafeArrayOutArg(aBstrArray));
78}
79
80struct HostDnsMonitor::Data
81{
82 std::vector<PCHostDnsMonitorProxy> proxies;
83 HostDnsInformation info;
84};
85
86struct HostDnsMonitorProxy::Data
87{
88 Data(const HostDnsMonitor *aMonitor, const VirtualBox *aParent)
89 : info(NULL)
90 , virtualbox(aParent)
91 , monitor(aMonitor)
92 , fModified(true)
93 {}
94
95 virtual ~Data()
96 {
97 if (info)
98 {
99 delete info;
100 info = NULL;
101 }
102 }
103
104 HostDnsInformation *info;
105 const VirtualBox *virtualbox;
106 const HostDnsMonitor *monitor;
107 bool fModified;
108};
109
110
111HostDnsMonitor::HostDnsMonitor()
112 : m(NULL)
113{
114}
115
116HostDnsMonitor::~HostDnsMonitor()
117{
118 if (m)
119 {
120 delete m;
121 m = NULL;
122 }
123}
124
125const HostDnsMonitor *HostDnsMonitor::getHostDnsMonitor()
126{
127 /* XXX: Moved initialization from HostImpl.cpp */
128 if (!g_monitor)
129 {
130# if defined (RT_OS_DARWIN)
131 g_monitor = new HostDnsServiceDarwin();
132# elif defined(RT_OS_WINDOWS)
133 g_monitor = new HostDnsServiceWin();
134# elif defined(RT_OS_LINUX)
135 g_monitor = new HostDnsServiceLinux();
136# elif defined(RT_OS_SOLARIS)
137 g_monitor = new HostDnsServiceSolaris();
138# elif defined(RT_OS_OS2)
139 g_monitor = new HostDnsServiceOs2();
140# else
141 g_monitor = new HostDnsService();
142# endif
143 g_monitor->init();
144 }
145
146 return g_monitor;
147}
148
149void HostDnsMonitor::addMonitorProxy(PCHostDnsMonitorProxy proxy) const
150{
151 ALock l(this);
152 m->proxies.push_back(proxy);
153 proxy->notify();
154}
155
156void HostDnsMonitor::releaseMonitorProxy(PCHostDnsMonitorProxy proxy) const
157{
158 ALock l(this);
159 std::vector<PCHostDnsMonitorProxy>::iterator it;
160 it = std::find(m->proxies.begin(), m->proxies.end(), proxy);
161
162 if (it == m->proxies.end())
163 return;
164
165 m->proxies.erase(it);
166}
167
168void HostDnsMonitor::shutdown()
169{
170 if (g_monitor)
171 {
172 delete g_monitor;
173 g_monitor = NULL;
174 }
175}
176
177const HostDnsInformation &HostDnsMonitor::getInfo() const
178{
179 return m->info;
180}
181
182void HostDnsMonitor::notifyAll() const
183{
184 ALock l(this);
185 std::vector<PCHostDnsMonitorProxy>::const_iterator it;
186 for (it = m->proxies.begin(); it != m->proxies.end(); ++it)
187 (*it)->notify();
188}
189
190void HostDnsMonitor::setInfo(const HostDnsInformation &info)
191{
192 ALock l(this);
193 m->info = info;
194}
195
196HRESULT HostDnsMonitor::init()
197{
198 m = new HostDnsMonitor::Data();
199 return S_OK;
200}
201
202
203/* HostDnsMonitorProxy */
204HostDnsMonitorProxy::HostDnsMonitorProxy()
205 : m(NULL)
206{
207}
208
209HostDnsMonitorProxy::~HostDnsMonitorProxy()
210{
211 if (m)
212 {
213 if (m->monitor)
214 m->monitor->releaseMonitorProxy(this);
215 delete m;
216 m = NULL;
217 }
218}
219
220void HostDnsMonitorProxy::init(const HostDnsMonitor *mon, const VirtualBox* aParent)
221{
222 m = new HostDnsMonitorProxy::Data(mon, aParent);
223 m->monitor->addMonitorProxy(this);
224 updateInfo();
225}
226
227void HostDnsMonitorProxy::notify() const
228{
229 m->fModified = true;
230 const_cast<VirtualBox *>(m->virtualbox)->onHostNameResolutionConfigurationChange();
231}
232
233HRESULT HostDnsMonitorProxy::GetNameServers(ComSafeArrayOut(BSTR, aNameServers))
234{
235 AssertReturn(m && m->info, E_FAIL);
236 ALock l(this);
237
238 if (m->fModified)
239 updateInfo();
240
241 detachVectorOfString(m->info->servers, ComSafeArrayOutArg(aNameServers));
242
243 return S_OK;
244}
245
246HRESULT HostDnsMonitorProxy::GetDomainName(BSTR *aDomainName)
247{
248 AssertReturn(m && m->info, E_FAIL);
249 ALock l(this);
250
251 if (m->fModified)
252 updateInfo();
253
254 Utf8Str(m->info->domain.c_str()).cloneTo(aDomainName);
255
256 return S_OK;
257}
258
259HRESULT HostDnsMonitorProxy::GetSearchStrings(ComSafeArrayOut(BSTR, aSearchStrings))
260{
261 AssertReturn(m && m->info, E_FAIL);
262 ALock l(this);
263
264 if (m->fModified)
265 updateInfo();
266
267 detachVectorOfString(m->info->searchList, ComSafeArrayOutArg(aSearchStrings));
268
269 return S_OK;
270}
271
272bool HostDnsMonitorProxy::operator==(PCHostDnsMonitorProxy& rhs)
273{
274 if (!m || !rhs->m)
275 return false;
276
277 /**
278 * we've assigned to the same instance of VirtualBox.
279 */
280 return m->virtualbox == rhs->m->virtualbox;
281}
282
283void HostDnsMonitorProxy::updateInfo()
284{
285 HostDnsInformation *info = new HostDnsInformation(m->monitor->getInfo());
286 HostDnsInformation *old = m->info;
287
288 m->info = info;
289 if (old)
290 delete old;
291
292 m->fModified = false;
293}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette