[Click] Timers too fast

Frederic Van Quickenborne frederic.vanquickenborne at intec.ugent.be
Wed Oct 6 04:38:01 EDT 2004


Hi,

I prefer working with tasks.

Here you can find some code of an element that sends packets with a 
pre-defined _gap between them. _gap can be identified with the config file.

#include <click/config.h>
#include "packetshaper.hh"
#include <click/confparse.hh>
#include <click/error.hh>
#include <click/standard/scheduleinfo.hh>

uint32_t Time2Uint32_t(struct timeval a)
{
    return (a.tv_sec * 1000000 + a.tv_usec);
}

struct timeval Uint32_t2Time(uint32_t a)
{
    struct timeval time;
    time.tv_sec = a / 1000000;
    time.tv_usec = a % 1000000;
    return time;
}

CLICK_DECLS

PacketShaper::PacketShaper()
   : Element(1, 1), _task(this), _gap(1000)
{
   struct timeval now;
   click_gettimeofday(&now);
   exptime = now;
   MOD_INC_USE_COUNT;
}

int
PacketShaper::configure(Vector<String> &conf, ErrorHandler *errh)
{
   _active = true;
   if (cp_va_parse(conf, this, errh,
                   cpUnsigned, "interval in us. between packets", &_gap,
                   0) < 0)
     return -1;
   if (_gap <= 0)
     return errh->error("gap must be > 0");
   return 0;
}

int
PacketShaper::initialize(ErrorHandler *errh)
{
   ScheduleInfo::initialize_task(this, &_task, _active, errh);
   _signal =  Notifier::upstream_pull_signal(this, 0, &_task);
   return 0;
}

bool
PacketShaper::run_task()
{
   if (!_active)
     return false;

   int worked = 0;
   struct timeval now;
   click_gettimeofday(&now);

   if (timercmp(&exptime,&now,<) || timercmp(&exptime,&now,==)) {
     if (Packet *p = input(0).pull()) {
       struct timeval gapval = Uint32_t2Time(_gap);
       timeradd(&now,&gapval,&exptime);
//      click_chatter("sent now=%u",Time2Uint32_t(now));
       output(0).push(p);
       worked++;
     } else {
       if (!_signal)
         return worked > 0;
     }
   }

   _task.fast_reschedule();
   return worked > 0;
}

CLICK_ENDDECLS
EXPORT_ELEMENT(PacketShaper)

Most of the code I get from another existing element. I don't know which 
one it was.

Many regards,
Frederic.

At 09:26 6/10/2004 +0200, Bart Braem wrote:
>Hi,
>
>I'm trying to use timers to generate a constant flow of Hello messages from a
>node. These Hello messages should be sent at a very limited rate (1 per
>minute) so using RatedUnQueue is undesirable, right?
>
>For the moment my code works but the rate is way to high, I get hundreds of
>messages in 1 second instead of 1 every 10 seconds...
>The code looks like this:
>
>AODVHelloGenerator::AODVHelloGenerator():
>  Element(0,1),
>  timer(this)
>{
>  // CONSTRUCTOR MUST MOD_INC_USE_COUNT
>  MOD_INC_USE_COUNT;
>}
>int AODVHelloGenerator::initialize(ErrorHandler *errh)
>{
>  timer.initialize(this);
>  timer.schedule_now();
>  return 0;
>}
>void AODVHelloGenerator::run_timer(){
>  printf("packet sent\n");
>  fflush(stdout);
>  timer.reschedule_after_ms(10000);
>}
>
>Could someone explain me what I'm doing wrong here? Am I using timers the
>wrong way?
>
>Thanks
>Bart
>--
>dwars, studentenblad van de UA
>www.dwars.ua.ac.be
>_______________________________________________
>click mailing list
>click at amsterdam.lcs.mit.edu
>https://amsterdam.lcs.mit.edu/mailman/listinfo/click

==================================================================
Frederic Van Quickenborne
Department of Information Technology (INTEC)
Ghent University - IMEC
Sint-Pietersnieuwstraat 41, B-9000 Gent, Belgium
tel.: +32 (0)9-264 9957; tel. secr.: +32 (0)9-264 9970
fax: +32 (0)9-264 9960
Frederic.VanQuickenborne at intec.ugent.be
http://www.ibcn.intec.ugent.be



More information about the click mailing list