|
|
| 1 |
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 |
/* |
| 3 |
* Copyright (c) 2016 University of Washington |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License version 2 as |
| 7 |
* published by the Free Software Foundation; |
| 8 |
* |
| 9 |
* This program is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
* GNU General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU General Public License |
| 15 |
* along with this program; if not, write to the Free Software |
| 16 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 |
* |
| 18 |
* Author: Tom Henderson <tomhend@u.washington.edu> |
| 19 |
*/ |
| 20 |
|
| 21 |
// |
| 22 |
// Simple example showing how to add a vector of Ipv4Address and Ipv6Address |
| 23 |
// instances to an attribute |
| 24 |
// |
| 25 |
|
| 26 |
#include "ns3/object.h" |
| 27 |
#include "ns3/string.h" |
| 28 |
#include "ns3/config.h" |
| 29 |
#include "ns3/address-utils.h" |
| 30 |
|
| 31 |
using namespace ns3; |
| 32 |
|
| 33 |
class SampleIpAddressVectorClass : public Object |
| 34 |
{ |
| 35 |
public: |
| 36 |
static TypeId GetTypeId (void); |
| 37 |
virtual TypeId GetInstanceTypeId (void) const; |
| 38 |
|
| 39 |
void Print (std::ostream &os) const; |
| 40 |
|
| 41 |
private: |
| 42 |
static Ipv4AddressVector GetDefaultIpv4Vector (void); |
| 43 |
static Ipv6AddressVector GetDefaultIpv6Vector (void); |
| 44 |
Ipv4AddressVector m_vector4; |
| 45 |
Ipv4AddressVector m_vector4_1; |
| 46 |
Ipv6AddressVector m_vector6; |
| 47 |
Ipv6AddressVector m_vector6_1; |
| 48 |
}; |
| 49 |
|
| 50 |
TypeId |
| 51 |
SampleIpAddressVectorClass::GetTypeId (void) |
| 52 |
{ |
| 53 |
static TypeId tid = TypeId ("ns3::SampleIpAddressVectorClass") |
| 54 |
.SetParent<Object> () |
| 55 |
.AddConstructor<SampleIpAddressVectorClass> () |
| 56 |
.AddAttribute ("Ipv4Addresses", |
| 57 |
"Ipv4 addresses", |
| 58 |
Ipv4AddressVectorValue (SampleIpAddressVectorClass::GetDefaultIpv4Vector ()), |
| 59 |
MakeIpv4AddressVectorAccessor (&SampleIpAddressVectorClass::m_vector4), |
| 60 |
MakeIpv4AddressVectorChecker ()) |
| 61 |
// Another way to configure the default value is with a StringValue |
| 62 |
.AddAttribute ("Ipv4Addresses2", |
| 63 |
"Ipv4 addresses", |
| 64 |
StringValue ("127.0.0.1,127.0.1.1"), |
| 65 |
MakeIpv4AddressVectorAccessor (&SampleIpAddressVectorClass::m_vector4_1), |
| 66 |
MakeIpv4AddressVectorChecker ()) |
| 67 |
.AddAttribute ("Ipv6Addresses", |
| 68 |
"Ipv6 addresses", |
| 69 |
Ipv6AddressVectorValue (SampleIpAddressVectorClass::GetDefaultIpv6Vector ()), |
| 70 |
MakeIpv6AddressVectorAccessor (&SampleIpAddressVectorClass::m_vector6), |
| 71 |
MakeIpv6AddressVectorChecker ()) |
| 72 |
.AddAttribute ("Ipv6Addresses2", |
| 73 |
"Ipv6 addresses", |
| 74 |
StringValue ("2001:DB8::1,2001:DB8::2"), |
| 75 |
MakeIpv6AddressVectorAccessor (&SampleIpAddressVectorClass::m_vector6_1), |
| 76 |
MakeIpv6AddressVectorChecker ()) |
| 77 |
; |
| 78 |
return tid; |
| 79 |
} |
| 80 |
|
| 81 |
TypeId |
| 82 |
SampleIpAddressVectorClass::GetInstanceTypeId (void) const |
| 83 |
{ |
| 84 |
return GetTypeId (); |
| 85 |
} |
| 86 |
|
| 87 |
void |
| 88 |
SampleIpAddressVectorClass::Print (std::ostream &os) const |
| 89 |
{ |
| 90 |
for (std::vector<Ipv4Address>::const_iterator it = m_vector4.begin (); |
| 91 |
it != m_vector4.end (); ) |
| 92 |
{ |
| 93 |
it->Print (os); |
| 94 |
if (++it != m_vector4.end ()) |
| 95 |
{ |
| 96 |
os << ","; |
| 97 |
} |
| 98 |
else |
| 99 |
{ |
| 100 |
break; |
| 101 |
} |
| 102 |
} |
| 103 |
os << std::endl; |
| 104 |
for (std::vector<Ipv6Address>::const_iterator it = m_vector6.begin (); |
| 105 |
it != m_vector6.end (); ) |
| 106 |
{ |
| 107 |
it->Print (os); |
| 108 |
if (++it != m_vector6.end ()) |
| 109 |
{ |
| 110 |
os << ","; |
| 111 |
} |
| 112 |
else |
| 113 |
{ |
| 114 |
break; |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
Ipv4AddressVector |
| 120 |
SampleIpAddressVectorClass::GetDefaultIpv4Vector (void) |
| 121 |
{ |
| 122 |
Ipv4AddressVector v; |
| 123 |
v.push_back (Ipv4Address ("0.0.0.0")); |
| 124 |
return v; |
| 125 |
} |
| 126 |
|
| 127 |
Ipv6AddressVector |
| 128 |
SampleIpAddressVectorClass::GetDefaultIpv6Vector (void) |
| 129 |
{ |
| 130 |
Ipv6AddressVector v; |
| 131 |
v.push_back (Ipv6Address ("::")); |
| 132 |
return v; |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
int main (int argc, char *argv[]) |
| 137 |
{ |
| 138 |
Ptr<SampleIpAddressVectorClass> t = CreateObject<SampleIpAddressVectorClass> (); |
| 139 |
|
| 140 |
// Print out defaults |
| 141 |
t->Print (std::cout); |
| 142 |
std::cout << std::endl; |
| 143 |
|
| 144 |
Config::SetDefault ("ns3::SampleIpAddressVectorClass::Ipv4Addresses", StringValue ("192.0.2.1,192.0.2.2")); |
| 145 |
Config::SetDefault ("ns3::SampleIpAddressVectorClass::Ipv6Addresses", StringValue ("2001:DB8:A::1,2001:DB8:A::2")); |
| 146 |
|
| 147 |
Ptr<SampleIpAddressVectorClass> t2 = CreateObject<SampleIpAddressVectorClass> (); |
| 148 |
// Print out new defaults |
| 149 |
t2->Print (std::cout); |
| 150 |
std::cout << std::endl; |
| 151 |
|
| 152 |
// Modify the attribute values |
| 153 |
Ipv4AddressVector v; |
| 154 |
v.push_back (Ipv4Address ("203.0.113.1")); |
| 155 |
v.push_back (Ipv4Address ("203.0.113.2")); |
| 156 |
v.push_back (Ipv4Address ("203.0.113.3")); |
| 157 |
t->SetAttribute ("Ipv4Addresses", Ipv4AddressVectorValue (v)); |
| 158 |
Ipv6AddressVector v6; |
| 159 |
v6.push_back (Ipv6Address ("2001:DB8:C::1")); |
| 160 |
v6.push_back (Ipv6Address ("2001:DB8:C::101")); |
| 161 |
t->SetAttribute ("Ipv6Addresses", Ipv6AddressVectorValue (v6)); |
| 162 |
|
| 163 |
// Print out modified |
| 164 |
t->Print (std::cout); |
| 165 |
std::cout << std::endl; |
| 166 |
|
| 167 |
return 0; |
| 168 |
} |