Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
int->size_t
[simgrid.git] / examples / cpp / dht-chord / s4u-dht-chord.hpp
1 /* Copyright (c) 2016-2022. 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 #include <xbt/str.h>
12
13 namespace sg4 = simgrid::s4u;
14
15 constexpr double MAX_SIMULATION_TIME              = 1000;
16 constexpr double PERIODIC_STABILIZE_DELAY         = 20;
17 constexpr double PERIODIC_FIX_FINGERS_DELAY       = 120;
18 constexpr double PERIODIC_CHECK_PREDECESSOR_DELAY = 120;
19 constexpr double PERIODIC_LOOKUP_DELAY            = 10;
20 constexpr double SLEEP_DELAY                      = 4.9999;
21
22 extern int nb_bits;
23 extern int nb_keys;
24 extern int timeout;
25
26 /* Types of tasks exchanged between nodes. */
27 enum class MessageType {
28   FIND_SUCCESSOR,
29   FIND_SUCCESSOR_ANSWER,
30   GET_PREDECESSOR,
31   GET_PREDECESSOR_ANSWER,
32   NOTIFY,
33   SUCCESSOR_LEAVING,
34   PREDECESSOR_LEAVING,
35   PREDECESSOR_ALIVE,
36   PREDECESSOR_ALIVE_ANSWER
37 };
38
39 class ChordMessage {
40 public:
41   MessageType type;                                                                    // type of message
42   std::string issuer_host_name = sg4::this_actor::get_host()->get_name();              // used for logging
43   int request_id     = -1;            // id (used by some types of messages)
44   int request_finger = 1;             // finger parameter (used by some types of messages)
45   int answer_id      = -1;            // answer (used by some types of messages)
46   sg4::Mailbox* answer_to      = nullptr;       // mailbox to send an answer to (if any)
47
48   explicit ChordMessage(MessageType type) : type(type) {}
49
50   static void destroy(void* message);
51 };
52
53 class Node {
54   int known_id_      = -1;
55   double start_time_ = -1;
56   double deadline_   = -1;
57   bool joined        = false;
58   int id_;                           // my id
59   int pred_id_ = -1;                 // predecessor id
60   simgrid::xbt::random::XbtRandom random; // random number generator for this node
61   sg4::Mailbox* mailbox_;                 // my mailbox
62   std::vector<int> fingers_;         // finger table,(fingers[0] is my successor)
63   int next_finger_to_fix;            // index of the next finger to fix in fix_fingers()
64
65 public:
66   explicit Node(std::vector<std::string> args);
67   Node(const Node&) = delete;
68   Node& operator=(const Node&) = delete;
69   void join(int known_id);
70   void leave();
71   void notifyAndQuit();
72
73   void randomLookup();
74   void setFinger(int finger_index, int id);
75   void fixFingers();
76   void printFingerTable();
77
78   void setPredecessor(int predecessor_id);
79   void checkPredecessor();
80   int remoteGetPredecessor(int ask_to);
81   int closestPrecedingFinger(int id);
82   int findSuccessor(int id);
83   int remoteFindSuccessor(int ask_to, int id);
84
85   void notify(int predecessor_candidate_id);
86   void remoteNotify(int notify_id, int predecessor_candidate_id) const;
87   void stabilize();
88   void handleMessage(ChordMessage* message);
89
90   void operator()();
91 };
92
93 #endif