|
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 instances to an |
23 |
// 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 SampleIpv4AddressVectorClass : 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 GetDefault (void); |
43 |
Ipv4AddressVector m_vector; |
44 |
Ipv4AddressVector m_vector2; |
45 |
}; |
46 |
|
47 |
TypeId |
48 |
SampleIpv4AddressVectorClass::GetTypeId (void) |
49 |
{ |
50 |
static TypeId tid = TypeId ("ns3::SampleIpv4AddressVectorClass") |
51 |
.SetParent<Object> () |
52 |
.AddConstructor<SampleIpv4AddressVectorClass> () |
53 |
.AddAttribute ("Ipv4Addresses", |
54 |
"Ipv4 addresses", |
55 |
Ipv4AddressVectorValue (SampleIpv4AddressVectorClass::GetDefault ()), |
56 |
MakeIpv4AddressVectorAccessor (&SampleIpv4AddressVectorClass::m_vector), |
57 |
MakeIpv4AddressVectorChecker ()) |
58 |
// Another way to configure the default value is with a StringValue |
59 |
.AddAttribute ("Ipv4Addresses2", |
60 |
"Ipv4 addresses", |
61 |
StringValue ("127.0.0.1,127.0.1.1"), |
62 |
MakeIpv4AddressVectorAccessor (&SampleIpv4AddressVectorClass::m_vector2), |
63 |
MakeIpv4AddressVectorChecker ()) |
64 |
; |
65 |
return tid; |
66 |
} |
67 |
|
68 |
TypeId |
69 |
SampleIpv4AddressVectorClass::GetInstanceTypeId (void) const |
70 |
{ |
71 |
return GetTypeId (); |
72 |
} |
73 |
|
74 |
void |
75 |
SampleIpv4AddressVectorClass::Print (std::ostream &os) const |
76 |
{ |
77 |
for (std::vector<Ipv4Address>::const_iterator it = m_vector.begin (); |
78 |
it != m_vector.end (); ) |
79 |
{ |
80 |
it->Print (os); |
81 |
if (++it != m_vector.end ()) |
82 |
{ |
83 |
os << ","; |
84 |
} |
85 |
else |
86 |
{ |
87 |
break; |
88 |
} |
89 |
} |
90 |
} |
91 |
|
92 |
Ipv4AddressVector |
93 |
SampleIpv4AddressVectorClass::GetDefault (void) |
94 |
{ |
95 |
Ipv4AddressVector v; |
96 |
v.push_back (Ipv4Address ("0.0.0.0")); |
97 |
return v; |
98 |
} |
99 |
|
100 |
int main (int argc, char *argv[]) |
101 |
{ |
102 |
Ptr<SampleIpv4AddressVectorClass> t = CreateObject<SampleIpv4AddressVectorClass> (); |
103 |
|
104 |
// Print out default |
105 |
t->Print (std::cout); |
106 |
std::cout << std::endl; |
107 |
|
108 |
Config::SetDefault ("ns3::SampleIpv4AddressVectorClass::Ipv4Addresses", StringValue ("10.0.0.1,10.0.0.2")); |
109 |
|
110 |
Ptr<SampleIpv4AddressVectorClass> t2 = CreateObject<SampleIpv4AddressVectorClass> (); |
111 |
// Print out new default |
112 |
t2->Print (std::cout); |
113 |
std::cout << std::endl; |
114 |
|
115 |
// Modify the attribute value |
116 |
Ipv4AddressVector v; |
117 |
v.push_back (Ipv4Address ("192.168.0.1")); |
118 |
v.push_back (Ipv4Address ("192.168.1.1")); |
119 |
v.push_back (Ipv4Address ("192.168.2.1")); |
120 |
t->SetAttribute ("Ipv4Addresses", Ipv4AddressVectorValue (v)); |
121 |
|
122 |
// Print out modified |
123 |
t->Print (std::cout); |
124 |
std::cout << std::endl; |
125 |
|
126 |
return 0; |
127 |
} |