blob: 771e9e13f39f8ecc610a523ab66d58e575ccc1b6 [file] [log] [blame]
Darin Petkov50308cd2011-06-01 18:25:07 -07001// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef SHILL_DHCP_PROVIDER_
6#define SHILL_DHCP_PROVIDER_
7
Darin Petkovd1b715b2011-06-02 21:21:22 -07008#include <map>
Darin Petkovaceede32011-07-18 15:32:38 -07009#include <string>
Darin Petkovd1b715b2011-06-02 21:21:22 -070010
11#include <base/memory/scoped_ptr.h>
Darin Petkov50308cd2011-06-01 18:25:07 -070012#include <base/memory/singleton.h>
Darin Petkovf7897bc2011-06-08 17:13:36 -070013#include <gtest/gtest_prod.h> // for FRIEND_TEST
Darin Petkovd1b715b2011-06-02 21:21:22 -070014
Chris Masone2b105542011-06-22 10:58:09 -070015#include "shill/refptr_types.h"
Darin Petkov50308cd2011-06-01 18:25:07 -070016
17namespace shill {
18
Darin Petkovaceede32011-07-18 15:32:38 -070019class DHCPCDListener;
Chris Masone2b105542011-06-22 10:58:09 -070020class GLib;
Darin Petkovd1b715b2011-06-02 21:21:22 -070021
22// DHCPProvider is a singleton providing the main DHCP configuration
23// entrypoint. Once the provider is initialized through its Init method, DHCP
24// configurations for devices can be obtained through its CreateConfig
25// method. For example, a single DHCP configuration request can be initiated as:
26//
Darin Petkovf65e9282011-06-21 14:29:56 -070027// DHCPProvider::GetInstance()->CreateConfig(device_name)->Request();
Darin Petkov50308cd2011-06-01 18:25:07 -070028class DHCPProvider {
29 public:
30 // This is a singleton -- use DHCPProvider::GetInstance()->Foo()
31 static DHCPProvider *GetInstance();
32
Darin Petkovd1b715b2011-06-02 21:21:22 -070033 // Initializes the provider singleton. This method hooks up a D-Bus signal
Darin Petkovaceede32011-07-18 15:32:38 -070034 // listener that catches signals from spawned DHCP clients and dispatches them
35 // to the appropriate DHCP configuration instance.
36 void Init(GLib *glib);
Darin Petkovd1b715b2011-06-02 21:21:22 -070037
Darin Petkovf65e9282011-06-21 14:29:56 -070038 // Creates a new DHCPConfig for |device_name|. The DHCP configuration for the
Darin Petkovd1b715b2011-06-02 21:21:22 -070039 // device can then be initiated through DHCPConfig::Request and
40 // DHCPConfig::Renew.
Darin Petkovf65e9282011-06-21 14:29:56 -070041 DHCPConfigRefPtr CreateConfig(const std::string &device_name);
Darin Petkovd1b715b2011-06-02 21:21:22 -070042
43 // Returns the DHCP configuration associated with DHCP client |pid|. Return
44 // NULL if |pid| is not bound to a configuration.
Darin Petkov98dd6a02011-06-10 15:12:57 -070045 DHCPConfigRefPtr GetConfig(int pid);
Darin Petkovd1b715b2011-06-02 21:21:22 -070046
47 // Binds a |pid| to a DHCP |config|. When a DHCP config spawns a new DHCP
48 // client, it binds itself to that client's |pid|.
Chris Masone2b105542011-06-22 10:58:09 -070049 void BindPID(int pid, const DHCPConfigRefPtr &config);
Darin Petkovd1b715b2011-06-02 21:21:22 -070050
51 // Unbinds a |pid|. This method is used by a DHCP config to signal the
52 // provider that the DHCP client has been terminated. This may result in
53 // destruction of the DHCP config instance if its reference count goes to 0.
Darin Petkov92c43902011-06-09 20:46:06 -070054 void UnbindPID(int pid);
Darin Petkovd1b715b2011-06-02 21:21:22 -070055
Darin Petkov50308cd2011-06-01 18:25:07 -070056 private:
57 friend struct DefaultSingletonTraits<DHCPProvider>;
Darin Petkov98dd6a02011-06-10 15:12:57 -070058 friend class DHCPProviderTest;
Darin Petkovafa6fc42011-06-21 16:21:08 -070059 friend class DeviceInfoTest;
60 friend class DeviceTest;
Darin Petkov98dd6a02011-06-10 15:12:57 -070061 FRIEND_TEST(DHCPProviderTest, CreateConfig);
Darin Petkovf7897bc2011-06-08 17:13:36 -070062
Darin Petkov92c43902011-06-09 20:46:06 -070063 typedef std::map<int, DHCPConfigRefPtr> PIDConfigMap;
Darin Petkov50308cd2011-06-01 18:25:07 -070064
Darin Petkovd1b715b2011-06-02 21:21:22 -070065 // Private to ensure that this behaves as a singleton.
Darin Petkov50308cd2011-06-01 18:25:07 -070066 DHCPProvider();
67 virtual ~DHCPProvider();
68
Darin Petkovd1b715b2011-06-02 21:21:22 -070069 // A single listener is used to catch signals from all DHCP clients and
Darin Petkovafa6fc42011-06-21 16:21:08 -070070 // dispatch them to the appropriate DHCP configuration instance.
Darin Petkovaceede32011-07-18 15:32:38 -070071 scoped_ptr<DHCPCDListener> listener_;
Darin Petkovd1b715b2011-06-02 21:21:22 -070072
73 // A map that binds PIDs to DHCP configuration instances.
74 PIDConfigMap configs_;
75
Darin Petkov3258a812011-06-23 11:28:45 -070076 GLib *glib_;
Darin Petkovf7897bc2011-06-08 17:13:36 -070077
Darin Petkov50308cd2011-06-01 18:25:07 -070078 DISALLOW_COPY_AND_ASSIGN(DHCPProvider);
79};
80
81} // namespace shill
82
83#endif // SHILL_DHCP_PROVIDER_