View | Details | Raw Unified | Return to bug 2348
Collapse All | Expand All

(-)a3c5fea29467 (+127 lines)
Added Link Here 
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
}
(-)a/src/network/examples/wscript (+3 lines)
 Lines 10-14    Link Here 
10
    obj = bld.create_ns3_program('main-packet-tag', ['network'])
10
    obj = bld.create_ns3_program('main-packet-tag', ['network'])
11
    obj.source = 'main-packet-tag.cc'
11
    obj.source = 'main-packet-tag.cc'
12
12
13
    obj = bld.create_ns3_program('ipv4-address-vector-example', ['network'])
14
    obj.source = 'ipv4-address-vector-example.cc'
15
13
    obj = bld.create_ns3_program('packet-socket-apps', ['core', 'network'])
16
    obj = bld.create_ns3_program('packet-socket-apps', ['core', 'network'])
14
    obj.source = 'packet-socket-apps.cc'
17
    obj.source = 'packet-socket-apps.cc'
(-)a/src/network/utils/address-utils.cc (+46 lines)
 Lines 126-129    Link Here 
126
126
127
} // namespace addressUtils
127
} // namespace addressUtils
128
128
129
ATTRIBUTE_HELPER_CPP (Ipv4AddressVector);
130
131
std::ostream &
132
operator << (std::ostream &os, const Ipv4AddressVector &ipv4AddressVector)
133
{
134
  for (uint32_t i = 0; i < ipv4AddressVector.size (); i++)
135
    {
136
      os << ipv4AddressVector[i];
137
      if (i < (ipv4AddressVector.size () - 1))
138
        {
139
          os << ",";
140
        }
141
    }
142
  return os;
143
}
144
145
std::istream &
146
operator >> (std::istream &is, Ipv4AddressVector &ipv4AddressVector)
147
{
148
  char delim = ',';
149
  std::string addressStr;
150
  is >> addressStr;
151
  std::vector<std::string> addrs;
152
153
  // parse input stream for addresses
154
  for (size_t p=0, q=0; p != addressStr.npos; p = q)
155
    {
156
      addrs.push_back (addressStr.substr (p + (p != 0), (q = addressStr.find (delim, p+1)) - p - (p != 0)));
157
    }
158
  if (addrs.size () > 0)
159
    {
160
      ipv4AddressVector.clear ();
161
      ipv4AddressVector.resize (addrs.size ());
162
      for (uint32_t i = 0; i < addrs.size (); i++)
163
        {
164
          ipv4AddressVector[i] = Ipv4Address (addrs[i].c_str ());
165
        }
166
    }
167
  else
168
    {
169
      is.setstate (std::ios_base::failbit);
170
    }
171
  
172
  return is;
173
}
174
129
} // namespace ns3
175
} // namespace ns3
(-)a/src/network/utils/address-utils.h (+5 lines)
 Lines 124-129    Link Here 
124
bool IsMulticast (const Address &ad);
124
bool IsMulticast (const Address &ad);
125
};
125
};
126
126
127
typedef std::vector<Ipv4Address> Ipv4AddressVector;
128
std::ostream &operator << (std::ostream &os, const Ipv4AddressVector &ipv4AddressVector);
129
std::istream &operator >> (std::istream &is, Ipv4AddressVector &ipv4AddressVector);
130
ATTRIBUTE_HELPER_HEADER (Ipv4AddressVector);
131
127
};
132
};
128
133
129
#endif /* ADDRESS_UTILS_H */
134
#endif /* ADDRESS_UTILS_H */

Return to bug 2348