Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix a doc error about actors (Tutorial_algorithms)
[simgrid.git] / src / s4u / s4u_Link.cpp
index 512c2a7..bb48a1b 100644 (file)
@@ -1,10 +1,11 @@
-/* Copyright (c) 2013-2018. The SimGrid Team. All rights reserved.          */
+/* Copyright (c) 2013-2019. 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. */
 
 #include <algorithm>
 
+#include "simgrid/s4u/Engine.hpp"
 #include "simgrid/s4u/Link.hpp"
 #include "simgrid/sg_config.hpp"
 #include "simgrid/simix.hpp"
 
 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_link, s4u, "Logging specific to the S4U links");
 
-/*********
- * C API *
- *********/
+namespace simgrid {
+namespace s4u {
 
-const char* sg_link_name(sg_link_t link)
+xbt::signal<void(Link&)> Link::on_creation;
+xbt::signal<void(Link const&)> Link::on_destruction;
+xbt::signal<void(Link const&)> Link::on_state_change;
+xbt::signal<void(Link const&)> Link::on_bandwidth_change;
+xbt::signal<void(kernel::resource::NetworkAction&, Host* src, Host* dst)> Link::on_communicate;
+xbt::signal<void(kernel::resource::NetworkAction&, kernel::resource::Action::State)>
+    Link::on_communication_state_change;
+
+Link* Link::by_name(const std::string& name)
 {
-  return link->get_cname();
+  return Engine::get_instance()->link_by_name(name);
 }
-sg_link_t sg_link_by_name(const char* name)
+
+Link* Link::by_name_or_null(const std::string& name)
 {
-  return simgrid::s4u::Link::by_name(name);
+  return Engine::get_instance()->link_by_name_or_null(name);
 }
 
-int sg_link_is_shared(sg_link_t link)
+const std::string& Link::get_name() const
 {
-  return (int)link->sharingPolicy();
+  return this->pimpl_->get_name();
 }
-double sg_link_bandwidth(sg_link_t link)
+const char* Link::get_cname() const
 {
-  return link->bandwidth();
+  return this->pimpl_->get_cname();
 }
-double sg_link_latency(sg_link_t link)
+bool Link::is_used()
 {
-  return link->latency();
+  return this->pimpl_->is_used();
 }
-void* sg_link_data(sg_link_t link)
+
+double Link::get_latency() const
 {
-  return link->getData();
+  return this->pimpl_->get_latency();
 }
-void sg_link_data_set(sg_link_t link, void* data)
+
+double Link::get_bandwidth() const
 {
-  link->setData(data);
+  return this->pimpl_->get_bandwidth();
 }
-int sg_link_count()
+
+Link::SharingPolicy Link::get_sharing_policy()
 {
-  return simgrid::kernel::resource::LinkImpl::linksCount();
+  return this->pimpl_->get_sharing_policy();
 }
-sg_link_t* sg_link_list()
-{
-  simgrid::kernel::resource::LinkImpl** list = simgrid::kernel::resource::LinkImpl::linksList();
-  sg_link_t* res                             = (sg_link_t*)list; // Use the same memory area
-
-  int size = sg_link_count();
-  for (int i = 0; i < size; i++)
-    res[i] = &(list[i]->piface_); // Convert each entry into its interface
 
-  return res;
-}
-void sg_link_exit()
+double Link::get_usage()
 {
-  simgrid::kernel::resource::LinkImpl::linksExit();
+  return this->pimpl_->get_constraint()->get_usage();
 }
 
-/***********
- * C++ API *
- ***********/
-
-namespace simgrid {
-namespace s4u {
-Link* Link::by_name(const char* name)
+void Link::turn_on()
 {
-  kernel::resource::LinkImpl* res = kernel::resource::LinkImpl::byName(name);
-  if (res == nullptr)
-    return nullptr;
-  return &res->piface_;
+  simgrid::kernel::actor::simcall([this]() { this->pimpl_->turn_on(); });
 }
-const std::string& Link::get_name() const
+void Link::turn_off()
 {
-  return this->pimpl_->get_name();
+  simgrid::kernel::actor::simcall([this]() { this->pimpl_->turn_off(); });
 }
-const char* Link::get_cname() const
+
+bool Link::is_on() const
 {
-  return this->pimpl_->get_cname();
+  return this->pimpl_->is_on();
 }
-const char* Link::name()
+
+void Link::set_state_profile(kernel::profile::Profile* profile)
 {
-  return get_cname();
+  simgrid::kernel::actor::simcall([this, profile]() { this->pimpl_->set_state_profile(profile); });
 }
-bool Link::isUsed()
+void Link::set_bandwidth_profile(kernel::profile::Profile* profile)
 {
-  return this->pimpl_->is_used();
+  simgrid::kernel::actor::simcall([this, profile]() { this->pimpl_->set_bandwidth_profile(profile); });
 }
-
-double Link::latency()
+void Link::set_latency_profile(kernel::profile::Profile* trace)
 {
-  return this->pimpl_->latency();
+  simgrid::kernel::actor::simcall([this, trace]() { this->pimpl_->set_latency_profile(trace); });
 }
 
-double Link::bandwidth()
+const char* Link::get_property(const std::string& key) const
 {
-  return this->pimpl_->bandwidth();
+  return this->pimpl_->get_property(key);
 }
-
-Link::SharingPolicy Link::sharingPolicy()
+void Link::set_property(const std::string& key, const std::string& value)
 {
-  return this->pimpl_->sharingPolicy();
+  simgrid::kernel::actor::simcall([this, &key, &value] { this->pimpl_->set_property(key, value); });
 }
+} // namespace s4u
+} // namespace simgrid
 
-double Link::getUsage()
-{
-  return this->pimpl_->get_constraint()->get_usage();
-}
+/* **************************** Public C interface *************************** */
 
-void Link::turnOn()
+const char* sg_link_name(sg_link_t link)
 {
-  simgrid::simix::kernelImmediate([this]() { this->pimpl_->turn_on(); });
+  return link->get_cname();
 }
-void Link::turnOff()
+sg_link_t sg_link_by_name(const char* name)
 {
-  simgrid::simix::kernelImmediate([this]() { this->pimpl_->turn_off(); });
+  return simgrid::s4u::Link::by_name(name);
 }
 
-void* Link::getData()
+int sg_link_is_shared(sg_link_t link)
 {
-  return this->pimpl_->getData();
+  return (int)link->get_sharing_policy();
 }
-void Link::setData(void* d)
+double sg_link_bandwidth(sg_link_t link)
 {
-  simgrid::simix::kernelImmediate([this, d]() { this->pimpl_->setData(d); });
+  return link->get_bandwidth();
 }
-
-void Link::setStateTrace(tmgr_trace_t trace)
+double sg_link_latency(sg_link_t link)
 {
-  simgrid::simix::kernelImmediate([this, trace]() { this->pimpl_->setStateTrace(trace); });
+  return link->get_latency();
 }
-void Link::setBandwidthTrace(tmgr_trace_t trace)
+void* sg_link_data(sg_link_t link)
 {
-  simgrid::simix::kernelImmediate([this, trace]() { this->pimpl_->setBandwidthTrace(trace); });
+  return link->get_data();
 }
-void Link::setLatencyTrace(tmgr_trace_t trace)
+void sg_link_data_set(sg_link_t link, void* data)
 {
-  simgrid::simix::kernelImmediate([this, trace]() { this->pimpl_->setLatencyTrace(trace); });
+  link->set_data(data);
 }
-
-const char* Link::getProperty(const char* key)
+int sg_link_count()
 {
-  return this->pimpl_->getProperty(key);
+  return simgrid::s4u::Engine::get_instance()->get_link_count();
 }
-void Link::setProperty(std::string key, std::string value)
+
+sg_link_t* sg_link_list()
 {
-  simgrid::simix::kernelImmediate([this, key, value] { this->pimpl_->setProperty(key, value); });
-}
+  std::vector<simgrid::s4u::Link*> links = simgrid::s4u::Engine::get_instance()->get_all_links();
 
-/*************
- * Callbacks *
- *************/
-simgrid::xbt::signal<void(s4u::Link&)> Link::onCreation;
-simgrid::xbt::signal<void(s4u::Link&)> Link::onDestruction;
-simgrid::xbt::signal<void(s4u::Link&)> Link::onStateChange;
-simgrid::xbt::signal<void(kernel::resource::NetworkAction*, s4u::Host* src, s4u::Host* dst)> Link::onCommunicate;
-simgrid::xbt::signal<void(kernel::resource::NetworkAction*)> Link::onCommunicationStateChange;
-} // namespace s4u
-} // namespace simgrid
+  sg_link_t* res = xbt_new(sg_link_t, links.size());
+  memcpy(res, links.data(), sizeof(sg_link_t) * links.size());
+
+  return res;
+}