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 "NAMTrace.h"
00019 #include <errno.h>
00020 
00021 Define_Module(NAMTrace);
00022 
00023 NAMTrace::NAMTrace()
00024 {
00025     nams = NULL;
00026 }
00027 
00028 NAMTrace::~NAMTrace()
00029 {
00030     if (nams)
00031     {
00032         nams->close();
00033         delete nams;
00034     }
00035 }
00036 
00037 void NAMTrace::initialize()
00038 {
00039     lastnamid = 0;
00040     nams = NULL;
00041     const char *namlog = par("logfile");
00042     if (namlog && namlog[0])
00043     {
00044         EV << "nam tracing enabled (file " << namlog << ")" << endl;
00045 
00046         
00047         if (unlink(namlog)!=0 && errno!=ENOENT)
00048             error("cannot remove old `%s' file: %s", namlog, strerror(errno));
00049         nams = new std::ofstream;
00050         nams->open(namlog, std::ios::out);
00051         if (nams->fail())
00052             error("cannot open `%s' for write", namlog);
00053 
00054         
00055         const char *prolog = par("prolog");
00056         if (strlen(prolog))
00057         {
00058             cStringTokenizer tokenizer(prolog, ";");
00059             const char *token;
00060             while((token = tokenizer.nextToken())!=NULL)
00061                     *nams << token << endl;
00062             *nams << std::flush;
00063         }
00064     }
00065 }
00066 
00067 void NAMTrace::handleMessage(cMessage *msg)
00068 {
00069     error("This module doesn't process messages");
00070 }
00071 
00072 int NAMTrace::assignNamId(cModule *node, int namid)
00073 {
00074     
00075     return modid2namid[node->getId()] = namid==-1 ? ++lastnamid : namid;
00076 }
00077 
00078 int NAMTrace::getNamId(cModule *node) const
00079 {
00080     int modid = node->getId();
00081     std::map<int,int>::const_iterator it = modid2namid.find(modid);
00082     if (it == modid2namid.end())
00083         error("getNamId(): assignNamId() on module '%s' not yet called", node->getFullPath().c_str());
00084     return it->second;
00085 }
00086 
00087