Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Kill dead code.
[simgrid.git] / examples / s4u / dht-chord / s4u-dht-chord.hpp
1 /* Copyright (c) 2016-2019. 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 <random>
10 #include <string>
11 #include <xbt/str.h>
12
13 constexpr double MAX_SIMULATION_TIME              = 1000;
14 constexpr double PERIODIC_STABILIZE_DELAY         = 20;
15 constexpr double PERIODIC_FIX_FINGERS_DELAY       = 120;
16 constexpr double PERIODIC_CHECK_PREDECESSOR_DELAY = 120;
17 constexpr double PERIODIC_LOOKUP_DELAY            = 10;
18 constexpr double SLEEP_DELAY                      = 4.9999;
19
20 extern int nb_bits;
21 extern int nb_keys;
22 extern int timeout;
23
24 extern std::default_random_engine generator;
25
26 /* Types of tasks exchanged between nodes. */
27 enum e_message_type_t {
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   e_message_type_t type;              // type of message
42   std::string issuer_host_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   simgrid::s4u::Mailbox* answer_to = nullptr;       // mailbox to send an answer to (if any)
47
48   explicit ChordMessage(e_message_type_t type)
49       : type(type), issuer_host_name(simgrid::s4u::this_actor::get_host()->get_name())
50   {
51   }
52
53   static void destroy(void* message);
54 };
55
56 class Node {
57   int known_id_      = -1;
58   double start_time_ = -1;
59   double deadline_   = -1;
60   bool joined        = false;
61   int id_;                           // my id
62   int pred_id_ = -1;                 // predecessor id
63   simgrid::s4u::Mailbox* mailbox_;   // my mailbox
64   std::vector<int> fingers_;         // finger table,(fingers[0] is my successor)
65   int next_finger_to_fix;            // index of the next finger to fix in fix_fingers()
66
67 public:
68   explicit Node(std::vector<std::string> args);
69   Node(const Node&) = delete;
70   Node& operator=(const Node&) = delete;
71   void join(int known_id);
72   void leave();
73   void notifyAndQuit();
74
75   void randomLookup();
76   void setFinger(int finger_index, int id);
77   void fixFingers();
78   void printFingerTable();
79
80   void setPredecessor(int predecessor_id);
81   void checkPredecessor();
82   int remoteGetPredecessor(int ask_to);
83   int closestPrecedingFinger(int id);
84   int findSuccessor(int id);
85   int remoteFindSuccessor(int ask_to, int id);
86
87   void notify(int predecessor_candidate_id);
88   void remoteNotify(int notify_id, int predecessor_candidate_id);
89   void stabilize();
90   void handleMessage(ChordMessage* message);
91
92   void operator()()
93   {
94     simgrid::s4u::this_actor::sleep_for(start_time_);
95     if (known_id_ == -1) {
96       setPredecessor(-1); // -1 means that I have no predecessor
97       printFingerTable();
98       joined = true;
99     } else {
100       join(known_id_);
101     }
102
103     if (not joined)
104       return;
105     void* data                         = nullptr;
106     double now                         = simgrid::s4u::Engine::get_clock();
107     double next_stabilize_date         = start_time_ + PERIODIC_STABILIZE_DELAY;
108     double next_fix_fingers_date       = start_time_ + PERIODIC_FIX_FINGERS_DELAY;
109     double next_check_predecessor_date = start_time_ + PERIODIC_CHECK_PREDECESSOR_DELAY;
110     double next_lookup_date            = start_time_ + PERIODIC_LOOKUP_DELAY;
111     simgrid::s4u::CommPtr comm_receive = nullptr;
112     while ((now < (start_time_ + deadline_)) && now < MAX_SIMULATION_TIME) {
113       if (comm_receive == nullptr)
114         comm_receive = mailbox_->get_async(&data);
115       while ((now < (start_time_ + deadline_)) && now < MAX_SIMULATION_TIME && not comm_receive->test()) {
116         // no task was received: make some periodic calls
117         if (now >= next_stabilize_date) {
118           stabilize();
119           next_stabilize_date = simgrid::s4u::Engine::get_clock() + PERIODIC_STABILIZE_DELAY;
120         } else if (now >= next_fix_fingers_date) {
121           fixFingers();
122           next_fix_fingers_date = simgrid::s4u::Engine::get_clock() + PERIODIC_FIX_FINGERS_DELAY;
123         } else if (now >= next_check_predecessor_date) {
124           checkPredecessor();
125           next_check_predecessor_date = simgrid::s4u::Engine::get_clock() + PERIODIC_CHECK_PREDECESSOR_DELAY;
126         } else if (now >= next_lookup_date) {
127           randomLookup();
128           next_lookup_date = simgrid::s4u::Engine::get_clock() + PERIODIC_LOOKUP_DELAY;
129         } else {
130           // nothing to do: sleep for a while
131           simgrid::s4u::this_actor::sleep_for(SLEEP_DELAY);
132         }
133         now = simgrid::s4u::Engine::get_clock();
134       }
135
136       if (data != nullptr) {
137         ChordMessage* message = static_cast<ChordMessage*>(data);
138         handleMessage(message);
139         comm_receive = nullptr;
140         data         = nullptr;
141       }
142       now = simgrid::s4u::Engine::get_clock();
143     }
144     if (comm_receive != nullptr) {
145       if (comm_receive->test())
146         delete static_cast<ChordMessage*>(data);
147       else
148         comm_receive->cancel();
149     }
150     // leave the ring
151     leave();
152   }
153 };
154
155 #endif