Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines with new year.
[simgrid.git] / examples / s4u / app-chainsend / s4u-app-chainsend.cpp
index dcf8e3c..6da10d6 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2007-2019. The SimGrid Team. All rights reserved.          */
+/* Copyright (c) 2007-2020. The SimGrid Team. All rights reserved.          */
 
 /* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */
@@ -47,7 +47,7 @@ public:
 
   void joinChain()
   {
-    ChainMessage* msg = static_cast<ChainMessage*>(me->get());
+    const ChainMessage* msg = static_cast<ChainMessage*>(me->get());
     prev              = msg->prev_;
     next              = msg->next_;
     total_pieces      = msg->num_pieces;
@@ -96,36 +96,18 @@ public:
 
   void buildChain()
   {
-    auto cur                      = mailboxes.begin();
-    simgrid::s4u::Mailbox* prev   = nullptr;
-    simgrid::s4u::Mailbox* last   = nullptr;
-
     /* Build the chain if there's at least one peer */
-    if (cur != mailboxes.end()) {
-      /* init: prev=NULL, host=current cur, next=next cur */
-      simgrid::s4u::Mailbox* next   = *cur;
-      first                         = next;
-
-      /* This iterator iterates one step ahead: cur is current iterated element, but is actually next in the chain */
-      do {
-        /* following steps: prev=last, host=next, next=cur */
-        ++cur;
-        prev                                     = last;
-        simgrid::s4u::Mailbox* current_mailbox   = next;
-        if (cur != mailboxes.end())
-          next = *cur;
-        else
-          next = nullptr;
-
-        XBT_DEBUG("Building chain--broadcaster:\"%s\" dest:\"%s\" prev:\"%s\" next:\"%s\"",
-                  simgrid::s4u::Host::current()->get_cname(), current_mailbox->get_cname(),
-                  prev ? prev->get_cname() : nullptr, next ? next->get_cname() : nullptr);
-
-        /* Send message to current peer */
-        current_mailbox->put(new ChainMessage(prev, next, piece_count), MESSAGE_BUILD_CHAIN_SIZE);
-
-        last = current_mailbox;
-      } while (cur != mailboxes.end());
+    if (not mailboxes.empty())
+      first = mailboxes.front();
+
+    for (unsigned i = 0; i < mailboxes.size(); i++) {
+      simgrid::s4u::Mailbox* prev = i > 0 ? mailboxes[i - 1] : nullptr;
+      simgrid::s4u::Mailbox* next = i < mailboxes.size() - 1 ? mailboxes[i + 1] : nullptr;
+      XBT_DEBUG("Building chain--broadcaster:\"%s\" dest:\"%s\" prev:\"%s\" next:\"%s\"",
+                simgrid::s4u::Host::current()->get_cname(), mailboxes[i]->get_cname(),
+                prev ? prev->get_cname() : nullptr, next ? next->get_cname() : nullptr);
+      /* Send message to current peer */
+      mailboxes[i]->put(new ChainMessage(prev, next, piece_count), MESSAGE_BUILD_CHAIN_SIZE);
     }
   }