clsap.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 
00031 #include<iostream>
00032 #include "clsap.h"
00033 #include "module.h"
00034 #include "node-core.h"
00035 
00036 #include "deprecated.h" 
00037 
00038 /* ======================================================================
00039    TCL Hooks for the simulator
00040    ====================================================================== */
00041 static class ClSAPClass : public TclClass {
00042 public:
00043         ClSAPClass() : TclClass("ConnectorTrace/ClSAP") {}
00044         TclObject* create(int, const char*const*) {
00045                 return (new ClSAP());
00046         }
00047 } class_clsap;
00048 
00049 /*
00050 Non static methods for CLSAP
00051 */
00052 ClSAP::ClSAP() : ConnectorTrace(), pluginPtr_(0), nodeCorePtr_(0)
00053 {
00054         depthNC_ = DEFAULTDEPTH;
00055         bind("depthToNC_", &depthNC_);
00056         depthP_ = DEFAULTDEPTH;
00057         bind("depthToP_", &depthP_);
00058 }
00059 
00060 ClSAP::~ClSAP()
00061 {
00062         //printf("Distruttore CLSAP\n");
00063         //ConnectorTrace::~ConnectorTrace();
00064 }
00065 
00066 int ClSAP::command(int argc, const char* const* argv)
00067 {
00068         Tcl& tcl = Tcl::instance();
00069         if (argc==3)
00070         {
00071                 // delay settabile solo da c++ o command tcl ??? chi lo setta ?
00072                 // ha senso un settaggio fisso da tcl: ho il problema che �unico per differenti comunicazioni
00073                 
00074                 if (strcasecmp(argv[1],"module")==0)
00075                 {
00076                         Module *module = (Module*)TclObject::lookup(argv[2]);
00077                         if (pluginPtr_!=0)
00078                                 {
00079                                         tcl.resultf("Error ClSAP cmd = %s: a module is already installed", argv[1]);
00080                                         return (TCL_ERROR);
00081                                 }
00082                                 pluginPtr_ = module;
00083                                 return (TCL_OK);
00084                 }
00085                 if (strcasecmp(argv[1],"nodecore")==0)
00086                 {
00087                         NodeCore *nodecore = (NodeCore*)TclObject::lookup(argv[2]);
00088                         if (nodeCorePtr_!=0)
00089                                 {
00090                                         tcl.resultf("Error ClSAP cmd = %s: a nodecore is already installed", argv[1]);
00091                                         return (TCL_ERROR);
00092                                 }
00093                                 nodeCorePtr_ = nodecore;
00094                                 return (TCL_OK);
00095                 }
00096         }
00097         return ConnectorTrace::command(argc, argv);
00098 }
00099 
00100 
00101 void ClSAP::sendModule(ClMessage* m, double delay)
00102 {
00103         // TEST direction???
00104         if (pluginPtr_==0)
00105         {
00106                 fprintf(stderr, "Error, ClSAP.sendModule: module not yet installed\n");
00107                 exit(1);
00108         }
00109         m->direction(TOMODULE);
00110         if(delay > 0)
00111         {
00112                 Scheduler::instance().schedule(this, m, delay);
00113         }
00114         else
00115         {
00116 //              printf("ClSAP::sendModule depth=%i verb=%i\n",depth_, m->verbosity());
00117                 if (depthP_ >= m->verbosity())
00118                         trace(m);
00119                 RUN_DEPRECATED_OR_NEW_VIRTUAL_METHOD(pluginPtr_->crLayCommand(m), pluginPtr_->recvAsyncClMsg(m));               
00120         }
00121 }
00122 
00123 void ClSAP::sendSynchronousModule(ClMessage* m)
00124 {
00125         // TEST direction???
00126         if (pluginPtr_==0)
00127         {
00128                 fprintf(stderr, "Error, ClSAP.sendModule: module not yet installed\n");
00129                 exit(1);
00130         }
00131         m->direction(TOMODULE);
00132         if (depthP_ >= m->verbosity())
00133                 traceSync(m);
00134         RUN_DEPRECATED_OR_NEW_VIRTUAL_METHOD(pluginPtr_->crLaySynchronousCommand(m), pluginPtr_->recvSyncClMsg(m));
00135         m->direction(TONODECORE);
00136         if (depthNC_ >= m->verbosity())
00137                 traceSync(m);
00138 }
00139 
00140 void ClSAP::sendClLayer(ClMessage* m, double delay)
00141 {
00142         // TEST direction???
00143         if (nodeCorePtr_==0)
00144         {
00145                 fprintf(stderr, "Error, ClSAP.sendClModule: nodeCorePtr not yet installed\n");
00146                 exit(1);
00147         }
00148         m->direction(TONODECORE);
00149         if (depthNC_ >= m->verbosity())
00150                 trace(m);
00151         if(delay > 0)
00152         {
00153                 Scheduler::instance().schedule(this, m, delay);
00154         }
00155         else
00156         {
00157                 nodeCorePtr_->crLayCommand(m);
00158         }
00159 }
00160 
00161 void ClSAP::sendSynchronousClLayer(ClMessage* m)
00162 {
00163         // TEST direction???
00164   //  fprintf(stderr,  "%s received message type %d\n", __PRETTY_FUNCTION__, m->type());
00165         if (nodeCorePtr_==0)
00166         {
00167                 fprintf(stderr, "Error, ClSAP.sendClModule: nodeCorePtr not yet installed\n");
00168                 exit(1);
00169         }
00170         m->direction(TONODECORE);
00171         if (depthNC_ >= m->verbosity())
00172                 traceSync(m);
00173         nodeCorePtr_->synchronousCrLayCommand(m);
00174         m->direction(TOMODULE);
00175         if (depthP_ >= m->verbosity())
00176                 traceSync(m);
00177 }
00178 
00179 int ClSAP::getPluginId()
00180 {
00181         return pluginPtr_?pluginPtr_->getId():NO_MODULE;
00182 }
00183 
00184 void ClSAP::handle(Event *e)
00185 {
00186         ClMessage *m = (ClMessage *)e;
00187         if(m->direction() == TONODECORE)
00188                 nodeCorePtr_->crLayCommand(m);
00189         else
00190         {
00191                 if (depthP_ >= m->verbosity())
00192                         trace(m);
00193                 RUN_DEPRECATED_OR_NEW_VIRTUAL_METHOD(pluginPtr_->crLayCommand(m), pluginPtr_->recvAsyncClMsg(m));
00194         }
00195 }
00196 
00197 Position* ClSAP::getPosition()
00198 {
00199         return nodeCorePtr_->getPosition();
00200 }
00201 
00202 

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