pipewire/pinos/server/daemon.c

780 lines
22 KiB
C
Raw Normal View History

2015-06-30 18:06:36 +02:00
/* Pinos
2015-04-16 16:58:33 +02:00
* Copyright (C) 2015 Wim Taymans <wim.taymans@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <string.h>
#include <stdio.h>
2015-04-16 16:58:33 +02:00
#include <gio/gio.h>
#include <gio/gunixfdlist.h>
2015-04-16 16:58:33 +02:00
#include "config.h"
#include "pinos/client/pinos.h"
#include "pinos/client/log.h"
#include "pinos/server/daemon.h"
2016-07-18 17:40:58 +02:00
#include "pinos/server/node.h"
#include "pinos/server/client-node.h"
#include "pinos/server/client.h"
#include "pinos/server/link.h"
2016-11-10 15:42:14 +01:00
#include "pinos/server/node-factory.h"
#include "pinos/server/data-loop.h"
#include "pinos/server/main-loop.h"
#include "pinos/dbus/org-pinos.h"
2015-04-16 16:58:33 +02:00
2016-11-10 15:42:14 +01:00
typedef struct {
PinosDaemon daemon;
PinosObject object;
PinosDaemon1 *iface;
2015-04-16 16:58:33 +02:00
guint id;
GDBusConnection *connection;
GDBusObjectManagerServer *server_manager;
gchar *object_path;
PinosDataLoop *data_loop;
PinosListener object_added;
PinosListener object_removed;
2016-11-10 15:42:14 +01:00
PinosListener port_unlinked;
PinosListener notify_state;
2016-11-10 15:42:14 +01:00
GHashTable *clients;
GHashTable *node_factories;
2016-11-10 15:42:14 +01:00
} PinosDaemonImpl;
2016-11-10 15:42:14 +01:00
static void try_link_port (PinosNode *node, PinosPort *port, PinosDaemon *daemon);
static void
handle_client_appeared (PinosClient *client, gpointer user_data)
{
2016-11-10 15:42:14 +01:00
PinosDaemonImpl *impl = user_data;
2016-11-10 15:42:14 +01:00
pinos_log_debug ("daemon %p: appeared %p", impl, client);
2016-11-10 15:42:14 +01:00
g_hash_table_insert (impl->clients, (gpointer) client->sender, client);
}
static void
handle_client_vanished (PinosClient *client, gpointer user_data)
{
2016-11-10 15:42:14 +01:00
PinosDaemonImpl *impl = user_data;
pinos_log_debug ("daemon %p: vanished %p", daemon, client);
2016-11-10 15:42:14 +01:00
g_hash_table_remove (impl->clients, (gpointer) client->sender);
}
2016-04-11 15:26:15 +02:00
static PinosClient *
sender_get_client (PinosDaemon *daemon,
const gchar *sender,
gboolean create)
{
2016-11-10 15:42:14 +01:00
PinosDaemonImpl *impl = (PinosDaemonImpl *) daemon;
PinosClient *client;
2016-11-10 15:42:14 +01:00
client = g_hash_table_lookup (impl->clients, sender);
if (client == NULL && create) {
2016-11-10 15:42:14 +01:00
client = pinos_client_new (daemon->core, sender, NULL);
pinos_log_debug ("daemon %p: new client %p for %s", daemon, client, sender);
g_signal_connect (client,
"appeared",
(GCallback) handle_client_appeared,
daemon);
g_signal_connect (client,
"vanished",
(GCallback) handle_client_vanished,
daemon);
}
return client;
}
2015-05-27 18:16:52 +02:00
static void
2016-07-18 17:40:58 +02:00
handle_remove_node (PinosNode *node,
gpointer user_data)
2015-05-27 18:16:52 +02:00
{
2016-11-10 15:42:14 +01:00
//PinosClient *client = user_data;
2016-04-11 15:26:15 +02:00
2016-11-10 15:42:14 +01:00
//pinos_log_debug ("client %p: node %p remove", daemon, node);
//pinos_client_remove_object (client, &node->object);
2015-05-27 18:16:52 +02:00
}
2015-04-16 16:58:33 +02:00
static gboolean
handle_create_node (PinosDaemon1 *interface,
GDBusMethodInvocation *invocation,
const gchar *arg_factory_name,
const gchar *arg_name,
GVariant *arg_properties,
gpointer user_data)
2015-04-16 16:58:33 +02:00
{
2016-11-10 15:42:14 +01:00
PinosDaemonImpl *impl = user_data;
PinosNodeFactory *factory;
PinosNode *node;
PinosClient *client;
const gchar *sender, *object_path;
PinosProperties *props;
2015-04-16 16:58:33 +02:00
sender = g_dbus_method_invocation_get_sender (invocation);
2016-11-10 15:42:14 +01:00
client = sender_get_client (&impl->daemon, sender, TRUE);
2015-04-16 16:58:33 +02:00
2016-11-10 15:42:14 +01:00
pinos_log_debug ("daemon %p: create node: %s", impl, sender);
2016-04-11 15:26:15 +02:00
props = pinos_properties_from_variant (arg_properties);
2016-11-10 15:42:14 +01:00
factory = g_hash_table_lookup (impl->node_factories, arg_factory_name);
if (factory == NULL)
goto no_factory;
node = pinos_node_factory_create_node (factory,
client,
arg_name,
props);
pinos_properties_free (props);
if (node == NULL)
goto no_node;
2015-04-16 16:58:33 +02:00
2016-11-10 15:42:14 +01:00
//pinos_client_add_object (client, &node->object);
2015-04-16 16:58:33 +02:00
2016-11-10 15:42:14 +01:00
//g_signal_connect (node,
// "remove",
// (GCallback) handle_remove_node,
// client);
2016-07-18 17:40:58 +02:00
object_path = pinos_node_get_object_path (node);
2016-11-10 15:42:14 +01:00
pinos_log_debug ("daemon %p: added node %p with path %s", impl, node, object_path);
g_dbus_method_invocation_return_value (invocation,
g_variant_new ("(o)", object_path));
g_object_unref (node);
2015-04-16 16:58:33 +02:00
return TRUE;
/* ERRORS */
no_factory:
{
2016-11-10 15:42:14 +01:00
pinos_log_debug ("daemon %p: could find factory named %s", impl, arg_factory_name);
g_dbus_method_invocation_return_dbus_error (invocation,
"org.pinos.Error", "can't find factory");
return TRUE;
}
no_node:
{
2016-11-10 15:42:14 +01:00
pinos_log_debug ("daemon %p: could create node named %s from factory %s", impl, arg_name, arg_factory_name);
g_dbus_method_invocation_return_dbus_error (invocation,
"org.pinos.Error", "can't create node");
return TRUE;
}
2015-04-16 16:58:33 +02:00
}
static void
2016-11-10 15:42:14 +01:00
on_link_port_unlinked (PinosListener *listener,
void *object,
void *data)
{
2016-11-10 15:42:14 +01:00
PinosLink *link = object;
PinosPort *port = data;
PinosDaemonImpl *impl = SPA_CONTAINER_OF (listener, PinosDaemonImpl, port_unlinked);
pinos_log_debug ("daemon %p: link %p: port %p unlinked", impl, link, port);
if (port->direction == PINOS_DIRECTION_OUTPUT && link->input)
2016-11-10 15:42:14 +01:00
try_link_port (link->input->node, link->input, &impl->daemon);
}
static void
2016-11-10 15:42:14 +01:00
on_link_notify_state (PinosListener *listener,
void *object,
void *data)
{
2016-11-10 15:42:14 +01:00
PinosLink *link = object;
PinosDaemonImpl *impl = SPA_CONTAINER_OF (listener, PinosDaemonImpl, notify_state);
GError *error = NULL;
PinosLinkState state;
state = pinos_link_get_state (link, &error);
switch (state) {
case PINOS_LINK_STATE_ERROR:
{
2016-11-10 15:42:14 +01:00
pinos_log_debug ("daemon %p: link %p: state error: %s", impl, link, error->message);
if (link->input && link->input->node)
pinos_node_report_error (link->input->node, g_error_copy (error));
if (link->output && link->output->node)
pinos_node_report_error (link->output->node, g_error_copy (error));
break;
}
case PINOS_LINK_STATE_UNLINKED:
2016-11-10 15:42:14 +01:00
pinos_log_debug ("daemon %p: link %p: unlinked", impl, link);
#if 0
g_set_error (&error,
PINOS_ERROR,
PINOS_ERROR_NODE_LINK,
"error node unlinked");
if (link->input && link->input->node)
pinos_node_report_error (link->input->node, g_error_copy (error));
if (link->output && link->output->node)
pinos_node_report_error (link->output->node, g_error_copy (error));
#endif
break;
case PINOS_LINK_STATE_INIT:
case PINOS_LINK_STATE_NEGOTIATING:
case PINOS_LINK_STATE_ALLOCATING:
case PINOS_LINK_STATE_PAUSED:
case PINOS_LINK_STATE_RUNNING:
break;
}
}
static void
try_link_port (PinosNode *node, PinosPort *port, PinosDaemon *this)
{
2016-11-10 15:42:14 +01:00
PinosDaemonImpl *impl = (PinosDaemonImpl *) this;
PinosClient *client;
2016-08-09 14:49:27 +02:00
PinosProperties *props;
const gchar *path;
GError *error = NULL;
PinosLink *link;
2016-11-10 15:42:14 +01:00
props = node->properties;
2016-09-24 18:32:46 +02:00
if (props == NULL)
return;
2016-08-09 14:49:27 +02:00
path = pinos_properties_get (props, "pinos.target.node");
if (path) {
PinosPort *target;
target = pinos_daemon_find_port (this,
port,
2016-08-09 14:49:27 +02:00
path,
NULL,
NULL,
&error);
2016-09-19 09:16:58 +02:00
if (target == NULL)
goto error;
if (port->direction == PINOS_DIRECTION_OUTPUT)
link = pinos_port_link (port, target, NULL, NULL, &error);
else
link = pinos_port_link (target, port, NULL, NULL, &error);
2016-09-19 09:16:58 +02:00
if (link == NULL)
goto error;
client = pinos_node_get_client (node);
if (client)
2016-11-10 15:42:14 +01:00
pinos_client_add_object (client, &link->object);
impl->port_unlinked.notify = on_link_port_unlinked;
pinos_signal_add (&link->port_unlinked, &impl->port_unlinked);
impl->notify_state.notify = on_link_notify_state;
pinos_signal_add (&link->notify_state, &impl->notify_state);
pinos_link_activate (link);
g_object_unref (link);
2016-08-09 14:49:27 +02:00
}
2016-09-19 09:16:58 +02:00
return;
error:
{
pinos_node_report_error (node, error);
return;
}
}
static void
on_port_added (PinosNode *node, PinosPort *port, PinosDaemon *this)
{
try_link_port (node, port, this);
}
static void
on_port_removed (PinosNode *node, PinosPort *port, PinosDaemon *this)
{
}
static void
on_node_created (PinosNode *node,
PinosDaemon *this)
{
GList *ports, *walk;
ports = pinos_node_get_ports (node, PINOS_DIRECTION_INPUT);
for (walk = ports; walk; walk = g_list_next (walk))
on_port_added (node, walk->data, this);
ports = pinos_node_get_ports (node, PINOS_DIRECTION_OUTPUT);
for (walk = ports; walk; walk = g_list_next (walk))
on_port_added (node, walk->data, this);
g_signal_connect (node, "port-added", (GCallback) on_port_added, this);
g_signal_connect (node, "port-removed", (GCallback) on_port_removed, this);
}
static void
on_node_state_change (PinosNode *node,
PinosNodeState old,
PinosNodeState state,
PinosDaemon *this)
{
pinos_log_debug ("daemon %p: node %p state change %s -> %s", this, node,
pinos_node_state_as_string (old),
pinos_node_state_as_string (state));
if (old == PINOS_NODE_STATE_CREATING && state == PINOS_NODE_STATE_SUSPENDED)
on_node_created (node, this);
}
static void
on_node_added (PinosDaemon *daemon, PinosNode *node)
{
2016-11-10 15:42:14 +01:00
PinosDaemonImpl *impl = (PinosDaemonImpl *) daemon;
2016-11-10 15:42:14 +01:00
pinos_log_debug ("daemon %p: node %p added", impl, node);
2016-11-10 15:42:14 +01:00
g_object_set (node, "data-loop", impl->data_loop, NULL);
2016-11-10 15:42:14 +01:00
g_signal_connect (node, "state-change", (GCallback) on_node_state_change, impl);
2016-11-10 15:42:14 +01:00
if (node->state > PINOS_NODE_STATE_CREATING) {
on_node_created (node, daemon);
}
}
static void
on_node_removed (PinosDaemon *daemon, PinosNode *node)
{
pinos_log_debug ("daemon %p: node %p removed", daemon, node);
g_signal_handlers_disconnect_by_data (node, daemon);
}
static gboolean
handle_create_client_node (PinosDaemon1 *interface,
GDBusMethodInvocation *invocation,
const gchar *arg_name,
GVariant *arg_properties,
gpointer user_data)
{
2016-11-10 15:42:14 +01:00
PinosDaemonImpl *impl = user_data;
PinosDaemon *this = &impl->daemon;
PinosClientNode *node;
PinosClient *client;
const gchar *sender, *object_path;
PinosProperties *props;
GError *error = NULL;
GUnixFDList *fdlist;
2016-11-10 15:42:14 +01:00
int socket, rtsocket;
gint fdidx, rtfdidx;
sender = g_dbus_method_invocation_get_sender (invocation);
2016-11-10 15:42:14 +01:00
client = sender_get_client (&impl->daemon, sender, TRUE);
2016-11-10 15:42:14 +01:00
pinos_log_debug ("daemon %p: create client-node: %s", impl, sender);
props = pinos_properties_from_variant (arg_properties);
2016-11-10 15:42:14 +01:00
node = pinos_client_node_new (this->core,
client,
arg_name,
props);
pinos_properties_free (props);
2016-11-10 15:42:14 +01:00
socket = pinos_client_node_get_socket_pair (node, &error);
if (socket == -1)
goto no_socket;
2016-11-10 15:42:14 +01:00
rtsocket = pinos_client_node_get_rtsocket_pair (node, &error);
if (rtsocket == -1)
goto no_socket;
2016-11-10 15:42:14 +01:00
//pinos_client_add_object (client, &node->object);
2016-11-10 15:42:14 +01:00
object_path = pinos_node_get_object_path (node->node);
pinos_log_debug ("daemon %p: add client-node %p, %s", impl, node, object_path);
fdlist = g_unix_fd_list_new ();
2016-11-10 15:42:14 +01:00
fdidx = g_unix_fd_list_append (fdlist, socket, &error);
rtfdidx = g_unix_fd_list_append (fdlist, rtsocket, &error);
g_dbus_method_invocation_return_value_with_unix_fd_list (invocation,
g_variant_new ("(ohh)", object_path, fdidx, rtfdidx), fdlist);
g_object_unref (fdlist);
return TRUE;
no_socket:
{
2016-11-10 15:42:14 +01:00
pinos_log_debug ("daemon %p: could not create socket %s", impl, error->message);
g_object_unref (node);
goto exit_error;
}
exit_error:
{
g_dbus_method_invocation_return_gerror (invocation, error);
g_clear_error (&error);
return TRUE;
}
}
2015-04-16 16:58:33 +02:00
static void
export_server_object (PinosDaemon *daemon,
GDBusObjectManagerServer *manager)
2015-04-16 16:58:33 +02:00
{
2016-11-10 15:42:14 +01:00
PinosDaemonImpl *impl = (PinosDaemonImpl *) daemon;
PinosObjectSkeleton *skel;
2015-04-16 16:58:33 +02:00
skel = pinos_object_skeleton_new (PINOS_DBUS_OBJECT_SERVER);
2016-11-10 15:42:14 +01:00
pinos_object_skeleton_set_daemon1 (skel, impl->iface);
g_dbus_object_manager_server_export (manager, G_DBUS_OBJECT_SKELETON (skel));
2016-11-10 15:42:14 +01:00
g_free (impl->object_path);
impl->object_path = g_strdup (g_dbus_object_get_object_path (G_DBUS_OBJECT (skel)));
2015-04-16 16:58:33 +02:00
g_object_unref (skel);
}
static void
bus_acquired_handler (GDBusConnection *connection,
const gchar *name,
gpointer user_data)
2015-04-16 16:58:33 +02:00
{
2016-11-10 15:42:14 +01:00
PinosDaemonImpl *impl = user_data;
GDBusObjectManagerServer *manager = impl->server_manager;
2015-04-16 16:58:33 +02:00
2016-11-10 15:42:14 +01:00
impl->connection = connection;
2016-11-10 15:42:14 +01:00
export_server_object (&impl->daemon, manager);
g_dbus_object_manager_server_set_connection (manager, connection);
2015-04-16 16:58:33 +02:00
}
static void
name_acquired_handler (GDBusConnection *connection,
const gchar *name,
gpointer user_data)
2015-04-16 16:58:33 +02:00
{
}
static void
name_lost_handler (GDBusConnection *connection,
const gchar *name,
gpointer user_data)
2015-04-16 16:58:33 +02:00
{
2016-11-10 15:42:14 +01:00
PinosDaemonImpl *impl = user_data;
GDBusObjectManagerServer *manager = impl->server_manager;
2015-04-16 16:58:33 +02:00
g_dbus_object_manager_server_unexport (manager, PINOS_DBUS_OBJECT_SERVER);
g_dbus_object_manager_server_set_connection (manager, connection);
2016-11-10 15:42:14 +01:00
g_clear_pointer (&impl->object_path, g_free);
impl->connection = connection;
2015-04-16 16:58:33 +02:00
}
const gchar *
pinos_daemon_get_object_path (PinosDaemon *daemon)
{
2016-11-10 15:42:14 +01:00
PinosDaemonImpl *impl = (PinosDaemonImpl *) daemon;
2016-11-10 15:42:14 +01:00
g_return_val_if_fail (impl, NULL);
2016-11-10 15:42:14 +01:00
return impl->object_path;
}
2016-11-10 15:42:14 +01:00
static SpaResult
daemon_start (PinosDaemon *daemon)
2015-04-16 16:58:33 +02:00
{
2016-11-10 15:42:14 +01:00
PinosDaemonImpl *impl = SPA_CONTAINER_OF (daemon, PinosDaemonImpl, daemon);
2015-04-16 16:58:33 +02:00
2016-11-10 15:42:14 +01:00
g_return_val_if_fail (impl, SPA_RESULT_INVALID_ARGUMENTS);
g_return_val_if_fail (impl->id == 0, SPA_RESULT_INVALID_ARGUMENTS);
2015-04-16 16:58:33 +02:00
pinos_log_debug ("daemon %p: start", daemon);
2016-04-11 15:26:15 +02:00
2016-11-10 15:42:14 +01:00
impl->id = g_bus_own_name (G_BUS_TYPE_SESSION,
PINOS_DBUS_SERVICE,
2015-04-16 16:58:33 +02:00
G_BUS_NAME_OWNER_FLAGS_REPLACE,
bus_acquired_handler,
name_acquired_handler,
name_lost_handler,
daemon,
NULL);
2016-11-10 15:42:14 +01:00
return SPA_RESULT_OK;
2015-04-16 16:58:33 +02:00
}
2016-11-10 15:42:14 +01:00
static SpaResult
daemon_stop (PinosDaemon *daemon)
2015-04-16 16:58:33 +02:00
{
2016-11-10 15:42:14 +01:00
PinosDaemonImpl *impl = SPA_CONTAINER_OF (daemon, PinosDaemonImpl, daemon);
2015-04-16 16:58:33 +02:00
2016-11-10 15:42:14 +01:00
g_return_val_if_fail (impl, SPA_RESULT_INVALID_ARGUMENTS);
2015-04-16 16:58:33 +02:00
pinos_log_debug ("daemon %p: stop", daemon);
2016-04-11 15:26:15 +02:00
2016-11-10 15:42:14 +01:00
if (impl->id != 0) {
g_bus_unown_name (impl->id);
impl->id = 0;
}
2016-11-10 15:42:14 +01:00
return SPA_RESULT_OK;
2015-04-16 16:58:33 +02:00
}
/**
* pinos_daemon_export_uniquely:
* @daemon: a #PinosDaemon
* @skel: a #GDBusObjectSkeleton
*
* Export @skel with @daemon with a unique name
*
* Returns: the unique named used to export @skel.
*/
2015-04-16 16:58:33 +02:00
gchar *
pinos_daemon_export_uniquely (PinosDaemon *daemon,
GDBusObjectSkeleton *skel)
2015-04-16 16:58:33 +02:00
{
2016-11-10 15:42:14 +01:00
PinosDaemonImpl *impl = (PinosDaemonImpl *) daemon;
g_return_val_if_fail (impl, NULL);
2015-04-16 16:58:33 +02:00
g_return_val_if_fail (G_IS_DBUS_OBJECT_SKELETON (skel), NULL);
2016-11-10 15:42:14 +01:00
g_dbus_object_manager_server_export_uniquely (impl->server_manager, skel);
2015-04-16 16:58:33 +02:00
return g_strdup (g_dbus_object_get_object_path (G_DBUS_OBJECT (skel)));
}
/**
* pinos_daemon_unexport:
* @daemon: a #PinosDaemon
* @object_path: an object path
*
* Unexport the object on @object_path
*/
2015-04-16 16:58:33 +02:00
void
pinos_daemon_unexport (PinosDaemon *daemon,
const gchar *object_path)
2015-04-16 16:58:33 +02:00
{
2016-11-10 15:42:14 +01:00
PinosDaemonImpl *impl = (PinosDaemonImpl *) daemon;
g_return_if_fail (impl);
2015-04-16 16:58:33 +02:00
g_return_if_fail (g_variant_is_object_path (object_path));
2016-11-10 15:42:14 +01:00
g_dbus_object_manager_server_unexport (impl->server_manager, object_path);
2015-04-16 16:58:33 +02:00
}
static void
on_registry_object_added (PinosListener *listener,
2016-11-10 15:42:14 +01:00
void *object,
void *data)
{
2016-11-10 15:42:14 +01:00
PinosObject *obj = data;
PinosDaemonImpl *impl = SPA_CONTAINER_OF (listener, PinosDaemonImpl, object_added);
PinosDaemon *this = &impl->daemon;
2016-11-10 15:42:14 +01:00
if (obj->type == this->core->registry.uri.node) {
PinosNode *node = obj->implementation;
2016-11-10 15:42:14 +01:00
on_node_added (this, node);
} else if (obj->type == this->core->registry.uri.node_factory) {
PinosNodeFactory *factory = obj->implementation;
gchar *name;
g_object_get (factory, "name", &name, NULL);
2016-11-10 15:42:14 +01:00
g_hash_table_insert (impl->node_factories, name, g_object_ref (factory));
}
}
static void
on_registry_object_removed (PinosListener *listener,
2016-11-10 15:42:14 +01:00
void *object,
void *data)
{
2016-11-10 15:42:14 +01:00
PinosObject *obj = data;
PinosDaemonImpl *impl = SPA_CONTAINER_OF (listener, PinosDaemonImpl, object_added);
PinosDaemon *this = &impl->daemon;
2016-11-10 15:42:14 +01:00
if (obj->type == this->core->registry.uri.node) {
PinosNode *node = obj->implementation;
2016-11-10 15:42:14 +01:00
on_node_removed (this, node);
} else if (obj->type == this->core->registry.uri.node_factory) {
PinosNodeFactory *factory = obj->implementation;
gchar *name;
g_object_get (factory, "name", &name, NULL);
2016-11-10 15:42:14 +01:00
g_hash_table_remove (impl->node_factories, name);
g_free (name);
}
}
/**
* pinos_daemon_find_port:
* @daemon: a #PinosDaemon
* @other_port: a port to be compatible with
* @name: a port name
* @props: port properties
* @format_filter: a format filter
* @error: location for an error
*
* Find the best port in @daemon that matches the given parameters.
*
* Returns: a #PinosPort or %NULL when no port could be found.
*/
PinosPort *
pinos_daemon_find_port (PinosDaemon *daemon,
PinosPort *other_port,
const gchar *name,
PinosProperties *props,
GPtrArray *format_filters,
GError **error)
{
PinosPort *best = NULL;
gboolean have_name;
2016-11-10 15:42:14 +01:00
void *state = NULL;
PinosObject *o;
2016-11-10 15:42:14 +01:00
g_return_val_if_fail (daemon, NULL);
have_name = name ? strlen (name) > 0 : FALSE;
pinos_log_debug ("name \"%s\", %d", name, have_name);
2016-11-10 15:42:14 +01:00
while ((o = pinos_registry_iterate_nodes (&daemon->core->registry, &state))) {
PinosNode *n = o->implementation;
if (o->flags & PINOS_OBJECT_FLAG_DESTROYING)
continue;
pinos_log_debug ("node path \"%s\"", pinos_node_get_object_path (n));
if (have_name) {
if (g_str_has_suffix (pinos_node_get_object_path (n), name)) {
pinos_log_debug ("name \"%s\" matches node %p", name, n);
best = pinos_node_get_free_port (n, pinos_direction_reverse (other_port->direction));
if (best)
break;
}
} else {
}
}
if (best == NULL) {
g_set_error (error,
G_IO_ERROR,
G_IO_ERROR_NOT_FOUND,
"No matching Node found");
}
return best;
}
2015-06-02 18:00:57 +02:00
static void
2016-11-10 15:42:14 +01:00
daemon_destroy (PinosObject *object)
2015-06-02 18:00:57 +02:00
{
2016-11-10 15:42:14 +01:00
PinosDaemonImpl *impl = SPA_CONTAINER_OF (object, PinosDaemonImpl, object);
PinosDaemon *this = &impl->daemon;
2015-06-02 18:00:57 +02:00
2016-11-10 15:42:14 +01:00
pinos_log_debug ("daemon %p: destroy", impl);
2016-04-11 15:26:15 +02:00
pinos_daemon_stop (this);
2016-11-10 15:42:14 +01:00
pinos_signal_remove (&impl->object_added);
pinos_signal_remove (&impl->object_removed);
2015-06-02 18:00:57 +02:00
2016-11-10 15:42:14 +01:00
g_clear_object (&impl->server_manager);
g_clear_object (&impl->iface);
g_hash_table_unref (impl->clients);
g_hash_table_unref (impl->node_factories);
2016-11-09 12:57:51 +01:00
2016-11-10 15:42:14 +01:00
pinos_registry_remove_object (&this->core->registry, &impl->object);
free (impl);
2015-04-16 16:58:33 +02:00
}
2016-11-10 15:42:14 +01:00
void
pinos_daemon_destroy (PinosDaemon *daemon)
2015-04-16 16:58:33 +02:00
{
2016-11-10 15:42:14 +01:00
PinosDaemonImpl *impl = SPA_CONTAINER_OF (daemon, PinosDaemonImpl, daemon);
2016-11-10 15:42:14 +01:00
pinos_object_destroy (&impl->object);
2015-04-16 16:58:33 +02:00
}
2016-11-10 15:42:14 +01:00
/**
* pinos_daemon_new:
* @core: #PinosCore
* @properties: #PinosProperties
*
* Make a new #PinosDaemon object with given @properties
*
* Returns: a new #PinosDaemon
*/
PinosDaemon *
pinos_daemon_new (PinosCore *core,
PinosProperties *properties)
2015-04-16 16:58:33 +02:00
{
2016-11-10 15:42:14 +01:00
PinosDaemonImpl *impl;
PinosDaemon *this;
impl = calloc (1, sizeof (PinosDaemonImpl));
this = &impl->daemon;
pinos_log_debug ("daemon %p: new", impl);
2015-04-16 16:58:33 +02:00
2016-11-10 15:42:14 +01:00
this->core = core;
this->properties = properties;
2016-11-10 15:42:14 +01:00
this->start = daemon_start;
this->stop = daemon_stop;
2016-11-10 15:42:14 +01:00
pinos_object_init (&impl->object,
core->registry.uri.daemon,
impl,
daemon_destroy);
2016-11-09 12:57:51 +01:00
2016-11-10 15:42:14 +01:00
impl->object_added.notify = on_registry_object_added;
pinos_signal_add (&core->registry.object_added, &impl->object_added);
2016-11-09 12:57:51 +01:00
2016-11-10 15:42:14 +01:00
impl->object_removed.notify = on_registry_object_removed;
pinos_signal_add (&core->registry.object_removed, &impl->object_removed);
2016-11-10 15:42:14 +01:00
impl->server_manager = g_dbus_object_manager_server_new (PINOS_DBUS_OBJECT_PREFIX);
impl->clients = g_hash_table_new (g_str_hash, g_str_equal);
impl->node_factories = g_hash_table_new_full (g_str_hash,
g_str_equal,
g_free,
g_object_unref);
2016-11-10 15:42:14 +01:00
impl->iface = pinos_daemon1_skeleton_new ();
g_signal_connect (impl->iface, "handle-create-node", (GCallback) handle_create_node, daemon);
g_signal_connect (impl->iface, "handle-create-client-node", (GCallback) handle_create_client_node, daemon);
pinos_daemon1_set_user_name (impl->iface, g_get_user_name ());
pinos_daemon1_set_host_name (impl->iface, g_get_host_name ());
pinos_daemon1_set_version (impl->iface, PACKAGE_VERSION);
pinos_daemon1_set_name (impl->iface, PACKAGE_NAME);
pinos_daemon1_set_cookie (impl->iface, g_random_int());
pinos_daemon1_set_properties (impl->iface, pinos_properties_to_variant (this->properties));
pinos_registry_add_object (&core->registry, &impl->object);
return this;
}