Hi all!
I use ns-2.1b6 in a linux Mandrake 8.0 platform.
I try to create a new agent attached to node1 which simply sends packets with
a regular interval to another agent attached to node2 which recv and simply
discards the packets.
I have also created a new packet header and modified the file: packet.h,
ns-default,ns-packet,Makefile,cmu-trace.cc.
my problem is that when i run a simulation it gives me: SEGMENTATION FAULT
this is the code that i wrote, it is very simple but i don' t know why it
doesn't work. Please help me
thanks in advance!
****THIS IS THE TCL SCRIPT***********************************************
set ns [new Simulator]
set tracefd [open prova.tr w]
$ns trace-all $tracefd
set n0 [$ns node]
set n1 [$ns node]
$ns duplex-link $n0 $n1 1Mb 150ms DropTail
set pozzo [!
! new Agent/RXAGENT]
$ns attach-agent $n1 $pozzo
set sorgente [new Agent/NEWAGENT]
$ns attach-agent $n0 $sorgente
$sorgente set interval_ 0.3
$ns connect $sorgente $pozzo
$ns at 0.01 "$sorgente start_sending"
$ns at 20 "$sorgente stoppa"
$ns at 20.02 "puts \"NS EXITING ....\"; $ns halt"
$ns at 20.01 "stop"
proc stop {} {
global ns tracefd
close $tracefd
}
puts "Starting simulation... "
$ns run
*****************************************************************************
******************
THIS IS THE PACKET HEADER**********************************************
#ifndef ns_packprova_h
#define ns_packprova_h
#include <stdlib.h>
#include <string.h>
#include "agent.h"
#include "packet.h"
#include "address.h"
#include "ip!
.h! "
#include "timer-handler.h"
#define SEND_TIME 0
typedef enum {DATAP} mytype;
struct hdr_packpr {
int seqnumber;
double delay;
mytype type;
static int offset_;
inline static int& offset() { return offset_; }
inline static hdr_packpr* access(Packet* p) {
return (hdr_packpr*) p->access(offset_);
}
};
#endif
******************************************************************!
****! *******
*********************************
THIS IS THE AGENT THAT SEND PACKET (.H)
#ifndef ns_newagent_h
#define ns_newagent_h
#include "packprova.h"
class newagent;
class SENDTIMER:public TimerHandler{
public:
SENDTIMER(newagent *a):TimerHandler(){a_=a;}
protected:
void expire(Event *e);
newagent *a_;
};
class newagent:public Agent{
public:
newagent();
void timeout(int);
protected:
int command (int arcg, const char*const*argv);
void send_data();
double interval;
int seq;
int off_newp_;
SENDTIMER send_timer;
};
#endif
*****************************************************************************
********************* THIS IS THE AGENT THAT SEND PACKET (.CC)
#include "newagent.h"
#include <iostream.h>
int hdr_packpr::offset_;
static class NuovoheaderClass : public PacketHeaderClass{
public:
NuovoheaderClass(): PacketHeaderClass("Packe!
tHeade! r/NEWP",sizeof(hdr_packpr))
{
bind_offset(&hdr_packpr::offset_);
}
}class_newhdr;
static class NEWAgentClass:public TclClass{
public:
NEWAgentClass():TclClass("Agent/NEWAGENT"){}
TclObject* create(int,const char*const*){
return(new newagent());
}
}class_NEWagent;
newagent::newagent():Agent(PT_NEWP),send_timer(this),seq(0){
bind_time("interval_",&interval);
bind("off_newp_",&off_newp_);
}
void newagent::send_data(){
Packet *p=allocpkt();
hdr_packpr *hdrp=(hdr_packpr*)p->access(off_newp_);
hdrp->type=DATAP;
hdrp->seqnumber=++seq;
hdr!
p->de! lay=Scheduler::instance().clock();
send(p,0);
}
void SENDTIMER::expire(Event *e){
a_->timeout(SEND_TIME);
}
void newagent::timeout(int tno){
switch(tno){
case SEND_TIME:
{
send_data();
send_timer.resched(interval);
}
}
}
int newagent::command(int argc,const char*const*argv){
if(argc==2){
if (strcmp(argv[1],"start_sending")==0){
timeout(SEND_TIME);
return (TCL_OK);
}
if (strcmp(argv[1],"stoppa")==0){
send_timer.cancel();
return (TCL_OK);
}
}
return(Agent::command(argc,argv));
}
*****************************************************************************
************************************** THIS IS THE AGENT THAT RECV THE PACKET
(.H)
#ifndef ns_rxagent_h
#define ns_rxagent_h
#include "packprova.h"
class rxagent:public Agent{
public:
rxagent();
void recv(Packet*,Handler*);
int off_n!
ewp_;
}! ;
*****************************************************************************
******************* THIS IS THE AGENT THAT RECV THE PACKET (.CC)
#endif
#include "rxagent.h"
#include "iostream.h"
static class NuovoheaderClass : public PacketHeaderClass{
public:
NuovoheaderClass(): PacketHeaderClass("PacketHeader/NEWP",sizeof(hdr_packpr))
{
bind_offset(&hdr_packpr::offset_);
}
}class_newhdr;
static class RXAgentClass:public TclClass{
public:
RXAgentClass():TclClass("Agent/RXAGENT"){}
TclObject* create(int,const char*const*){
return(new rxagent());
}
}class! _RXagent;
rxagent::rxagent():Agent(PT_NEWP){
bind("off_newp_",&off_newp_);
}
void rxagent::recv(Packet *p,Handler*){
hdr_packpr *hdrp=(hdr_packpr*)p->access(off_newp_);
cout<<"numero di sequenza del pacchetto: " << hdrp->seqnumber << endl;
double time=Scheduler::instance().clock();
double t=time-hdrp->delay;
cout << " ritardo nell'attraversare la rete : "<< t << endl;
Packet::free(p);
}
-------------------------------------------------------
-------------------------------------------------------