📄 mpr.h
字号:
/** * 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. * */ #ifndef _MPR_H_#define _MPR_H_#include <vector>//////////////////// Node's state ////////////////////typedef enum { MPR_STATUS_HELLO = 0x1, // Node is collecting neighbors information. MPR_STATUS_FIRST_DECISION = 0x2, // Node is waiting information about MPR set. MPR_STATUS_LAST_DECISION = 0x3, // Node is waiting last color from neighbors. MPR_STATUS_END = 0x4 // Node ended coloration algorithm.} MprStatus;//////////////////////// Tipi di messaggi ////////////////////////typedef enum{ MPR_NEIGHBORS = 0x0, // Node send its neighbors list. MPR_SELECTION = 0x1, // A Node communicate to a neighbor its selection about MPR set. MPR_COLOR = 0x2, // A Node communicate final color to neighbors. MPR_REQUEST = 0x3, // Request of retransmission. MPR_DATA = 0x4 // Data message.} MprMessageType;//////////////////////////// Header del pacchetto ////////////////////////////struct hdr_mpr { MprMessageType msg_type; // Message Type. MprStatus status; // Request message. NodeList * node_list; // Neighbors list. bool mpr_selected; // Notify if a node was selected as MPR. Color color; // Used to notify last color. //////////////////// // Accessibilita' // //////////////////// static int offset_; inline static int& offset() { return offset_; } inline static hdr_mpr* access(const Packet* p) { return (hdr_mpr*) p->access(offset_); }};///////////// Timer /////////////class MprTimer;///////////// Agent /////////////class MPR_Agent : public ClusteringModule{public: MPR_Agent(); // Funzione di ricezione dell'algoritmo di clustering. virtual void receive(Packet * p, Handler * h); // Fa partire il modulo. virtual void startModule(); // Termine del lavoro del modulo. virtual void endModule(); // Segnala lo scadere di un timeout per un messaggio. void timeout(MprStatus type, NodeAddress from); // Segnala il raggiungimento del massimo numero di timeout per un messaggio. void lastTimeout(MprStatus type, NodeAddress from); // Verifica che un pacchetto sia di tipo DATA. virtual bool isDataPacket(Packet * p); // Preparazione all'invio di un messaggio al layer inferiore virtual void prepareSendDataDown(Packet * p); protected: MprStatus status; // Agent's state. NodeNeighbors nodeNeighbors; // Neighbors of node's neighbors. NodeList mprNeighbors; // MPR Set of node. NodeList twoHopNeighbors; // Two Hop Neighbors. map<NodeAddress, bool> mprSelection; // Final MPR selection for interested nodes. MprTimer * timer; // Timeout manager. NodeColor finalColor; // Final color of node and neighbors. int selection; // MPR selection of smallest neighbor. NodeAddress smallerNeighbor; // Smaller Neighbors between its neighbors and current node. protected: /////////////// // Procedure // /////////////// // Manage phase change. void processStatus(); // Send neighbors list to neighbors. void send_NEIGHBORS(NodeAddress to); // Notify final color. void send_COLOR(NodeAddress to, Color color); // Notify MPR selection. void send_MPR(NodeAddress to, bool selection); // Invia la richiesta di un certo messaggio ad un vicino. void send_REQUEST(MprStatus message, NodeAddress to); // Ricezione della lista dei vicini di un vicino. void receive_NEIGHBORS(NodeAddress from, NodeList & neighbors); // Ricezione del colore NERO di un vicino per una fase del protocollo. void receive_COLOR(NodeAddress from, Color color); // Ricezione del colore NERO di un vicino per una fase del protocollo. void receive_MPR(NodeAddress from, bool selected); // Ricezione di una richiesta di ritrasmissione per un messaggio da parte di un vicino. void receive_REQUEST(NodeAddress from, MprStatus message); //////////////////// // Interrogazioni // //////////////////// // Verifica se due vicini del nodo solo NON connessi. bool twoNeighborsAreUnlinked(); // Verifica la REGOLA sulla base delle lunghezze specificate. void applyRules(); // Calculate MPR set (rule 1 and 2). void calculateMPRSet(); // Restituisce il piu'grande vicino NERO. NodeAddress greaterCluster(); public: static int enhanced; // Enanched Rule. static int enhanced_2; // Enhanced Rule 2. static int _DEBUG_; static double max_delay; static int max_timeout_hello; static double jitter_timeout_hello; static double timeout_hello; static int max_timeout_first_decision; static double jitter_timeout_first_decision; static double timeout_first_decision; static int max_timeout_last_decision; static double jitter_timeout_last_decision; static double timeout_last_decision;};/////////////////////////////////// Timer per le ritrasmissioni ///////////////////////////////////class MprTimer: public Handler {public: MprTimer(MPR_Agent * a) : Handler(), agent(a) {} void handle(Event* e); // Inizializza i timeout di un nodo void initTimeouts(NodeList & neighbors); // Lancia tutti i timeout per i messaggi HELLO. void launchHelloTimeouts(); // Lancia tutti i timeout per i messaggi FIRST DECISION. void launchFirstDecisionTimeout(NodeAddress node); // Lancia tutti i timeout per i messaggi SECOND DECISION. void launchLastDecisionTimeouts(); // Viene segnalata la ricezione di un messaggio di HELLO: si elimina il timeout. void receivedHello(NodeAddress from); // Viene segnalata la ricezione di un messaggio di FIRST DECISION: si elimina il timeout. void receivedFirstDecision(NodeAddress from); // Viene segnalata la ricezione di un messaggio di SECOND DECISION: si elimina il timeout. void receivedLastDecision(NodeAddress from); // Verifica che il timeout sia per il messaggio di HELLO. bool isHelloTimeout(Event * e); // Verifica che il timeout sia per il messaggio di FIRST DECISION. bool isFirstDecisionTimeout(Event * e); // Verifica che il timeout sia per il messaggio di LAST DECISION. bool isLastDecisionTimeout(Event * e); protected: // Timeout attivi per i messaggi HELLO. TimeoutMap hello_timeouts; // Timeout attivi per i messaggi FIRST DECISION (MPR Selection). TimeoutMap first_decision_timeouts; // Timeout attivi per i messaggi LAST DECISION (Color). TimeoutMap last_decision_timeouts; // Agente proprietario del Timer. MPR_Agent * agent;};#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -