📄 clusteringmodule.cc
字号:
/** * Copyright (c) 2006 Michele Mastrogiovanni. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */#include "ClusteringModule.h"#include "packet.h"#include "agent.h"#include "ip.h"#include "mac.h"#include "random.h"/** * Create a new clustering module. */ClusteringModule::ClusteringModule(enum packet_t pkttype) : Agent(pkttype), statusModule(UNDEFINED), myAddress(-1), utility(NULL), upTarget(NULL), lowerModule(false){}/** * Manage Tcl command. */intClusteringModule::command(int argc, const char * const * argv){ if (argc == 2) { /** * Start module */ if (strcmp (argv[1], "start") == 0) { startModule(); return (TCL_OK); } /** * Set random seed: each simulation will be different. */ else if (strcmp (argv[1], "randomize") == 0) { Random::seed_heuristically(); return TCL_OK; } else if (strcmp (argv[1], "lower") == 0) { lowerModule = true; return TCL_OK; } } else if (argc == 3) { /** * Set node address */ if (strcasecmp (argv[1], "addr") == 0) { myAddress = Address::instance().str2addr(argv[2]); return TCL_OK; } /** * Set node mobile model. */ else if (strcasecmp (argv[1], "node") == 0) { myMobileNode = (MobileNode*) TclObject::lookup (argv[2]); return TCL_OK; } /** * Set utility class. */ else if (strcasecmp (argv[1], "utility") == 0) { utility = (CommonUtility *) TclObject::lookup(argv[2]); return TCL_OK; } /** * Set upper layer. */ else if (strcasecmp (argv[1], "up-target") == 0) { upTarget=(Agent*) TclObject::lookup(argv[2]); return TCL_OK; } } return Agent::command(argc, argv);}/** * Start a clustering module: save initial energy value, * initial time and set internal state of module. */void ClusteringModule::startModule(){ initialEnergy = myMobileNode->energy_model()->energy(); initialTime = NOW; statusModule = RUNNING; loadNeighbors(neighbors);}/** * Notify end of module execution: set internal state of module * and enable packet management for upper layer. * Set information for dumping. */void ClusteringModule::endModule(){ statusModule = CALCULATED; /** * Set energy and time consumption. */ if (utility) { utility->set(myAddress, initialEnergy - myMobileNode->energy_model()->energy(), ENERGY); utility->set(myAddress, NOW - initialTime, TIME); }}/** * Send a packet to lower layer in network stack. * Size of packet must be specified. * If a destination is no specified packet is broadcasted. * Send action save information about bytes transmission * in dumping utility. */void ClusteringModule::sendDown(Packet * p, size_t size, NodeAddress to, double maxDelay){ hdr_ip* iph = HDR_IP(p); hdr_cmn* cmh = HDR_CMN(p); // Common datas. cmh->addr_type() = NS_AF_ILINK; cmh->direction() = hdr_cmn::DOWN; // DEBUG // printf("[UP] %d sent %d bytes\n", myAddress, size); cmh->size() += size; iph->src_.addr_ = myAddress; if (to == -1) { cmh->next_hop_ = MAC_BROADCAST; iph->dst_.addr_ = IP_BROADCAST; } else { cmh->next_hop_ = to; iph->dst_.addr_ = to; } if ((lowerModule) && (to == -1)) Scheduler::instance().schedule(target_, p, Random::uniform(maxDelay)); else target_->recv(p, (Handler*)NULL); /** * Save statistics about sent bytes and packets. */ if (utility) { utility->add(myAddress, size, to != -1 ? TX_BYTE_UNICAST : TX_BYTE_BROADCAST); utility->add(myAddress, 1, to != -1 ? TX_MSG_UNICAST : TX_MSG_BROADCAST); }}/** * That method is called when node receive a message. */voidClusteringModule::recv(Packet * p, Handler * h){ hdr_cmn *cmh = HDR_CMN(p); hdr_ip *iph = HDR_IP(p); /** * If packet is caming from upper layer and module ended execution * of algorithm, it delivers to bottom target. */ if (cmh->direction() == hdr_cmn::DOWN) { if (statusModule == CALCULATED) { // Send packet to lower layer. prepareSendDataDown(p); target_->recv(p, (Handler*)NULL); } /** * Discard packet. */ return; } /** * If packet is caming from bottom layer and packet is * destined to current node, it call method to manage it. */ if (cmh->direction() == hdr_cmn::UP) { /** * Packet is destined to upper layer. */ if (isDataPacket(p)) { /** * If module algorithm is ended. */ if (statusModule == CALCULATED) { // Send data to upper layer. upTarget->recv(p, h); } } else { /** * Packet is destined to current node: * manage it. */ struct hdr_ip *iph = HDR_IP(p); if ((iph->daddr() != IP_BROADCAST) && (iph->daddr() != myAddress)) return; struct hdr_cmn *cmh = HDR_CMN(p); if (utility) { utility->add(myAddress, cmh->size(), iph->daddr() != IP_BROADCAST ? RX_BYTE_UNICAST : RX_BYTE_BROADCAST); utility->add(myAddress, 1, iph->daddr() != IP_BROADCAST ? RX_MSG_UNICAST : RX_MSG_BROADCAST); } receive(p, h); } }}/** * Load node list. */voidClusteringModule::loadNeighbors(NodeList & neighbors){ neighbors.clear(); // To get information about neighborood GridKeeper * gk = GridKeeper::instance(); MobileNode **output = new MobileNode *[gk->size_]; if (gk != NULL) { int num_neighbors = gk->get_neighbors(myMobileNode, output); for (int i = 0; i < num_neighbors; i++) { MobileNode * current = output[i]; // Initialize neighbor's info NodeAddress neighborAddress = current->address(); neighbors.insert(neighborAddress); } } delete[] output;}/** * Query if algorithm support a particular protocol: * that method is probably called before a getData * method. */bool ClusteringModule::supportProtocol(string protocol){ /** * That base class do not support any protocol. */ return false;} /** * That method is used to get data from an algorithm * that was check to implements a particular protocol */void * ClusteringModule::getData(string data){ /** * That base class do not provide any data */ return NULL;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -