From e114ea3254cea61fb97213e18be8bf266bcc1be1 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Thu, 3 May 2012 00:04:58 +0000 Subject: SocketReceiver => SocketListener. git-svn-id: http://svn.drobilla.net/lad/trunk/ingen@4317 a436a847-0d15-0410-975c-d299462d15a1 --- src/socket/SocketListener.cpp | 149 +++++++++++++++++++++++++++++++++++++ src/socket/SocketListener.hpp | 48 ++++++++++++ src/socket/SocketReceiver.cpp | 149 ------------------------------------- src/socket/SocketReceiver.hpp | 48 ------------ src/socket/ingen_socket_server.cpp | 8 +- src/socket/wscript | 2 +- 6 files changed, 202 insertions(+), 202 deletions(-) create mode 100644 src/socket/SocketListener.cpp create mode 100644 src/socket/SocketListener.hpp delete mode 100644 src/socket/SocketReceiver.cpp delete mode 100644 src/socket/SocketReceiver.hpp (limited to 'src/socket') diff --git a/src/socket/SocketListener.cpp b/src/socket/SocketListener.cpp new file mode 100644 index 00000000..ba427433 --- /dev/null +++ b/src/socket/SocketListener.cpp @@ -0,0 +1,149 @@ +/* + 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 "ingen/Interface.hpp" +#include "ingen/shared/World.hpp" +#include "ingen/shared/AtomReader.hpp" +#include "sord/sordmm.hpp" +#include "sratom/sratom.h" +#include "SocketListener.hpp" + +#define LOG(s) s << "[SocketListener] " + +namespace Ingen { +namespace Socket { + +SocketListener::SocketListener(Ingen::Shared::World& world, + SharedPtr 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(); +} + +SocketListener::~SocketListener() +{ + stop(); + join(); + close(_sock); + unlink(_sock_path.c_str()); +} + +void +SocketListener::_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. 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/SocketListener.hpp b/src/socket/SocketListener.hpp new file mode 100644 index 00000000..896b08b4 --- /dev/null +++ b/src/socket/SocketListener.hpp @@ -0,0 +1,48 @@ +/* + 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 "raul/SharedPtr.hpp" +#include "raul/Thread.hpp" + +namespace Ingen { + +class Interface; + +namespace Shared { class World; } + +namespace Socket { + +class SocketListener : public Raul::Thread +{ +public: + SocketListener(Ingen::Shared::World& world, + SharedPtr iface); + + ~SocketListener(); + +private: + virtual void _run(); + + Ingen::Shared::World& _world; + SharedPtr _iface; + std::string _sock_path; + int _sock; +}; + +} // namespace Ingen +} // namespace Socket diff --git a/src/socket/SocketReceiver.cpp b/src/socket/SocketReceiver.cpp deleted file mode 100644 index 833c90ac..00000000 --- a/src/socket/SocketReceiver.cpp +++ /dev/null @@ -1,149 +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 "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 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. 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 deleted file mode 100644 index 5c7ea316..00000000 --- a/src/socket/SocketReceiver.hpp +++ /dev/null @@ -1,48 +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 "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 iface); - - ~SocketReceiver(); - -private: - virtual void _run(); - - Ingen::Shared::World& _world; - SharedPtr _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 index 79e2e05e..7ee20217 100644 --- a/src/socket/ingen_socket_server.cpp +++ b/src/socket/ingen_socket_server.cpp @@ -22,7 +22,7 @@ #include "../server/Engine.hpp" #include "../server/ServerInterfaceImpl.hpp" -#include "SocketReceiver.hpp" +#include "SocketListener.hpp" using namespace Ingen; @@ -31,13 +31,13 @@ struct IngenSocketServerModule : public Ingen::Shared::Module { Server::Engine* engine = (Server::Engine*)world->local_engine().get(); SharedPtr interface( new Server::ServerInterfaceImpl(*engine)); - receiver = SharedPtr( - new Ingen::Socket::SocketReceiver(*world, interface)); + receiver = SharedPtr( + new Ingen::Socket::SocketListener(*world, interface)); engine->add_event_source(interface); } - SharedPtr receiver; + SharedPtr receiver; }; extern "C" { diff --git a/src/socket/wscript b/src/socket/wscript index e5de6cec..6b104906 100644 --- a/src/socket/wscript +++ b/src/socket/wscript @@ -4,7 +4,7 @@ from waflib.extras import autowaf as autowaf def build(bld): if bld.is_defined('HAVE_SOCKET'): obj = bld(features = 'cxx cxxshlib', - source = ['SocketReceiver.cpp', + source = ['SocketListener.cpp', 'ingen_socket_server.cpp'], includes = ['.', '../..'], name = 'libingen_socket_server', -- cgit v1.2.1