⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 antnet.cc.bak

📁 基于 NS2 的 AntNet 源代码
💻 BAK
📖 第 1 页 / 共 2 页
字号:
/* * antnet.cc * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License, * version 2, as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU General Public License for more details. * * If you are using this program for any publication, we kindly request that you cite: * "Ant Colony Optimisation Based Routing on NS-2",  * V. Laxmi, Lavina Jain and M. S. Gaur,  * International Conference on Wireless Communication and Sensor Networks (WCSN),  * India, December 2006. *  * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. * * Author: Lavina Jain * *//////////////////////////////////////////////////// \file antnet.cc/// \brief Implementation file for Agent Antnet////////////////////////////////////////////////#include "antnet.h"#include "address.h" // to fascilitate inlines#include <stdio.h>#include <stdlib.h>#include <math.h> int hdr_ant_pkt::offset_;	///< to access ant packet header  偏移量extern double r;		///< reinforcement factorextern int N;			///< number of neighbors of a nodeextern int NUM_NODES;		///< total number of nodes in the topology////////////////////////////////////////////////////////////////////////////// \brief tcl binding for new packet: Ant///////////////////////////////////////////////////////////////////////////static class AntHeaderClass : public PacketHeaderClass { 	public: 	AntHeaderClass() : PacketHeaderClass("PacketHeader/Ant",sizeof(hdr_ant_pkt)) { 		bind_offset(&hdr_ant_pkt::offset_); 	} } class_rtProtoAnt_hdr;////////////////////////////////////////////////////////////////////////////// \brief tcl binding for new Agent: Antnet///////////////////////////////////////////////////////////////////////////static class AntnetClass : public TclClass {	public:	AntnetClass() : TclClass("Agent/Antnet") {}	TclObject* create(int argc, const char*const* argv) {		assert(argc == 5);		return (new Antnet((nsaddr_t)Address::instance().str2addr(argv[4])));	}} class_rtProtoAntnet; ////////////////////////////////////////////////////////////////////////////// tcl binding for agent parameters/// default values defined in ns-default.tcl/////////////////////////////////////////////////////////////////////////// Antnet::Antnet(nsaddr_t id) : Agent(PT_ANT), ant_timer_(this), dmux_(0) {		bind("num_nodes_", &num_nodes_);	// number of nodes in topology	bind("num_nodes_x_", &num_nodes_x_);	// number of nodes in row (for regular mesh topology)	bind("num_nodes_y_", &num_nodes_y_);	// number of nodes in column (for regular mesh topology)	bind("r_factor_", &r_factor_);		// reinforcement factor    r = r_factor_	bind("timer_ant_", &timer_ant_);	// timer for generation of forward ants		ra_addr_ = id;		// agent address 	ant_seq_num_ = 0;	// initialize sequence number of ant packets to zero}//////////////////////////////////////////////////////////////////// commands that the agent can handle/////////////////////////////////////////////////////////////////int Antnet::command(int argc, const char*const* argv) {	if (argc == 2) {		if(strcasecmp(argv[1], "start") == 0) {	// begin AntNet algorithm			initialize_rtable();	// initialize routing tables			ant_timer_.resched(0.);	// schedule timer to begin ant generation now			return TCL_OK;		}		else if(strcasecmp(argv[1], "stop") == 0) {	// stop AntNet algorithm			ant_timer_.cancel();	// cancel any scheduled timers			return TCL_OK;		}		else if (strcasecmp(argv[1], "print_rtable") == 0) {	// print routing tables to a file			FILE *fp = fopen(file_rtable,"a");	// file name defined in antnet_common.h			fprintf(fp,"\nRouting table at node %d\n",addr());			fclose(fp);			rtable_.print();	// call method to print routing table			return TCL_OK;		} 	}	else if (argc == 3) {		// obtain corresponding dmux to carry packets		if (strcmp(argv[1], "port-dmux") == 0) {			dmux_ = (PortClassifier*)TclObject::lookup(argv[2]);			if (dmux_ == 0) {				fprintf(stderr, "%s: %s lookup of %s failed\n",__FILE__,argv[1],argv[2]);				return TCL_ERROR; 			}			return TCL_OK; 		}		// obtain corresponding tracer		else if (strcmp(argv[1], "log-target") == 0 || strcmp(argv[1], "tracetarget") == 0) {			logtarget_ = (Trace*)TclObject::lookup(argv[2]);			if (logtarget_ == 0)				return TCL_ERROR;			return TCL_OK;		}	}	// add node1 to neighbor list of node2 and vice-versa (we assume duplex link)	else if (argc == 4) {		if(strcmp(argv[1], "add-neighbor") == 0) {			Node *node1 = (Node*)TclObject::lookup(argv[2]);			Node *node2 = (Node*)TclObject::lookup(argv[3]);			add_Neighbor(node1, node2);			return TCL_OK;		}	} 	// Pass the command to the base class	return Agent::command(argc, argv);}//////////////////////////////////////////////////////////////////// Agent recieves ant packets/////////////////////////////////////////////////////////////////void Antnet::recv(Packet *p, Handler *h) {	struct hdr_cmn *ch = HDR_CMN(p);	// common header	struct hdr_ip *ih = HDR_IP(p);		// ip header	struct hdr_ant_pkt *ah = HDR_ANT_PKT(p);// ant header			if((int)ih->saddr() == ra_addr()) {		// If loop, drop the packet		if (ch->num_forwards() > 0) {			drop(p, DROP_RTR_ROUTE_LOOP);		}		// else if reciever is the source		else if (ch->num_forwards() == 0){				}	}	else {		// recieved packet is an Ant packet		if(ch->ptype() == PT_ANT) {			// call method to handle ant packet			recv_ant_pkt(p);		}		// if not Ant packet, drop		else {			drop(p, DROP_RTR_ROUTE_LOOP);		}	}}//////////////////////////////////////////////////////////////////// Method to send a forward ant/// Called when ant timer expires/////////////////////////////////////////////////////////////////void Antnet::send_ant_pkt() {	nsaddr_t next, dest;	Packet* p = allocpkt();			// allocate new packet	struct hdr_cmn* ch = HDR_CMN(p);	// common header	struct hdr_ip* ih = HDR_IP(p);		// ip header	struct hdr_ant_pkt* ah = HDR_ANT_PKT(p);// ant header		ah->pkt_type() = FORWARD_ANT;		// set ant type as FORWARD ant	ah->pkt_src() = addr();			// source address	ah->pkt_len() = ANT_SIZE;		// length of ant header	ah->pkt_seq_num() = ant_seq_num_++;	// sequence number	ah->pkt_start_time() = CURRENT_TIME;	// packet generation time	dest = rtable_.calc_destination(addr());// generate random destination	ah->pkt_dst() = dest;			// set packet destination	ah->pkt_mem_size() = 0;			// initialize size of memory	ah->pkt_memory_[0].node_addr = addr();	// add source node to memory	ah->pkt_memory_[0].trip_time = 0.0;	// add trip time to this node to memory	ah->pkt_mem_size()++;			// increment size of memory			ch->ptype() = PT_ANT;			// set packet type as Ant	ch->direction() = hdr_cmn::DOWN;	// forward ant	ch->size() = IP_HDR_LEN + ah->pkt_len();// packet header size	ch->error() = 0;	ch->addr_type() = NS_AF_INET;	// generate next hop as per AntNet algorithm	next = rtable_.calc_next(addr(), ah->pkt_dst(), addr());	// if next hop same as this node, release packet	if(next == addr()) {		Packet::free(p);		return;	}	ch->next_hop() = next;		// set next hop address in common header		ih->saddr() = addr();		// set source address in ip header	ih->daddr() = next;		// set destination address in ip header	ih->ttl() = 2 * (NUM_NODES);	// set time-to-live	if(DEBUG)		fprintf(stdout,"sending antnet packet from %d to %d next hop %d\n", ah->pkt_src(), ah->pkt_dst(), ih->daddr());		target_->recv(p);	// send forward ant packet}////////////////////////////////////////////////////////////////////// Method to recieve Ant packet at the Agent/// Calls appropriate methods to process forward and backward ants///////////////////////////////////////////////////////////////////void Antnet::recv_ant_pkt(Packet* p) {	struct hdr_ip* ih = HDR_IP(p);		// ip header	struct hdr_cmn* ch = HDR_CMN(p);	// common header	struct hdr_ant_pkt* ah = HDR_ANT_PKT(p);// ant header		assert(ih->sport() == RT_PORT);	assert(ih->dport() == RT_PORT);		if(DEBUG)		printf("In recv_antnet_pkt() %d at node %d %d source %d dest %d\n", ch->direction(), addr(), ih->daddr(), ah->pkt_src(), ah->pkt_dst());		if(ch->direction() == hdr_cmn::DOWN) {	// forward ant		if(addr() == ah->pkt_dst()) {	// destination node			// add this node to memory			memorize(p);			// create backward ant			create_backward_ant_pkt(p);		}		else {		// not destination node			// add this node to memory			memorize(p);			// send forward ant to next hop node as determined by AntNet algorithm			forward_ant_pkt(p);		}	}	else 	if(ch->direction() == hdr_cmn::UP) {	// backward ant		if(addr() == ah->pkt_dst()) {	// destination node, travel complete		    //00000000000000		     //update_traffic(p);			// update routing table			update_table(p);			// release packet			Packet::free(p);			return;		}		else {		// not destination node		     //00000000000000000000		    // update_traffic(p);			// update routing table			update_table(p);			// send backward ant to next hop node as determined by memory			backward_ant_pkt(p);		}	}}////////////////////////////////////////////////////////////////////// Method to build meory of forward ant///////////////////////////////////////////////////////////////////void Antnet::memorize(Packet* p) {	struct hdr_ant_pkt* tmp = HDR_ANT_PKT(p);	// ant header		double time = CURRENT_TIME - tmp->pkt_start_time();	// trip time to this node		// If node revisited, there is a loop, remove loop and corresponding memory	for(int i=0; i<tmp->pkt_mem_size(); i++) {		if(tmp->pkt_memory_[i].node_addr == addr()) {			double t = time - tmp->pkt_memory_[i].trip_time;			tmp->pkt_mem_size() = i+1;			for(int j=0; j <= i; j++) {				tmp->pkt_memory_[j].trip_time += t;			}			return;		}	}		// add current node to memory	tmp->pkt_memory_[tmp->pkt_mem_size()].node_addr = addr();	tmp->pkt_memory_[tmp->pkt_mem_size()].trip_time = time;	tmp->pkt_mem_size() = tmp->pkt_mem_size()+1;	if(DEBUG) {		fprintf(stdout,"adding %d to memory of pkt %d\n", addr(), tmp->pkt_seq_num());	}}//////////////////////////////////////////////////////////////////////////////////////////// Method to send forward ant packet to next hop node as determined by AntNet algorithm////////////////////////////////////////////////////////////////////////////////////////void Antnet::forward_ant_pkt(Packet* p) {	struct hdr_ip* ih = HDR_IP(p);		// ip header	struct hdr_cmn* ch = HDR_CMN(p);	// common header	struct hdr_ant_pkt* ah = HDR_ANT_PKT(p);// ant header	nsaddr_t parent = ih->saddr();	// parent node	// find next hop node as per AntNet algorithm

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -