00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
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
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
00050
00051 PktTracer* SAP::pktTr_ = 0;
00052
00053 void SAP::addTracer(Tracer *tr)
00054 {
00055 if(!pktTr_)
00056 pktTr_ = new PktTracer();
00057
00058 pktTr_->addTracer(tr);
00059 }
00060
00061
00062
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
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
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
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
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
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
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
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
00255 int SAP::getModuleUpId()
00256 {
00257 return (upModule_->getId());
00258 }
00259
00260
00261 int SAP::getModuleDownId()
00262 {
00263 return (downModule_->getId());
00264 }
00265
00266
00267
00268
00269
00270
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