|
8 | 8 |
|
9 | 9 | using namespace std; |
10 | 10 |
|
11 | | -DroppingPacketQueue::DroppingPacketQueue( const string & args ) |
12 | | - : packet_limit_( get_arg( args, "packets" ) ), |
13 | | - byte_limit_( get_arg( args, "bytes" ) ) |
| 11 | +DroppingPacketQueue::DroppingPacketQueue( const map<string, string> & args ) |
| 12 | + : packet_limit_( get_int_arg( args, "packets" ) ), |
| 13 | + byte_limit_( get_int_arg( args, "bytes" ) ) |
14 | 14 | { |
15 | 15 | if ( packet_limit_ == 0 and byte_limit_ == 0 ) { |
16 | 16 | throw runtime_error( "Dropping queue must have a byte or packet limit." ); |
17 | 17 | } |
18 | 18 | } |
19 | 19 |
|
| 20 | +unsigned int DroppingPacketQueue::get_int_arg(const map<string, string> & args, const string & name) { |
| 21 | + if (args.count(name) > 0) { |
| 22 | + return myatoi(args.at(name)); |
| 23 | + } else { |
| 24 | + return 0; |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +double DroppingPacketQueue::get_float_arg(const map<string, string> & args, const string & name) { |
| 29 | + if (args.count(name) > 0) { |
| 30 | + return myatof(args.at(name)); |
| 31 | + } else { |
| 32 | + return 0; |
| 33 | + } |
| 34 | +} |
| 35 | + |
20 | 36 | QueuedPacket DroppingPacketQueue::dequeue( void ) |
21 | 37 | { |
22 | 38 | assert( not internal_queue_.empty() ); |
@@ -98,66 +114,3 @@ string DroppingPacketQueue::to_string( void ) const |
98 | 114 |
|
99 | 115 | return ret; |
100 | 116 | } |
101 | | - |
102 | | -string DroppingPacketQueue::parse_number_arg(const string & args, const string & name, bool isfloat) |
103 | | -{ |
104 | | - auto offset = args.find( name ); |
105 | | - if ( offset == string::npos ) { |
106 | | - return ""; /* default value */ |
107 | | - } else { |
108 | | - /* extract the value */ |
109 | | - |
110 | | - /* advance by length of name */ |
111 | | - offset += name.size(); |
112 | | - |
113 | | - /* make sure next char is "=" */ |
114 | | - if ( args.substr( offset, 1 ) != "=" ) { |
115 | | - throw runtime_error( "could not parse queue arguments: " + args ); |
116 | | - } |
117 | | - |
118 | | - /* advance by length of "=" */ |
119 | | - offset++; |
120 | | - |
121 | | - /* find the first non-valid character */ |
122 | | - string validchars; |
123 | | - if (isfloat) { |
124 | | - validchars = "0123456789."; |
125 | | - } else { |
126 | | - validchars = "0123456789"; |
127 | | - } |
128 | | - auto offset2 = args.substr( offset ).find_first_not_of( validchars ); |
129 | | - |
130 | | - auto digit_string = args.substr( offset ).substr( 0, offset2 ); |
131 | | - |
132 | | - if ( digit_string.empty() ) { |
133 | | - throw runtime_error( "could not parse queue arguments: " + args ); |
134 | | - } |
135 | | - return digit_string; |
136 | | - } |
137 | | -} |
138 | | - |
139 | | -unsigned int DroppingPacketQueue::get_arg( const string & args, const string & name) |
140 | | -{ |
141 | | - auto offset = args.find( name ); |
142 | | - if ( offset == string::npos ) { |
143 | | - return 0; /* default value */ |
144 | | - } else { |
145 | | - auto digit_string = parse_number_arg(args, name, false); |
146 | | - |
147 | | - return myatoi( digit_string ); |
148 | | - } |
149 | | -} |
150 | | - |
151 | | -double DroppingPacketQueue::get_float_arg( const string & args, const string & name) |
152 | | -{ |
153 | | - auto offset = args.find( name ); |
154 | | - if ( offset == string::npos ) { |
155 | | - return 0; /* default value */ |
156 | | - } else { |
157 | | - auto digit_string = parse_number_arg(args, name, true); |
158 | | - return myatof( digit_string ); |
159 | | - } |
160 | | - |
161 | | -} |
162 | | - |
163 | | - |
0 commit comments