Prathmesh Prabhu | ba99b59 | 2013-04-17 15:13:14 -0700 | [diff] [blame] | 1 | // Copyright (c) 2013 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 | #include "shill/ip_address_store.h" |
| 6 | |
| 7 | #include <iterator> |
| 8 | |
| 9 | #include <stdlib.h> |
| 10 | #include <time.h> |
| 11 | |
| 12 | using std::advance; |
| 13 | |
| 14 | namespace shill { |
| 15 | |
| 16 | // This is a less than comparison so that IPAddress can be stored in a set. |
| 17 | // We do not care about a semantically meaningful comparison. This is |
| 18 | // deterministic, and that's all that matters. |
| 19 | bool IPAddressLTIgnorePrefix::operator () (const IPAddress &lhs, |
| 20 | const IPAddress &rhs) const { |
| 21 | return lhs.ToString() < rhs.ToString(); |
| 22 | } |
| 23 | |
| 24 | IPAddressStore::IPAddressStore() { |
| 25 | srand(time(NULL)); |
| 26 | } |
| 27 | |
| 28 | IPAddressStore::~IPAddressStore() {} |
| 29 | |
| 30 | void IPAddressStore::AddUnique(const IPAddress &ip) { |
| 31 | ip_addresses_.insert(ip); |
| 32 | } |
| 33 | |
| 34 | void IPAddressStore::Clear() { |
| 35 | ip_addresses_.clear(); |
| 36 | } |
| 37 | |
| 38 | size_t IPAddressStore::Count() const { |
| 39 | return ip_addresses_.size(); |
| 40 | } |
| 41 | |
| 42 | bool IPAddressStore::Empty() const { |
| 43 | return ip_addresses_.empty(); |
| 44 | } |
| 45 | |
| 46 | IPAddress IPAddressStore::GetRandomIP() { |
| 47 | if (ip_addresses_.empty()) |
| 48 | return IPAddress(IPAddress::kFamilyUnknown); |
| 49 | int index = rand() % ip_addresses_.size(); |
| 50 | IPAddresses::const_iterator cit = ip_addresses_.begin(); |
| 51 | advance(cit, index); |
| 52 | return *cit; |
| 53 | } |
| 54 | |
| 55 | } // namespace shill |