plugin.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 "plugin.h"
00031 #include "scheduler.h"
00032 
00033 #include "deprecated.h" 
00034 #include "clmsg-discovery.h"
00035 // #include <string>
00036 
00037 /* ======================================================================
00038    TCL Hooks for the simulator
00039    ====================================================================== */
00040 static class PlugInClass : public TclClass {
00041 public:
00042         PlugInClass() : TclClass("PlugIn") {}
00043         TclObject* create(int, const char*const*) {
00044                 return (new PlugIn());
00045         }
00046 } class_plugin;
00047 
00048 PlugIn::PlugIn() :  clsap_(0), binPtr_(0), uLayerId_(0)
00049 {
00050         sprintf(tag_, "---");
00051 }
00052 
00053 PlugIn::~PlugIn()
00054 {
00055 }
00056 
00057 // TCL command interpreter
00058 int PlugIn::command(int argc, const char*const* argv)
00059 {
00060 //      printf("PlugIn::command -- %s (%d)\n", argv[1], argc);
00061         Tcl& tcl = Tcl::instance();
00062         if (argc==2)
00063         {
00064                 if (strcasecmp(argv[1],"Id_")==0)
00065                 {
00066                         tcl.resultf("%d", getId());
00067                         return (TCL_OK);
00068                 }
00069                 else if (strcasecmp(argv[1],"gettag")==0)
00070                 {
00071                         tcl.resultf("%s",tag_);
00072                         return TCL_OK;
00073                 }
00074         }
00075         if (argc==3)
00076         {
00077                 if (strcasecmp(argv[1],"Id_")==0)
00078                 {
00079                         int val = atoi(argv[2]);
00080                         setId(val);
00081                         return (TCL_OK);
00082                 }
00083                 if (strcasecmp(argv[1],"addclsap")==0)
00084                 {
00085                         ClSAP *clsap = dynamic_cast<ClSAP*>(TclObject::lookup(argv[2]));
00086                         if (clsap_!=0)
00087                         {
00088                                 tcl.resultf("Error PlugIn::command = %s:  a crLay is already installed", argv[1]);
00089                                 return (TCL_ERROR);
00090                         }
00091                         clsap_ = clsap;
00092                         return (TCL_OK);
00093                 }
00094                 if (strcasecmp(argv[1],"addbin")==0)
00095                 {
00096                         Bin *bin = dynamic_cast<Bin*>(TclObject::lookup(argv[2]));
00097                         if (binPtr_!=0)
00098                         {
00099                                 tcl.resultf("Error PlugIn::command = %s:  a bin is already installed", argv[1]);
00100                                 return (TCL_ERROR);
00101                         }
00102                         binPtr_ = bin;
00103                         return (TCL_OK);
00104                 }
00105                 // set the plugin tag
00106                 if (strcasecmp(argv[1],"settag")==0)
00107                 {
00108                         if (strlen(argv[2])>MAX_TAG_LENGTH)
00109                         {
00110                                 tcl.resultf("Error PlugIn::command cmd = %s: plugin tag too long", argv[1]);
00111                                 return (TCL_ERROR);
00112                         }
00113                         strcpy(tag_, argv[2]);
00114                         return TCL_OK;
00115                 }
00116         }
00117         return NsObject::command(argc, argv);
00118 }
00119 
00120 
00121 int PlugIn::getId()
00122 {
00123         return(uid_);
00124 }
00125 
00126 void PlugIn::setId(int val)
00127 {
00128         uid_ = val;
00129 }
00130 
00131 // it returns -1 if it's impossible to return the tag
00132 // it returns 0 if the size of the buffer is not sufficient (but the string is copied)
00133 // if returns >0 if all is ok
00134 int PlugIn::getTag(char* buf, int size)
00135 {
00136   if ((buf==0)||(size<=0)||(tag_==0))
00137     {
00138       return (-1);
00139     }
00140 
00141   strncpy(buf, tag_, size);
00142   if (buf[size-1] != '\0')
00143     {
00144       buf[size-1] = '\0';
00145       return 0;
00146     } 
00147   else 
00148     return 1;
00149 
00150 }
00151 
00152         
00153 int PlugIn::recvAsyncClMsg(ClMessage* m)
00154 {
00155   //  cerr << "PlugIn::recvAsyncClMsg() NOT IMPLEMENTED" << endl;
00156 
00157   // NOT deallocating message since if this is not implemented then
00158   // crLayCommand() will for sure be called with the same parameter
00159 
00160   return RETVAL_NOT_IMPLEMENTED;
00161 }
00162 
00163 
00164 // standard cross-layer messages intepreter
00165 // by default an unknown cross-layer message does not interest the
00166 // plugin, so we just throw it away
00167 // ATTENTION: remember to dellocate the message after the use
00168 int PlugIn::crLayCommand(ClMessage* m)
00169 {
00170   delete m;
00171   return RETVAL_NOT_IMPLEMENTED;
00172 }
00173 
00174 int PlugIn::getLayer()
00175 {
00176         return(uLayerId_);
00177 }
00178 
00179 void PlugIn::setLayer(int val)
00180 {
00181         uLayerId_ = val;
00182 }
00183 
00184 
00185 
00186 int PlugIn::recvSyncClMsg(ClMessage* m)
00187 {
00188   if (m->type() == CLMSG_DISCOVERY) {
00189      Tcl& tcl = Tcl::instance();
00190      tcl.evalf("%s info class", name() );
00191      const char* class_name = tcl.result();
00192      (dynamic_cast<ClMsgDiscovery*>(m))->addData((const PlugIn*)this, getLayer(), getId(), class_name , getTag() );
00193      return 0;
00194   }
00195   else return RETVAL_NOT_IMPLEMENTED;
00196 }
00197 
00198 
00199 int PlugIn::crLaySynchronousCommand(ClMessage* m)
00200 {
00201   return RETVAL_NOT_IMPLEMENTED;
00202 }
00203 
00204 
00205 
00206 void PlugIn::sendCl(ClMessage* m, double delay)
00207 {
00208   PRINT_WARNING_DEPRECATED_FUNCTION;
00209   sendAsyncClMsg(m, delay);
00210 }
00211 
00212 void PlugIn::sendAsyncClMsg(ClMessage* m, double delay)
00213 {
00214         if (clsap_==0)
00215         {
00216                 fprintf(stderr, "Error, PlugIn.sendCl: crLayBus_ is not yet installed\n");
00217                 exit(1);
00218         }
00219         m->setSource(getId()); // this makes src IDs more spoofing-proof
00220         clsap_->sendClLayer(m, delay);
00221 }
00222 
00223 
00224 void PlugIn::sendSynchronousCl(ClMessage* m)
00225 {
00226   PRINT_WARNING_DEPRECATED_FUNCTION;
00227   sendSyncClMsg(m);
00228 }
00229 
00230 void PlugIn::sendSyncClMsg(ClMessage* m)
00231 {
00232         if (clsap_==0)
00233         {
00234                 fprintf(stderr, "Error, PlugIn.sendCl: crLayBus_ is not yet installed\n");
00235                 exit(1);
00236         }
00237         m->setSource(getId()); // this makes src IDs more spoofing-proof
00238         clsap_->sendSynchronousClLayer(m);
00239 }
00240 
00241 
00242 void PlugIn::drop(ClMessage* m, const char* reason)
00243 {
00244         binPtr_->drop(m, reason);
00245 }
00246 
00247 
00248 void PlugIn::recv(Packet* p, Handler* callback)
00249 {
00250         // an FE can receive only cross-layer messages
00251         // but recv must be implemeted since a FunctionalEntity is a NsObject child
00252 }
00253 
00254 Position* PlugIn::getPosition()
00255 {
00256         return clsap_->getPosition();
00257 }
00258 

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