Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include <stdio.h>
00019 #include <string.h>
00020 #include <omnetpp.h>
00021 #include "NAMTrace.h"
00022 #include "NAMTraceWriter.h"
00023 #include "NotificationBoard.h"
00024 #include "TxNotifDetails.h"
00025 #include "IInterfaceTable.h"
00026 #include "InterfaceTableAccess.h"
00027
00028 Define_Module(NAMTraceWriter);
00029
00030
00031 void NAMTraceWriter::initialize(int stage)
00032 {
00033 if (stage==1)
00034 {
00035
00036 cModule *namMod = simulation.getModuleByPath("nam");
00037 if (!namMod)
00038 {
00039 nt = NULL;
00040 EV << "NAMTraceWriter: nam module not found, no trace will be written\n";
00041 return;
00042 }
00043
00044
00045 nt = check_and_cast<NAMTrace*>(namMod);
00046
00047
00048 int namid0 = par("namid");
00049 cModule *node = getParentModule();
00050 namid = nt->assignNamId(node, namid0);
00051 if (namid0==-1)
00052 par("namid") = namid;
00053
00054
00055 if (nt->isEnabled())
00056 recordNodeEvent("UP", "circle");
00057
00058
00059 NotificationBoard *nb = NotificationBoardAccess().get();
00060 nb->subscribe(this, NF_NODE_FAILURE);
00061 nb->subscribe(this, NF_NODE_RECOVERY);
00062 nb->subscribe(this, NF_PP_TX_BEGIN);
00063 nb->subscribe(this, NF_PP_RX_END);
00064 nb->subscribe(this, NF_L2_Q_DROP);
00065 }
00066 else if (stage==2 && nt!=NULL && nt->isEnabled())
00067 {
00068
00069 IInterfaceTable *ift = InterfaceTableAccess().get();
00070 cModule *node = getParentModule();
00071 for (int i=0; i<ift->getNumInterfaces(); i++)
00072 {
00073
00074 InterfaceEntry *ie = ift->getInterface(i);
00075 if (ie->isLoopback()) continue;
00076 if (!ie->isPointToPoint()) continue;
00077
00078
00079 cGate *outgate = node->gate(ie->getNodeOutputGateId());
00080 if (!outgate || !outgate->getNextGate()) continue;
00081 cModule *peernode = outgate->getNextGate()->getOwnerModule();
00082 cModule *peerwriter = peernode->getSubmodule("namTrace");
00083 if (!peerwriter) error("module %s doesn't have a submodule named namTrace", peernode->getFullPath().c_str());
00084 int peernamid = peerwriter->par("namid");
00085 ie->setPeerNamId(peernamid);
00086
00087
00088 simtime_t delay = 0;
00089 cDatarateChannel *chan = dynamic_cast<cDatarateChannel*>(outgate->getChannel());
00090 if (chan) delay = chan->getDelay();
00091
00092
00093 recordLinkEvent(peernamid, ie->getDatarate(), delay, "UP");
00094 }
00095 }
00096 }
00097
00098 NAMTraceWriter::~NAMTraceWriter()
00099 {
00100
00101
00102
00103
00104
00105
00106 }
00107
00108
00109 void NAMTraceWriter::receiveChangeNotification(int category, const cPolymorphic *details)
00110 {
00111
00112 if (!nt || !nt->isEnabled())
00113 return;
00114
00115 printNotificationBanner(category, details);
00116
00117
00118 if (category==NF_PP_TX_BEGIN || category==NF_PP_RX_END || category==NF_L2_Q_DROP)
00119 {
00120 TxNotifDetails *d = check_and_cast<TxNotifDetails *>(details);
00121 int peernamid = d->getInterfaceEntry()->getPeerNamId();
00122 cPacket *msg = d->getPacket();
00123
00124 switch(category)
00125 {
00126 case NF_PP_TX_BEGIN: recordPacketEvent('h', peernamid, msg); break;
00127 case NF_PP_RX_END: recordPacketEvent('r', peernamid, msg); break;
00128 case NF_L2_Q_DROP: recordPacketEvent('d', peernamid, msg); break;
00129 }
00130 }
00131 else
00132 {
00133 switch(category)
00134 {
00135 case NF_NODE_FAILURE: break;
00136 case NF_NODE_RECOVERY: break;
00137 }
00138 }
00139 }
00140
00141 void NAMTraceWriter::recordNodeEvent(const char *state, const char *shape)
00142 {
00143 ASSERT(nt && nt->isEnabled());
00144 std::ostream& out = nt->out();
00145 out << "n -t ";
00146 if (simTime() == 0.0)
00147 out << "*";
00148 else
00149 out << simTime();
00150 out << " -s " << namid << " -a " << namid << " -S " << state << " -v " << shape << endl;
00151 }
00152
00153 void NAMTraceWriter::recordLinkEvent(int peernamid, double datarate, simtime_t delay, const char *state)
00154 {
00155 ASSERT(nt && nt->isEnabled());
00156 std::ostream& out = nt->out();
00157
00158
00159
00160 if (namid < peernamid)
00161 out << "l -t * -s " << namid << " -d " << peernamid
00162 << " -S " << state << " -r " << (int)datarate << " -D " << delay << endl;
00163
00164
00165 out << "q -t * -s " << namid << " -d " << peernamid << " -a 0 " << endl;
00166 }
00167
00168 void NAMTraceWriter::recordPacketEvent(char event, int peernamid, cPacket *msg)
00169 {
00170 ASSERT(nt && nt->isEnabled());
00171 std::ostream& out = nt->out();
00172
00173 int size = msg->getByteLength();
00174 int color = 0;
00175 for (cPacket *em = msg; em; em = em->getEncapsulatedMsg())
00176 if (em->hasPar("color"))
00177 {color = em->par("color").longValue(); break;}
00178
00179 out << event << " -t " << simTime();
00180 if (event=='h')
00181 out << " -s " << namid << " -d " << peernamid;
00182 else
00183 out << " -s " << peernamid << " -d " << namid;
00184
00185 out << " -e " << size << " -a " << color << endl;
00186 }
00187