[ns] help! Problem of Timer

=?gb2312?B?0O235Q==?= atu82 at 163.com
Fri Sep 1 20:28:22 PDT 2006


Dear NS users:
	 I had a problem on Timer.
     In ex-linkage.cc, I defined a Timer class MyTimer:public TimerHandler, then I wanto to use this timer in the function of MyPrivateFunction(). the function expire() is very simple, just print some characters like "Time Expire...". But the problem is that I can't find the "Time Expire..." characters in the result. Why? How to use the Timer? 
     The C++ codes and Tcl codes are as follows£º

//ex-linkage.cc
#include <stdio.h>
#include <string.h>
#include "scheduler.h"
#include "agent.h"
#include "timer-handler.h"

class MyAgent;

class MyTimer:public TimerHandler
{
public:
	MyTimer(MyAgent* t):TimerHandler(), t_(t){}
	inline virtual void expire(Event*);
protected:
	MyAgent* t_;
};


class MyAgent : public Agent
{
public:
	MyAgent();
	void MyPrivateFunction();	
protected:
	int command(int argc, const char* const*);
	int my_var1;
	double my_var2;
	MyTimer timer;
	
};

static class MyAgentClass : public TclClass
{
public:
	MyAgentClass():TclClass("Agent/MyAgent") {}
	TclObject *create(int, const char* const*)
	{
		return (new MyAgent);
	}
}class_my_agent;

void MyTimer::expire(Event* e)
{
	printf("Time Expired...\n");
	resched(0.05);
}

MyAgent::MyAgent():Agent(PT_UDP),timer(this)
{
	bind("my_var1", &my_var1);
	bind("my_var2", &my_var2);
	my_var1 = 11;
	my_var2 = 22.0;
}
int MyAgent::command(int argc, const char* const* argv)
{
	if(argc == 2)
	{
		if((strcmp("call-my-private-function", argv[1])) == 0)
		{
			MyPrivateFunction();
			return(TCL_OK);
		}
	}	
	return(Agent::command(argc, argv));
}
void MyAgent::MyPrivateFunction()
{
	printf("Message from MyPrivateFunction:\n");
	printf("my_var1: %d\n", my_var1);
	printf("my_var2: %f\n", my_var2);

	Tcl& tcl = Tcl::instance();
	tcl.eval("puts \"Message from MyPrivateFunction:\n\"");
	tcl.evalf("puts \"my_var1 = %d\n\"", my_var1); 
	tcl.evalf("puts \"my_var2 = %f\n\"", my_var2); 
	printf("1111111111111\n");
	timer.sched(0.05);
	printf("2222222222222\n");
}

#ex-linkage.tcl
set ns [new Simulator]
set myagent [new Agent/MyAgent]
$myagent set my_var1 50
$myagent set my_var2 100.0
$myagent call-my-private-function

and the result is that:
Message from MyPrivateFunction:
my_var1: 50
my_var2: 100.000000
Message from MyPrivateFunction:

my_var1 = 50

my_var2 = 100.000000

1111111111111
2222222222222

Problem:
Where is the character "Time Expired..."???
	
Thanks!

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡                                                Yours Sincerely
                                                                 Howie
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡                                                atu82 at 163.com
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡                                             2006-09-01



More information about the Ns-users mailing list