/*
This file is part of Ingen.
Copyright 2007-2016 David Robillard
Ingen is free software: you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License as published by the Free
Software Foundation, either version 3 of the License, or any later version.
Ingen is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU Affero General Public License for details.
You should have received a copy of the GNU Affero General Public License
along with Ingen. If not, see .
*/
#include "Delete.hpp"
#include "BlockImpl.hpp"
#include "Broadcaster.hpp"
#include "BufferFactory.hpp"
#include "CompiledGraph.hpp"
#include "ControlBindings.hpp"
#include "DisconnectAll.hpp"
#include "Driver.hpp"
#include "DuplexPort.hpp"
#include "Engine.hpp"
#include "EnginePort.hpp"
#include "GraphImpl.hpp"
#include "PortImpl.hpp"
#include "PreProcessContext.hpp"
#include "ingen/Forge.hpp"
#include "ingen/Interface.hpp"
#include "ingen/Node.hpp"
#include "ingen/Status.hpp"
#include "ingen/Store.hpp"
#include "ingen/URI.hpp"
#include "ingen/URIs.hpp"
#include "ingen/World.hpp"
#include "ingen/paths.hpp"
#include "raul/Array.hpp"
#include "raul/Path.hpp"
#include
#include
#include
#include
#include
#include
namespace ingen::server {
class RunContext;
namespace events {
Delete::Delete(Engine& engine,
const std::shared_ptr& client,
FrameTime timestamp,
const ingen::Del& msg)
: Event(engine, client, msg.seq, timestamp)
, _msg(msg)
{
if (uri_is_path(msg.uri)) {
_path = uri_to_path(msg.uri);
}
}
Delete::~Delete()
{
for (auto* b : _removed_bindings) {
delete b;
}
}
bool
Delete::pre_process(PreProcessContext& ctx)
{
const ingen::URIs& uris = _engine.world().uris();
if (_path.is_root() || _path == "/control" || _path == "/notify") {
return Event::pre_process_done(Status::NOT_DELETABLE, _path);
}
_engine.control_bindings()->get_all(_path, _removed_bindings);
auto iter = _engine.store()->find(_path);
if (iter == _engine.store()->end()) {
return Event::pre_process_done(Status::NOT_FOUND, _path);
}
if (!(_block = std::dynamic_pointer_cast(iter->second))) {
_port = std::dynamic_pointer_cast(iter->second);
}
if ((!_block && !_port) || (_port && !_engine.driver()->dynamic_ports())) {
return Event::pre_process_done(Status::NOT_DELETABLE, _path);
}
GraphImpl* parent = _block ? _block->parent_graph() : _port->parent_graph();
if (!parent) {
return Event::pre_process_done(Status::INTERNAL_ERROR, _path);
}
// Take a writer lock while we modify the store
const std::lock_guard lock{_engine.store()->mutex()};
_engine.store()->remove(iter, _removed_objects);
if (_block) {
parent->remove_block(*_block);
_disconnect_event =
std::make_unique(_engine, parent, _block.get());
_disconnect_event->pre_process(ctx);
_compiled_graph = ctx.maybe_compile(*parent);
} else if (_port) {
parent->remove_port(*_port);
_disconnect_event =
std::make_unique(_engine, parent, _port.get());
_disconnect_event->pre_process(ctx);
_compiled_graph = ctx.maybe_compile(*parent);
if (parent->enabled()) {
_ports_array = parent->build_ports_array(*_engine.maid());
assert(_ports_array->size() == parent->num_ports_non_rt());
// Adjust port indices if necessary and record changes for later
for (size_t i = 0; i < _ports_array->size(); ++i) {
PortImpl* const port = _ports_array->at(i);
if (port->index() != i) {
_port_index_changes.emplace(
port->path(), std::make_pair(port->index(), i));
port->remove_property(uris.lv2_index, uris.patch_wildcard);
port->set_property(uris.lv2_index,
_engine.buffer_factory()->forge().make(
static_cast(i)));
}
}
}
if (!parent->parent()) {
_engine_port = _engine.driver()->get_port(_port->path());
}
}
return Event::pre_process_done(Status::SUCCESS);
}
void
Delete::execute(RunContext& ctx)
{
if (_status != Status::SUCCESS) {
return;
}
if (_disconnect_event) {
_disconnect_event->execute(ctx);
}
if (!_removed_bindings.empty()) {
_engine.control_bindings()->remove(ctx, _removed_bindings);
}
GraphImpl* parent = _block ? _block->parent_graph() : nullptr;
if (_ports_array && _port) {
// Adjust port indices if necessary
for (size_t i = 0; i < _ports_array->size(); ++i) {
PortImpl* const port = _ports_array->at(i);
if (port->index() != i) {
port->set_index(ctx, i);
}
}
// Replace ports array in graph
parent = _port->parent_graph();
parent->set_external_ports(std::move(_ports_array));
if (_engine_port) {
_engine.driver()->remove_port(ctx, _engine_port);
}
}
if (parent && _compiled_graph) {
_compiled_graph = parent->swap_compiled_graph(std::move(_compiled_graph));
}
}
void
Delete::post_process()
{
const Broadcaster::Transfer t{*_engine.broadcaster()};
if (respond() == Status::SUCCESS && (_block || _port)) {
if (_block) {
_block->deactivate();
}
_engine.broadcaster()->message(_msg);
}
if (_engine_port) {
_engine.driver()->unregister_port(*_engine_port);
delete _engine_port;
}
}
void
Delete::undo(Interface& target)
{
const ingen::URIs& uris = _engine.world().uris();
ingen::Forge& forge = _engine.buffer_factory()->forge();
auto i = _removed_objects.find(_path);
if (i != _removed_objects.end()) {
// Undo disconnect
if (_disconnect_event) {
_disconnect_event->undo(target);
}
// Put deleted item back
target.put(_msg.uri, i->second->properties());
// Adjust port indices
for (const auto& c : _port_index_changes) {
if (c.first != _msg.uri.path()) {
target.set_property(path_to_uri(c.first),
uris.lv2_index,
forge.make(static_cast(c.second.first)));
}
}
}
}
} // namespace events
} // namespace ingen::server