Skip to content

Commit 8e1457f

Browse files
committed
Parse args properly.
1 parent 71ca64e commit 8e1457f

11 files changed

+103
-84
lines changed

src/frontend/linkshell.cc

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* -*-mode:c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
22

33
#include <getopt.h>
4-
4+
#include <map>
55
#include "infinite_packet_queue.hh"
66
#include "drop_tail_packet_queue.hh"
77
#include "drop_head_packet_queue.hh"
@@ -10,6 +10,7 @@
1010
#include "red_packet_queue.hh"
1111
#include "link_queue.hh"
1212
#include "packetshell.cc"
13+
#include "tokenize.hh"
1314

1415
using namespace std;
1516

@@ -33,7 +34,7 @@ void usage_error( const string & program_name )
3334
throw runtime_error( "invalid arguments" );
3435
}
3536

36-
unique_ptr<AbstractPacketQueue> get_packet_queue( const string & type, const string & args, const string & program_name )
37+
unique_ptr<AbstractPacketQueue> get_packet_queue( const string & type, const map<string, string> & args, const string & program_name )
3738
{
3839
if ( type == "infinite" ) {
3940
return unique_ptr<AbstractPacketQueue>( new InfinitePacketQueue( args ) );
@@ -71,6 +72,26 @@ string shell_quote( const string & arg )
7172
return ret;
7273
}
7374

75+
map<string, string> parse_queue_args( const string & arg) {
76+
map<string, string> argMap = map<string, string>();
77+
if (arg.size() == 0) {
78+
return argMap;
79+
}
80+
vector<string> argList = split(arg, ",");
81+
82+
for (size_t i = 0;i < argList.size();i++) {
83+
string s = argList[i];
84+
vector<string> argParts = split(s, "=");
85+
if (argParts.size() != 2) {
86+
throw runtime_error("Queue args passed in wrong format");
87+
}
88+
89+
argMap.insert(pair<string, string>(argParts[0], argParts[1]));
90+
}
91+
92+
return argMap;
93+
}
94+
7495
int main( int argc, char *argv[] )
7596
{
7697
try {
@@ -186,11 +207,11 @@ int main( int argc, char *argv[] )
186207

187208
link_shell_app.start_uplink( "[link] ", command,
188209
"Uplink", uplink_filename, uplink_logfile, repeat, meter_uplink, meter_uplink_delay,
189-
get_packet_queue( uplink_queue_type, uplink_queue_args, argv[ 0 ] ),
210+
get_packet_queue( uplink_queue_type, parse_queue_args(uplink_queue_args), argv[ 0 ] ),
190211
command_line );
191212

192213
link_shell_app.start_downlink( "Downlink", downlink_filename, downlink_logfile, repeat, meter_downlink, meter_downlink_delay,
193-
get_packet_queue( downlink_queue_type, downlink_queue_args, argv[ 0 ] ),
214+
get_packet_queue( downlink_queue_type, parse_queue_args(downlink_queue_args), argv[ 0 ] ),
194215
command_line );
195216

196217
return link_shell_app.wait_for_exit();

src/packet/codel_packet_queue.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55

66
using namespace std;
77

8-
CODELPacketQueue::CODELPacketQueue( const string & args )
8+
CODELPacketQueue::CODELPacketQueue( const map<string, string> & args )
99
: DroppingPacketQueue(args),
10-
target_ ( get_arg( args, "target") ),
11-
interval_ ( get_arg( args, "interval") ),
10+
target_ ( get_int_arg( args, "target") ),
11+
interval_ ( get_int_arg( args, "interval") ),
1212
first_above_time_ ( 0 ),
1313
drop_next_( 0 ),
1414
count_ ( 0 ),

src/packet/codel_packet_queue.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ private:
4545
uint64_t control_law ( uint64_t t, uint32_t count );
4646

4747
public:
48-
CODELPacketQueue( const std::string & args );
48+
CODELPacketQueue( const std::map<std::string, std::string> & args );
4949

5050
void enqueue( QueuedPacket && p ) override;
5151

src/packet/dropping_packet_queue.cc

Lines changed: 19 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,31 @@
88

99
using namespace std;
1010

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" ) )
1414
{
1515
if ( packet_limit_ == 0 and byte_limit_ == 0 ) {
1616
throw runtime_error( "Dropping queue must have a byte or packet limit." );
1717
}
1818
}
1919

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+
2036
QueuedPacket DroppingPacketQueue::dequeue( void )
2137
{
2238
assert( not internal_queue_.empty() );
@@ -98,66 +114,3 @@ string DroppingPacketQueue::to_string( void ) const
98114

99115
return ret;
100116
}
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-

src/packet/dropping_packet_queue.hh

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include <queue>
77
#include <cassert>
8+
#include <map>
89

910
#include "abstract_packet_queue.hh"
1011
#include "exception.hh"
@@ -34,7 +35,7 @@ protected:
3435
const unsigned int size_in_packets ) const;
3536

3637
public:
37-
DroppingPacketQueue( const std::string & args );
38+
DroppingPacketQueue( const std::map<std::string, std::string> & args );
3839

3940
virtual void enqueue( QueuedPacket && p ) = 0;
4041

@@ -45,8 +46,10 @@ public:
4546
std::string to_string( void ) const override;
4647

4748
static std::string parse_number_arg(const std::string & args, const std::string & name, bool isfloat);
49+
50+
static unsigned int get_int_arg( const std::map<std::string, std::string> & args, const std::string & name );
4851
static unsigned int get_arg( const std::string & args, const std::string & name );
49-
static double get_float_arg( const std::string & args, const std::string & name );
52+
static double get_float_arg( const std::map<std::string, std::string> & args, const std::string & name );
5053
};
5154

5255
#endif /* DROPPING_PACKET_QUEUE_HH */

src/packet/infinite_packet_queue.hh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@
99
#include "queued_packet.hh"
1010
#include "abstract_packet_queue.hh"
1111
#include "exception.hh"
12+
#include <map>
1213

1314
class InfinitePacketQueue : public AbstractPacketQueue
1415
{
1516
private:
1617
std::queue<QueuedPacket> internal_queue_ {};
1718

1819
public:
19-
InfinitePacketQueue( const std::string & args )
20+
InfinitePacketQueue( const std::map<std::string, std::string> & args )
2021
{
2122
if ( not args.empty() ) {
2223
throw std::runtime_error( "InfinitePacketQueue does not take arguments." );

src/packet/pie_packet_queue.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ using namespace std;
88

99
#define DQ_COUNT_INVALID (uint32_t)-1
1010

11-
PIEPacketQueue::PIEPacketQueue( const string & args )
11+
PIEPacketQueue::PIEPacketQueue( const map<string, string> & args )
1212
: DroppingPacketQueue(args),
13-
qdelay_ref_ ( get_arg( args, "qdelay_ref" ) ),
14-
max_burst_ ( get_arg( args, "max_burst" ) ),
13+
qdelay_ref_ ( get_int_arg( args, "qdelay_ref" ) ),
14+
max_burst_ ( get_int_arg( args, "max_burst" ) ),
1515
alpha_ ( 0.125 ),
1616
beta_ ( 1.25 ),
1717
t_update_ ( 30 ),

src/packet/pie_packet_queue.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ private:
5252
void calculate_drop_prob ( void );
5353

5454
public:
55-
PIEPacketQueue( const std::string & args );
55+
PIEPacketQueue( const std::map<std::string, std::string> & args );
5656

5757
void enqueue( QueuedPacket && p ) override;
5858

src/packet/red_packet_queue.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
using namespace std;
66

7-
REDPacketQueue::REDPacketQueue( const string & args)
7+
REDPacketQueue::REDPacketQueue( const map<string, string> & args)
88
: DroppingPacketQueue(args),
99
wq_(get_float_arg(args, "wq")),
1010
min_thresh_(get_float_arg(args, "minthresh")),
1111
max_thresh_(get_float_arg(args, "maxthresh")),
12-
transmission_time_(get_arg(args, "transmission_time")),
12+
transmission_time_(get_int_arg(args, "transmission_time")),
1313
time_at_zero_q_(0),
1414
prng_( random_device()() ),
1515
drop_dist_ (0, 1),

src/packet/red_packet_queue.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ private:
4242
unsigned int max_queue_depth_packets() const;
4343

4444
public:
45-
REDPacketQueue( const std::string & args );
45+
REDPacketQueue( const std::map<std::string, std::string> & args );
4646
QueuedPacket dequeue( void ) override;
4747
void enqueue( QueuedPacket && p ) override;
4848
};

0 commit comments

Comments
 (0)