📄 module-send.cc
字号:
//// Copyright (c) 2006 Regents of the SIGNET lab, University of Padova.// All rights reserved.//// Redistribution and use in source and binary forms, with or without// modification, are permitted provided that the following conditions// are met:// 1. Redistributions of source code must retain the above copyright// notice, this list of conditions and the following disclaimer.// 2. Redistributions in binary form must reproduce the above copyright// notice, this list of conditions and the following disclaimer in the// documentation and/or other materials provided with the distribution.// 3. Neither the name of the University of Padova (SIGNET lab) nor the // names of its contributors may be used to endorse or promote products // derived from this software without specific prior written permission.//// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.//#include <random.h>#include <node-core.h>#include <ip.h>#include "module-send.h"#include "modsend-clmsg.h"#include "sandbox-plugin1-clmsg.h"extern ClMessage_t SANDBOXPLG1MSG; // discover cl-msgextern ClMessage_t SANDBOXPLG1MSGS; // set size cl-msg/* ====================================================================== TCL Hooks for the simulator ====================================================================== */static class ModuleSendClass : public TclClass {public: ModuleSendClass() : TclClass("Module/SendModule") {} TclObject* create(int, const char*const*) { return (new ModuleSend()); }} class_modulesend;void SendTimer::expire(Event *e){ m_->expire();}int ModuleSend::uidcnt_; // unique id of the packet generatedModuleSend::ModuleSend() : sendTmr_(this) , pktSize_(DEFAULT_SIZE){ // binding to TCL variables bind("period_", &period_); bind("destPort_", &dstPort_); bind("destAddr_", &dstAddr_); bind("packetSize_", &pktSize_); uidcnt_ = 0;}ModuleSend::~ModuleSend(){} // TCL command interpreterint ModuleSend::command(int argc, const char*const* argv){ Tcl& tcl = Tcl::instance(); if(argc==2) { if(strcasecmp(argv[1], "start") == 0) // TCL command to start the packet generation and transmission { start(); return TCL_OK; } if(strcasecmp(argv[1], "stop") == 0) // TCL command to stop the packet generation { stop(); return TCL_OK; } } if (argc==3) { if(strcasecmp(argv[1], "changeiface") == 0) // TCL command to send a cross layer message to all module(s) middle (in BROADCAST fashion) // in which is defined the new iface to be used { int newiface = atoi(argv[2]); if (newiface<0) { tcl.resultf("Error ModuleSend cmd %s: interface must be greater than zero\n",argv[1]); return(TCL_ERROR); } ClMessage *c = new ClMsgModuleSend(DEFAULT_CLMSG_VERBOSITY, BROADCAST, getId(), CLBROADCASTADDR, newiface);// printf("ModuleSend: rx a request to change the interface to %d... send a clmsg broadcast\n", newiface); // send cross-layer message broadcast to inform the other modules sendCl(c, 0); return TCL_OK; } } if (argc==4) { if(strcasecmp(argv[1], "changeiface") == 0) // TCL command to send a cross layer message to all module(s) middle (in BROADCAST fashion) // in which is defined the new iface to be used after the delay indicated { int newiface = atoi(argv[2]); double delay = atof(argv[3]); if (newiface<0 || delay < 0) { tcl.resultf("Error ModuleSend cmd %s: interface and delay must be greater than zero\n",argv[1]); return(TCL_ERROR); } ClMessage *c = new ClMsgModuleSend(DEFAULT_CLMSG_VERBOSITY, BROADCAST, getId(), CLBROADCASTADDR, newiface);// printf("ModuleSend: rx a request to change the interface to %d... send a clmsg broadcast\n", newiface); // send cross-layer message broadcast to inform the other modules sendCl(c, delay); return TCL_OK; } } return Module::command(argc, argv);}int ModuleSend::crLayCommand(ClMessage* m){ if (m->type()==SANDBOXPLG1MSG) { // return the same message to answer to FE1 (i.e., answer to the discovery procedure started from FE1) if (debug_ > 5) printf("ModuleSend, answer to REQ of SandboxPLG1(%d)\n", m->getSource()); ClMessage *c = new ClMsgSandboxPlg1Disc(DEFAULT_CLMSG_VERBOSITY, UNICAST, getId(), m->getSource()); sendCl(c, 0); delete m; return 1; } if (m->type()==SANDBOXPLG1MSGS) { // set the new size of the transmitted packets according to the value specified in the SANDBOXPLG1MSGS cross layer message if (debug_ > 5) printf("ModuleSend, rx a request to change the packet size from SandboxPLG1: old=%d new=%d\n", pktSize_, ((ClMsgSandboxPlg1Set*)m)->getSize()); pktSize_ = ((ClMsgSandboxPlg1Set*)m)->getSize(); delete m; return 1; } // unrecognized cl-msg -> drop it! if (debug_ > 5) printf("ModuleSend, rx an unknown cl-msg -> drop it (type = %i SANDBOXPLG1MSG=%i)\n",m->type(),SANDBOXPLG1MSG); //drop(m, "UNKNOWN"); return Module::crLayCommand(m);}void ModuleSend::initPkt(Packet* p){ hdr_cmn* ch = hdr_cmn::access(p); ch->uid() = uidcnt_++;// ch->ptype() = type_; ch->size() = pktSize_;// ch->timestamp() = Scheduler::instance().clock(); ch->iface() = 0;// ch->direction() = hdr_cmn::NONE;// // ch->error() = 0; /* pkt not corrupt to start with */// hdr_ip* iph = hdr_ip::access(p);// iph->saddr() = here_.addr_;// iph->sport() = here_.port_; iph->daddr() = dstAddr_; iph->dport() = dstPort_;// // iph->flowid() = fid_;// iph->prio() = prio_;// iph->ttl() = 10;}void ModuleSend::start(){ if (period_<0) { fprintf(stderr,"Error ModuleSend.start: period <= 0"); exit(1); } sendTmr_.resched(period_);}void ModuleSend::expire(){ // send a packet after a random delay (unirom in [0, DELAYINTERAVAL])// double delay = Random::uniform(DELAYINTERVAL); double delay = 0; Packet* p = Packet::alloc(); initPkt(p); if (debug_>10) printf("ModuleSend(%d)::expire, send a pkt\n",getId()); //sendUp(p, Handler* h, delay); sendDown(p,delay); // schedule the next trasnmission sendTmr_.resched(period_);}void ModuleSend::stop(){ sendTmr_.cancel();}void ModuleSend::recv(Packet* p, Handler* h){}void ModuleSend::setClMsgType(ClMessage_t type){ clMsgType_ = type;}ClMessage_t ModuleSend::getClMsgType(){ return(clMsgType_);}ClMessage_t SANDBOXMSG = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -