Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
adding sleep sets to reduction techniques
[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 extern int nb_bits;
22 extern int nb_keys;
23 extern int timeout;
24
25 /* Types of tasks exchanged between nodes. */
26 enum class MessageType {
27   FIND_SUCCESSOR,
28   FIND_SUCCESSOR_ANSWER,
29   GET_PREDECESSOR,
30   GET_PREDECESSOR_ANSWER,
31   NOTIFY,
32   SUCCESSOR_LEAVING,
33   PREDECESSOR_LEAVING,
34   PREDECESSOR_ALIVE,
35   PREDECESSOR_ALIVE_ANSWER
36 };
37
38 class ChordMessage {
39 public:
40   MessageType type;                                                                    // type of message
41   std::string issuer_host_name = sg4::this_actor::get_host()->get_name();              // used for logging
42   int request_id     = -1;            // id (used by some types of messages)
43   int request_finger = 1;             // finger parameter (used by some types of messages)
44   int answer_id      = -1;            // answer (used by some types of messages)
45   sg4::Mailbox* answer_to      = nullptr;       // mailbox to send an answer to (if any)
46
47   explicit ChordMessage(MessageType type) : type(type) {}
48
49   static void destroy(void* message);
50 };
51
52 class Node {
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 public:
65   explicit Node(std::vector<std::string> args);
66   Node(const Node&) = delete;
67   Node& operator=(const Node&) = delete;
68   void join(int known_id);
69   void leave();
70   void notifyAndQuit();
71
72   void randomLookup();
73   void setFinger(int finger_index, int id);
74   void fixFingers();
75   void printFingerTable();
76
77   void setPredecessor(int predecessor_id);
78   void checkPredecessor();
79   int remoteGetPredecessor(int ask_to);
80   int closestPrecedingFinger(int id);
81   int findSuccessor(int id);
82   int remoteFindSuccessor(int ask_to, int id);
83
84   void notify(int predecessor_candidate_id);
85   void remoteNotify(int notify_id, int predecessor_candidate_id) const;
86   void stabilize();
87   void handleMessage(ChordMessage* message);
88
89   void operator()();
90 };
91
92 #endif