some more rework

This commit is contained in:
Wim Taymans 2016-07-18 17:40:58 +02:00
parent ca4f3d84cd
commit eefe6aacb9
24 changed files with 849 additions and 2829 deletions

View file

@ -1,408 +0,0 @@
/* Pinos
* 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 <gio/gio.h>
#include <gio/gunixfdlist.h>
#include "pinos/client/pinos.h"
#include "pinos/client/subscribe.h"
#include "pinos/client/enumtypes.h"
#include "pinos/client/context.h"
#include "pinos/client/private.h"
#include "pinos/client/client-node.h"
#include "pinos/client/client-port.h"
#define PINOS_CLIENT_NODE_GET_PRIVATE(node) \
(G_TYPE_INSTANCE_GET_PRIVATE ((node), PINOS_TYPE_CLIENT_NODE, PinosClientNodePrivate))
struct _PinosClientNodePrivate
{
PinosContext *context;
GDBusProxy *proxy;
};
G_DEFINE_TYPE (PinosClientNode, pinos_client_node, PINOS_TYPE_NODE);
enum
{
PROP_0,
PROP_CONTEXT,
PROP_PROXY,
};
static gboolean
client_node_set_state (PinosNode *node,
PinosNodeState state)
{
return FALSE;
}
typedef struct {
PinosDirection direction;
gchar *name;
PinosProperties *properties;
GBytes *possible_formats;
GSocket *socket;
} CreatePortData;
static void
create_port_data_free (CreatePortData *data)
{
g_free (data->name);
if (data->properties)
pinos_properties_free (data->properties);
g_clear_object (&data->socket);
g_slice_free (CreatePortData, data);
}
static void
on_port_proxy (GObject *source_object,
GAsyncResult *res,
gpointer user_data)
{
GTask *task = user_data;
CreatePortData *data = g_task_get_task_data (task);
PinosClientNode *node = g_task_get_source_object (task);
PinosClientNodePrivate *priv = node->priv;
PinosContext *context = priv->context;
GError *error = NULL;
GDBusProxy *proxy;
PinosClientPort *port;
proxy = pinos_subscribe_get_proxy_finish (context->priv->subscribe,
res,
&error);
if (proxy == NULL)
goto port_failed;
port = pinos_client_port_new (node,
proxy,
data->socket);
g_task_return_pointer (task, port, (GDestroyNotify) g_object_unref);
g_object_unref (task);
return;
port_failed:
{
g_warning ("failed to get port proxy: %s", error->message);
g_task_return_error (task, error);
g_object_unref (task);
return;
}
}
static void
on_port_created (GObject *source_object,
GAsyncResult *res,
gpointer user_data)
{
GTask *task = user_data;
PinosClientNode *node = g_task_get_source_object (task);
CreatePortData *data = g_task_get_task_data (task);
PinosClientNodePrivate *priv = node->priv;
PinosContext *context = priv->context;
GVariant *ret;
GError *error = NULL;
const gchar *port_path;
GUnixFDList *fdlist;
gint fd, fd_idx;
g_assert (priv->proxy == G_DBUS_PROXY (source_object));
ret = g_dbus_proxy_call_with_unix_fd_list_finish (priv->proxy, &fdlist, res, &error);
if (ret == NULL)
goto create_failed;
g_variant_get (ret, "(&oh)", &port_path, &fd_idx);
fd = g_unix_fd_list_get (fdlist, fd_idx, &error);
g_object_unref (fdlist);
if (fd == -1)
goto create_failed;
data->socket = g_socket_new_from_fd (fd, &error);
if (data->socket == NULL)
goto create_failed;
pinos_subscribe_get_proxy (context->priv->subscribe,
PINOS_DBUS_SERVICE,
port_path,
"org.pinos.Port1",
NULL,
on_port_proxy,
task);
g_variant_unref (ret);
return;
/* ERRORS */
create_failed:
{
g_warning ("failed to create port: %s", error->message);
g_task_return_error (task, error);
g_object_unref (task);
if (ret)
g_variant_unref (ret);
return;
}
}
static gboolean
do_create_port (GTask *task)
{
PinosClientNode *node = g_task_get_source_object (task);
PinosClientNodePrivate *priv = node->priv;
CreatePortData *data = g_task_get_task_data (task);
g_dbus_proxy_call (priv->proxy,
"CreatePort",
g_variant_new ("(us@a{sv}s)",
data->direction,
data->name,
pinos_properties_to_variant (data->properties),
g_bytes_get_data (data->possible_formats, NULL)),
G_DBUS_CALL_FLAGS_NONE,
-1,
NULL, /* GCancellable *cancellable */
on_port_created,
task);
return FALSE;
}
static void
client_node_create_port (PinosNode *node,
PinosDirection direction,
const gchar *name,
GBytes *possible_formats,
PinosProperties *properties,
GTask *task)
{
PinosClientNodePrivate *priv = PINOS_CLIENT_NODE (node)->priv;
PinosContext *context = priv->context;
CreatePortData *data;
data = g_slice_new (CreatePortData);
data->direction = direction;
data->name = g_strdup (name);
data->possible_formats = possible_formats ? g_bytes_ref (possible_formats) : NULL;
data->properties = pinos_properties_merge (pinos_node_get_properties (node), properties);
data->socket = NULL;
g_task_set_task_data (task, data, (GDestroyNotify) create_port_data_free);
g_main_context_invoke (context->priv->context,
(GSourceFunc) do_create_port,
task);
}
static void
client_node_remove_port (PinosNode *node,
PinosPort *port)
{
}
static void
pinos_client_node_get_property (GObject *_object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
PinosClientNode *node = PINOS_CLIENT_NODE (_object);
PinosClientNodePrivate *priv = node->priv;
switch (prop_id) {
case PROP_CONTEXT:
g_value_set_object (value, priv->context);
break;
case PROP_PROXY:
g_value_set_object (value, priv->proxy);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (node, prop_id, pspec);
break;
}
}
static void
pinos_client_node_set_property (GObject *_object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
PinosClientNode *node = PINOS_CLIENT_NODE (_object);
PinosClientNodePrivate *priv = node->priv;
switch (prop_id) {
case PROP_CONTEXT:
priv->context = g_value_dup_object (value);
break;
case PROP_PROXY:
priv->proxy = g_value_dup_object (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (node, prop_id, pspec);
break;
}
}
static void
pinos_client_node_constructed (GObject * obj)
{
PinosClientNode *node = PINOS_CLIENT_NODE (obj);
g_debug ("client-node %p: constructed", node);
G_OBJECT_CLASS (pinos_client_node_parent_class)->constructed (obj);
}
static void
pinos_client_node_dispose (GObject * obj)
{
PinosClientNode *node = PINOS_CLIENT_NODE (obj);
g_debug ("client-node %p: dispose", node);
G_OBJECT_CLASS (pinos_client_node_parent_class)->dispose (obj);
}
static void
pinos_client_node_finalize (GObject * obj)
{
PinosClientNode *node = PINOS_CLIENT_NODE (obj);
PinosClientNodePrivate *priv = node->priv;
g_debug ("client-node %p: finalize", node);
g_clear_object (&priv->context);
g_clear_object (&priv->proxy);
G_OBJECT_CLASS (pinos_client_node_parent_class)->finalize (obj);
}
static void
pinos_client_node_class_init (PinosClientNodeClass * klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
PinosNodeClass *node_class = PINOS_NODE_CLASS (klass);
g_type_class_add_private (klass, sizeof (PinosClientNodePrivate));
gobject_class->constructed = pinos_client_node_constructed;
gobject_class->dispose = pinos_client_node_dispose;
gobject_class->finalize = pinos_client_node_finalize;
gobject_class->set_property = pinos_client_node_set_property;
gobject_class->get_property = pinos_client_node_get_property;
g_object_class_install_property (gobject_class,
PROP_CONTEXT,
g_param_spec_object ("context",
"Context",
"The Context",
PINOS_TYPE_CONTEXT,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class,
PROP_PROXY,
g_param_spec_object ("proxy",
"Proxy",
"The Proxy",
G_TYPE_DBUS_PROXY,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS));
node_class->set_state = client_node_set_state;
node_class->create_port = client_node_create_port;
node_class->remove_port = client_node_remove_port;
}
static void
pinos_client_node_init (PinosClientNode * node)
{
node->priv = PINOS_CLIENT_NODE_GET_PRIVATE (node);
g_debug ("client-node %p: new", node);
}
/**
* pinos_client_node_get_context:
* @node: a #PinosClientNode
*
* Get the context of @node.
*
* Returns: the context of @node.
*/
PinosContext *
pinos_client_node_get_context (PinosClientNode *node)
{
PinosClientNodePrivate *priv;
g_return_val_if_fail (PINOS_IS_CLIENT_NODE (node), NULL);
priv = node->priv;
return priv->context;
}
/**
* pinos_client_port_new:
* @node: a #PinosClientNode
* @id: an id
* @socket: a socket with the server port
*
* Create a new client port.
*
* Returns: a new client port
*/
PinosClientNode *
pinos_client_node_new (PinosContext *context,
gpointer id)
{
PinosClientNode *node;
GDBusProxy *proxy = id;
GVariant *variant;
PinosProperties *properties = NULL;
const gchar *name = NULL;
variant = g_dbus_proxy_get_cached_property (G_DBUS_PROXY (proxy), "Name");
if (variant != NULL) {
name = g_variant_get_string (variant, NULL);
g_variant_unref (variant);
}
variant = g_dbus_proxy_get_cached_property (G_DBUS_PROXY (proxy), "Properties");
if (variant != NULL) {
properties = pinos_properties_from_variant (variant);
g_variant_unref (variant);
}
node = g_object_new (PINOS_TYPE_CLIENT_NODE,
"context", context,
"proxy", proxy,
"name", name,
"properties", properties,
NULL);
return node;
}

View file

@ -1,74 +0,0 @@
/* Pinos
* 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.
*/
#ifndef __PINOS_CLIENT_NODE_H__
#define __PINOS_CLIENT_NODE_H__
#include <glib-object.h>
G_BEGIN_DECLS
typedef struct _PinosClientNode PinosClientNode;
typedef struct _PinosClientNodeClass PinosClientNodeClass;
typedef struct _PinosClientNodePrivate PinosClientNodePrivate;
#include <pinos/client/introspect.h>
#include <pinos/client/node.h>
#define PINOS_TYPE_CLIENT_NODE (pinos_client_node_get_type ())
#define PINOS_IS_CLIENT_NODE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), PINOS_TYPE_CLIENT_NODE))
#define PINOS_IS_CLIENT_NODE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), PINOS_TYPE_CLIENT_NODE))
#define PINOS_CLIENT_NODE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), PINOS_TYPE_CLIENT_NODE, PinosClientNodeClass))
#define PINOS_CLIENT_NODE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), PINOS_TYPE_CLIENT_NODE, PinosClientNode))
#define PINOS_CLIENT_NODE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), PINOS_TYPE_CLIENT_NODE, PinosClientNodeClass))
#define PINOS_CLIENT_NODE_CAST(obj) ((PinosClientNode*)(obj))
#define PINOS_CLIENT_NODE_CLASS_CAST(klass) ((PinosClientNodeClass*)(klass))
/**
* PinosClientNode:
*
* Pinos client node class.
*/
struct _PinosClientNode {
PinosNodeClass object;
PinosClientNodePrivate *priv;
};
/**
* PinosClientNodeClass:
* @set_state: called to change the current state of the client node
*
* Pinos client node class.
*/
struct _PinosClientNodeClass {
PinosNodeClass parent_class;
};
/* normal GObject stuff */
GType pinos_client_node_get_type (void);
PinosClientNode * pinos_client_node_new (PinosContext *context,
gpointer id);
PinosContext * pinos_client_node_get_context (PinosClientNode *node);
G_END_DECLS
#endif /* __PINOS_CLIENT_NODE_H__ */

View file

@ -22,7 +22,6 @@
#include "pinos/client/context.h"
#include "pinos/client/enumtypes.h"
#include "pinos/client/subscribe.h"
#include "pinos/client/client-node.h"
#include "pinos/client/private.h"
@ -619,177 +618,3 @@ pinos_context_get_error (PinosContext *context)
return priv->error;
}
typedef struct {
gchar *factory_name;
gchar *name;
PinosProperties *properties;
} CreateNodeData;
static void
create_node_data_free (CreateNodeData *data)
{
g_free (data->factory_name);
g_free (data->name);
if (data->properties)
pinos_properties_free (data->properties);
g_slice_free (CreateNodeData, data);
}
static void
on_node_proxy (GObject *source_object,
GAsyncResult *res,
gpointer user_data)
{
GTask *task = user_data;
PinosContext *context = g_task_get_source_object (task);
GError *error = NULL;
GDBusProxy *proxy;
PinosClientNode *node;
proxy = pinos_subscribe_get_proxy_finish (context->priv->subscribe,
res,
&error);
if (proxy == NULL)
goto node_failed;
node = pinos_client_node_new (context, proxy);
g_task_return_pointer (task, node, (GDestroyNotify) g_object_unref);
g_object_unref (task);
return;
node_failed:
{
g_warning ("failed to get node proxy: %s", error->message);
g_task_return_error (task, error);
g_object_unref (task);
return;
}
}
static void
on_node_created (GObject *source_object,
GAsyncResult *res,
gpointer user_data)
{
GTask *task = user_data;
PinosContext *context = g_task_get_source_object (task);
GVariant *ret;
GError *error = NULL;
const gchar *node_path;
g_assert (context->priv->daemon == G_DBUS_PROXY (source_object));
ret = g_dbus_proxy_call_finish (context->priv->daemon, res, &error);
if (ret == NULL)
goto create_failed;
g_variant_get (ret, "(&o)", &node_path);
pinos_subscribe_get_proxy (context->priv->subscribe,
PINOS_DBUS_SERVICE,
node_path,
"org.pinos.Node1",
NULL,
on_node_proxy,
task);
g_variant_unref (ret);
return;
/* ERRORS */
create_failed:
{
g_warning ("failed to create node: %s", error->message);
g_task_return_error (task, error);
g_object_unref (task);
return;
}
}
static gboolean
do_create_node (GTask *task)
{
PinosContext *context = g_task_get_source_object (task);
CreateNodeData *data = g_task_get_task_data (task);
g_dbus_proxy_call (context->priv->daemon,
"CreateNode",
g_variant_new ("(ss@a{sv})",
data->factory_name,
data->name,
pinos_properties_to_variant (data->properties)),
G_DBUS_CALL_FLAGS_NONE,
-1,
NULL, /* GCancellable *cancellable */
on_node_created,
task);
return FALSE;
}
/**
* pinos_context_create_node:
* @context: a #PinosContext
* @name: the name of the Node
* @properties: properties of the node
* @cancelable: a #GCancellable
* @callback: a #GAsyncReadyCallback
* @user_data: user data.
*
* Asynchronously create a new node in @context.
*/
void
pinos_context_create_node (PinosContext *context,
const gchar *factory_name,
const gchar *name,
PinosProperties *properties,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
PinosContextPrivate *priv;
GTask *task;
CreateNodeData *data;
g_return_if_fail (PINOS_IS_CONTEXT (context));
priv = context->priv;
task = g_task_new (context,
cancellable,
callback,
user_data);
data = g_slice_new (CreateNodeData);
data->factory_name = g_strdup (factory_name);
data->name = g_strdup (name);
data->properties = pinos_properties_merge (priv->properties, properties);
g_task_set_task_data (task, data, (GDestroyNotify) create_node_data_free);
g_main_context_invoke (context->priv->context,
(GSourceFunc) do_create_node,
task);
}
/**
* pinos_context_create_node_finish:
* @context: a #PinosContext
* @res: a #GAsyncResult
* @error: a #GError or %NULL
*
* Get the newly created #PinosNode. This function should be called in the callback
* of pinos_context_create_node() to get the result or error.
*
* Returns: a new #PinosNode. If %NULL is returned, @error will
* be set.
*/
PinosNode *
pinos_context_create_node_finish (PinosContext *context,
GAsyncResult *res,
GError **error)
{
return g_task_propagate_pointer (G_TASK (res), error);
}

View file

@ -31,7 +31,6 @@ typedef struct _PinosContextPrivate PinosContextPrivate;
#include <pinos/client/subscribe.h>
#include <pinos/client/properties.h>
#include <pinos/client/node.h>
#define PINOS_TYPE_CONTEXT (pinos_context_get_type ())
#define PINOS_IS_CONTEXT(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), PINOS_TYPE_CONTEXT))
@ -106,17 +105,6 @@ PinosContext * pinos_context_new (GMainContext *ctx,
gboolean pinos_context_connect (PinosContext *context, PinosContextFlags flags);
gboolean pinos_context_disconnect (PinosContext *context);
void pinos_context_create_node (PinosContext *context,
const gchar *factory_name,
const gchar *name,
PinosProperties *properties,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data);
PinosNode * pinos_context_create_node_finish (PinosContext *context,
GAsyncResult *res,
GError **error);
PinosContextState pinos_context_get_state (PinosContext *context);
const GError * pinos_context_get_error (PinosContext *context);

View file

@ -1,568 +0,0 @@
/* Pinos
* 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 <gio/gio.h>
#include "pinos/client/pinos.h"
#include "pinos/client/enumtypes.h"
#include "pinos/client/node.h"
#define PINOS_NODE_GET_PRIVATE(node) \
(G_TYPE_INSTANCE_GET_PRIVATE ((node), PINOS_TYPE_NODE, PinosNodePrivate))
struct _PinosNodePrivate
{
gchar *name;
PinosNodeState state;
GError *error;
guint idle_timeout;
PinosProperties *properties;
GList *ports;
};
G_DEFINE_ABSTRACT_TYPE (PinosNode, pinos_node, G_TYPE_OBJECT);
enum
{
PROP_0,
PROP_NAME,
PROP_STATE,
PROP_PROPERTIES,
};
enum
{
SIGNAL_REMOVE,
LAST_SIGNAL
};
static guint signals[LAST_SIGNAL] = { 0 };
static void
pinos_node_get_property (GObject *_object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
PinosNode *node = PINOS_NODE (_object);
PinosNodePrivate *priv = node->priv;
switch (prop_id) {
case PROP_NAME:
g_value_set_string (value, priv->name);
break;
case PROP_STATE:
g_value_set_enum (value, priv->state);
break;
case PROP_PROPERTIES:
g_value_set_boxed (value, priv->properties);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (node, prop_id, pspec);
break;
}
}
static void
pinos_node_set_property (GObject *_object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
PinosNode *node = PINOS_NODE (_object);
PinosNodePrivate *priv = node->priv;
switch (prop_id) {
case PROP_NAME:
priv->name = g_value_dup_string (value);
break;
case PROP_PROPERTIES:
if (priv->properties)
pinos_properties_free (priv->properties);
priv->properties = g_value_dup_boxed (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (node, prop_id, pspec);
break;
}
}
static void
pinos_node_constructed (GObject * obj)
{
PinosNode *node = PINOS_NODE (obj);
g_debug ("node %p: constructed", node);
G_OBJECT_CLASS (pinos_node_parent_class)->constructed (obj);
}
static void
pinos_node_dispose (GObject * obj)
{
PinosNode *node = PINOS_NODE (obj);
PinosNodePrivate *priv = node->priv;
g_debug ("node %p: dispose", node);
pinos_node_set_state (node, PINOS_NODE_STATE_SUSPENDED);
g_list_free_full (priv->ports, (GDestroyNotify) g_object_unref);
priv->ports = NULL;
G_OBJECT_CLASS (pinos_node_parent_class)->dispose (obj);
}
static void
pinos_node_finalize (GObject * obj)
{
PinosNode *node = PINOS_NODE (obj);
PinosNodePrivate *priv = node->priv;
g_debug ("node %p: finalize", node);
g_free (priv->name);
g_clear_error (&priv->error);
if (priv->properties)
pinos_properties_free (priv->properties);
G_OBJECT_CLASS (pinos_node_parent_class)->finalize (obj);
}
static void
pinos_node_class_init (PinosNodeClass * klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
g_type_class_add_private (klass, sizeof (PinosNodePrivate));
gobject_class->constructed = pinos_node_constructed;
gobject_class->dispose = pinos_node_dispose;
gobject_class->finalize = pinos_node_finalize;
gobject_class->set_property = pinos_node_set_property;
gobject_class->get_property = pinos_node_get_property;
g_object_class_install_property (gobject_class,
PROP_NAME,
g_param_spec_string ("name",
"Name",
"The node name",
NULL,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class,
PROP_STATE,
g_param_spec_enum ("state",
"State",
"The state of the node",
PINOS_TYPE_NODE_STATE,
PINOS_NODE_STATE_SUSPENDED,
G_PARAM_READABLE |
G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class,
PROP_PROPERTIES,
g_param_spec_boxed ("properties",
"Properties",
"The properties of the node",
PINOS_TYPE_PROPERTIES,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT |
G_PARAM_STATIC_STRINGS));
signals[SIGNAL_REMOVE] = g_signal_new ("remove",
G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_LAST,
0,
NULL,
NULL,
g_cclosure_marshal_generic,
G_TYPE_NONE,
0,
G_TYPE_NONE);
}
static void
pinos_node_init (PinosNode * node)
{
PinosNodePrivate *priv = node->priv = PINOS_NODE_GET_PRIVATE (node);
g_debug ("node %p: new", node);
priv->state = PINOS_NODE_STATE_SUSPENDED;
}
/**
* pinos_node_get_name:
* @node: a #PinosNode
*
* Get the name of @node
*
* Returns: the name of @node
*/
const gchar *
pinos_node_get_name (PinosNode *node)
{
PinosNodePrivate *priv;
g_return_val_if_fail (PINOS_IS_NODE (node), NULL);
priv = node->priv;
return priv->name;
}
/**
* pinos_node_get_state:
* @node: a #PinosNode
*
* Get the state of @node
*
* Returns: the state of @node
*/
PinosNodeState
pinos_node_get_state (PinosNode *node)
{
PinosNodePrivate *priv;
g_return_val_if_fail (PINOS_IS_NODE (node), PINOS_NODE_STATE_ERROR);
priv = node->priv;
return priv->state;
}
/**
* pinos_node_get_properties:
* @node: a #PinosNode
*
* Get the properties of @node
*
* Returns: the properties of @node
*/
PinosProperties *
pinos_node_get_properties (PinosNode *node)
{
PinosNodePrivate *priv;
g_return_val_if_fail (PINOS_IS_NODE (node), NULL);
priv = node->priv;
return priv->properties;
}
/**
* pinos_node_remove:
* @node: a #PinosNode
*
* Remove @node. This will stop the transfer on the node and
* free the resources allocated by @node.
*/
void
pinos_node_remove (PinosNode *node)
{
g_return_if_fail (PINOS_IS_NODE (node));
g_debug ("node %p: remove", node);
g_signal_emit (node, signals[SIGNAL_REMOVE], 0, NULL);
}
static void
handle_remove_port (PinosPort *port, PinosNode *node)
{
pinos_node_remove_port (node, port);
}
/**
* pinos_node_create_port:
* @node: a #PinosNode
* @direction: the direction of the port
* @name: the name of the port
* @possible_formats: possible media formats for the port
* @props: extra properties for the port
*
* Create a new #PinosPort from @node.
*
* Returns: a new #PinosPort that should be freed with
* pinos_node_remove_port().
*/
void
pinos_node_create_port (PinosNode *node,
PinosDirection direction,
const gchar *name,
GBytes *possible_formats,
PinosProperties *props,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
PinosNodeClass *klass;
GTask *task;
g_return_if_fail (PINOS_IS_NODE (node));
klass = PINOS_NODE_GET_CLASS (node);
if (!klass->create_port)
return;
g_debug ("node %p: create port", node);
task = g_task_new (node, cancellable, callback, user_data);
klass->create_port (node, direction, name, possible_formats, props, task);
}
PinosPort *
pinos_node_create_port_finish (PinosNode *node,
GAsyncResult *res,
GError **error)
{
PinosPort *port;
PinosNodePrivate *priv;
g_return_val_if_fail (PINOS_IS_NODE (node), NULL);
priv = node->priv;
port = g_task_propagate_pointer (G_TASK (res), error);
if (port != NULL) {
pinos_node_add_port (node, port);
}
g_debug ("node %p: created port %p", node, port);
return port;
}
/**
* pinos_node_add_port:
* @node: a #PinosNode
* @port: (transfer full): a #PinosPort
*
* Add the #PinosPort to @node
*/
void
pinos_node_add_port (PinosNode *node, PinosPort *port)
{
PinosNodePrivate *priv;
GList *find;
g_return_if_fail (PINOS_IS_NODE (node));
g_return_if_fail (PINOS_IS_PORT (port));
priv = node->priv;
find = g_list_find (priv->ports, port);
if (find == NULL) {
g_debug ("node %p: add port %p", node, port);
priv->ports = g_list_append (priv->ports, port);
g_signal_connect (port, "remove", (GCallback) handle_remove_port, node);
}
}
/**
* pinos_node_remove_port:
* @node: a #PinosNode
* @port: (transfer full): a #PinosPort
*
* Remove the #PinosPort from @node
*/
void
pinos_node_remove_port (PinosNode *node, PinosPort *port)
{
PinosNodePrivate *priv;
PinosNodeClass *klass;
GList *find;
g_return_if_fail (PINOS_IS_NODE (node));
g_return_if_fail (PINOS_IS_PORT (port));
priv = node->priv;
find = g_list_find (priv->ports, port);
if (find) {
klass = PINOS_NODE_GET_CLASS (node);
if (klass->remove_port)
klass->remove_port (node, port);
g_debug ("node %p: removed port %p", node, port);
priv->ports = g_list_delete_link (priv->ports, find);
g_object_unref (port);
}
}
/**
* pinos_node_get_ports:
* @node: a #PinosNode
*
* Get the list of ports in @node.
*
* Returns: a #GList of ports owned by @node.
*/
GList *
pinos_node_get_ports (PinosNode *node)
{
PinosNodePrivate *priv;
g_return_val_if_fail (PINOS_IS_NODE (node), NULL);
priv = node->priv;
return priv->ports;
}
static void
remove_idle_timeout (PinosNode *node)
{
PinosNodePrivate *priv = node->priv;
if (priv->idle_timeout) {
g_source_remove (priv->idle_timeout);
priv->idle_timeout = 0;
}
}
/**
* pinos_node_set_state:
* @node: a #PinosNode
* @state: a #PinosNodeState
*
* Set the state of @node to @state.
*
* Returns: %TRUE on success.
*/
gboolean
pinos_node_set_state (PinosNode *node,
PinosNodeState state)
{
PinosNodeClass *klass;
gboolean res;
g_return_val_if_fail (PINOS_IS_NODE (node), FALSE);
klass = PINOS_NODE_GET_CLASS (node);
remove_idle_timeout (node);
g_debug ("node %p: set state to %s", node, pinos_node_state_as_string (state));
if (klass->set_state)
res = klass->set_state (node, state);
else
res = FALSE;
return res;
}
/**
* pinos_node_update_state:
* @node: a #PinosNode
* @state: a #PinosNodeState
*
* Update the state of a node. This method is used from
* inside @node itself.
*/
void
pinos_node_update_state (PinosNode *node,
PinosNodeState state)
{
PinosNodePrivate *priv;
g_return_if_fail (PINOS_IS_NODE (node));
priv = node->priv;
if (priv->state != state) {
g_debug ("node %p: update state to %s", node, pinos_node_state_as_string (state));
priv->state = state;
g_object_notify (G_OBJECT (node), "state");
}
}
/**
* pinos_node_report_error:
* @node: a #PinosNode
* @error: a #GError
*
* Report an error from within @node.
*/
void
pinos_node_report_error (PinosNode *node,
GError *error)
{
PinosNodePrivate *priv;
g_return_if_fail (PINOS_IS_NODE (node));
priv = node->priv;
g_clear_error (&priv->error);
remove_idle_timeout (node);
priv->error = error;
priv->state = PINOS_NODE_STATE_ERROR;
g_debug ("node %p: got error state %s", node, error->message);
g_object_notify (G_OBJECT (node), "state");
}
static gboolean
idle_timeout (PinosNode *node)
{
PinosNodePrivate *priv = node->priv;
priv->idle_timeout = 0;
g_debug ("node %p: idle timeout", node);
pinos_node_set_state (node, PINOS_NODE_STATE_SUSPENDED);
return G_SOURCE_REMOVE;
}
/**
* pinos_node_report_idle:
* @node: a #PinosNode
*
* Mark @node as being idle. This will start a timeout that will
* set the node to SUSPENDED.
*/
void
pinos_node_report_idle (PinosNode *node)
{
PinosNodePrivate *priv;
g_return_if_fail (PINOS_IS_NODE (node));
priv = node->priv;
g_debug ("node %p: report idle", node);
pinos_node_set_state (node, PINOS_NODE_STATE_IDLE);
priv->idle_timeout = g_timeout_add_seconds (3,
(GSourceFunc) idle_timeout,
node);
}
/**
* pinos_node_report_busy:
* @node: a #PinosNode
*
* Mark @node as being busy. This will set the state of the node
* to the RUNNING state.
*/
void
pinos_node_report_busy (PinosNode *node)
{
g_return_if_fail (PINOS_IS_NODE (node));
g_debug ("node %p: report busy", node);
pinos_node_set_state (node, PINOS_NODE_STATE_RUNNING);
}

View file

@ -1,115 +0,0 @@
/* Pinos
* 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.
*/
#ifndef __PINOS_NODE_H__
#define __PINOS_NODE_H__
#include <glib-object.h>
G_BEGIN_DECLS
typedef struct _PinosNode PinosNode;
typedef struct _PinosNodeClass PinosNodeClass;
typedef struct _PinosNodePrivate PinosNodePrivate;
#include <pinos/client/introspect.h>
#include <pinos/client/port.h>
#define PINOS_TYPE_NODE (pinos_node_get_type ())
#define PINOS_IS_NODE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), PINOS_TYPE_NODE))
#define PINOS_IS_NODE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), PINOS_TYPE_NODE))
#define PINOS_NODE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), PINOS_TYPE_NODE, PinosNodeClass))
#define PINOS_NODE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), PINOS_TYPE_NODE, PinosNode))
#define PINOS_NODE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), PINOS_TYPE_NODE, PinosNodeClass))
#define PINOS_NODE_CAST(obj) ((PinosNode*)(obj))
#define PINOS_NODE_CLASS_CAST(klass) ((PinosNodeClass*)(klass))
/**
* PinosNode:
*
* Pinos node class.
*/
struct _PinosNode {
GObject object;
PinosNodePrivate *priv;
};
/**
* PinosNodeClass:
* @set_state: called to change the current state of the node
* @create_node: make a new port
* @remove_node: remove a port
*
* Pinos node class.
*/
struct _PinosNodeClass {
GObjectClass parent_class;
gboolean (*set_state) (PinosNode *node,
PinosNodeState state);
void (*create_port) (PinosNode *node,
PinosDirection direction,
const gchar *name,
GBytes *possible_formats,
PinosProperties *props,
GTask *task);
void (*remove_port) (PinosNode *node,
PinosPort *port);
};
/* normal GObject stuff */
GType pinos_node_get_type (void);
const gchar * pinos_node_get_name (PinosNode *node);
PinosNodeState pinos_node_get_state (PinosNode *node);
PinosProperties * pinos_node_get_properties (PinosNode *node);
void pinos_node_remove (PinosNode *node);
void pinos_node_create_port (PinosNode *node,
PinosDirection direction,
const gchar *name,
GBytes *possible_formats,
PinosProperties *props,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data);
PinosPort * pinos_node_create_port_finish (PinosNode *node,
GAsyncResult *res,
GError **error);
void pinos_node_add_port (PinosNode *node,
PinosPort *port);
void pinos_node_remove_port (PinosNode *node,
PinosPort *port);
GList * pinos_node_get_ports (PinosNode *node);
gboolean pinos_node_set_state (PinosNode *node, PinosNodeState state);
void pinos_node_update_state (PinosNode *node, PinosNodeState state);
void pinos_node_report_error (PinosNode *node, GError *error);
void pinos_node_report_idle (PinosNode *node);
void pinos_node_report_busy (PinosNode *node);
G_END_DECLS
#endif /* __PINOS_NODE_H__ */

File diff suppressed because it is too large Load diff

View file

@ -1,109 +0,0 @@
/* Pinos
* 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.
*/
#ifndef __PINOS_PORT_H__
#define __PINOS_PORT_H__
#include <glib-object.h>
G_BEGIN_DECLS
typedef struct _PinosPort PinosPort;
typedef struct _PinosPortClass PinosPortClass;
typedef struct _PinosPortPrivate PinosPortPrivate;
#include <pinos/client/introspect.h>
#include <pinos/client/buffer.h>
#define PINOS_TYPE_PORT (pinos_port_get_type ())
#define PINOS_IS_PORT(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), PINOS_TYPE_PORT))
#define PINOS_IS_PORT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), PINOS_TYPE_PORT))
#define PINOS_PORT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), PINOS_TYPE_PORT, PinosPortClass))
#define PINOS_PORT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), PINOS_TYPE_PORT, PinosPort))
#define PINOS_PORT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), PINOS_TYPE_PORT, PinosPortClass))
#define PINOS_PORT_CAST(obj) ((PinosPort*)(obj))
#define PINOS_PORT_CLASS_CAST(klass) ((PinosPortClass*)(klass))
/**
* PinosPort:
*
* Pinos port object class.
*/
struct _PinosPort {
GObject object;
PinosPortPrivate *priv;
};
/**
* PinosPortClass:
*
* Pinos port object class.
*/
struct _PinosPortClass {
GObjectClass parent_class;
};
typedef void (*PinosReceivedBufferCallback) (PinosPort *port, gpointer user_data);
/* normal GObject stuff */
GType pinos_port_get_type (void);
void pinos_port_set_received_buffer_cb (PinosPort *port,
PinosReceivedBufferCallback cb,
gpointer user_data,
GDestroyNotify notify);
void pinos_port_remove (PinosPort *port);
PinosNode * pinos_port_get_node (PinosPort *port);
GSocket * pinos_port_get_socket (PinosPort *port);
const gchar * pinos_port_get_name (PinosPort *port);
PinosDirection pinos_port_get_direction (PinosPort *port);
GBytes * pinos_port_get_possible_formats (PinosPort *port);
GBytes * pinos_port_get_format (PinosPort *port);
PinosProperties * pinos_port_get_properties (PinosPort *port);
GBytes * pinos_port_filter_formats (PinosPort *port,
GBytes *filter,
GError **error);
GSocket * pinos_port_get_socket_pair (PinosPort *port,
GError **error);
gboolean pinos_port_link (PinosPort *source,
PinosPort *destination);
gboolean pinos_port_unlink (PinosPort *source,
PinosPort *destination);
PinosPort * pinos_port_get_links (PinosPort *port,
guint *n_links);
PinosBuffer * pinos_port_peek_buffer (PinosPort *port);
void pinos_port_buffer_builder_init (PinosPort *port,
PinosBufferBuilder *builder);
gboolean pinos_port_send_buffer (PinosPort *port,
PinosBuffer *buffer,
GError **error);
G_END_DECLS
#endif /* __PINOS_PORT_H__ */

View file

@ -577,131 +577,12 @@ pinos_stream_get_error (PinosStream *stream)
return stream->priv->error;
}
static void
on_received_buffer (PinosPort *port,
gpointer user_data)
{
PinosStream *stream = user_data;
g_signal_emit (stream, signals[SIGNAL_NEW_BUFFER], 0, NULL);
}
static void
on_port_notify (GObject *object,
GParamSpec *pspec,
gpointer user_data)
{
PinosPort *port = PINOS_PORT (object);
PinosStream *stream = user_data;
PinosStreamPrivate *priv = stream->priv;
if (pspec == NULL || strcmp (g_param_spec_get_name (pspec), "format")) {
g_clear_pointer (&priv->format, g_bytes_unref);
g_object_get (port, "format", &priv->format, NULL);
g_object_notify (G_OBJECT (stream), "format");
}
if (pspec == NULL || strcmp (g_param_spec_get_name (pspec), "possible-formats")) {
g_clear_pointer (&priv->possible_formats, g_bytes_unref);
g_object_get (port, "possible-formats", &priv->possible_formats, NULL);
g_object_notify (G_OBJECT (stream), "possible-formats");
}
}
static void
on_port_created (GObject *source_object,
GAsyncResult *res,
gpointer user_data)
{
PinosStream *stream = user_data;
PinosStreamPrivate *priv = stream->priv;
GError *error = NULL;
g_assert (priv->node == PINOS_NODE (source_object));
priv->port = pinos_node_create_port_finish (priv->node,
res,
&error);
if (priv->port == NULL)
goto create_failed;
on_port_notify (G_OBJECT (priv->port), NULL, stream);
g_signal_connect (priv->port, "notify", (GCallback) on_port_notify, stream);
pinos_port_set_received_buffer_cb (priv->port, on_received_buffer, stream, NULL);
stream_set_state (stream, PINOS_STREAM_STATE_READY, NULL);
g_object_unref (stream);
return;
/* ERRORS */
create_failed:
{
g_warning ("failed to create port: %s", error->message);
stream_set_state (stream, PINOS_STREAM_STATE_ERROR, error);
g_object_unref (stream);
return;
}
}
static void
on_node_created (GObject *source_object,
GAsyncResult *res,
gpointer user_data)
{
PinosStream *stream = user_data;
PinosStreamPrivate *priv = stream->priv;
PinosContext *context = priv->context;
GError *error = NULL;
priv->node = pinos_context_create_node_finish (context, res, &error);
if (priv->node == NULL)
goto create_failed;
if (priv->properties == NULL)
priv->properties = pinos_properties_new (NULL, NULL);
if (priv->flags & PINOS_STREAM_FLAG_AUTOCONNECT)
pinos_properties_set (priv->properties, "autoconnect", "1");
else
pinos_properties_set (priv->properties, "autoconnect", "0");
if (priv->path)
pinos_properties_set (priv->properties, "target-path", priv->path);
pinos_node_create_port (priv->node,
priv->direction,
"client-port",
priv->possible_formats,
priv->properties,
NULL, /* GCancellable *cancellable */
on_port_created,
stream);
return;
/* ERRORS */
create_failed:
{
g_warning ("failed to create node: %s", error->message);
stream_set_state (stream, PINOS_STREAM_STATE_ERROR, error);
g_object_unref (stream);
return;
}
}
static gboolean
do_connect (PinosStream *stream)
{
PinosStreamPrivate *priv = stream->priv;
PinosContext *context = priv->context;
pinos_context_create_node (context,
"client-node",
"client-node",
priv->properties,
NULL, /* GCancellable *cancellable */
on_node_created,
stream);
return FALSE;
}
@ -841,8 +722,6 @@ do_disconnect (PinosStream *stream)
{
PinosStreamPrivate *priv = stream->priv;
pinos_node_remove (priv->node);
return FALSE;
}
@ -896,7 +775,7 @@ pinos_stream_peek_buffer (PinosStream *stream)
priv = stream->priv;
//g_return_val_if_fail (priv->state == PINOS_STREAM_STATE_STREAMING, FALSE);
return pinos_port_peek_buffer (priv->port);
return NULL;
}
/**
@ -916,7 +795,6 @@ pinos_stream_buffer_builder_init (PinosStream *stream, PinosBufferBuilder *buil
g_return_if_fail (PINOS_IS_STREAM (stream));
priv = stream->priv;
pinos_port_buffer_builder_init (priv->port, builder);
}
/**
@ -947,17 +825,5 @@ pinos_stream_send_buffer (PinosStream *stream,
priv = stream->priv;
g_return_val_if_fail (priv->state == PINOS_STREAM_STATE_STREAMING, FALSE);
if (!pinos_port_send_buffer (priv->port, buffer, &error))
goto send_error;
return TRUE;
/* ERRORS */
send_error:
{
g_warning ("failed to send message: %s", error->message);
stream_set_state (stream, PINOS_STREAM_STATE_ERROR, error);
g_clear_error (&error);
return FALSE;
}
}