Skip to content

Commit

Permalink
Refactored the arguments to be a ParsedArguments class.
Browse files Browse the repository at this point in the history
  • Loading branch information
squidarth authored and Sidharth Shanker committed Nov 17, 2018
1 parent 421f42f commit fee2dc3
Show file tree
Hide file tree
Showing 13 changed files with 92 additions and 51 deletions.
11 changes: 6 additions & 5 deletions src/frontend/linkshell.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "link_queue.hh"
#include "packetshell.cc"
#include "tokenize.hh"
#include "parsed_arguments.hh"

using namespace std;

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

throw runtime_error( "invalid arguments" );
}

unique_ptr<AbstractPacketQueue> get_packet_queue( const string & type, const map<string, string> & args, const string & program_name )
unique_ptr<AbstractPacketQueue> get_packet_queue( const string & type, ParsedArguments args, const string & program_name )
{
if ( type == "infinite" ) {
return unique_ptr<AbstractPacketQueue>( new InfinitePacketQueue( args ) );
Expand Down Expand Up @@ -73,10 +74,10 @@ string shell_quote( const string & arg )
return ret;
}

map<string, string> parse_queue_args( const string & arg) {
ParsedArguments parse_queue_args( const string & arg) {
map<string, string> argMap = map<string, string>();
if (arg.size() == 0) {
return argMap;
return ParsedArguments( argMap );
}
vector<string> argList = split(arg, ",");

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

return argMap;
return ParsedArguments( argMap );
}

int main( int argc, char *argv[] )
Expand Down
65 changes: 65 additions & 0 deletions src/frontend/parsed_arguments.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#ifndef PARSED_ARGUMENTS_HH
#define PARSED_ARGUMENTS_HH

#include <map>
#include <string>
#include "util.hh"
#include "ezio.hh"

using namespace std;

class ParsedArguments
{
private:
map<string, string> argMap_;
public:
ParsedArguments(map<string, string> argMap ): argMap_(argMap)
{
}

double get_float_arg(const string & argName)
{
if (argMap_.count(argName) > 0) {
return myatof(argMap_.at(argName));
} else {
throw runtime_error("Missing argument " + argName);
}
}

double get_float_arg(const string & argName, double defaultArg)
{
if (argMap_.count(argName) > 0) {
return myatof(argMap_.at(argName));
} else {
return defaultArg;
}
}

int get_int_arg(const string & argName)
{

if (argMap_.count(argName) > 0) {
return myatoi(argMap_.at(argName));
} else {
throw runtime_error("Missing argument " + argName);
}
}

int get_int_arg(const string & argName, int defaultArg)
{
if (argMap_.count(argName) > 0) {
return myatoi(argMap_.at(argName));
} else {
return defaultArg;
}

}

bool empty()
{
return argMap_.empty();
}

};

#endif /* PARSED_ARGUMENTS_HH */
2 changes: 1 addition & 1 deletion src/packet/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ libpacket_a_SOURCES = packetshell.hh packetshell.cc queued_packet.hh \
codel_packet_queue.cc codel_packet_queue.hh \
red_packet_queue.cc red_packet_queue.hh \
pie_packet_queue.cc pie_packet_queue.hh \
bindworkaround.hh
bindworkaround.hh ../frontend/parsed_arguments.hh
1 change: 1 addition & 0 deletions src/packet/abstract_packet_queue.hh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <string>

#include "queued_packet.hh"
#include "../frontend/parsed_arguments.hh"

class AbstractPacketQueue
{
Expand Down
9 changes: 3 additions & 6 deletions src/packet/codel_packet_queue.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,16 @@

using namespace std;

CODELPacketQueue::CODELPacketQueue( const map<string, string> & args )
CODELPacketQueue::CODELPacketQueue( ParsedArguments & args )
: DroppingPacketQueue(args),
target_ ( get_int_arg( args, "target") ),
interval_ ( get_int_arg( args, "interval") ),
target_ ( args.get_int_arg("target") ),
interval_ ( args.get_int_arg( "interval") ),
first_above_time_ ( 0 ),
drop_next_( 0 ),
count_ ( 0 ),
lastcount_ ( 0 ),
dropping_ ( 0 )
{
if ( target_ == 0 || interval_ == 0 ) {
throw runtime_error( "CoDel queue must have target and interval arguments." );
}
}

//NOTE: CoDel makes drop decisions at dequeueing.
Expand Down
2 changes: 1 addition & 1 deletion src/packet/codel_packet_queue.hh
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ private:
uint64_t control_law ( uint64_t t, uint32_t count );

public:
CODELPacketQueue( const std::map<std::string, std::string> & args );
CODELPacketQueue( ParsedArguments & args );

void enqueue( QueuedPacket && p ) override;

Expand Down
22 changes: 3 additions & 19 deletions src/packet/dropping_packet_queue.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,15 @@

using namespace std;

DroppingPacketQueue::DroppingPacketQueue( const map<string, string> & args )
: packet_limit_( get_int_arg( args, "packets" ) ),
byte_limit_( get_int_arg( args, "bytes" ) )
DroppingPacketQueue::DroppingPacketQueue( ParsedArguments & args )
: packet_limit_( args.get_int_arg( "packets", 0 ) ),
byte_limit_( args.get_int_arg( "bytes", 0 ) )
{
if ( packet_limit_ == 0 and byte_limit_ == 0 ) {
throw runtime_error( "Dropping queue must have a byte or packet limit." );
}
}

unsigned int DroppingPacketQueue::get_int_arg(const map<string, string> & args, const string & name) {
if (args.count(name) > 0) {
return myatoi(args.at(name));
} else {
return 0;
}
}

double DroppingPacketQueue::get_float_arg(const map<string, string> & args, const string & name) {
if (args.count(name) > 0) {
return myatof(args.at(name));
} else {
return 0;
}
}

QueuedPacket DroppingPacketQueue::dequeue( void )
{
assert( not internal_queue_.empty() );
Expand Down
2 changes: 1 addition & 1 deletion src/packet/dropping_packet_queue.hh
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ protected:
const unsigned int size_in_packets ) const;

public:
DroppingPacketQueue( const std::map<std::string, std::string> & args );
DroppingPacketQueue( ParsedArguments & args );

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

Expand Down
2 changes: 1 addition & 1 deletion src/packet/infinite_packet_queue.hh
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ private:
int queue_size_in_bytes_ = 0, queue_size_in_packets_ = 0;

public:
InfinitePacketQueue( const std::map<std::string, std::string> & args )
InfinitePacketQueue( ParsedArguments & args )
{
if ( not args.empty() ) {
throw std::runtime_error( "InfinitePacketQueue does not take arguments." );
Expand Down
6 changes: 3 additions & 3 deletions src/packet/pie_packet_queue.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ using namespace std;

#define DQ_COUNT_INVALID (uint32_t)-1

PIEPacketQueue::PIEPacketQueue( const map<string, string> & args )
PIEPacketQueue::PIEPacketQueue( ParsedArguments & args )
: DroppingPacketQueue(args),
qdelay_ref_ ( get_int_arg( args, "qdelay_ref" ) ),
max_burst_ ( get_int_arg( args, "max_burst" ) ),
qdelay_ref_ ( args.get_int_arg( "qdelay_ref" ) ),
max_burst_ ( args.get_int_arg( "max_burst" ) ),
alpha_ ( 0.125 ),
beta_ ( 1.25 ),
t_update_ ( 30 ),
Expand Down
2 changes: 1 addition & 1 deletion src/packet/pie_packet_queue.hh
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ private:
void calculate_drop_prob ( void );

public:
PIEPacketQueue( const std::map<std::string, std::string> & args );
PIEPacketQueue( ParsedArguments & args );

void enqueue( QueuedPacket && p ) override;

Expand Down
17 changes: 5 additions & 12 deletions src/packet/red_packet_queue.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,18 @@

using namespace std;

REDPacketQueue::REDPacketQueue( const map<string, string> & args)
REDPacketQueue::REDPacketQueue( ParsedArguments & args)
: DroppingPacketQueue(args),
wq_(get_float_arg(args, "wq")),
min_thresh_(get_float_arg(args, "minthresh")),
max_thresh_(get_float_arg(args, "maxthresh")),
transmission_time_(get_int_arg(args, "transmission_time")),
wq_(args.get_float_arg("wq")),
min_thresh_(args.get_float_arg("minthresh")),
max_thresh_(args.get_float_arg("maxthresh")),
transmission_time_(args.get_int_arg("transmission_time")),
time_at_zero_q_(0),
prng_( random_device()() ),
drop_dist_ (0, 1),
current_random_val_(0),
count_(0)
{
if (packet_limit_ == 0) {
throw runtime_error( "RED queue must have packet limit." );
}

if ( wq_ == 0.0 || min_thresh_ == 0.0 || max_thresh_ == 0.0 || transmission_time_ == 0 ) {
throw runtime_error( "RED queue must have wq, minthresh, maxthresh, and transmission_time arguments." );
}

}

Expand Down
2 changes: 1 addition & 1 deletion src/packet/red_packet_queue.hh
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private:
unsigned int max_queue_depth_packets() const;

public:
REDPacketQueue( const std::map<std::string, std::string> & args );
REDPacketQueue( ParsedArguments & args );
QueuedPacket dequeue( void ) override;
void enqueue( QueuedPacket && p ) override;
};
Expand Down

0 comments on commit fee2dc3

Please sign in to comment.