Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fix some newly introduced smells
[simgrid.git] / examples / s4u / dht-chord / s4u_dht-chord.hpp
1 /* Copyright (c) 2016. The SimGrid Team.
2 * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #ifndef S4U_CHORD_HPP
8 #define S4U_CHORD_HPP
9 #include "simgrid/s4u.hpp"
10 #include <string>
11 #include <xbt/RngStream.h>
12 #include <xbt/ex.hpp>
13 #include <xbt/str.h>
14
15 #define MAX_SIMULATION_TIME 1000
16 #define PERIODIC_STABILIZE_DELAY 20
17 #define PERIODIC_FIX_FINGERS_DELAY 120
18 #define PERIODIC_CHECK_PREDECESSOR_DELAY 120
19 #define PERIODIC_LOOKUP_DELAY 10
20 #define SLEEP_DELAY 4.9999
21
22 extern int nb_bits;
23 extern int nb_keys;
24 extern int timeout;
25 extern int* powers2;
26
27 class HostChord {
28   RngStream stream_;
29   simgrid::s4u::Host* host = nullptr;
30
31 public:
32   static simgrid::xbt::Extension<simgrid::s4u::Host, HostChord> EXTENSION_ID;
33
34   explicit HostChord(simgrid::s4u::Host* ptr) : host(ptr)
35   {
36     std::string descr = std::string("RngSream<") + host->cname() + ">";
37     stream_           = RngStream_CreateStream(descr.c_str());
38   }
39
40   ~HostChord() { RngStream_DeleteStream(&stream_); };
41
42   RngStream getStream() { return stream_; };
43 };
44
45 /* Types of tasks exchanged between nodes. */
46 typedef enum {
47   FIND_SUCCESSOR,
48   FIND_SUCCESSOR_ANSWER,
49   GET_PREDECESSOR,
50   GET_PREDECESSOR_ANSWER,
51   NOTIFY,
52   SUCCESSOR_LEAVING,
53   PREDECESSOR_LEAVING,
54   PREDECESSOR_ALIVE,
55   PREDECESSOR_ALIVE_ANSWER
56 } e_message_type_t;
57
58 class ChordMessage {
59 public:
60   e_message_type_t type;              // type of message
61   std::string issuer_host_name;       // used for logging
62   int request_id     = -1;            // id (used by some types of messages)
63   int request_finger = 1;             // finger parameter (used by some types of messages)
64   int answer_id      = -1;            // answer (used by some types of messages)
65   simgrid::s4u::MailboxPtr answer_to; // mailbox to send an answer to (if any)
66
67   explicit ChordMessage(e_message_type_t type) : type(type)
68   {
69     issuer_host_name = simgrid::s4u::this_actor::host()->name();
70   }
71
72   ~ChordMessage() = default;
73 };
74
75 class Node {
76   int known_id_      = -1;
77   double start_time_ = -1;
78   double deadline_   = -1;
79   bool joined        = false;
80   int id_;                           // my id
81   int pred_id_ = -1;                 // predecessor id
82   simgrid::s4u::MailboxPtr mailbox_; // my mailbox
83   int* fingers_;                     // finger table,(fingers[0] is my successor)
84   int next_finger_to_fix;            // index of the next finger to fix in fix_fingers()
85   RngStream stream;
86
87 public:
88   explicit Node(std::vector<std::string> args);
89   ~Node();
90   void join(int known_id);
91   void leave();
92   void notifyAndQuit();
93
94   void randomLookup();
95   void setFinger(int finger_index, int id);
96   void fixFingers();
97   void printFingerTable();
98
99   void setPredecessor(int predecessor_id);
100   void checkPredecessor();
101   int remoteGetPredecessor(int ask_to);
102   int closestPrecedingFinger(int id);
103   int findSuccessor(int id);
104   int remoteFindSuccessor(int ask_to, int id);
105
106   void notify(int predecessor_candidate_id);
107   void remoteNotify(int notify_id, int predecessor_candidate_id);
108   void stabilize();
109   void handleMessage(ChordMessage* message);
110
111   void operator()()
112   {
113     simgrid::s4u::this_actor::sleep_for(start_time_);
114     if (known_id_ == -1) {
115       setPredecessor(-1); // -1 means that I have no predecessor
116       printFingerTable();
117       joined = true;
118     } else {
119       join(known_id_);
120     }
121
122     if (!joined)
123       return;
124     ChordMessage* message              = nullptr;
125     void* data                         = nullptr;
126     double now                         = simgrid::s4u::Engine::instance()->getClock();
127     double next_stabilize_date         = start_time_ + PERIODIC_STABILIZE_DELAY;
128     double next_fix_fingers_date       = start_time_ + PERIODIC_FIX_FINGERS_DELAY;
129     double next_check_predecessor_date = start_time_ + PERIODIC_CHECK_PREDECESSOR_DELAY;
130     double next_lookup_date            = start_time_ + PERIODIC_LOOKUP_DELAY;
131
132     while ((now < (start_time_ + deadline_)) && now < MAX_SIMULATION_TIME) {
133       data                             = nullptr;
134       simgrid::s4u::Comm& comm_receive = simgrid::s4u::this_actor::irecv(mailbox_, &data);
135       while ((now < (start_time_ + deadline_)) && now < MAX_SIMULATION_TIME && !comm_receive.test()) {
136         // no task was received: make some periodic calls
137         if (now >= next_stabilize_date) {
138           stabilize();
139           next_stabilize_date = simgrid::s4u::Engine::getClock() + PERIODIC_STABILIZE_DELAY;
140         } else if (now >= next_fix_fingers_date) {
141           fixFingers();
142           next_fix_fingers_date = simgrid::s4u::Engine::getClock() + PERIODIC_FIX_FINGERS_DELAY;
143         } else if (now >= next_check_predecessor_date) {
144           checkPredecessor();
145           next_check_predecessor_date = simgrid::s4u::Engine::getClock() + PERIODIC_CHECK_PREDECESSOR_DELAY;
146         } else if (now >= next_lookup_date) {
147           randomLookup();
148           next_lookup_date = simgrid::s4u::Engine::getClock() + PERIODIC_LOOKUP_DELAY;
149         } else {
150           // nothing to do: sleep for a while
151           simgrid::s4u::this_actor::sleep_for(SLEEP_DELAY);
152         }
153         now = simgrid::s4u::Engine::getClock();
154       }
155
156       if (data != nullptr) {
157         message = static_cast<ChordMessage*>(data);
158         handleMessage(message);
159       }
160       now = simgrid::s4u::Engine::getClock();
161     }
162     if (data != nullptr) {
163       delete static_cast<ChordMessage*>(data);
164     }
165     // leave the ring
166     leave();
167   }
168 };
169
170 #endif