From 21365044f63e70deb2e8bd0685349aad6ea14e85 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Mon, 10 Jan 2011 23:50:45 +0000 Subject: Add missing files. git-svn-id: http://svn.drobilla.net/lad/trunk/machina@2824 a436a847-0d15-0410-975c-d299462d15a1 --- src/engine/Controller.cpp | 187 ++++++++++++++++++++++++++++++++++++++ src/engine/URIs.cpp | 24 +++++ src/engine/Updates.cpp | 143 +++++++++++++++++++++++++++++ src/engine/machina/Controller.hpp | 78 ++++++++++++++++ src/engine/machina/URIs.hpp | 74 +++++++++++++++ src/engine/machina/Updates.hpp | 48 ++++++++++ 6 files changed, 554 insertions(+) create mode 100644 src/engine/Controller.cpp create mode 100644 src/engine/URIs.cpp create mode 100644 src/engine/Updates.cpp create mode 100644 src/engine/machina/Controller.hpp create mode 100644 src/engine/machina/URIs.hpp create mode 100644 src/engine/machina/Updates.hpp (limited to 'src/engine') diff --git a/src/engine/Controller.cpp b/src/engine/Controller.cpp new file mode 100644 index 0000000..9c39711 --- /dev/null +++ b/src/engine/Controller.cpp @@ -0,0 +1,187 @@ +/* This file is part of Machina. + * Copyright (C) 2010 David Robillard + * + * Machina is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Machina 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Machina. If not, see . + */ + +#include "client/ClientModel.hpp" +#include "client/ClientObject.hpp" +#include "machina/Controller.hpp" +#include "machina/Engine.hpp" +#include "machina/Machine.hpp" +#include "machina/Updates.hpp" +#include "machina/Machine.hpp" + +#include "Edge.hpp" + +namespace Machina { + +Controller::Controller(SharedPtr engine, Client::ClientModel& client_model) + : _engine(engine) + , _client_model(client_model) + , _updates(new UpdateBuffer(4096)) +{ + _engine->driver()->set_update_sink(_updates); +} + +uint64_t +Controller::create(const Client::ClientObject& properties) +{ + TimeDuration dur(_engine->machine()->time().unit(), + properties.get(URIs::instance().machina_duration).get_float()); + SharedPtr node(new Machina::Node(dur)); + SharedPtr obj(new Client::ClientObject(properties, node->id())); + _objects.insert(node); + _client_model.new_object(obj); + _engine->machine()->add_node(node); + return node->id(); +} + +void +Controller::announce(SharedPtr machine) +{ + for (Machina::Machine::Nodes::const_iterator n = machine->nodes().begin(); + n != machine->nodes().end(); ++n) { + + SharedPtr obj(new Machina::Client::ClientObject((*n)->id())); + obj->set(URIs::instance().rdf_type, "machina:Node"); + obj->set(URIs::instance().machina_duration, Raul::Atom(float((*n)->duration().to_double()))); + obj->set(URIs::instance().machina_canvas_x, 0.0f); + obj->set(URIs::instance().machina_canvas_y, 0.0f); + + _objects.insert(*n); + _client_model.new_object(obj); + } + + for (Machina::Machine::Nodes::const_iterator n = machine->nodes().begin(); + n != machine->nodes().end(); ++n) { + for (Machina::Node::Edges::const_iterator e = (*n)->edges().begin(); + e != (*n)->edges().end(); ++e) { + _objects.insert(*e); + + SharedPtr eobj(new Client::ClientObject((*e)->id())); + eobj->set(URIs::instance().rdf_type, "machina:Edge"); + eobj->set(URIs::instance().machina_probability, (*e)->probability()); + eobj->set(URIs::instance().machina_tail_id, (int32_t)(*n)->id()); + eobj->set(URIs::instance().machina_head_id, (int32_t)(*e)->head()->id()); + + _client_model.new_object(eobj); + } + } +} + +SharedPtr +Controller::find(uint64_t id) +{ + SharedPtr key(new StatefulKey(id)); + Objects::iterator i = _objects.find(key); + if (i != _objects.end()) + return *i; + return SharedPtr(); +} + +void +Controller::learn(SharedPtr maid, uint64_t node_id) +{ + SharedPtr node = PtrCast(find(node_id)); + if (node) + _engine->machine()->learn(maid, node); + else + std::cerr << "Failed to find node " << node_id << " for learn" << std::endl; +} + +void +Controller::set_property(uint64_t object_id, URIInt key, const Raul::Atom& value) +{ + SharedPtr object = find(object_id); + if (object) { + object->set(key, value); + _client_model.property(object_id, key, value); + } +} + +uint64_t +Controller::connect(uint64_t tail_id, uint64_t head_id) +{ + SharedPtr tail = PtrCast(find(tail_id)); + SharedPtr head = PtrCast(find(head_id)); + + SharedPtr edge(new Machina::Edge(tail, head)); + tail->add_edge(edge); + _objects.insert(edge); + + SharedPtr obj(new Client::ClientObject(/**this,*/ edge->id())); + obj->set(URIs::instance().rdf_type, "machina:Edge"); + obj->set(URIs::instance().machina_probability, 1.0f); + obj->set(URIs::instance().machina_tail_id, (int32_t)tail->id()); + obj->set(URIs::instance().machina_head_id, (int32_t)head->id()); + + _client_model.new_object(obj); + + return edge->id(); +} + +void +Controller::disconnect(uint64_t tail_id, uint64_t head_id) +{ + SharedPtr tail = PtrCast(find(tail_id)); + SharedPtr head = PtrCast(find(head_id)); + + SharedPtr edge = tail->remove_edge_to(head); + if (edge) + _client_model.erase_object(edge->id()); + else + Raul::error << "Edge not found" << std::endl; +} + +void +Controller::erase(uint64_t id) +{ + SharedPtr key(new StatefulKey(id)); + Objects::iterator i = _objects.find(key); + if (i == _objects.end()) { + return; + } + + SharedPtr node = PtrCast(*i); + if (node) { + _engine->machine()->remove_node(node); + } + + _client_model.erase_object((*i)->id()); + _objects.erase(i); +} + +void +Controller::process_updates() +{ + const uint32_t read_space = _updates->read_space(); + + uint64_t subject; + URIInt key; + Raul::Atom value; + for (uint32_t i = 0; i < read_space;) { + i += read_set(_updates, &subject, &key, &value); + SharedPtr obj = _client_model.find(subject); + if (obj) { + obj->set(key, value); + } else { + SharedPtr obj(new Client::ClientObject(subject)); + obj->set(key, value); + _client_model.new_object(obj); + } + } +} + +} diff --git a/src/engine/URIs.cpp b/src/engine/URIs.cpp new file mode 100644 index 0000000..ccaf90c --- /dev/null +++ b/src/engine/URIs.cpp @@ -0,0 +1,24 @@ +/* This file is part of Machina. + * Copyright (C) 2010 David Robillard + * + * Machina is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Machina 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Machina. If not, see . + */ + +#include "machina/URIs.hpp" + +namespace Machina { + +URIs* URIs::_instance = NULL; + +} // namespace Machina diff --git a/src/engine/Updates.cpp b/src/engine/Updates.cpp new file mode 100644 index 0000000..3d1c0f3 --- /dev/null +++ b/src/engine/Updates.cpp @@ -0,0 +1,143 @@ +/* This file is part of Machina. + * Copyright (C) 2010 David Robillard + * + * Machina is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Machina 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Machina. If not, see . + */ + +#include "raul/Atom.hpp" +#include "raul/SharedPtr.hpp" + +#include "machina/types.hpp" +#include "machina/Updates.hpp" + +namespace Machina { + +static inline void +write_atom(SharedPtr buf, + const Raul::Atom& value) +{ + const Raul::Atom::Type type = value.type(); + buf->write(sizeof(type), &type); + int32_t ival; + float fval; + size_t szval; + switch (value.type()) { + case Raul::Atom::INT: + ival = value.get_int32(); + buf->write(sizeof(ival), &ival); + break; + case Raul::Atom::FLOAT: + ival = value.get_float(); + buf->write(sizeof(fval), &fval); + break; + case Raul::Atom::BOOL: + ival = value.get_bool() ? 1 : 0; + buf->write(sizeof(ival), &ival); + break; + case Raul::Atom::STRING: + szval = value.data_size(); + buf->write(sizeof(size_t), &szval); + buf->write(value.data_size(), value.get_string()); + break; + case Raul::Atom::URI: + szval = value.data_size(); + buf->write(sizeof(size_t), &szval); + buf->write(value.data_size(), value.get_uri()); + break; + default: + assert(false); + } +} + +uint32_t +read_atom(SharedPtr buf, + Raul::Atom* value) +{ + Raul::Atom::Type type; + buf->read(sizeof(type), &type); + + int32_t ival; + float fval; + char* sval; + size_t val_size; + switch (type) { + case Raul::Atom::INT: + val_size = sizeof(ival); + buf->read(val_size, &ival); + *value = Raul::Atom(ival); + break; + case Raul::Atom::FLOAT: + val_size = sizeof(fval); + buf->read(val_size, &fval); + *value = Raul::Atom(fval); + break; + case Raul::Atom::BOOL: + val_size = sizeof(ival); + buf->read(val_size, &ival); + assert(ival == 0 || ival == 1); + *value = Raul::Atom(bool(ival)); + break; + case Raul::Atom::STRING: + buf->read(sizeof(val_size), &val_size); + sval = (char*)malloc(val_size); + buf->read(val_size, sval); + val_size += sizeof(val_size); + *value = Raul::Atom(sval); + free(sval); + break; + case Raul::Atom::URI: + buf->read(sizeof(val_size), &val_size); + sval = (char*)malloc(val_size); + buf->read(val_size, sval); + val_size += sizeof(val_size); + *value = Raul::Atom(Raul::Atom::URI, sval); + free(sval); + break; + default: + assert(false); + } + + return sizeof(type) + val_size; +} + +void +write_set(SharedPtr buf, + uint64_t subject, + URIInt key, + const Raul::Atom& value) +{ + const uint32_t update_type = UPDATE_SET; + buf->write(sizeof(update_type), &update_type); + buf->write(sizeof(subject), &subject); + buf->write(sizeof(key), &key); + write_atom(buf, value); +} + +uint32_t +read_set(SharedPtr buf, + uint64_t* subject, + URIInt* key, + Raul::Atom* value) +{ + uint32_t update_type; + buf->read(sizeof(update_type), &update_type); + assert(update_type == UPDATE_SET); + buf->read(sizeof(*subject), subject); + buf->read(sizeof(*key), key); + + const uint32_t value_size = read_atom(buf, value); + return sizeof(update_type) + sizeof(*subject) + sizeof(*key) + value_size; +} + +} diff --git a/src/engine/machina/Controller.hpp b/src/engine/machina/Controller.hpp new file mode 100644 index 0000000..000fcad --- /dev/null +++ b/src/engine/machina/Controller.hpp @@ -0,0 +1,78 @@ +/* This file is part of Machina. + * Copyright (C) 2010 David Robillard + * + * Machina is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Machina 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Machina. If not, see . + */ + +#ifndef MACHINA_CONTROLLER_HPP +#define MACHINA_CONTROLLER_HPP + +#include + +#include + +#include "raul/SharedPtr.hpp" +#include "raul/RingBuffer.hpp" + +#include "machina/types.hpp" +#include "machina/URIs.hpp" + +#include "Stateful.hpp" + +namespace Raul { class Atom; class Maid; } + +namespace Machina { + +class Engine; +class Machine; +class Stateful; + +namespace Client { class ClientModel; class ClientObject; } + +class Controller { +public: + Controller(SharedPtr engine, Client::ClientModel& client_model); + + uint64_t create(const Client::ClientObject& obj); + uint64_t connect(uint64_t tail_id, uint64_t head_id); + void set_property(uint64_t object_id, URIInt key, const Raul::Atom& value); + void learn(SharedPtr maid, uint64_t node_id); + void disconnect(uint64_t tail_id, uint64_t head_id); + void erase(uint64_t id); + + void announce(SharedPtr machine); + + void process_updates(); + +private: + SharedPtr find(uint64_t id); + + struct StatefulComparator { + inline bool operator()(SharedPtr lhs, SharedPtr rhs) const { + return lhs->id() < rhs->id(); + } + }; + + typedef std::set, StatefulComparator> Objects; + Objects _objects; + + SharedPtr _engine; + Client::ClientModel& _client_model; + + SharedPtr _updates; +}; + +} + +#endif // MACHINA_CONTROLLER_HPP diff --git a/src/engine/machina/URIs.hpp b/src/engine/machina/URIs.hpp new file mode 100644 index 0000000..74e5451 --- /dev/null +++ b/src/engine/machina/URIs.hpp @@ -0,0 +1,74 @@ +/* This file is part of Machina. + * Copyright (C) 2010 David Robillard + * + * Machina is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Machina 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Machina. If not, see . + */ + +#ifndef MACHINA_URIS_HPP +#define MACHINA_URIS_HPP + +#include + +#include "raul/Atom.hpp" + +#include "machina/types.hpp" + +namespace Machina { + +class URIs { +public: + static void init() { _instance = new URIs(); } + + static inline const URIs& instance() { assert(_instance); return *_instance; } + + Raul::Atom machina_MidiAction_atom; + + URIInt machina_active; + URIInt machina_canvas_x; + URIInt machina_canvas_y; + URIInt machina_duration; + URIInt machina_enter_action; + URIInt machina_exit_action; + URIInt machina_head_id; + URIInt machina_initial; + URIInt machina_note_number; + URIInt machina_probability; + URIInt machina_selector; + URIInt machina_tail_id; + URIInt rdf_type; + +private: + URIs() + : machina_MidiAction_atom(Raul::Atom::URI, "machina:MidiAction") + , machina_active(1) + , machina_canvas_x(2) + , machina_canvas_y(3) + , machina_duration(4) + , machina_enter_action(11) + , machina_exit_action(12) + , machina_head_id(5) + , machina_initial(6) + , machina_note_number(13) + , machina_probability(7) + , machina_selector(8) + , machina_tail_id(9) + , rdf_type(10) + {} + + static URIs* _instance; +}; + +} // namespace Machina + +#endif // MACHINA_URIS_HPP diff --git a/src/engine/machina/Updates.hpp b/src/engine/machina/Updates.hpp new file mode 100644 index 0000000..31c7e2b --- /dev/null +++ b/src/engine/machina/Updates.hpp @@ -0,0 +1,48 @@ +/* This file is part of Machina. + * Copyright (C) 2010 David Robillard + * + * Machina is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Machina 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Machina. If not, see . + */ + +#ifndef MACHINA_UPDATES_HPP +#define MACHINA_UPDATES_HPP + +#include + +#include "raul/Atom.hpp" +#include "raul/SharedPtr.hpp" + +#include "machina/types.hpp" + +namespace Machina { + +enum UpdateType { + UPDATE_SET = 1 +}; + +void +write_set(SharedPtr buf, + uint64_t subject, + URIInt key, + const Raul::Atom& value); + +uint32_t +read_set(SharedPtr buf, + uint64_t* subject, + URIInt* key, + Raul::Atom* value); + +} // namespace Machina + +#endif // MACHINA_UPDATES_HPP -- cgit v1.2.1