sap.cc

00001 /*
00002  * Copyright (c) 2006 Regents of the SIGNET lab, University of Padova.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the University of Padova (SIGNET lab) nor the 
00014  *    names of its contributors may be used to endorse or promote products 
00015  *    derived from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
00018  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 
00019  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
00020  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
00021  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
00022  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
00023  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
00024  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
00025  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
00026  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
00027  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00028  */
00029  
00030 #include "sap.h"
00031 #include "module.h"
00032 #include "scheduler.h"
00033 #include<iostream>
00034 
00035 #include "deprecated.h"
00036 
00037 /* ======================================================================
00038    TCL Hooks for the simulator
00039    ====================================================================== */
00040 static class SAPClass : public TclClass {
00041 public:
00042         SAPClass() : TclClass("ConnectorTrace/SAP") {}
00043         TclObject* create(int, const char*const*) {
00044                 return (new SAP());
00045         }
00046 } class_sap;
00047 
00048 /*
00049 Static attributes and methods for SAP
00050 */
00051 PktTracer* SAP::pktTr_ = 0;
00052 
00053 void SAP::addTracer(Tracer *tr)
00054 {
00055         if(!pktTr_)
00056                 pktTr_ = new PktTracer();
00057         //printf("pktTr_=%p size=%i(%i)\n", pktTr_, sizeof(*pktTr_), sizeof(PktTracer));
00058         pktTr_->addTracer(tr);
00059 }
00060 
00061 /*
00062 Non static methods for SAP
00063 */
00064 
00065 SAP::SAP() : ConnectorTrace(), upModule_(0), downModule_(0)
00066 {
00067         depthUp_ = DEFAULTDEPTH;
00068         depthDown_ = DEFAULTDEPTH;
00069         bind("depthUp_", &depthUp_);
00070         bind("depthDown_", &depthDown_);
00071 }
00072 
00073 SAP::~SAP()
00074 {
00075         //ConnectorTrace::~ConnectorTrace();
00076 }
00077 
00078 
00079 int SAP::command(int argc, const char* const* argv)
00080 {
00081         Tcl& tcl = Tcl::instance();
00082         if (argc==3)
00083         {
00084 
00085                 if (strcasecmp(argv[1],"upmodule")==0)
00086                 {
00087                         Module *module = (Module*)TclObject::lookup(argv[2]);
00088                         if (upModule_!=0)
00089                                 {
00090                                         tcl.resultf("Error SAP cmd = %s: an above module is already installed", argv[1]);
00091                                         return (TCL_ERROR);
00092                                 }
00093                                 upModule_ = module;
00094                                 return (TCL_OK);
00095                 }
00096                 if (strcasecmp(argv[1],"downmodule")==0)
00097                 {
00098                         Module *module = (Module*)TclObject::lookup(argv[2]);
00099                         if (downModule_!=0)
00100                                 {
00101                                         tcl.resultf("Error SAP cmd = %s: a bottom module is already installed", argv[1]);
00102                                         return (TCL_ERROR);
00103                                 }
00104                                 downModule_ = module;
00105                                 return (TCL_OK);
00106                 }
00107         }
00108         return ConnectorTrace::command(argc, argv);
00109 }
00110 
00111 int SAP::depthUp()
00112 {
00113         return depthUp_;
00114 }
00115 
00116 int SAP::depthDown()
00117 {
00118         return depthDown_;
00119 }
00120 
00121 void SAP::sendUp(Packet* p, double delay)
00122 {
00123 
00124         if (upModule_==0)
00125         {
00126                 fprintf(stderr, "Error, SAP.sendUp: upModule not yet installed\n");
00127                 exit(1);
00128         }
00129         if(debug_)
00130         {
00131                 printf("%f -- SAP::sendUp(%p,%f) --- depthUp_=%i\n", Scheduler::instance().clock(), p, delay, depthUp_);
00132                 fflush(stdout);
00133         }
00134         
00135         hdr_cmn *ch = HDR_CMN(p);
00136         ch->direction() = hdr_cmn::UP;
00137         
00138         if(debug_)
00139         {
00140                 printf("\ttrace done\n");
00141         }
00142         if(delay > 0)
00143         {
00144                 Scheduler::instance().schedule(this, p, delay);
00145         }
00146         else
00147         {
00148                 if (depthUp_)
00149                         trace(p);
00150                 upModule_->recv(p, getModuleDownId());
00151         }
00152 }
00153 
00154 void SAP::sendDown(Packet* p,  double delay)
00155 {
00156 
00157         if (downModule_==0)
00158         {
00159                 fprintf(stderr, "Error, SAP.downUp: downModule not yet installed\n");
00160                 exit(1);
00161         }
00162         hdr_cmn *ch = HDR_CMN(p);
00163         ch->direction() = hdr_cmn::DOWN;
00164 //      printf("SAP %p send to downmodule %d\n",this, downModule_->getId());
00165         if(delay > 0)
00166         {
00167                 Scheduler::instance().schedule(this, p, delay);
00168         }
00169         else
00170         {
00171                 if (depthDown_)
00172                         trace(p);
00173                 downModule_->recv(p, getModuleUpId());
00174         }
00175 }
00176 
00177 void SAP::sendUp(ClMessage* m, double delay)
00178 {
00179         // TEST direction???
00180         if (upModule_==0)
00181         {
00182                 fprintf(stderr, "Error, SAP.sendUp(ClMessage): upModule not yet installed\n");
00183                 exit(1);
00184         }
00185         m->direction(UP);
00186         if(delay > 0)
00187         {
00188                 Scheduler::instance().schedule(this, m, delay);
00189         }
00190         else
00191         {
00192 //              printf("ClSAP::sendModule depth=%i verb=%i\n",depth_, m->verbosity());
00193                 if (depthUp_ >= m->verbosity())
00194                         trace(m);
00195                 RUN_DEPRECATED_OR_NEW_VIRTUAL_METHOD(upModule_->crLayCommand(m), upModule_->recvAsyncClMsg(m));
00196         }
00197 }
00198 
00199 void SAP::sendDown(ClMessage* m, double delay)
00200 {
00201         // TEST direction???
00202         if (downModule_==0)
00203         {
00204                 fprintf(stderr, "Error, SAP.sendDown(ClMessage): downModule not yet installed\n");
00205                 exit(1);
00206         }
00207         m->direction(DOWN);
00208         if(delay > 0)
00209         {
00210                 Scheduler::instance().schedule(this, m, delay);
00211         }
00212         else
00213         {
00214                 if (depthDown_ >= m->verbosity())
00215                         trace(m);
00216                 RUN_DEPRECATED_OR_NEW_VIRTUAL_METHOD(downModule_->crLayCommand(m), downModule_->recvAsyncClMsg(m));
00217         }
00218 }
00219 
00220 void SAP::sendSynchronousUp(ClMessage* m)
00221 {
00222         // TEST direction???
00223         if (upModule_==0)
00224         {
00225                 fprintf(stderr, "Error, SAP.sendSynchronousUp: upModule not yet installed\n");
00226                 exit(1);
00227         }
00228         m->direction(UP);
00229         if (depthUp_ >= m->verbosity())
00230                 traceSync(m);
00231         RUN_DEPRECATED_OR_NEW_VIRTUAL_METHOD(upModule_->crLaySynchronousCommand(m), upModule_->recvSyncClMsg(m));
00232         m->direction(DOWN);
00233         if (depthDown_ >= m->verbosity())
00234                 traceSync(m);
00235 }
00236 
00237 void SAP::sendSynchronousDown(ClMessage* m)
00238 {
00239         // TEST direction???
00240         if (downModule_==0)
00241         {
00242                 fprintf(stderr, "Error, SAP.sendSynchronousDown: downModule not yet installed\n");
00243                 exit(1);
00244         }
00245         m->direction(DOWN);
00246         if (depthDown_ >= m->verbosity())
00247                 traceSync(m);
00248         RUN_DEPRECATED_OR_NEW_VIRTUAL_METHOD(downModule_->crLaySynchronousCommand(m), downModule_->recvSyncClMsg(m));
00249         m->direction(UP);
00250         if (depthUp_ >= m->verbosity())
00251                 traceSync(m);
00252 }
00253 
00254 // get the module id of the module of the above layer
00255 int SAP::getModuleUpId()
00256 {
00257         return (upModule_->getId());
00258 }
00259 
00260 // get the module id of the module of the bottom layer
00261 int SAP::getModuleDownId()
00262 {
00263         return (downModule_->getId());
00264 }
00265 
00266 
00267 /* FIXED ONLY BY TCL ???
00268 void SAP::depthTrace(int depth)
00269 {
00270         depthTrace_ = depth;
00271 }
00272 */
00273 
00274 void SAP::trace(Packet *p)
00275 {
00276         if(debug_>5)
00277                 printf("%f --- SAP::trace(%p) --- pktTr_=%p -- [%c %.9f %s]\n", Scheduler::instance().clock(), p, pktTr_,dirDown_, Scheduler::instance().clock(), preambleDown_);
00278         hdr_cmn *ch = HDR_CMN(p);
00279         if(ch->direction() == hdr_cmn::DOWN)
00280                 writeTrace("%c %.9f %s",dirDown_, Scheduler::instance().clock(), preambleDown_);
00281         else
00282                 writeTrace("%c %.9f %s",dirUp_, Scheduler::instance().clock(), preambleUp_);
00283         if(debug_>10)
00284                 printf("SAP::trace(%p) --- pktTr_=%p\n", p, pktTr_);
00285         if(pktTr_)
00286                 pktTr_->trace(p, this);
00287         dump();
00288 }
00289 
00290 void SAP::trace(ClMessage *m)
00291 {
00292         ConnectorTrace::trace(m);
00293 }
00294 
00295 void SAP::handle(Event* e)
00296 {
00297         if(debug_)
00298         {
00299                 printf("SAP::handle(%p) --- downModule_=%p\n", e, downModule_);
00300                 fflush(stdout);
00301         }
00302         
00303         hdr_cmn *ch = HDR_CMN((Packet *)e);
00304         if(ch->direction() == hdr_cmn::DOWN)
00305         {
00306                 if (depthDown_)
00307                         trace((Packet *)e);
00308                 downModule_->recv((Packet*)e, getModuleUpId());
00309         }
00310         else
00311         {
00312                 if (depthUp_)
00313                         trace((Packet *)e);
00314                 upModule_->recv((Packet*)e, getModuleDownId());
00315         }
00316 }
00317 

Generated on Wed Nov 26 15:47:28 2008 for NS-MIRACLE library by  doxygen 1.5.2