Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
include cleanups in src/s4u
[simgrid.git] / src / s4u / s4u_Link.cpp
index 65d544b..493d5c4 100644 (file)
@@ -3,18 +3,16 @@
 /* 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. */
 
-#include <algorithm>
+#include <simgrid/Exception.hpp>
+#include <simgrid/s4u/Engine.hpp>
+#include <simgrid/s4u/Link.hpp>
+#include <simgrid/sg_config.hpp>
+#include <simgrid/simix.hpp>
+#include <xbt/parse_units.hpp>
 
-#include "simgrid/Exception.hpp"
-#include "simgrid/s4u/Engine.hpp"
-#include "simgrid/s4u/Link.hpp"
-#include "simgrid/sg_config.hpp"
-#include "simgrid/simix.hpp"
 #include "src/surf/SplitDuplexLinkImpl.hpp"
 #include "src/surf/network_interface.hpp"
 #include "src/surf/network_wifi.hpp"
-#include "xbt/log.h"
-#include "xbt/parse_units.hpp"
 
 namespace simgrid {
 
@@ -37,10 +35,12 @@ Link* Link::by_name(const std::string& name)
 
 kernel::resource::LinkImpl* Link::get_impl() const
 {
-  xbt_assert(
-      get_sharing_policy() != SharingPolicy::SPLITDUPLEX,
-      "Impossible to get a LinkImpl* from a Split-Duplex link. You should call this method to each UP/DOWN member");
-  return dynamic_cast<kernel::resource::LinkImpl*>(pimpl_);
+  auto* link_impl = dynamic_cast<kernel::resource::LinkImpl*>(pimpl_);
+  xbt_assert(link_impl != nullptr, "Impossible to get a LinkImpl* from link. %s.",
+             (get_sharing_policy() == SharingPolicy::SPLITDUPLEX
+                  ? "For a Split-Duplex link, you should call this method to each UP/DOWN member"
+                  : "Please report this bug"));
+  return link_impl;
 }
 
 Link* Link::by_name_or_null(const std::string& name)
@@ -100,13 +100,13 @@ Link* Link::set_bandwidth(double value)
   return this;
 }
 
-Link* Link::set_sharing_policy(Link::SharingPolicy policy)
+Link* Link::set_sharing_policy(Link::SharingPolicy policy, const NonLinearResourceCb& cb)
 {
-  if (policy == SharingPolicy::SPLITDUPLEX)
-    throw std::invalid_argument(std::string("Impossible to set split-duplex for the link: ") + get_name() +
-                                std::string(". Use NetZone::create_split_duplex_link."));
+  if (policy == SharingPolicy::SPLITDUPLEX || policy == SharingPolicy::WIFI)
+    throw std::invalid_argument(std::string("Impossible to set wifi or split-duplex for the link: ") + get_name() +
+                                std::string(". Use appropriate create function in NetZone."));
 
-  kernel::actor::simcall([this, policy] { pimpl_->set_sharing_policy(policy); });
+  kernel::actor::simcall([this, policy, &cb] { pimpl_->set_sharing_policy(policy, cb); });
   return this;
 }
 Link::SharingPolicy Link::get_sharing_policy() const
@@ -143,6 +143,7 @@ void Link::turn_off()
 Link* Link::seal()
 {
   kernel::actor::simcall([this]() { this->pimpl_->seal(); });
+  s4u::Link::on_creation(*this); // notify the signal
   return this;
 }
 
@@ -195,12 +196,16 @@ Link* Link::set_properties(const std::unordered_map<std::string, std::string>& p
 
 Link* SplitDuplexLink::get_link_up() const
 {
-  return dynamic_cast<kernel::resource::SplitDuplexLinkImpl*>(pimpl_)->get_link_up();
+  const auto* pimpl = dynamic_cast<kernel::resource::SplitDuplexLinkImpl*>(pimpl_);
+  xbt_assert(pimpl, "Requesting link_up from a non split-duplex link: %s", get_cname());
+  return pimpl->get_link_up();
 }
 
 Link* SplitDuplexLink::get_link_down() const
 {
-  return dynamic_cast<kernel::resource::SplitDuplexLinkImpl*>(pimpl_)->get_link_down();
+  const auto* pimpl = dynamic_cast<kernel::resource::SplitDuplexLinkImpl*>(pimpl_);
+  xbt_assert(pimpl, "Requesting link_down from a non split-duplex link: %s", get_cname());
+  return pimpl->get_link_down();
 }
 
 SplitDuplexLink* SplitDuplexLink::by_name(const std::string& name)