From 82ab6f73fab060860addb30691d0e926b4723ee2 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sat, 7 Feb 2015 01:05:13 +0000 Subject: Demodularize socket stuff. git-svn-id: http://svn.drobilla.net/lad/trunk/ingen@5535 a436a847-0d15-0410-975c-d299462d15a1 --- src/SocketReader.cpp | 187 ++++++++++++++++++++++++++++++++++++ src/SocketWriter.cpp | 96 +++++++++++++++++++ src/World.cpp | 1 + src/ingen/ingen.cpp | 12 +-- src/server/Engine.cpp | 19 +++- src/server/Engine.hpp | 6 +- src/server/SocketServer.hpp | 61 ++++++++++++ src/server/wscript | 3 +- src/socket/SocketClient.hpp | 55 ----------- src/socket/SocketReader.cpp | 190 ------------------------------------- src/socket/SocketReader.hpp | 74 --------------- src/socket/SocketServer.hpp | 60 ------------ src/socket/SocketWriter.cpp | 99 ------------------- src/socket/SocketWriter.hpp | 67 ------------- src/socket/ingen_socket_client.cpp | 68 ------------- src/socket/ingen_socket_server.cpp | 152 ----------------------------- src/socket/wscript | 28 ------ src/wscript | 41 ++++---- 18 files changed, 398 insertions(+), 821 deletions(-) create mode 100644 src/SocketReader.cpp create mode 100644 src/SocketWriter.cpp create mode 100644 src/server/SocketServer.hpp delete mode 100644 src/socket/SocketClient.hpp delete mode 100644 src/socket/SocketReader.cpp delete mode 100644 src/socket/SocketReader.hpp delete mode 100644 src/socket/SocketServer.hpp delete mode 100644 src/socket/SocketWriter.cpp delete mode 100644 src/socket/SocketWriter.hpp delete mode 100644 src/socket/ingen_socket_client.cpp delete mode 100644 src/socket/ingen_socket_server.cpp delete mode 100644 src/socket/wscript (limited to 'src') diff --git a/src/SocketReader.cpp b/src/SocketReader.cpp new file mode 100644 index 00000000..125d904f --- /dev/null +++ b/src/SocketReader.cpp @@ -0,0 +1,187 @@ +/* + This file is part of Ingen. + Copyright 2007-2015 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 +#include + +#include "ingen/AtomReader.hpp" +#include "ingen/Interface.hpp" +#include "ingen/Log.hpp" +#include "ingen/SocketReader.hpp" +#include "ingen/URIMap.hpp" +#include "ingen/World.hpp" +#include "sord/sordmm.hpp" +#include "sratom/sratom.h" + +namespace Ingen { + +SocketReader::SocketReader(Ingen::World& world, + Interface& iface, + SPtr sock) + : _world(world) + , _iface(iface) + , _inserter(NULL) + , _msg_node(NULL) + , _socket(sock) + , _exit_flag(false) + , _thread(&SocketReader::run, this) +{} + +SocketReader::~SocketReader() +{ + _exit_flag = true; + _socket->shutdown(); + _thread.join(); +} + +SerdStatus +SocketReader::set_base_uri(SocketReader* iface, + const SerdNode* uri_node) +{ + return sord_inserter_set_base_uri(iface->_inserter, uri_node); +} + +SerdStatus +SocketReader::set_prefix(SocketReader* iface, + const SerdNode* name, + const SerdNode* uri_node) +{ + return sord_inserter_set_prefix(iface->_inserter, name, uri_node); +} + +SerdStatus +SocketReader::write_statement(SocketReader* iface, + SerdStatementFlags flags, + const SerdNode* graph, + const SerdNode* subject, + const SerdNode* predicate, + const SerdNode* object, + const SerdNode* object_datatype, + const SerdNode* object_lang) +{ + if (!iface->_msg_node) { + iface->_msg_node = sord_node_from_serd_node( + iface->_world.rdf_world()->c_obj(), iface->_env, subject, 0, 0); + } + + return sord_inserter_write_statement( + iface->_inserter, flags, graph, + subject, predicate, object, + object_datatype, object_lang); +} + +void +SocketReader::run() +{ + Sord::World* world = _world.rdf_world(); + LV2_URID_Map* map = &_world.uri_map().urid_map_feature()->urid_map; + + // Open socket as a FILE for reading directly with serd + FILE* f = fdopen(_socket->fd(), "r"); + if (!f) { + _world.log().error(fmt("Failed to open connection (%1%)\n") + % strerror(errno)); + // Connection gone, exit + _socket.reset(); + return; + } + + // Use as base URI so e.g. will be a path + SordNode* base_uri = sord_new_uri( + world->c_obj(), (const uint8_t*)"ingen:/root/"); + + // Set up sratom and a forge to build LV2 atoms from 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); + + // Make a model and reader to parse the next Turtle message + _env = world->prefixes().c_obj(); + SordModel* model = sord_new(world->c_obj(), SORD_SPO, false); + + _inserter = sord_inserter_new(model, _env); + + SerdReader* reader = serd_reader_new( + SERD_TURTLE, this, NULL, + (SerdBaseSink)set_base_uri, + (SerdPrefixSink)set_prefix, + (SerdStatementSink)write_statement, + NULL); + + serd_env_set_base_uri(_env, sord_node_to_serd_node(base_uri)); + serd_reader_start_stream(reader, f, (const uint8_t*)"(socket)", false); + + // Make an AtomReader to call Ingen Interface methods based on Atom + AtomReader ar(_world.uri_map(), + _world.uris(), + _world.log(), + _world.forge(), + _iface); + + struct pollfd pfd; + pfd.fd = _socket->fd(); + pfd.events = POLLIN; + pfd.revents = 0; + + while (!_exit_flag) { + if (feof(f)) { + break; // Lost connection + } + + // Wait for input to arrive at socket + int ret = poll(&pfd, 1, -1); + if (ret == -1 || (pfd.revents & (POLLERR|POLLHUP|POLLNVAL))) { + break; // Hangup + } else if (!ret) { + continue; // No data, shouldn't happen + } + + // Read until the next '.' + SerdStatus st = serd_reader_read_chunk(reader); + if (st == SERD_FAILURE || !_msg_node) { + continue; // Read nothing, e.g. just whitespace + } else if (st) { + _world.log().error(fmt("Read error: %1%\n") + % serd_strerror(st)); + continue; + } + + // Build an LV2_Atom at chunk.buf from the message + sratom_read(sratom, &forge, world->c_obj(), model, _msg_node); + + // Call _iface methods based on atom content + ar.write((const LV2_Atom*)chunk.buf); + + // Reset everything for the next iteration + chunk.len = 0; + sord_node_free(world->c_obj(), _msg_node); + _msg_node = NULL; + } + + fclose(f); + sord_inserter_free(_inserter); + serd_reader_end_stream(reader); + sratom_free(sratom); + serd_reader_free(reader); + sord_free(model); + free((uint8_t*)chunk.buf); + _socket.reset(); +} + +} // namespace Ingen diff --git a/src/SocketWriter.cpp b/src/SocketWriter.cpp new file mode 100644 index 00000000..8ce8354a --- /dev/null +++ b/src/SocketWriter.cpp @@ -0,0 +1,96 @@ +/* + This file is part of Ingen. + Copyright 2012-2015 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 +#include +#include + +#include "ingen/SocketWriter.hpp" +#include "ingen/URIMap.hpp" + +#ifndef MSG_NOSIGNAL +# define MSG_NOSIGNAL 0 +#endif + +namespace Ingen { + +static size_t +socket_sink(const void* buf, size_t len, void* stream) +{ + SocketWriter* writer = (SocketWriter*)stream; + ssize_t ret = send(writer->fd(), buf, len, MSG_NOSIGNAL); + if (ret < 0) { + return 0; + } + return ret; +} + +SocketWriter::SocketWriter(URIMap& map, + URIs& uris, + const Raul::URI& uri, + SPtr sock) + : AtomWriter(map, uris, *this) + , _map(map) + , _sratom(sratom_new(&map.urid_map_feature()->urid_map)) + , _uri(uri) + , _socket(sock) +{ + // Use as base URI so e.g. will be a path + _base = serd_node_from_string(SERD_URI, (const uint8_t*)"ingen:/root/"); + + serd_uri_parse(_base.buf, &_base_uri); + + _env = serd_env_new(&_base); + _writer = serd_writer_new( + SERD_TURTLE, + (SerdStyle)(SERD_STYLE_RESOLVED|SERD_STYLE_ABBREVIATED|SERD_STYLE_CURIED), + _env, + &_base_uri, + socket_sink, + this); + + sratom_set_sink(_sratom, + (const char*)_base.buf, + (SerdStatementSink)serd_writer_write_statement, + (SerdEndSink)serd_writer_end_anon, + _writer); +} + +SocketWriter::~SocketWriter() +{ + sratom_free(_sratom); +} + +bool +SocketWriter::write(const LV2_Atom* msg) +{ + sratom_write(_sratom, &_map.urid_unmap_feature()->urid_unmap, 0, + NULL, NULL, msg->type, msg->size, LV2_ATOM_BODY_CONST(msg)); + serd_writer_finish(_writer); + return true; +} + +void +SocketWriter::bundle_end() +{ + AtomWriter::bundle_end(); + + // Send a NULL byte to indicate end of bundle + const char end[] = { 0 }; + send(fd(), end, 1, MSG_NOSIGNAL); +} + +} // namespace Ingen diff --git a/src/World.cpp b/src/World.cpp index 5b7214a4..badbf353 100644 --- a/src/World.cpp +++ b/src/World.cpp @@ -267,6 +267,7 @@ World::load_module(const char* name) if (i != _impl->modules.end()) { return true; } + log().info(fmt("Loading %1% module\n") % name); Glib::Module* lib = ingen_load_module(log(), name); Ingen::Module* (*module_load)() = NULL; if (lib && lib->get_symbol("ingen_module_load", (void*&)module_load)) { diff --git a/src/ingen/ingen.cpp b/src/ingen/ingen.cpp index f37608b9..6355c753 100644 --- a/src/ingen/ingen.cpp +++ b/src/ingen/ingen.cpp @@ -39,6 +39,9 @@ #ifdef WITH_BINDINGS #include "bindings/ingen_bindings.hpp" #endif +#ifdef HAVE_SOCKET +#include "ingen/client/SocketClient.hpp" +#endif using namespace std; using namespace Ingen; @@ -117,13 +120,9 @@ main(int argc, char** argv) "Unable to load server module"); ingen_try(bool(world->engine()), "Unable to create engine"); + world->engine()->listen(); engine_interface = world->interface(); - -#ifdef HAVE_SOCKET - ingen_try(world->load_module("socket_server"), - "Unable to load socket server module"); -#endif } // If we don't have a local engine interface (for GUI), use network @@ -131,8 +130,7 @@ main(int argc, char** argv) ingen_try(world->load_module("client"), "Unable to load client module"); #ifdef HAVE_SOCKET - ingen_try(world->load_module("socket_client"), - "Unable to load socket client module"); + Client::SocketClient::register_factories(world); #endif const char* const uri = conf.option("connect").ptr(); ingen_try(Raul::URI::is_valid(uri), diff --git a/src/server/Engine.cpp b/src/server/Engine.cpp index 76755a2c..c905eed2 100644 --- a/src/server/Engine.cpp +++ b/src/server/Engine.cpp @@ -1,6 +1,6 @@ /* This file is part of Ingen. - Copyright 2007-2013 David Robillard + Copyright 2007-2015 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 @@ -14,6 +14,8 @@ along with Ingen. If not, see . */ +#include "ingen_config.h" + #include #include @@ -45,6 +47,9 @@ #include "ProcessContext.hpp" #include "ThreadManager.hpp" #include "Worker.hpp" +#ifdef HAVE_SOCKET +#include "SocketListener.hpp" +#endif using namespace std; @@ -67,6 +72,7 @@ Engine::Engine(Ingen::World* world) , _post_processor(new PostProcessor(*this)) , _root_graph(NULL) , _worker(new Worker(world->log(), event_queue_size())) + , _listener(NULL) , _process_context(*this) , _rand_engine(0) , _uniform_dist(0.0f, 1.0f) @@ -119,6 +125,9 @@ Engine::~Engine() _world->set_store(SPtr()); +#ifdef HAVE_SOCKET + delete _listener; +#endif delete _pre_processor; delete _post_processor; delete _block_factory; @@ -135,6 +144,14 @@ Engine::~Engine() munlockall(); } +void +Engine::listen() +{ +#ifdef HAVE_SOCKET + _listener = new SocketListener(*this); +#endif +} + SPtr Engine::store() const { diff --git a/src/server/Engine.hpp b/src/server/Engine.hpp index 851b3127..ecc90c9b 100644 --- a/src/server/Engine.hpp +++ b/src/server/Engine.hpp @@ -1,6 +1,6 @@ /* This file is part of Ingen. - Copyright 2007-2012 David Robillard + Copyright 2007-2015 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 @@ -48,6 +48,7 @@ class LV2Options; class PostProcessor; class PreProcessor; class ProcessContext; +class SocketListener; class Worker; /** @@ -78,6 +79,8 @@ public: SPtr client); virtual bool unregister_client(const Raul::URI& uri); + void listen(); + /** Return a random [0..1] float with uniform distribution */ float frand() { return _uniform_dist(_rand_engine); } @@ -130,6 +133,7 @@ private: PostProcessor* _post_processor; GraphImpl* _root_graph; Worker* _worker; + SocketListener* _listener; ProcessContext _process_context; diff --git a/src/server/SocketServer.hpp b/src/server/SocketServer.hpp new file mode 100644 index 00000000..5ebcaa6f --- /dev/null +++ b/src/server/SocketServer.hpp @@ -0,0 +1,61 @@ +/* + This file is part of Ingen. + Copyright 2007-2015 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 . +*/ + +#ifndef INGEN_SERVER_SOCKET_SERVER_HPP +#define INGEN_SERVER_SOCKET_SERVER_HPP + +#include "raul/Socket.hpp" + +#include "ingen/SocketReader.hpp" +#include "ingen/SocketWriter.hpp" + +#include "EventWriter.hpp" + +namespace Ingen { +namespace Server { + +/** The server side of an Ingen socket connection. */ +class SocketServer : public EventWriter, public SocketReader +{ +public: + SocketServer(World& world, + Server::Engine& engine, + SPtr sock) + : EventWriter(engine) + , SocketReader(world, *this, sock) + , _engine(engine) + , _writer(new SocketWriter(world.uri_map(), + world.uris(), + sock->uri(), + sock)) + { + set_respondee(_writer); + engine.register_client(_writer->uri(), _writer); + } + + ~SocketServer() { + _engine.unregister_client(_writer->uri()); + } + +private: + Server::Engine& _engine; + SPtr _writer; +}; + +} // namespace Ingen +} // namespace Socket + +#endif // INGEN_SERVER_SOCKET_SERVER_HPP diff --git a/src/server/wscript b/src/server/wscript index 76362fa5..9d1238a4 100644 --- a/src/server/wscript +++ b/src/server/wscript @@ -25,6 +25,7 @@ def build(bld): PortImpl.cpp PostProcessor.cpp PreProcessor.cpp + SocketListener.cpp Worker.cpp events/Connect.cpp events/CreateBlock.cpp @@ -53,7 +54,7 @@ def build(bld): name = 'libingen_server', target = 'ingen_server', install_path = '${LIBDIR}', - use = 'libingen', + use = 'libingen libingen_socket', cxxflags = bld.env.PTHREAD_CFLAGS, linkflags = bld.env.PTHREAD_LINKFLAGS) core_libs = 'GLIBMM LV2 LILV RAUL SERD SORD' diff --git a/src/socket/SocketClient.hpp b/src/socket/SocketClient.hpp deleted file mode 100644 index ae4b47d0..00000000 --- a/src/socket/SocketClient.hpp +++ /dev/null @@ -1,55 +0,0 @@ -/* - This file is part of Ingen. - Copyright 2012 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 . -*/ - -#ifndef INGEN_SOCKET_SOCKET_CLIENT_HPP -#define INGEN_SOCKET_SOCKET_CLIENT_HPP - -#include "SocketReader.hpp" -#include "SocketWriter.hpp" - -namespace Ingen { -namespace Socket { - -/** The client side of an Ingen socket connection. */ -class SocketClient : public SocketWriter -{ -public: - SocketClient(World& world, - const Raul::URI& uri, - SPtr sock, - SPtr respondee) - : SocketWriter(world.uri_map(), world.uris(), uri, sock) - , _respondee(respondee) - , _reader(world, *respondee.get(), sock) - {} - - virtual SPtr respondee() const { - return _respondee; - } - - virtual void set_respondee(SPtr respondee) { - _respondee = respondee; - } - -private: - SPtr _respondee; - SocketReader _reader; -}; - -} // namespace Socket -} // namespace Ingen - -#endif // INGEN_SOCKET_SOCKET_CLIENT_HPP diff --git a/src/socket/SocketReader.cpp b/src/socket/SocketReader.cpp deleted file mode 100644 index 7e1a9e91..00000000 --- a/src/socket/SocketReader.cpp +++ /dev/null @@ -1,190 +0,0 @@ -/* - This file is part of Ingen. - Copyright 2007-2012 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 -#include - -#include "ingen/AtomReader.hpp" -#include "ingen/Interface.hpp" -#include "ingen/Log.hpp" -#include "ingen/URIMap.hpp" -#include "ingen/World.hpp" -#include "sord/sordmm.hpp" -#include "sratom/sratom.h" - -#include "SocketReader.hpp" - -namespace Ingen { -namespace Socket { - -SocketReader::SocketReader(Ingen::World& world, - Interface& iface, - SPtr sock) - : _world(world) - , _iface(iface) - , _inserter(NULL) - , _msg_node(NULL) - , _socket(sock) - , _exit_flag(false) - , _thread(&SocketReader::run, this) -{} - -SocketReader::~SocketReader() -{ - _exit_flag = true; - _socket->shutdown(); - _thread.join(); -} - -SerdStatus -SocketReader::set_base_uri(SocketReader* iface, - const SerdNode* uri_node) -{ - return sord_inserter_set_base_uri(iface->_inserter, uri_node); -} - -SerdStatus -SocketReader::set_prefix(SocketReader* iface, - const SerdNode* name, - const SerdNode* uri_node) -{ - return sord_inserter_set_prefix(iface->_inserter, name, uri_node); -} - -SerdStatus -SocketReader::write_statement(SocketReader* iface, - SerdStatementFlags flags, - const SerdNode* graph, - const SerdNode* subject, - const SerdNode* predicate, - const SerdNode* object, - const SerdNode* object_datatype, - const SerdNode* object_lang) -{ - if (!iface->_msg_node) { - iface->_msg_node = sord_node_from_serd_node( - iface->_world.rdf_world()->c_obj(), iface->_env, subject, 0, 0); - } - - return sord_inserter_write_statement( - iface->_inserter, flags, graph, - subject, predicate, object, - object_datatype, object_lang); -} - -void -SocketReader::run() -{ - Sord::World* world = _world.rdf_world(); - LV2_URID_Map* map = &_world.uri_map().urid_map_feature()->urid_map; - - // Open socket as a FILE for reading directly with serd - FILE* f = fdopen(_socket->fd(), "r"); - if (!f) { - _world.log().error(fmt("Failed to open connection (%1%)\n") - % strerror(errno)); - // Connection gone, exit - _socket.reset(); - return; - } - - // Use as base URI so e.g. will be a path - SordNode* base_uri = sord_new_uri( - world->c_obj(), (const uint8_t*)"ingen:/root/"); - - // Set up sratom and a forge to build LV2 atoms from 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); - - // Make a model and reader to parse the next Turtle message - _env = world->prefixes().c_obj(); - SordModel* model = sord_new(world->c_obj(), SORD_SPO, false); - - _inserter = sord_inserter_new(model, _env); - - SerdReader* reader = serd_reader_new( - SERD_TURTLE, this, NULL, - (SerdBaseSink)set_base_uri, - (SerdPrefixSink)set_prefix, - (SerdStatementSink)write_statement, - NULL); - - serd_env_set_base_uri(_env, sord_node_to_serd_node(base_uri)); - serd_reader_start_stream(reader, f, (const uint8_t*)"(socket)", false); - - // Make an AtomReader to call Ingen Interface methods based on Atom - AtomReader ar(_world.uri_map(), - _world.uris(), - _world.log(), - _world.forge(), - _iface); - - struct pollfd pfd; - pfd.fd = _socket->fd(); - pfd.events = POLLIN; - pfd.revents = 0; - - while (!_exit_flag) { - if (feof(f)) { - break; // Lost connection - } - - // Wait for input to arrive at socket - int ret = poll(&pfd, 1, -1); - if (ret == -1 || (pfd.revents & (POLLERR|POLLHUP|POLLNVAL))) { - break; // Hangup - } else if (!ret) { - continue; // No data, shouldn't happen - } - - // Read until the next '.' - SerdStatus st = serd_reader_read_chunk(reader); - if (st == SERD_FAILURE || !_msg_node) { - continue; // Read nothing, e.g. just whitespace - } else if (st) { - _world.log().error(fmt("Read error: %1%\n") - % serd_strerror(st)); - continue; - } - - // Build an LV2_Atom at chunk.buf from the message - sratom_read(sratom, &forge, world->c_obj(), model, _msg_node); - - // Call _iface methods based on atom content - ar.write((const LV2_Atom*)chunk.buf); - - // Reset everything for the next iteration - chunk.len = 0; - sord_node_free(world->c_obj(), _msg_node); - _msg_node = NULL; - } - - fclose(f); - sord_inserter_free(_inserter); - serd_reader_end_stream(reader); - sratom_free(sratom); - serd_reader_free(reader); - sord_free(model); - free((uint8_t*)chunk.buf); - _socket.reset(); -} - -} // namespace Ingen -} // namespace Socket diff --git a/src/socket/SocketReader.hpp b/src/socket/SocketReader.hpp deleted file mode 100644 index 38afedef..00000000 --- a/src/socket/SocketReader.hpp +++ /dev/null @@ -1,74 +0,0 @@ -/* - This file is part of Ingen. - Copyright 2007-2012 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 . -*/ - -#ifndef INGEN_SOCKET_SOCKET_READER_HPP -#define INGEN_SOCKET_SOCKET_READER_HPP - -#include - -#include "raul/Socket.hpp" -#include "sord/sord.h" - -namespace Ingen { - -class Interface; -class World; - -namespace Socket { - -/** Calls Interface methods based on Turtle messages received via socket. */ -class SocketReader -{ -public: - SocketReader(World& world, - Interface& iface, - SPtr sock); - - ~SocketReader(); - -private: - void run(); - - static SerdStatus set_base_uri(SocketReader* iface, - const SerdNode* uri_node); - - static SerdStatus set_prefix(SocketReader* iface, - const SerdNode* name, - const SerdNode* uri_node); - - static SerdStatus write_statement(SocketReader* iface, - SerdStatementFlags flags, - const SerdNode* graph, - const SerdNode* subject, - const SerdNode* predicate, - const SerdNode* object, - const SerdNode* object_datatype, - const SerdNode* object_lang); - - World& _world; - Interface& _iface; - SerdEnv* _env; - SordInserter* _inserter; - SordNode* _msg_node; - SPtr _socket; - bool _exit_flag; - std::thread _thread; -}; - -} // namespace Ingen -} // namespace Socket - -#endif // INGEN_SOCKET_SOCKET_READER_HPP diff --git a/src/socket/SocketServer.hpp b/src/socket/SocketServer.hpp deleted file mode 100644 index a977bdbc..00000000 --- a/src/socket/SocketServer.hpp +++ /dev/null @@ -1,60 +0,0 @@ -/* - This file is part of Ingen. - Copyright 2007-2012 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 . -*/ - -#ifndef INGEN_SOCKET_SOCKET_SERVER_HPP -#define INGEN_SOCKET_SOCKET_SERVER_HPP - -#include "raul/Socket.hpp" - -#include "../server/EventWriter.hpp" -#include "SocketReader.hpp" -#include "SocketWriter.hpp" - -namespace Ingen { -namespace Socket { - -/** The server side of an Ingen socket connection. */ -class SocketServer : public Server::EventWriter, public SocketReader -{ -public: - SocketServer(World& world, - Server::Engine& engine, - SPtr sock) - : Server::EventWriter(engine) - , SocketReader(world, *this, sock) - , _engine(engine) - , _writer(new SocketWriter(world.uri_map(), - world.uris(), - sock->uri(), - sock)) - { - set_respondee(_writer); - engine.register_client(_writer->uri(), _writer); - } - - ~SocketServer() { - _engine.unregister_client(_writer->uri()); - } - -private: - Server::Engine& _engine; - SPtr _writer; -}; - -} // namespace Ingen -} // namespace Socket - -#endif // INGEN_SOCKET_SOCKET_SERVER_HPP diff --git a/src/socket/SocketWriter.cpp b/src/socket/SocketWriter.cpp deleted file mode 100644 index faed014c..00000000 --- a/src/socket/SocketWriter.cpp +++ /dev/null @@ -1,99 +0,0 @@ -/* - This file is part of Ingen. - Copyright 2012 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 -#include -#include - -#include "ingen/URIMap.hpp" - -#include "SocketWriter.hpp" - -#ifndef MSG_NOSIGNAL -# define MSG_NOSIGNAL 0 -#endif - -namespace Ingen { -namespace Socket { - -static size_t -socket_sink(const void* buf, size_t len, void* stream) -{ - SocketWriter* writer = (SocketWriter*)stream; - ssize_t ret = send(writer->fd(), buf, len, MSG_NOSIGNAL); - if (ret < 0) { - return 0; - } - return ret; -} - -SocketWriter::SocketWriter(URIMap& map, - URIs& uris, - const Raul::URI& uri, - SPtr sock) - : AtomWriter(map, uris, *this) - , _map(map) - , _sratom(sratom_new(&map.urid_map_feature()->urid_map)) - , _uri(uri) - , _socket(sock) -{ - // Use as base URI so e.g. will be a path - _base = serd_node_from_string(SERD_URI, (const uint8_t*)"ingen:/root/"); - - serd_uri_parse(_base.buf, &_base_uri); - - _env = serd_env_new(&_base); - _writer = serd_writer_new( - SERD_TURTLE, - (SerdStyle)(SERD_STYLE_RESOLVED|SERD_STYLE_ABBREVIATED|SERD_STYLE_CURIED), - _env, - &_base_uri, - socket_sink, - this); - - sratom_set_sink(_sratom, - (const char*)_base.buf, - (SerdStatementSink)serd_writer_write_statement, - (SerdEndSink)serd_writer_end_anon, - _writer); -} - -SocketWriter::~SocketWriter() -{ - sratom_free(_sratom); -} - -bool -SocketWriter::write(const LV2_Atom* msg) -{ - sratom_write(_sratom, &_map.urid_unmap_feature()->urid_unmap, 0, - NULL, NULL, msg->type, msg->size, LV2_ATOM_BODY_CONST(msg)); - serd_writer_finish(_writer); - return true; -} - -void -SocketWriter::bundle_end() -{ - AtomWriter::bundle_end(); - - // Send a NULL byte to indicate end of bundle - const char end[] = { 0 }; - send(fd(), end, 1, MSG_NOSIGNAL); -} - -} // namespace Socket -} // namespace Ingen diff --git a/src/socket/SocketWriter.hpp b/src/socket/SocketWriter.hpp deleted file mode 100644 index 1b3523d1..00000000 --- a/src/socket/SocketWriter.hpp +++ /dev/null @@ -1,67 +0,0 @@ -/* - This file is part of Ingen. - Copyright 2012 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 . -*/ - -#ifndef INGEN_SOCKET_SOCKET_WRITER_HPP -#define INGEN_SOCKET_SOCKET_WRITER_HPP - -#include - -#include "ingen/AtomSink.hpp" -#include "ingen/AtomWriter.hpp" -#include "ingen/Interface.hpp" -#include "ingen/types.hpp" -#include "ingen/types.hpp" -#include "raul/Socket.hpp" -#include "raul/URI.hpp" -#include "sratom/sratom.h" - -namespace Ingen { -namespace Socket { - -/** An Interface that writes Turtle messages to a socket. - */ -class SocketWriter : public AtomWriter, public AtomSink -{ -public: - SocketWriter(URIMap& map, - URIs& uris, - const Raul::URI& uri, - SPtr sock); - - ~SocketWriter(); - - bool write(const LV2_Atom* msg); - - void bundle_end(); - - int fd() { return _socket->fd(); } - Raul::URI uri() const { return _uri; } - -protected: - URIMap& _map; - Sratom* _sratom; - SerdNode _base; - SerdURI _base_uri; - SerdEnv* _env; - SerdWriter* _writer; - Raul::URI _uri; - SPtr _socket; -}; - -} // namespace Socket -} // namespace Ingen - -#endif // INGEN_SOCKET_SOCKET_WRITER_HPP diff --git a/src/socket/ingen_socket_client.cpp b/src/socket/ingen_socket_client.cpp deleted file mode 100644 index 61a175d4..00000000 --- a/src/socket/ingen_socket_client.cpp +++ /dev/null @@ -1,68 +0,0 @@ -/* - This file is part of Ingen. - Copyright 2007-2012 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 - -#include "ingen/Log.hpp" -#include "ingen/Module.hpp" -#include "ingen/World.hpp" -#include "raul/Socket.hpp" - -#include "SocketClient.hpp" - -namespace Ingen { -namespace Socket { - -static Raul::Socket::Type type_from_uri(const Raul::URI uri) { - return (uri.scheme() == "unix") - ? Raul::Socket::Type::UNIX - : Raul::Socket::Type::TCP; -} - -static SPtr -new_socket_interface(Ingen::World* world, - const Raul::URI& uri, - SPtr respondee) -{ - SPtr sock(new Raul::Socket(type_from_uri(uri))); - if (!sock->connect(uri)) { - world->log().error(fmt("Failed to connect <%1%> (%2%)\n") - % sock->uri() % strerror(errno)); - return SPtr(); - } - SocketClient* client = new SocketClient(*world, uri, sock, respondee); - return SPtr(client); -} - -struct SocketClientModule : public Module { - void load(World* world) { - world->add_interface_factory("unix", &new_socket_interface); - world->add_interface_factory("tcp", &new_socket_interface); - } -}; - -} // namespace Socket -} // namespace Ingen - -extern "C" { - -Ingen::Module* -ingen_module_load() -{ - return new Ingen::Socket::SocketClientModule(); -} - -} // extern "C" diff --git a/src/socket/ingen_socket_server.cpp b/src/socket/ingen_socket_server.cpp deleted file mode 100644 index 5aff7179..00000000 --- a/src/socket/ingen_socket_server.cpp +++ /dev/null @@ -1,152 +0,0 @@ -/* - This file is part of Ingen. - Copyright 2007-2012 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 -#include - -#include -#include - -#include "ingen/Configuration.hpp" -#include "ingen/Module.hpp" -#include "ingen/World.hpp" -#include "raul/Socket.hpp" - -#include "../server/Engine.hpp" -#include "../server/EventWriter.hpp" - -#include "SocketServer.hpp" - -#define UNIX_SCHEME "unix://" - -namespace Ingen { -namespace Socket { - -static void -ingen_listen(Ingen::World* world, - Raul::Socket* unix_sock, - Raul::Socket* net_sock) -{ - const std::string unix_path(world->conf().option("socket").ptr()); - - SPtr engine = dynamic_ptr_cast( - world->engine()); - - // Bind UNIX socket - const Raul::URI unix_uri(UNIX_SCHEME + unix_path); - if (!unix_sock->bind(unix_uri) || !unix_sock->listen()) { - world->log().error("Failed to create UNIX socket\n"); - unix_sock->close(); - } else { - world->log().info(fmt("Listening on socket %1%\n") % unix_uri); - } - - // Bind TCP socket - const int port = world->conf().option("engine-port").get(); - std::ostringstream ss; - ss << "tcp://localhost:"; - ss << port; - if (!net_sock->bind(Raul::URI(ss.str())) || !net_sock->listen()) { - world->log().error("Failed to create TCP socket\n"); - net_sock->close(); - } else { - world->log().info(fmt("Listening on TCP port %1%\n") % port); - } - - if (unix_sock->fd() == -1 && net_sock->fd() == -1) { - return; // No sockets to listen to, exit thread - } - - struct pollfd pfds[2]; - int nfds = 0; - if (unix_sock->fd() != -1) { - pfds[nfds].fd = unix_sock->fd(); - pfds[nfds].events = POLLIN; - pfds[nfds].revents = 0; - ++nfds; - } - if (net_sock->fd() != -1) { - pfds[nfds].fd = net_sock->fd(); - pfds[nfds].events = POLLIN; - pfds[nfds].revents = 0; - ++nfds; - } - - while (true) { - // Wait for input to arrive at a socket - const int ret = poll(pfds, nfds, -1); - if (ret == -1) { - world->log().error(fmt("Poll error: %1%\n") % strerror(errno)); - break; - } else if ((pfds[0].revents & POLLHUP) || pfds[1].revents & POLLHUP) { - break; - } else if (ret == 0) { - world->log().error("Poll returned with no data\n"); - continue; - } - - if (pfds[0].revents & POLLIN) { - SPtr conn = unix_sock->accept(); - if (conn) { - new SocketServer(*world, *engine, conn); - } - } - - if (pfds[1].revents & POLLIN) { - SPtr conn = net_sock->accept(); - if (conn) { - new SocketServer(*world, *engine, conn); - } - } - } -} - -struct ServerModule : public Ingen::Module -{ - ServerModule() - : unix_sock(Raul::Socket::Type::UNIX) - , net_sock(Raul::Socket::Type::TCP) - {} - - ~ServerModule() { - unix_sock.shutdown(); - net_sock.shutdown(); - thread->join(); - unlink(unix_sock.uri().substr(strlen(UNIX_SCHEME)).c_str()); - } - - void load(World* world) { - thread = std::unique_ptr( - new std::thread(ingen_listen, world, &unix_sock, &net_sock)); - } - - Raul::Socket unix_sock; - Raul::Socket net_sock; - std::unique_ptr thread; -}; - -} // namespace Socket -} // namespace Ingen - -extern "C" { - -Ingen::Module* -ingen_module_load() -{ - return new Ingen::Socket::ServerModule(); -} - -} // extern "C" diff --git a/src/socket/wscript b/src/socket/wscript deleted file mode 100644 index 1ebc9f4c..00000000 --- a/src/socket/wscript +++ /dev/null @@ -1,28 +0,0 @@ -#!/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 = ['SocketReader.cpp', - 'SocketWriter.cpp', - 'ingen_socket_server.cpp'], - includes = ['.', '../..'], - name = 'libingen_socket_server', - target = 'ingen_socket_server', - install_path = '${LIBDIR}', - use = 'libingen_server', - lib = ['pthread']) - autowaf.use_lib(bld, obj, 'GLIBMM SERD SORD SRATOM RAUL LV2') - - obj = bld(features = 'cxx cxxshlib', - source = ['SocketReader.cpp', - 'SocketWriter.cpp', - 'ingen_socket_client.cpp'], - includes = ['.', '../..'], - name = 'libingen_socket_client', - target = 'ingen_socket_client', - install_path = '${LIBDIR}', - use = 'libingen_server', - lib = ['pthread']) - autowaf.use_lib(bld, obj, 'GLIBMM SERD SORD SRATOM RAUL LV2') diff --git a/src/wscript b/src/wscript index 7aad0dae..fbca8fe9 100644 --- a/src/wscript +++ b/src/wscript @@ -1,23 +1,25 @@ #!/usr/bin/env python from waflib.extras import autowaf as autowaf -sources = [ - 'AtomReader.cpp', - 'AtomWriter.cpp', - 'ClashAvoider.cpp', - 'Configuration.cpp', - 'Forge.cpp', - 'LV2Features.cpp', - 'Log.cpp', - 'Resource.cpp', - 'Store.cpp', - 'URIMap.cpp', - 'URIs.cpp', - 'World.cpp', - 'runtime_paths.cpp', -] - def build(bld): + sources = [ + 'AtomReader.cpp', + 'AtomWriter.cpp', + 'ClashAvoider.cpp', + 'Configuration.cpp', + 'Forge.cpp', + 'LV2Features.cpp', + 'Log.cpp', + 'Resource.cpp', + 'Store.cpp', + 'URIMap.cpp', + 'URIs.cpp', + 'World.cpp', + 'runtime_paths.cpp' + ] + if bld.is_defined('HAVE_SOCKET'): + sources += [ 'SocketReader.cpp', 'SocketWriter.cpp' ] + obj = bld(features = 'cxx cxxshlib', source = sources, export_includes = ['..'], @@ -26,7 +28,9 @@ def build(bld): target = 'ingen', vnum = '0.0.0', install_path = '${LIBDIR}', - lib = ['dl']) + lib = ['dl'], + cxxflags = bld.env.PTHREAD_CFLAGS, + linkflags = bld.env.PTHREAD_LINKFLAGS) autowaf.use_lib(bld, obj, 'GLIBMM LV2 LILV RAUL SERD SORD SRATOM') if bld.env.BUILD_TESTS: @@ -38,6 +42,7 @@ def build(bld): target = 'ingen_profiled', install_path = '', lib = ['dl'] + bld.env.INGEN_TEST_LIBS, - cxxflags = bld.env.INGEN_TEST_CXXFLAGS) + cxxflags = bld.env.PTHREAD_CFLAGS + bld.env.INGEN_TEST_CXXFLAGS, + linkflags = bld.env.PTHREAD_LINKFLAGS) autowaf.use_lib(bld, obj, 'GLIBMM LV2 LILV RAUL SERD SORD SRATOM') -- cgit v1.2.1