Skip to content

Commit ddba279

Browse files
squidarthSidharth Shanker
authored andcommitted
Parse args properly.
1 parent ce1e909 commit ddba279

11 files changed

+103
-84
lines changed

src/frontend/linkshell.cc

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

33
#include <getopt.h>
4+
#include <map>
45

56
#include "infinite_packet_queue.hh"
67
#include "drop_tail_packet_queue.hh"
@@ -10,6 +11,7 @@
1011
#include "red_packet_queue.hh"
1112
#include "link_queue.hh"
1213
#include "packetshell.cc"
14+
#include "tokenize.hh"
1315

1416
using namespace std;
1517

@@ -33,7 +35,7 @@ void usage_error( const string & program_name )
3335
throw runtime_error( "invalid arguments" );
3436
}
3537

36-
unique_ptr<AbstractPacketQueue> get_packet_queue( const string & type, const string & args, const string & program_name )
38+
unique_ptr<AbstractPacketQueue> get_packet_queue( const string & type, const map<string, string> & args, const string & program_name )
3739
{
3840
if ( type == "infinite" ) {
3941
return unique_ptr<AbstractPacketQueue>( new InfinitePacketQueue( args ) );
@@ -71,6 +73,26 @@ string shell_quote( const string & arg )
7173
return ret;
7274
}
7375

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

187209
link_shell_app.start_uplink( "[link] ", command,
188210
"Uplink", uplink_filename, uplink_logfile, repeat, meter_uplink, meter_uplink_delay,
189-
get_packet_queue( uplink_queue_type, uplink_queue_args, argv[ 0 ] ),
211+
get_packet_queue( uplink_queue_type, parse_queue_args(uplink_queue_args), argv[ 0 ] ),
190212
command_line );
191213

192214
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 ] ),
215+
get_packet_queue( downlink_queue_type, parse_queue_args(downlink_queue_args), argv[ 0 ] ),
194216
command_line );
195217

196218
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 & 3 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"
@@ -31,7 +32,7 @@ protected:
3132
const unsigned int size_in_packets ) const;
3233

3334
public:
34-
DroppingPacketQueue( const std::string & args );
35+
DroppingPacketQueue( const std::map<std::string, std::string> & args );
3536

3637
virtual void enqueue( QueuedPacket && p ) = 0;
3738

@@ -42,12 +43,13 @@ public:
4243
std::string to_string( void ) const override;
4344

4445
static std::string parse_number_arg(const std::string & args, const std::string & name, bool isfloat);
46+
4547
unsigned int size_bytes( void ) const override;
4648
unsigned int size_packets( void ) const override;
4749

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-
50-
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 );
5153
};
5254

5355
#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,6 +9,7 @@
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
{
@@ -17,7 +18,7 @@ private:
1718
int queue_size_in_bytes_ = 0, queue_size_in_packets_ = 0;
1819

1920
public:
20-
InfinitePacketQueue( const std::string & args )
21+
InfinitePacketQueue( const std::map<std::string, std::string> & args )
2122
{
2223
if ( not args.empty() ) {
2324
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)