📄 cip-reg.cc
字号:
#include <template.h>#include <cip.h>#include <random.h>#include <address.h>#include <mobilenode.h>#include <float.h>#include "wireless-phy.h"#define AGENT_ADS_SIZE 48#define REG_REQUEST_SIZE 52#define HANDOFF_RATIO 0.2//#define OPTIMIZED_HANDOFF//#define CIP_QUIETstatic class CIPHeaderClass : public PacketHeaderClass {public: CIPHeaderClass() : PacketHeaderClass("PacketHeader/CIP", sizeof(hdr_cip)) { }} class_ciphdr;static class CIPBSAgentClass : public TclClass {public: CIPBSAgentClass() : TclClass("Agent/CIPBS") {} TclObject* create(int, const char*const*) { return (new CIPBSAgent()); }} class_cipbsagent;static class CIPMHAgentClass : public TclClass {public: CIPMHAgentClass() : TclClass("Agent/CIPMH") {} TclObject* create(int, const char*const*) { return (new CIPMHAgent()); }} class_cipmhagent;CIPBSAgent::CIPBSAgent() : Agent(PT_UDP), beacon_(1.0), bcast_target_(0), ragent_(0), timer_(this), seqno_(-1), priority_(0), adlftm_(~0){ bind("adSize_", &size_); bind("ad_lifetime_", &adlftm_); bind("off_cip_", &off_cip_); size_ = AGENT_ADS_SIZE;}CIPMHAgent::CIPMHAgent() : Agent(PT_UDP) { bind("off_cip_", &off_cip_);}void CIPBSAgent::recv(Packet* p, Handler *){}void CIPBSAgent::timeout(int ){ send_ads(); timer_.resched(beacon_);}int CIPBSAgent::command(int argc, const char*const* argv){ if (argc == 3) { if (strcmp(argv[1], "beacon-period") == 0) { beacon_ = atof(argv[2]); timer_.resched(Random::uniform(0, beacon_)); return TCL_OK; } if (strcmp(argv[1], "priority") == 0) { // JCW priority_ = atoi(argv[2]); return TCL_OK; } if (strcmp(argv[1], "bcast-target") == 0) { bcast_target_ = (NsObject *)TclObject::lookup(argv[2]); return TCL_OK; } if (strcmp(argv[1], "ragent") == 0) { ragent_ = (NsObject *)TclObject::lookup(argv[2]); return TCL_OK; } } return (Agent::command(argc, argv));}void CIPBSAgent::send_ads(int dst, NsObject *target){ Packet *p = allocpkt(); hdr_cip *ciph = (hdr_cip *)p->access(off_cip_); hdr_ip *iph = (hdr_ip *)p->access(off_ip_);// ciph->haddr_ = ciph->ha_ = -1; ciph->coa_ = Address::instance().get_nodeaddr(addr()); ciph->type_ = CIPT_BEACON; ciph->lifetime_ = adlftm_; ciph->seqno_ = ++seqno_;// ciph->priority_ = priority_; if (dst != -1) { iph->daddr() = dst; iph->dport() = 0; } else { hdr_ip *iph = (hdr_ip*)p->access(off_ip_); hdr_cmn *ch = (hdr_cmn*)p->access(off_cmn_); ch->next_hop_ = IP_BROADCAST; ch->addr_type_ = NS_AF_INET; iph->daddr() = IP_BROADCAST; iph->dport() = 0; } if (target == NULL) { if (bcast_target_) bcast_target_->recv(p, (Handler*) 0); else if (target_) target_->recv(p, (Handler*) 0); else Packet::free(p); // drop; may log in future code } else { target->recv(p, (Handler*)0); }}void CIPMHAgent::recv(Packet* p, Handler *){ Tcl& tcl = Tcl::instance(); hdr_cip *ciph = (hdr_cip *)p->access(off_cip_); double RXThresh_ = 3.652e-10; switch (ciph->type_) { case CIPT_BEACON: { MobileNode *tnode_; printf(" %f MH received beacon from BS [%s] \n", Scheduler::instance().clock(),Address::instance().print_nodeaddr(ciph->coa_)); tnode_ = p->txinfo_.getNode(); // Mobile host follows Eager Cell Switching handoff scheme which means // as soon as it finds new beacon signal, it initiate handoff. if ((tnode_->address_ != node_->oldBS) && (p->txinfo_.RxPr >= RXThresh_)) { if( node_->activeBS != tnode_->address_) { //printf("lasthandoff: %f diff: %f num:%d\n", node_->lasthandoff, // Scheduler::instance().clock() - node_->lasthandoff, node_->Num_Handoff); if (((Scheduler::instance().clock() - node_->lasthandoff) > 2.0) || node_->Num_Handoff == 0) { if(!node_->semisoftenabled) node_->oldBS = node_->activeBS; node_->oldPr = p->txinfo_.RxPr; node_->activeBS = tnode_->myBS; node_->set_base_stn(ciph->coa_); node_->handoff = 1; node_->beacon_num = 1; printf("** Handoff to %d at %f Power: %e\n", tnode_->address_, Scheduler::instance().clock(), p->txinfo_.RxPr); node_->lasthandoff = Scheduler::instance().clock(); if(node_->semisoftenabled) { Tcl::instance().evalf("%s Semi_Handoff_called %s", node_->name(), tnode_->name()); } else { Tcl::instance().evalf("%s Handoff_called %s", node_->name(), tnode_->name()); } } } else { node_->beacon_num = node_->beacon_num + 1; if(node_->beacon_num > 2) node_->oldBS = 0; node_ ->oldPr = p->txinfo_.RxPr; } } else { if(node_->activeBS != tnode_->myBS) { // do nothing } else { node_->oldPr = p->txinfo_.RxPr; } } } break; default: break; } Packet::free(p);}int CIPMHAgent::command(int argc, const char*const* argv){ if (argc == 3) { if (strcmp (argv[1], "node") == 0) { node_ = (MobileNode*)TclObject::lookup(argv[2]); node_->beacon_num = 0; if (node_ == 0) { fprintf (stderr, "%s: %s lookup of %s failed\n", __FILE__, argv[1], argv[2]); return TCL_ERROR; } return TCL_OK; } } return (Agent::command(argc, argv));}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -