Skip to content

Commit a100967

Browse files
squidarthSidharth Shanker
authored andcommitted
Refactored the arguments to be a ParsedArguments class.
1 parent ddba279 commit a100967

13 files changed

+92
-51
lines changed

src/frontend/linkshell.cc

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "link_queue.hh"
1313
#include "packetshell.cc"
1414
#include "tokenize.hh"
15+
#include "parsed_arguments.hh"
1516

1617
using namespace std;
1718

@@ -27,15 +28,15 @@ void usage_error( const string & program_name )
2728
cerr << " --uplink-queue=QUEUE_TYPE --downlink-queue=QUEUE_TYPE" << endl;
2829
cerr << " --uplink-queue-args=QUEUE_ARGS --downlink-queue-args=QUEUE_ARGS" << endl;
2930
cerr << endl;
30-
cerr << " QUEUE_TYPE = infinite | droptail | drophead | codel | pie" << endl;
31+
cerr << " QUEUE_TYPE = infinite | droptail | drophead | codel | pie | red" << endl;
3132
cerr << " QUEUE_ARGS = \"NAME=NUMBER[, NAME2=NUMBER2, ...]\"" << endl;
3233
cerr << " (with NAME = bytes | packets | target | interval | qdelay_ref | max_burst)" << endl;
3334
cerr << " target, interval, qdelay_ref, max_burst are in milli-second" << endl << endl;
3435

3536
throw runtime_error( "invalid arguments" );
3637
}
3738

38-
unique_ptr<AbstractPacketQueue> get_packet_queue( const string & type, const map<string, string> & args, const string & program_name )
39+
unique_ptr<AbstractPacketQueue> get_packet_queue( const string & type, ParsedArguments args, const string & program_name )
3940
{
4041
if ( type == "infinite" ) {
4142
return unique_ptr<AbstractPacketQueue>( new InfinitePacketQueue( args ) );
@@ -73,10 +74,10 @@ string shell_quote( const string & arg )
7374
return ret;
7475
}
7576

76-
map<string, string> parse_queue_args( const string & arg) {
77+
ParsedArguments parse_queue_args( const string & arg) {
7778
map<string, string> argMap = map<string, string>();
7879
if (arg.size() == 0) {
79-
return argMap;
80+
return ParsedArguments( argMap );
8081
}
8182
vector<string> argList = split(arg, ",");
8283

@@ -90,7 +91,7 @@ map<string, string> parse_queue_args( const string & arg) {
9091
argMap.insert(pair<string, string>(argParts[0], argParts[1]));
9192
}
9293

93-
return argMap;
94+
return ParsedArguments( argMap );
9495
}
9596

9697
int main( int argc, char *argv[] )

src/frontend/parsed_arguments.hh

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#ifndef PARSED_ARGUMENTS_HH
2+
#define PARSED_ARGUMENTS_HH
3+
4+
#include <map>
5+
#include <string>
6+
#include "util.hh"
7+
#include "ezio.hh"
8+
9+
using namespace std;
10+
11+
class ParsedArguments
12+
{
13+
private:
14+
map<string, string> argMap_;
15+
public:
16+
ParsedArguments(map<string, string> argMap ): argMap_(argMap)
17+
{
18+
}
19+
20+
double get_float_arg(const string & argName)
21+
{
22+
if (argMap_.count(argName) > 0) {
23+
return myatof(argMap_.at(argName));
24+
} else {
25+
throw runtime_error("Missing argument " + argName);
26+
}
27+
}
28+
29+
double get_float_arg(const string & argName, double defaultArg)
30+
{
31+
if (argMap_.count(argName) > 0) {
32+
return myatof(argMap_.at(argName));
33+
} else {
34+
return defaultArg;
35+
}
36+
}
37+
38+
int get_int_arg(const string & argName)
39+
{
40+
41+
if (argMap_.count(argName) > 0) {
42+
return myatoi(argMap_.at(argName));
43+
} else {
44+
throw runtime_error("Missing argument " + argName);
45+
}
46+
}
47+
48+
int get_int_arg(const string & argName, int defaultArg)
49+
{
50+
if (argMap_.count(argName) > 0) {
51+
return myatoi(argMap_.at(argName));
52+
} else {
53+
return defaultArg;
54+
}
55+
56+
}
57+
58+
bool empty()
59+
{
60+
return argMap_.empty();
61+
}
62+
63+
};
64+
65+
#endif /* PARSED_ARGUMENTS_HH */

src/packet/Makefile.am

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ libpacket_a_SOURCES = packetshell.hh packetshell.cc queued_packet.hh \
99
codel_packet_queue.cc codel_packet_queue.hh \
1010
red_packet_queue.cc red_packet_queue.hh \
1111
pie_packet_queue.cc pie_packet_queue.hh \
12-
bindworkaround.hh
12+
bindworkaround.hh ../frontend/parsed_arguments.hh

src/packet/abstract_packet_queue.hh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <string>
77

88
#include "queued_packet.hh"
9+
#include "../frontend/parsed_arguments.hh"
910

1011
class AbstractPacketQueue
1112
{

src/packet/codel_packet_queue.cc

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

66
using namespace std;
77

8-
CODELPacketQueue::CODELPacketQueue( const map<string, string> & args )
8+
CODELPacketQueue::CODELPacketQueue( ParsedArguments & args )
99
: DroppingPacketQueue(args),
10-
target_ ( get_int_arg( args, "target") ),
11-
interval_ ( get_int_arg( args, "interval") ),
10+
target_ ( args.get_int_arg("target") ),
11+
interval_ ( args.get_int_arg( "interval") ),
1212
first_above_time_ ( 0 ),
1313
drop_next_( 0 ),
1414
count_ ( 0 ),
1515
lastcount_ ( 0 ),
1616
dropping_ ( 0 )
1717
{
18-
if ( target_ == 0 || interval_ == 0 ) {
19-
throw runtime_error( "CoDel queue must have target and interval arguments." );
20-
}
2118
}
2219

2320
//NOTE: CoDel makes drop decisions at dequeueing.

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::map<std::string, std::string> & args );
48+
CODELPacketQueue( ParsedArguments & args );
4949

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

src/packet/dropping_packet_queue.cc

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

99
using namespace std;
1010

11-
DroppingPacketQueue::DroppingPacketQueue( const map<string, string> & args )
12-
: packet_limit_( get_int_arg( args, "packets" ) ),
13-
byte_limit_( get_int_arg( args, "bytes" ) )
11+
DroppingPacketQueue::DroppingPacketQueue( ParsedArguments & args )
12+
: packet_limit_( args.get_int_arg( "packets", 0 ) ),
13+
byte_limit_( args.get_int_arg( "bytes", 0 ) )
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-
3620
QueuedPacket DroppingPacketQueue::dequeue( void )
3721
{
3822
assert( not internal_queue_.empty() );

src/packet/dropping_packet_queue.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ protected:
3232
const unsigned int size_in_packets ) const;
3333

3434
public:
35-
DroppingPacketQueue( const std::map<std::string, std::string> & args );
35+
DroppingPacketQueue( ParsedArguments & args );
3636

3737
virtual void enqueue( QueuedPacket && p ) = 0;
3838

src/packet/infinite_packet_queue.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ private:
1818
int queue_size_in_bytes_ = 0, queue_size_in_packets_ = 0;
1919

2020
public:
21-
InfinitePacketQueue( const std::map<std::string, std::string> & args )
21+
InfinitePacketQueue( ParsedArguments & args )
2222
{
2323
if ( not args.empty() ) {
2424
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 map<string, string> & args )
11+
PIEPacketQueue::PIEPacketQueue( ParsedArguments & args )
1212
: DroppingPacketQueue(args),
13-
qdelay_ref_ ( get_int_arg( args, "qdelay_ref" ) ),
14-
max_burst_ ( get_int_arg( args, "max_burst" ) ),
13+
qdelay_ref_ ( args.get_int_arg( "qdelay_ref" ) ),
14+
max_burst_ ( args.get_int_arg( "max_burst" ) ),
1515
alpha_ ( 0.125 ),
1616
beta_ ( 1.25 ),
1717
t_update_ ( 30 ),

0 commit comments

Comments
 (0)