Protected Member Functions | Protected Attributes

NAMTraceWriter Class Reference

#include <NAMTraceWriter.h>

Inheritance diagram for NAMTraceWriter:
INotifiable

List of all members.

Protected Member Functions

virtual void recordNodeEvent (const char *state, const char *shape)
virtual void recordLinkEvent (int peernamid, double datarate, simtime_t delay, const char *state)
virtual void recordPacketEvent (char event, int peernamid, cPacket *msg)
virtual int numInitStages () const
virtual void initialize (int stage)
virtual ~NAMTraceWriter ()
virtual void receiveChangeNotification (int category, const cPolymorphic *details)

Protected Attributes

int namid
NAMTracent

Detailed Description

Writes a "nam" trace.

Definition at line 35 of file NAMTraceWriter.h.


Constructor & Destructor Documentation

NAMTraceWriter::~NAMTraceWriter (  )  [protected, virtual]

Definition at line 98 of file NAMTraceWriter.cc.

{
/*FIXME this will crash if the "nt" module gets cleaned up sooner than this one
    if (nt && nt->isEnabled())
    {
        recordNodeEvent("DOWN", "circle");
    }
*/
}


Member Function Documentation

void NAMTraceWriter::initialize ( int  stage  )  [protected, virtual]

Definition at line 31 of file NAMTraceWriter.cc.

{
    if (stage==1)  // let NAMTrace module initialize in stage 0
    {
        // get pointer to the NAMTrace module
        cModule *namMod = simulation.getModuleByPath("nam");
        if (!namMod)
        {
            nt = NULL;
            EV << "NAMTraceWriter: nam module not found, no trace will be written\n";
            return;
        }

        // store ptr to namtrace module
        nt = check_and_cast<NAMTrace*>(namMod);

        // register given namid; -1 means autoconfigure
        int namid0 = par("namid");
        cModule *node = getParentModule();  // the host or router
        namid = nt->assignNamId(node, namid0);
        if (namid0==-1)
            par("namid") = namid;  // let parameter reflect autoconfigured namid

        // write "node" entry to the trace
        if (nt->isEnabled())
            recordNodeEvent("UP", "circle");

        // subscribe to the interesting notifications
        NotificationBoard *nb = NotificationBoardAccess().get();
        nb->subscribe(this, NF_NODE_FAILURE);
        nb->subscribe(this, NF_NODE_RECOVERY);
        nb->subscribe(this, NF_PP_TX_BEGIN);
        nb->subscribe(this, NF_PP_RX_END);
        nb->subscribe(this, NF_L2_Q_DROP);
    }
    else if (stage==2 && nt!=NULL && nt->isEnabled())
    {
        // write "link" entries
        IInterfaceTable *ift = InterfaceTableAccess().get();
        cModule *node = getParentModule();  // the host or router
        for (int i=0; i<ift->getNumInterfaces(); i++)
        {
            // skip loopback interfaces
            InterfaceEntry *ie = ift->getInterface(i);
            if (ie->isLoopback()) continue;
            if (!ie->isPointToPoint()) continue; // consider pont-to-point links only

            // fill in peerNamIds in InterfaceEntries
            cGate *outgate = node->gate(ie->getNodeOutputGateId());
            if (!outgate || !outgate->getNextGate()) continue;
            cModule *peernode = outgate->getNextGate()->getOwnerModule(); // FIXME not entirely correct: what if a subnet is "boxed"?
            cModule *peerwriter = peernode->getSubmodule("namTrace");
            if (!peerwriter) error("module %s doesn't have a submodule named namTrace", peernode->getFullPath().c_str());
            int peernamid = peerwriter->par("namid");
            ie->setPeerNamId(peernamid);

            // find delay
            simtime_t delay = 0;
            cDatarateChannel *chan = dynamic_cast<cDatarateChannel*>(outgate->getChannel());
            if (chan) delay = chan->getDelay();

            // write link entry into trace
            recordLinkEvent(peernamid, ie->getDatarate(), delay, "UP");
        }
    }
}

virtual int NAMTraceWriter::numInitStages (  )  const [inline, protected, virtual]

Definition at line 47 of file NAMTraceWriter.h.

{return 3;}

void NAMTraceWriter::receiveChangeNotification ( int  category,
const cPolymorphic *  details 
) [protected, virtual]

Redefined INotifiable method. Called by NotificationBoard on changes.

Implements INotifiable.

Definition at line 109 of file NAMTraceWriter.cc.

{
    // don't do anything if global NAMTrace module doesn't exist or does not have a file open
    if (!nt || !nt->isEnabled())
        return;

    printNotificationBanner(category, details);

    // process notification
    if (category==NF_PP_TX_BEGIN || category==NF_PP_RX_END || category==NF_L2_Q_DROP)
    {
        TxNotifDetails *d = check_and_cast<TxNotifDetails *>(details);
        int peernamid = d->getInterfaceEntry()->getPeerNamId();
        cPacket *msg = d->getPacket();

        switch(category)
        {
            case NF_PP_TX_BEGIN: recordPacketEvent('h', peernamid, msg); break;
            case NF_PP_RX_END:   recordPacketEvent('r', peernamid, msg); break;
            case NF_L2_Q_DROP:   recordPacketEvent('d', peernamid, msg); break;
        }
    }
    else
    {
        switch(category)
        {
            case NF_NODE_FAILURE: break; // TODO
            case NF_NODE_RECOVERY: break; // TODO
        }
    }
}

void NAMTraceWriter::recordLinkEvent ( int  peernamid,
double  datarate,
simtime_t  delay,
const char *  state 
) [protected, virtual]

Definition at line 153 of file NAMTraceWriter.cc.

Referenced by initialize().

{
    ASSERT(nt && nt->isEnabled());
    std::ostream& out = nt->out();

    // link entry (to be registered ON ONE END ONLY! This also means that
    // ns2 thinks that datarate and delay must be the same in both directions)
    if (namid < peernamid)
        out << "l -t * -s " << namid << " -d " << peernamid
            << " -S " << state << " -r " << (int)datarate << " -D " << delay << endl;

    // queue entry
    out << "q -t * -s " << namid << " -d " << peernamid << " -a 0 " << endl;
}

void NAMTraceWriter::recordNodeEvent ( const char *  state,
const char *  shape 
) [protected, virtual]

Definition at line 141 of file NAMTraceWriter.cc.

Referenced by initialize().

{
    ASSERT(nt && nt->isEnabled());
    std::ostream& out = nt->out();
    out << "n -t ";
    if (simTime() == 0.0)
        out << "*";
    else
        out << simTime();
    out << " -s " << namid << " -a " << namid << " -S " << state << " -v " << shape << endl;
}

void NAMTraceWriter::recordPacketEvent ( char  event,
int  peernamid,
cPacket *  msg 
) [protected, virtual]

Definition at line 168 of file NAMTraceWriter.cc.

Referenced by receiveChangeNotification().

{
    ASSERT(nt && nt->isEnabled());
    std::ostream& out = nt->out();

    int size = msg->getByteLength();
    int color = 0;
    for (cPacket *em = msg; em; em = em->getEncapsulatedMsg())
        if (em->hasPar("color"))
            {color = em->par("color").longValue(); break;}

    out << event << " -t " << simTime();
    if (event=='h')
        out << " -s " << namid << " -d " << peernamid;
    else
        out << " -s " << peernamid << " -d " << namid;

    out << " -e " << size << " -a " << color << endl;
}


Member Data Documentation

int NAMTraceWriter::namid [protected]

Definition at line 38 of file NAMTraceWriter.h.

Referenced by initialize(), recordLinkEvent(), recordNodeEvent(), and recordPacketEvent().


The documentation for this class was generated from the following files: