void sync_request_bouncer::operator()(const strong_actor_ptr& sender, const message_id& mid) const { if (sender && mid.is_request()) sender->enqueue(nullptr, mid.response_id(), make_message(make_error(sec::request_receiver_down)), // TODO: this breaks out of the execution unit nullptr); }
void actor_registry::put_impl(atom_value key, strong_actor_ptr value) { if (value) value->get()->attach_functor([=] { system_.registry().put_impl(key, nullptr); }); exclusive_guard guard{named_entries_mtx_}; named_entries_.emplace(key, std::move(value)); }
void actor_registry::put_impl(actor_id key, strong_actor_ptr val) { CAF_LOG_TRACE(CAF_ARG(key)); if (!val) return; { // lifetime scope of guard exclusive_guard guard(instances_mtx_); if (!entries_.emplace(key, val).second) return; } // attach functor without lock CAF_LOG_INFO("added actor:" << CAF_ARG(key)); actor_registry* reg = this; val->get()->attach_functor([key, reg]() { reg->erase(key); }); }
bool instance::dispatch(execution_unit* ctx, const strong_actor_ptr& sender, const std::vector<strong_actor_ptr>& forwarding_stack, const strong_actor_ptr& receiver, message_id mid, const message& msg) { CAF_LOG_TRACE(CAF_ARG(sender) << CAF_ARG(receiver) << CAF_ARG(mid) << CAF_ARG(msg)); CAF_ASSERT(receiver && system().node() != receiver->node()); auto path = lookup(receiver->node()); if (! path) { notify<hook::message_sending_failed>(sender, receiver, mid, msg); return false; } auto writer = make_callback([&](serializer& sink) { sink << forwarding_stack << msg; }); header hdr{message_type::dispatch_message, 0, 0, mid.integer_value(), sender ? sender->node() : this_node(), receiver->node(), sender ? sender->id() : invalid_actor_id, receiver->id()}; write(ctx, path->wr_buf, hdr, &writer); flush(*path); notify<hook::message_sent>(sender, path->next_hop, receiver, mid, msg); return true; }
bool operator==(const abstract_actor* x, const strong_actor_ptr& y) { return actor_control_block::from(x) == y.get(); }
bool operator==(const strong_actor_ptr& x, const abstract_actor* y) { return x.get() == actor_control_block::from(y); }
intptr_t actor::compare(const strong_actor_ptr& x) const noexcept { return actor_addr::compare(ptr_.get(), x.get()); }
bool actor_pool::filter(upgrade_lock<detail::shared_spinlock>& guard, const strong_actor_ptr& sender, message_id mid, message_view& mv, execution_unit* eu) { auto& content = mv.content(); CAF_LOG_TRACE(CAF_ARG(mid) << CAF_ARG(content)); if (content.match_elements<exit_msg>()) { // acquire second mutex as well std::vector<actor> workers; auto em = content.get_as<exit_msg>(0).reason; if (cleanup(std::move(em), eu)) { auto tmp = mv.move_content_to_message(); // send exit messages *always* to all workers and clear vector afterwards // but first swap workers_ out of the critical section upgrade_to_unique_lock<detail::shared_spinlock> unique_guard{guard}; workers_.swap(workers); unique_guard.unlock(); for (auto& w : workers) anon_send(w, tmp); unregister_from_system(); } return true; } if (content.match_elements<down_msg>()) { // remove failed worker from pool auto& dm = content.get_as<down_msg>(0); upgrade_to_unique_lock<detail::shared_spinlock> unique_guard{guard}; auto last = workers_.end(); auto i = std::find(workers_.begin(), workers_.end(), dm.source); CAF_LOG_DEBUG_IF(i == last, "received down message for an unknown worker"); if (i != last) workers_.erase(i); if (workers_.empty()) { planned_reason_ = exit_reason::out_of_workers; unique_guard.unlock(); quit(eu); } return true; } if (content.match_elements<sys_atom, put_atom, actor>()) { auto& worker = content.get_as<actor>(2); worker->attach(default_attachable::make_monitor(worker.address(), address())); upgrade_to_unique_lock<detail::shared_spinlock> unique_guard{guard}; workers_.push_back(worker); return true; } if (content.match_elements<sys_atom, delete_atom, actor>()) { upgrade_to_unique_lock<detail::shared_spinlock> unique_guard{guard}; auto& what = content.get_as<actor>(2); auto last = workers_.end(); auto i = std::find(workers_.begin(), last, what); if (i != last) { workers_.erase(i); } return true; } if (content.match_elements<sys_atom, get_atom>()) { auto cpy = workers_; guard.unlock(); sender->enqueue(nullptr, mid.response_id(), make_message(std::move(cpy)), eu); return true; } if (workers_.empty()) { guard.unlock(); if (sender && mid.valid()) { // tell client we have ignored this sync message by sending // and empty message back sender->enqueue(nullptr, mid.response_id(), message{}, eu); } return true; } return false; }
void basp_broker_state::deliver(const node_id& src_nid, actor_id src_aid, strong_actor_ptr dest, message_id mid, std::vector<strong_actor_ptr>& stages, message& msg) { CAF_LOG_TRACE(CAF_ARG(src_nid) << CAF_ARG(src_aid) << CAF_ARG(dest) << CAF_ARG(msg) << CAF_ARG(mid)); auto src = src_nid == this_node() ? system().registry().get(src_aid) : proxies().get_or_put(src_nid, src_aid); // Intercept link messages. Forwarding actor proxies signalize linking // by sending link_atom/unlink_atom message with src = dest. if (msg.type_token() == make_type_token<atom_value, strong_actor_ptr>()) { switch (static_cast<uint64_t>(msg.get_as<atom_value>(0))) { default: break; case link_atom::value.uint_value(): { if (src_nid != this_node()) { CAF_LOG_WARNING("received link message for an other node"); return; } auto ptr = msg.get_as<strong_actor_ptr>(1); if (!ptr) { CAF_LOG_WARNING("received link message with invalid target"); return; } if (!src) { CAF_LOG_DEBUG("received link for invalid actor, report error"); anon_send(actor_cast<actor>(ptr), make_error(sec::remote_linking_failed)); return; } static_cast<actor_proxy*>(ptr->get())->local_link_to(src->get()); return; } case unlink_atom::value.uint_value(): { if (src_nid != this_node()) { CAF_LOG_WARNING("received unlink message for an other node"); return; } auto ptr = msg.get_as<strong_actor_ptr>(1); if (!ptr) { CAF_LOG_DEBUG("received unlink message with invalid target"); return; } if (!src) { CAF_LOG_DEBUG("received unlink for invalid actor, report error"); return; } static_cast<actor_proxy*>(ptr->get())->local_unlink_from(src->get()); return; } } } if (!dest) { auto rsn = exit_reason::remote_link_unreachable; CAF_LOG_INFO("cannot deliver message, destination not found"); self->parent().notify<hook::invalid_message_received>(src_nid, src, invalid_actor_id, mid, msg); if (mid.valid() && src) { detail::sync_request_bouncer srb{rsn}; srb(src, mid); } return; } self->parent().notify<hook::message_received>(src_nid, src, dest, mid, msg); dest->enqueue(make_mailbox_element(std::move(src), mid, std::move(stages), std::move(msg)), nullptr); }