chsap.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 "chsap.h"
00031 #include "channel-module.h"
00032 
00033 /* ======================================================================
00034    TCL Hooks for the simulator
00035    ====================================================================== */
00036 static class ChSAPClass : public TclClass {
00037 public:
00038         ChSAPClass() : TclClass("ConnectorTrace/ChSAP") {}
00039         TclObject* create(int, const char*const*) {
00040                 return (new ChSAP());
00041         }
00042 } class_chsap;
00043 
00044 
00045 ChSAP::ChSAP() : channel_(0)
00046 {
00047 }
00048 
00049 ChSAP::~ChSAP()
00050 {
00051 }
00052 
00053 // TCL command interpreter
00054 int ChSAP::command(int argc, const char*const* argv)
00055 {
00056         Tcl& tcl = Tcl::instance();
00057         if (argc==3)
00058         {
00059                 // install the module
00060                 if (strcasecmp(argv[1],"module")==0)
00061                 {
00062                          Module *module = (Module*)(TclObject::lookup(argv[2]));
00063                         if (upModule_!=0)
00064                         {
00065                                 tcl.resultf("Error SAP cmd = %s: an above module is already installed", argv[1]);
00066                                 return (TCL_ERROR);
00067                         }
00068                         upModule_ = module;
00069                         return (TCL_OK);
00070                 }
00071                 else if (strcasecmp(argv[1],"channel")==0)
00072                 {
00073                         ChannelModule *module = (ChannelModule*)TclObject::lookup(argv[2]);
00074                         if (channel_!=0)
00075                                 {
00076                                         tcl.resultf("Error SAP cmd = %s: a channel module is already installed", argv[1]);
00077                                         return (TCL_ERROR);
00078                                 }
00079                                 channel_ = module;
00080                                 downModule_ = module;
00081                                 return (TCL_OK);
00082                 }
00083                 else if (strcasecmp(argv[1],"nodeCore")==0)
00084                 {
00085                         nodeCorePtr_ = (NodeCore*)TclObject::lookup(argv[2]);
00086                         if (!nodeCorePtr_)
00087                         {
00088                                 tcl.resultf("Error CHSAP cmd = %s: no node core instance found", argv[2]);
00089                                 return (TCL_ERROR);
00090                         }
00091                         return (TCL_OK);
00092                 }
00093         }
00094         return SAP::command(argc, argv);
00095 }
00096 
00097 void ChSAP::sendDown(Packet* p, double delay)
00098 {
00099         if (channel_==0)
00100         {
00101                 fprintf(stderr, "Error, ChSAP. downUp: downModule not yet installed\n");
00102                 exit(1);
00103         }
00104         if(debug_)
00105                 printf("ChSAP::sendDown(%p, %f) ---- depth_=%i\n", p, delay, depth_);
00106                 
00107         hdr_cmn *ch = HDR_CMN(p);
00108         ch->direction() = hdr_cmn::DOWN;
00109         if(delay > 0)
00110         {
00111                 Scheduler::instance().schedule(this, p, delay);
00112         }
00113         else
00114         {
00115                 if (depthDown_)
00116                         trace(p);
00117                 channel_->recv(p, this);
00118         }
00119 }
00120 
00121 void ChSAP::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) --- depth_=%i\n", Scheduler::instance().clock(), p, delay, depth_);
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, 0);
00151         }
00152 }
00153 
00154 void ChSAP::handle(Event* e)
00155 {
00156         if(debug_)
00157         {
00158                 printf("ChSAP::handle(%p) --- channel_=%p\n", e, channel_);
00159                 fflush(stdout);
00160         }
00161         hdr_cmn *ch = HDR_CMN((Packet *)e);
00162         if(ch->direction() == hdr_cmn::DOWN)
00163         {
00164                 if (depthDown_)
00165                         trace((Packet *)e);
00166                 channel_->recv((Packet*)e, this);
00167         }
00168         else
00169         {
00170                 if (depthUp_)
00171                         trace((Packet *)e);
00172                 upModule_->recv((Packet*)e);
00173         }
00174 }
00175 
00176 Position* ChSAP::getPosition()
00177 {
00178         return nodeCorePtr_->getPosition();
00179 }

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