Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of https://framagit.org/simgrid/simgrid
[simgrid.git] / examples / cpp / dht-chord / s4u-dht-chord.hpp
1 /* Copyright (c) 2016-2023. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #ifndef S4U_CHORD_HPP
7 #define S4U_CHORD_HPP
8 #include "simgrid/s4u.hpp"
9 #include <string>
10 #include <xbt/random.hpp>
11
12 namespace sg4 = simgrid::s4u;
13
14 constexpr double MAX_SIMULATION_TIME              = 1000;
15 constexpr double PERIODIC_STABILIZE_DELAY         = 20;
16 constexpr double PERIODIC_FIX_FINGERS_DELAY       = 120;
17 constexpr double PERIODIC_CHECK_PREDECESSOR_DELAY = 120;
18 constexpr double PERIODIC_LOOKUP_DELAY            = 10;
19 constexpr double SLEEP_DELAY                      = 4.9999;
20
21 /* Types of tasks exchanged between nodes. */
22 enum class MessageType {
23   FIND_SUCCESSOR,
24   FIND_SUCCESSOR_ANSWER,
25   GET_PREDECESSOR,
26   GET_PREDECESSOR_ANSWER,
27   NOTIFY,
28   SUCCESSOR_LEAVING,
29   PREDECESSOR_LEAVING,
30   PREDECESSOR_ALIVE,
31   PREDECESSOR_ALIVE_ANSWER
32 };
33
34 class ChordMessage {
35 public:
36   MessageType type;                                                                    // type of message
37   std::string issuer_host_name = sg4::this_actor::get_host()->get_name();              // used for logging
38   int request_id     = -1;            // id (used by some types of messages)
39   int request_finger = 1;             // finger parameter (used by some types of messages)
40   int answer_id      = -1;            // answer (used by some types of messages)
41   sg4::Mailbox* answer_to      = nullptr;       // mailbox to send an answer to (if any)
42
43   explicit ChordMessage(MessageType type) : type(type) {}
44
45   static void destroy(void* message);
46 };
47
48 class Node {
49   inline static int nb_bits_;
50   inline static int nb_keys_;
51   inline static int timeout_;
52
53   int known_id_      = -1;
54   double start_time_ = -1;
55   double deadline_   = -1;
56   bool joined_       = false;
57   int id_;                           // my id
58   int pred_id_ = -1;                 // predecessor id
59   simgrid::xbt::random::XbtRandom random_; // random number generator for this node
60   sg4::Mailbox* mailbox_;                  // my mailbox
61   std::vector<int> fingers_;         // finger table,(fingers[0] is my successor)
62   int next_finger_to_fix_;           // index of the next finger to fix in fix_fingers()
63
64   static bool is_in_interval(int id, int start, int end);
65
66 public:
67   static void set_parameters(int nb_bits, int nb_keys, int timeout);
68
69   explicit Node(std::vector<std::string> args);
70   Node(const Node&) = delete;
71   Node& operator=(const Node&) = delete;
72   void join(int known_id);
73   void leave();
74   void notifyAndQuit();
75
76   void randomLookup();
77   void setFinger(int finger_index, int id);
78   void fixFingers();
79   void printFingerTable();
80
81   void setPredecessor(int predecessor_id);
82   void checkPredecessor();
83   int remoteGetPredecessor(int ask_to);
84   int closestPrecedingFinger(int id);
85   int findSuccessor(int id);
86   int remoteFindSuccessor(int ask_to, int id);
87
88   void notify(int predecessor_candidate_id);
89   void remoteNotify(int notify_id, int predecessor_candidate_id) const;
90   void stabilize();
91   void handleMessage(ChordMessage* message);
92
93   void operator()();
94 };
95
96 #endif