Commit 1abb3705 authored by Jiachen,Chen's avatar Jiachen,Chen

Modified TestTimer

parent 719e9000
/*
* File: TestTimer.cc
* Author: Jiachen Chen
*
* Created on April 30, 2019, 4:24 AM
*/
#include "TestTimer.hh"
#include <click/timestamp.hh>
CLICK_DECLS
#define TIMER_DEBUG
#ifdef TIMER_DEBUG
#define DEBUG(...) _DEBUG(__VA_ARGS__, "dummy")
#define _DEBUG(fmt, ...) printf("[%s.%s():%d] " fmt "%.0s\n", \
class_name(), __func__, __LINE__, __VA_ARGS__)
#define DEBUG_NO_CLASS(...) _DEBUG_NO_CLASS(__VA_ARGS__, "dummy")
#define _DEBUG_NO_CLASS(fmt, ...) printf("[%s():%d] " fmt "%.0s\n", \
__func__, __LINE__, __VA_ARGS__)
#else
#define DEBUG(...)
#endif
static void runPacketTimeout(Timer *timer, void *user_data);
struct TimeoutData {
Packet *p;
TestTimer *element;
};
TestTimer::TestTimer() : _timer(this) {
}
TestTimer::~TestTimer() = default;
int TestTimer::initialize(ErrorHandler* errh __attribute ((__unused__))) {
_timer.initialize(this);
_timer.schedule_after_sec(5);
return 0;
}
void TestTimer::push(int port, Packet* p) {
(void) port;
struct TimeoutData *data = new TimeoutData{p, this};
DEBUG("push, data=%p, pkt=%p", data, p);
Timer *t = new Timer(runPacketTimeout, data);
t->initialize(this);
// t->initialize(this);
t->schedule_after_msec(1500);
}
void TestTimer::run_timer(Timer* timer) {
DEBUG(">>> Element timer, timer=%p", timer);
timer->reschedule_after_sec(5);
}
void TestTimer::handle_timeout(Packet* p) {
DEBUG("timeout, pkt=%p", p);
p->kill();
}
static void runPacketTimeout(Timer *timer, void *user_data) {
DEBUG_NO_CLASS("Static run timer function, timer=%p, data=%p", timer, user_data);
timer->clear();
delete timer;
struct TimeoutData *d = static_cast<struct TimeoutData *>(user_data);
d->element->handle_timeout(d->p);
delete d;
}
CLICK_ENDDECLS
EXPORT_ELEMENT(TestTimer)
\ No newline at end of file
require(package "click_for_comnet");
test :: TestTimer;
TimedSource(INTERVAL 1s, DATA "HELLO", LIMIT 10, STOP false)
-> test;
/*
* File: TestTimer.hh
* Author: Jiachen Chen
*
* Created on April 30, 2019, 4:24 AM
*/
#ifndef TESTTIMER_HH
#define TESTTIMER_HH
#include <click/config.h>
#include <click/element.hh>
#include <click/glue.hh>
#include <click/timer.hh>
CLICK_DECLS
class TestTimer : public Element {
public:
TestTimer();
~TestTimer();
const char* class_name() const { return "TestTimer"; }
const char* processing() const { return PUSH; }
const char* port_count() const { return "-1/0"; }
int initialize(ErrorHandler* errh) override;
void push(int port, Packet* p);
void run_timer(Timer* timer) override;
void handle_timeout(Packet *p);
private:
Timer _timer;
};
CLICK_ENDDECLS
#endif /* TESTTIMER_HH */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment