📄 pong-module.cc
字号:
/* * Copyright (c) 2008, Karlstad University * Erik Andersson, Emil Ljungdahl, Lars-Olof Moilanen * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 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. * * Neither the name of the <organization> 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 COPYRIGHT HOLDERS ``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 <copyright holder> 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. * * $Id: pong-module.cc 2 2008-06-04 10:05:48Z emil $ */#include <node-core.h>#include <ip.h>#include <address.h>#include "pong-module.h"extern packet_t PT_MPONG;int hdr_pong::offset_;static class PongPktClass : public PacketHeaderClass { public: PongPktClass() : PacketHeaderClass("PacketHeader/Pong", sizeof(hdr_pong)) { this->bind(); bind_offset(&hdr_pong::offset_); }} class_pong_pkt;static class PongModuleClass : public TclClass { public: PongModuleClass() : TclClass("Module/Pong") {} TclObject* create(int, const char*const*) { return (new PongModule()); }} class_module_pong;int PongModule::uidcnt_;PongModule::PongModule() : seq(0), oneway(0), rtt(0){ bind("packetSize_", &size_); uidcnt_ = 0;}PongModule::~PongModule(){}int PongModule::command(int argc, const char*const* argv){ Tcl& tcl = Tcl::instance(); if (argc == 2) { if (strcmp(argv[1], "getRTT") == 0) { tcl.resultf("%f",rtt); return (TCL_OK); } else if (strcmp(argv[1], "oneway") == 0) { oneway=1; return (TCL_OK); } } else if(argc == 3) { if (strcmp(argv[1], "send") == 0) { nsaddr_t addr = (nsaddr_t) atoi(argv[2]); sendPkt(addr); return (TCL_OK); } } return Module::command(argc, argv);}void PongModule::sendPkt(nsaddr_t addr){ Packet* pkt = Packet::alloc(); initPkt(pkt); hdr_ip *ih = HDR_IP(pkt); ih->daddr() = addr; hdr_cmn* ch = hdr_cmn::access(pkt); if (debug_>10) printf("PongModule(%d)::sendPkt, send a pkt (%d)\n",getId(), ch->uid()); sendDown(pkt, 0);}void PongModule::initPkt(Packet* p){ hdr_cmn* ch = hdr_cmn::access(p); ch->uid() = uidcnt_++; ch->ptype() = PT_PING; ch->size() = size_; hdr_pong* pongh = HDR_PONG(p); pongh->ret = 0; pongh->seq = seq++; pongh->send_time = Scheduler::instance().clock(); ch->timestamp() = pongh->send_time;}void PongModule::initRetPkt(Packet* p, double send_time, int seq){ hdr_cmn* ch = hdr_cmn::access(p); ch->uid() = uidcnt_++; ch->ptype() = PT_PING; ch->size() = size_; hdr_pong* pongh = HDR_PONG(p); pongh->ret = 1; pongh->seq = seq; pongh->send_time = send_time; pongh->rcv_time = Scheduler::instance().clock(); ch->timestamp() = pongh->rcv_time;}void PongModule::recv(Packet* p, Handler* h){ hdr_cmn* ch = hdr_cmn::access(p); recv(p);}void PongModule::recv(Packet* pkt){ hdr_cmn* ch = hdr_cmn::access(pkt); if (ch->ptype() != PT_PING) { drop(pkt, 1, PONG_DROP_REASON_UNKNOWN_TYPE); return; } hdr_pong* hdr = HDR_PONG(pkt); hdr_ip *hdrip = HDR_IP(pkt); // Is the 'ret' field = 0 (i.e. the receiving node is being pinged)? if (hdr->ret == 0) { // Send an 'echo'. First save the old packet's send_time double stime = hdr->send_time; int rcv_seq = hdr->seq; nsaddr_t daddr = hdrip->saddr(); Packet::free(pkt); Packet* pktret = Packet::alloc(); initRetPkt(pktret, stime, rcv_seq); hdr_ip *ih = HDR_IP(pktret); ih->daddr() = daddr; sendDown(pktret, 0); } else { // Update rtt rtt = Scheduler::instance().clock()-hdr->send_time; // A packet was received. Use tcl.eval to call the Tcl // interpreter with the ping results. // Note: In the Tcl code, a procedure 'Agent/Ping recv {from rtt}' // has to be defined which allows the user to react to the ping // result. char out[100]; // Prepare the output to the Tcl interpreter. Calculate the round // trip time /* if (oneway) //AG printf("%s recv %s %d %3.1f %3.1f", name(), Address::instance().print_nodeaddr(hdrip->saddr()), hdr->seq, (hdr->rcv_time - hdr->send_time) * 1000, (Scheduler::instance().clock()-hdr->rcv_time) * 1000); else printf("%s recv %s %3.1f", name(), Address::instance().print_nodeaddr(hdrip->saddr()), (Scheduler::instance().clock()-hdr->send_time) * 1000); Tcl& tcl = Tcl::instance(); tcl.eval(out); */ // Discard the packet Packet::free(pkt); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -