text: /*
   * Definition file for Agent AntHocNet
   *
   * @author Daniel Henrique Joppi 27/12/2008
   */
  
  #ifndef __ahn_router_h__
  #define __ahn_router_h__
  
  #include <agent.h>
  #include <node.h>
  #include <packet.h>
  #include <ip.h>
  #include <trace.h>
  #include <timer-handler.h>
  #include <random.h>
  #include <classifier-port.h>
  #include <tools/rng.h>
  
  #include "trace/cmu-trace.h"
  #include "tools/queue-monitor.h"
  #include "queue/drop-tail.h"
  
  #include "anthocnet/ant_packet.h"
  #include "anthocnet/ahn_traffic.h"
  #include "anthocnet/ahn_ant_nest.h"
  
  #include <map>
  #include <vector>
  #include <list>
  
  #define ANT_LINK_LAYER_DETECTION
  
  /// Macro to retrieve current simulator time
  #define HELLO_INTERVAL          1               // 1000 ms
  #define MaxHelloInterval        (1.25 * HELLO_INTERVAL)
  #define MinHelloInterval        (0.75 * HELLO_INTERVAL)
  
  #define PROACTIVE_INTERVAL      10             // 10000 ms
  #define MaxProactiveInterval    (2.5 * PROACTIVE_INTERVAL)
  #define MinProactiveInterval    (1.0 * PROACTIVE_INTERVAL)
  
  #define LIFE_ANT 				2			  // 2000 ms
  
  // Various constants used for the expanding ring search
  #define TTL_START     5
  #define TTL_THRESHOLD 7
  #define TTL_INCREMENT 2
  
  // Should be set by the user using best guess (conservative)
  #define NETWORK_DIAMETER        30             // 30 hops
  
  // The followings are used for the forward() function. Controls pacing.
  #define DELAY 1.0           // random delay
  #define NO_DELAY -1.0       // no delay
  
  /// file name in which routing tables generated by AntNet are dumped
  #define file_rtable "rtable.txt"
  
  class AntTimer;
  class AntHelloTimer;
  class AntHocNet; // forward declaration
  
  /**
   *  Class to implement timer for interval between generation of forward ants
   *
   *  @author Daniel Henrique Joppi 17/03/2009
   */
  class AntTimer:
  		public TimerHandler
  {
  	public:
  		AntTimer(AntHocNet* agent) :
  			TimerHandler()
  		{
  			agent_ = agent;
  		}
  	protected:
  		AntHocNet* agent_;
  		virtual void expire(Event* e);
  };
  
  class AntHelloTimer :
  		public Handler
  {
  	public:
  		AntHelloTimer(AntHocNet* a) : agent(a) {}
  		virtual ~AntHelloTimer() { }
  		void handle(Event*);
  	private:
  		AntHocNet *agent;
  		Event	intr;
  };
  
  class AntNeighborTimer : public Handler {
  	public:
  		AntNeighborTimer(AntHocNet* a) : agent(a) {}
  		virtual ~AntNeighborTimer() { }
  		void handle(Event*);
  	private:
  		AntHocNet *agent;
  		Event	intr;
  };
  
  class AntProactiveTimer :
  		public Handler
  {
  	public:
  		AntProactiveTimer(AntHocNet* a) : agent(a) {}
  		virtual ~AntProactiveTimer() { }
  		void handle(Event*);
  	private:
  		AntHocNet *agent;
  		Event	intr;
  };
  
  
  
  class AntRouteCacheTimer : public Handler {
  	public:
  		AntRouteCacheTimer(AntHocNet* a) : agent(a) {}
  		virtual ~AntRouteCacheTimer() { }
  		void	handle(Event*);
  	private:
  		AntHocNet    *agent;
  		Event	intr;
  };
  
  /**
   *  Class to implement AntHocNet agent
   *
   *  This agent implements AntNet algorithm
   *
   *  @author Daniel Henrique Joppi 17/03/2009
   */
  class AntHocNet:
  		public Agent
  {
  		friend class AntTimer;
  		friend class AntHelloTimer;
  		friend class AntProactiveTimer;
  		friend class AntNeighborTimer;
  		friend class AntRouteCacheTimer;
  
  	private:
  		AntNest* ant_nest_; // instance of ant nest class
  		state_t state_; // local traffic model
  		window_t window_; // window of trip times to all destinations
  
  		// parameters that can be set from tcl script
  		// default values defined in ns-default.tcl
  		double r_factor_; // reinforcement factors
  		double timer_ant_; // interval between generation of forward ants
  		int num_nodes_; // total number of nodes in topology
  		int num_nodes_x_; // number of nodes in row (only for regular mesh topology)
  		int num_nodes_y_; // number of nodes in column (only for regular mesh topology)
  
  		AntNeighborTime nb_timer_list;
  
  	protected:
  		PortClassifier* dmux_; // for passing packets to agent
  		Trace* logtarget_; // for logging
  
  		AntTimer ant_timer_; // timer for generation of ants
  		AntProactiveTimer proactive_timer_;
          AntHelloTimer hello_timer_;
          AntNeighborTimer neighbor_timer_;
          AntRouteCacheTimer router_cache_;
  
  		inline int& num_nodes_x()
  		{
  			return num_nodes_x_;
  		}
  		inline int& num_nodes_y()
  		{
  			return num_nodes_y_;
  		}
  		inline state_t& state()
  		{
  			return state_;
  		}
  		inline window_t& window()
  		{
  			return window_;
  		}
  
  		/*
  		 * Route Table Management
  		 */
  		void initializeRouterTable(); // initialize routing table
  		void search(Packet* p);
  		void updateRouterTable(Packet*); // update routing table
  		void purgeMissNeighbor(); // remove old neighbors
  		void purgeQueue(); // reapair router
  		void sendPacketsInQueue(Packet *p); // send packets
  
          /*
           * Neighbor Management
           */
  		/// add two nodes to each other's neighbor list (assuming duplex link)
  		void addNeighbor(Node* node1, Node* node2);
  		/// print neighbors of a node
  		// implemented to test and debug
  		void printNeighbors();
  		void debug(const char* str, Packet *p);
  
  
          /*
           * Packet TX Routines
           */
          void forward(Packet *p, double delay);
  		void sendAntHello(); // send hello ant
  		void sendREFA(nsaddr_t destination); //send reactive forward ant
  		void sendPRFA(); // send proative forward ant
  		void sendPRFA(nsaddr_t destination); // send proative forward ant
  		void sendPRFA(Packet* p); // send proative forward ant
  		void sendRRFA(nsaddr_t destination); // receive repair forward ant
  		void sendREBA(Packet* p); //send reactive back ant
  		void sendPRBA(Packet* p); // send proative back ant
  		void sendRRBA(Packet* p); // receive repair back ant
  		void sendAntPacket(Packet* p, int typeAnt); // send a forward ant to next hop as per AntNet algorithm
  
          /*
           * Packet RX Routines
           */
  		void recvAHN(Packet *p); // check receive an ant packet
  		void recvAntHello(Packet* p); // receive hello ant
  		void recvREFA(Packet* p); // receive reactive forward ant
  		void recvPRFA(Packet* p); // receive proative forward ant
  		void recvRRFA(Packet* p); // receive repair forward ant
  		void recvREBA(Packet* p); // receive reactive back ant
  		void recvPRBA(Packet* p); // receive proative back ant
  		void recvRRBA(Packet* p); // receive repair back ant
  		void recvAntPacket(Packet* p, int typeAnt); // receive an ant packet
  
  		/*
  		 * Route Table Management
  		 */
  		void resetAntTimer(); // reset ant timer
  
  
  
  	public:
  		AntHocNet(nsaddr_t); // Constructor
  		int command(int, const char* const *); // interface for tcl commands
  		void recv(Packet*, Handler*); // method to handle packet recieve events at the Agent
  
  		void link_failed(Packet *p);
  };
  
  #endif /* _ahn_router_h_ */
  