diff options
author | David Robillard <d@drobilla.net> | 2012-05-02 06:24:22 +0000 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2012-05-02 06:24:22 +0000 |
commit | fe353cce9665b33a7372ed2d1683b3c2748625f7 (patch) | |
tree | 81287558b014309820e9df64033edc0b29d23b4f /src/socket | |
parent | 1c736a348c59d98e4022fb02b49a8b4c93baa3d2 (diff) | |
download | ingen-fe353cce9665b33a7372ed2d1683b3c2748625f7.tar.gz ingen-fe353cce9665b33a7372ed2d1683b3c2748625f7.tar.bz2 ingen-fe353cce9665b33a7372ed2d1683b3c2748625f7.zip |
Preliminary socket control interface, and ingen_cmd command line interface which uses it.
git-svn-id: http://svn.drobilla.net/lad/trunk/ingen@4311 a436a847-0d15-0410-975c-d299462d15a1
Diffstat (limited to 'src/socket')
-rw-r--r-- | src/socket/SocketReceiver.cpp | 149 | ||||
-rw-r--r-- | src/socket/SocketReceiver.hpp | 48 | ||||
-rw-r--r-- | src/socket/ingen_socket_server.cpp | 51 | ||||
-rw-r--r-- | src/socket/wscript | 14 |
4 files changed, 262 insertions, 0 deletions
diff --git a/src/socket/SocketReceiver.cpp b/src/socket/SocketReceiver.cpp new file mode 100644 index 00000000..833c90ac --- /dev/null +++ b/src/socket/SocketReceiver.cpp @@ -0,0 +1,149 @@ +/* + This file is part of Ingen. + Copyright 2007-2012 David Robillard <http://drobilla.net/> + + 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 <http://www.gnu.org/licenses/>. +*/ + +#include <sys/fcntl.h> +#include <sys/socket.h> +#include <sys/un.h> + +#include "ingen/Interface.hpp" +#include "ingen/shared/World.hpp" +#include "ingen/shared/AtomReader.hpp" +#include "sord/sordmm.hpp" +#include "sratom/sratom.h" +#include "SocketReceiver.hpp" + +#define LOG(s) s << "[SocketReceiver] " + +namespace Ingen { +namespace Socket { + +SocketReceiver::SocketReceiver(Ingen::Shared::World& world, + SharedPtr<Interface> iface) + : _world(world) + , _iface(iface) +{ + // Create server socket + _sock = socket(AF_UNIX, SOCK_STREAM, 0); + if (_sock == -1) { + LOG(Raul::error) << "Failed to create socket" << std::endl; + return; + } + + _sock_path = world.conf()->option("socket").get_string(); + + // Make server socket address + struct sockaddr_un addr; + memset(&addr, 0, sizeof(struct sockaddr_un)); + addr.sun_family = AF_UNIX; + strncpy(addr.sun_path, _sock_path.c_str(), sizeof(addr.sun_path) - 1); + + // Bind socket to address + if (bind(_sock, (struct sockaddr*)&addr, + sizeof(struct sockaddr_un)) == -1) { + LOG(Raul::error) << "Failed to bind socket" << std::endl; + return; + } + + // Mark socket as a passive socket for accepting incoming connections + if (listen(_sock, 64) == -1) { + LOG(Raul::error) << "Failed to listen on socket" << std::endl; + } + + LOG(Raul::info) << "Opened socket at " << _sock_path << std::endl; + start(); +} + +SocketReceiver::~SocketReceiver() +{ + stop(); + join(); + close(_sock); + unlink(_sock_path.c_str()); +} + +void +SocketReceiver::_run() +{ + while (!_exit_flag) { + // Accept connection from client + socklen_t client_addr_size = sizeof(struct sockaddr_un); + struct sockaddr_un client_addr; + int conn = accept(_sock, (struct sockaddr*)&client_addr, + &client_addr_size); + if (conn == -1) { + LOG(Raul::error) << "Error accepting connection" << std::endl; + continue; + } + + // Set connection to non-blocking so parser can read until EOF + // and not block indefinitely waiting for more input + fcntl(conn, F_SETFL, fcntl(conn, F_GETFL, 0) | O_NONBLOCK); + + // Set up a reader to parse the Turtle message into a model + Sord::World* world = _world.rdf_world(); + SerdEnv* env = world->prefixes().c_obj(); + SordModel* model = sord_new(world->c_obj(), SORD_SPO, false); + SerdReader* reader = sord_new_reader(model, env, SERD_TURTLE, NULL); + + // Set base URI to path: so e.g. </foo/bar> will be a path + SordNode* base_uri = sord_new_uri( + world->c_obj(), (const uint8_t*)"path:"); + serd_env_set_base_uri(env, sord_node_to_serd_node(base_uri)); + + LV2_URID_Map* map = &_world.lv2_uri_map()->urid_map_feature()->urid_map; + + // Set up sratom to build an LV2_Atom from the model + Sratom* sratom = sratom_new(map); + SerdChunk chunk = { NULL, 0 }; + LV2_Atom_Forge forge; + lv2_atom_forge_init(&forge, map); + lv2_atom_forge_set_sink( + &forge, sratom_forge_sink, sratom_forge_deref, &chunk); + + // Read directly from the connection with serd + FILE* f = fdopen(conn, "r"); + serd_reader_read_file_handle(reader, f, (const uint8_t*)"(socket)"); + + // FIXME: Sratom needs work to be able to read resources + SordNode* msg_node = sord_new_blank( + world->c_obj(), (const uint8_t*)"genid1"); + + // Build an LV2_Atom at chunk.buf from the message + sratom_read(sratom, &forge, world->c_obj(), model, msg_node); + + // Make an AtomReader to read that atom and do Ingen things + Shared::AtomReader ar(*_world.lv2_uri_map().get(), + *_world.uris().get(), + _world.forge(), + *_iface.get()); + + // Call _iface methods based on atom content + ar.write((LV2_Atom*)chunk.buf); + + // Respond and close connection + write(conn, "OK", 2); + fclose(f); + close(conn); + + sratom_free(sratom); + sord_node_free(world->c_obj(), msg_node); + serd_reader_free(reader); + sord_free(model); + } +} + +} // namespace Ingen +} // namespace Socket diff --git a/src/socket/SocketReceiver.hpp b/src/socket/SocketReceiver.hpp new file mode 100644 index 00000000..5c7ea316 --- /dev/null +++ b/src/socket/SocketReceiver.hpp @@ -0,0 +1,48 @@ +/* + This file is part of Ingen. + Copyright 2007-2012 David Robillard <http://drobilla.net/> + + 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 <http://www.gnu.org/licenses/>. +*/ + +#include <string> + +#include "raul/SharedPtr.hpp" +#include "raul/Thread.hpp" + +namespace Ingen { + +class Interface; + +namespace Shared { class World; } + +namespace Socket { + +class SocketReceiver : public Raul::Thread +{ +public: + SocketReceiver(Ingen::Shared::World& world, + SharedPtr<Interface> iface); + + ~SocketReceiver(); + +private: + virtual void _run(); + + Ingen::Shared::World& _world; + SharedPtr<Interface> _iface; + std::string _sock_path; + int _sock; +}; + +} // namespace Ingen +} // namespace Socket diff --git a/src/socket/ingen_socket_server.cpp b/src/socket/ingen_socket_server.cpp new file mode 100644 index 00000000..79e2e05e --- /dev/null +++ b/src/socket/ingen_socket_server.cpp @@ -0,0 +1,51 @@ +/* + This file is part of Ingen. + Copyright 2007-2012 David Robillard <http://drobilla.net/> + + 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 <http://www.gnu.org/licenses/>. +*/ + +#include "raul/log.hpp" + +#include "ingen/shared/Module.hpp" +#include "ingen/shared/World.hpp" + +#include "../server/Engine.hpp" +#include "../server/ServerInterfaceImpl.hpp" + +#include "SocketReceiver.hpp" + +using namespace Ingen; + +struct IngenSocketServerModule : public Ingen::Shared::Module { + void load(Ingen::Shared::World* world) { + Server::Engine* engine = (Server::Engine*)world->local_engine().get(); + SharedPtr<Server::ServerInterfaceImpl> interface( + new Server::ServerInterfaceImpl(*engine)); + receiver = SharedPtr<Ingen::Socket::SocketReceiver>( + new Ingen::Socket::SocketReceiver(*world, interface)); + + engine->add_event_source(interface); + } + + SharedPtr<Ingen::Socket::SocketReceiver> receiver; +}; + +extern "C" { + +Ingen::Shared::Module* +ingen_module_load() +{ + return new IngenSocketServerModule(); +} + +} // extern "C" diff --git a/src/socket/wscript b/src/socket/wscript new file mode 100644 index 00000000..e5de6cec --- /dev/null +++ b/src/socket/wscript @@ -0,0 +1,14 @@ +#!/usr/bin/env python +from waflib.extras import autowaf as autowaf + +def build(bld): + if bld.is_defined('HAVE_SOCKET'): + obj = bld(features = 'cxx cxxshlib', + source = ['SocketReceiver.cpp', + 'ingen_socket_server.cpp'], + includes = ['.', '../..'], + name = 'libingen_socket_server', + target = 'ingen_socket_server', + install_path = '${LIBDIR}', + use = 'libingen_server') + autowaf.use_lib(bld, obj, 'RAUL LIBLO') |