#ifndef MKCOMMMODEL_H #define MKCOMMMODEL_H #include #include #include #include #include "include/bson/bson.h" #include "include/commmodel.hpp" #include "MKConfig.h" #include "MKLib.h" #include "MKNode.h" #include "MKPacket.h" #include "HeartBeatListener.h" #include "MKControl.h" /*! \brief A model of a MESH that is running heartbeats. */ class MKCommModel : public CommModel { public: friend class HeartBeatListenerTests; friend class MKControlTests; friend class MKCommModelTests; MKCommModel(); ~MKCommModel(); /*! \brief Returns a node with the specified MAC address. \param tar_mac_addr The mac address of the node to return. \return The node with the given MAC address or NULL if a node with the given MAC address does not exist. */ virtual node* get_node(mac_t tar_mac_addr); /*! Used for testing ease ONLY */ virtual node* get_node(ip4_t tar_ip_addr); /*! \brief Returns current measured directional throughput from n1 to n2. \param n1 The source node. \param n2 The destination node. \pre n1 is not NULL and must exist. \pre n2 is not NULL and must exist. \return The current measured directional throughput from n1 to n2. */ virtual throughput_t get_throughput(node* n1, node* n2); /*! \brief Returns the current directional latency from n1 to n2. \param n1 The source node. \param n2 The destination node. \pre n1 is not NULL and must exist. \pre n2 is not NULL and must exist. \return The current directional latency from n1 to n2. */ virtual latency_t get_latency(node* n1, node* n2); /*! \brief Returns the current the signal strength of n2 as measured by n1? \param n1 The node that reads the signal strength. \param n2 The node that emmits the signal. \pre n1 is not NULL and must exist. \pre n2 is not NULL and must exist. \return The current current signal strength of n2 as measured by n1. */ virtual signal_strength_t get_signal_strength(node* n1, node* n2); /*! \brief Returns the weight from n1 to n2, as computed by the routing algorithm. \param n1 The source node. \param n2 The destination node. \pre n1 is not NULL. \pre n2 is not NULL. \return The weight from n1 from n2. */ virtual weight_t get_weight(node* n1, node* n2); /*! \brief Returns the amount of time that has elapsed since n1 has receieved a packet from n2. \param n1 The source node. \param n2 The destination node. \pre n1 is not NULL and must exist. \pre n2 is not NULL and must exist. \return The amount of time that has elapsed since n1 has receieved a packet from n2. */ virtual inactive_t get_inactive_time(node *n1, node *n2); /*! \brief Returns a copy of the list of all nodes. \return A copy of the list of all nodes. */ virtual std::vector get_all_nodes(); /*! \brief Returns a copy of the list of neighbors of the given node. \param n The node and must exist. \pre n is not NULL and must exist. \return A copy of the list of neighbors of the given node or NULL if the given node does not exist. */ virtual std::vector get_neighbors(node*); /** Set le target en le manet, le mem chose, n'est pas. */ virtual bool set_target(node*); /** Identify the ocu machine. Call set target first */ virtual bool set_ocu_machine(node*); /** Get the path that has been optimal computed path, per the MANET alg'm */ virtual std::vector get_preferred_path(); /*! \brief Returns the number of nodes in the mesh. \return Returns the number of nodes in the mesh. */ virtual int size(); /*! \brief Updates the model given a heartbeat packet. The caller has ownership of the heartbeat object. \param packet The heartbeat data \pre packet is not NULL. */ void update(HeartBeatPacket *packet); /*! \brief Updates the model given a bson object. The caller has ownership of the bson object. \param packet The bson data \pre packet is not NULL. */ void update(mongo::BSONObj *, MKRequest *req); /*! \brief Returns a handle to write to the log file. \return A handle to write to the log file. */ static std::ostream &log() { return *logh; } private: /*! Static initialization */ struct MKCommModelInit { MKCommModelInit() { MKCommModel::staticInit(); } }; static MKCommModelInit init; static void staticInit(); /*! \brief Returns the node with the given mac address. \param addr The mac address. \pre No two nodes have the same MAC address. \return The node with the given mac address or NULL if not found. */ MKNode *find(mac_t addr); MKNode *find(node* node); /*! \brief Returns the first node with the given ip address. \param addr The ip version 4 address. \return The node with the given ip address or NULL if not found. */ MKNode *findByIP(ip4_t addr); /*! The ocu and target nodes as set by the monitor */ node *ocu, *target; /*! Number of nodes in the mesh. */ int count; /*! The nodes keyed to mac address */ std::map *nodes; sem_t sem1; /*! Conversion table of ip-version-4-address to mac-address. */ std::map *ipTable; /*! The heartbeat listener thread. */ pthread_t heartBeatThread; /*! The Command and Control center */ MKControl *control; /*! Stream handler to the log */ static std::ostream *logh; }; #endif