NAMTrace.cc

Go to the documentation of this file.
00001 //
00002 // Copyright (C) 2005 Andras Varga
00003 //
00004 // This program is free software; you can redistribute it and/or
00005 // modify it under the terms of the GNU Lesser General Public License
00006 // as published by the Free Software Foundation; either version 2
00007 // of the License, or (at your option) any later version.
00008 //
00009 // This program is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU Lesser General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU Lesser General Public License
00015 // along with this program; if not, see <http://www.gnu.org/licenses/>.
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         // open namlog for write
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         // print prolog into the file
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     // FIXME make sure nobody's using that namid yet
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