<commit_before>/*
 * tcmmd - traffic control multimedia daemon
 * Copyright (C) 2014 Collabora Ltd.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser 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 "tcmmd-dbus.h"
#include "tcmmd-generated.h"

G_DEFINE_TYPE (TcmmdDbus, tcmmd_dbus, G_TYPE_OBJECT)

struct _TcmmdDbusPrivate
{
  GDBusConnection *connection;
  TcmmdManagedConnections *iface;
  guint own_name_id;
  guint watch_id;
};

enum
{
  SET_POLICY,
  SET_FIXED_POLICY,
  UNSET_POLICY,
  LAST_SIGNAL
};

static guint signals[LAST_SIGNAL];

static void
name_vanished_cb (GDBusConnection *connection,
    const gchar *name,
    gpointer user_data)
{
  TcmmdDbus *self = user_data;

  g_bus_unwatch_name (self->priv->watch_id);
  self->priv->watch_id = 0;

  g_signal_emit (self, signals[UNSET_POLICY], 0);
}

static void
watch_name (TcmmdDbus *self,
    const gchar *name)
{
  if (self->priv->watch_id != 0)
    g_bus_unwatch_name (self->priv->watch_id);

  self->priv->watch_id = g_bus_watch_name_on_connection (self->priv->connection,
      name, G_BUS_NAME_WATCHER_FLAGS_NONE, NULL,
      name_vanished_cb, self, NULL);
}

static gboolean
handle_set_policy_cb (TcmmdManagedConnections *iface,
    GDBusMethodInvocation *invocation,
    const gchar *src_ip,
    guint src_port,
    const gchar *dest_ip,
    guint dest_port,
    guint bitrate,
    gdouble buffer_fill,
    gpointer user_data)
{
  TcmmdDbus *self = user_data;

  g_print ("SetPolicy: src=%s:%d, dest=%s:%d, bitrate=%d, buffer=%d%%\n",
      src_ip, src_port, dest_ip, dest_port, bitrate, (gint) (buffer_fill * 100.0));

  watch_name (self, g_dbus_method_invocation_get_sender (invocation));

  tcmmd_managed_connections_set_bitrate (self->priv->iface, bitrate);
  tcmmd_managed_connections_set_buffer_fill (self->priv->iface, buffer_fill);

  g_signal_emit (self, signals[SET_POLICY], 0,
      src_ip, src_port, dest_ip, dest_port, bitrate, buffer_fill);

  return TRUE;
}

static gboolean
handle_set_fixed_policy_cb (TcmmdManagedConnections *iface,
    GDBusMethodInvocation *invocation,
    const gchar *src_ip,
    guint src_port,
    const gchar *dest_ip,
    guint dest_port,
    guint stream_rate,
    guint background_rate,
    gpointer user_data)
{
  TcmmdDbus *self = user_data;

  g_print ("SetFixedPolicy: %s:%d -> %s:%d stream_rate=%d, background_rate=%d\n",
      src_ip, src_port, dest_ip, dest_port, stream_rate, background_rate);

  watch_name (self, g_dbus_method_invocation_get_sender (invocation));

  g_signal_emit (self, signals[SET_FIXED_POLICY], 0,
      src_ip, src_port, dest_ip, dest_port, stream_rate, background_rate);

  return TRUE;
}

static gboolean
handle_unset_policy_cb (TcmmdManagedConnections *iface,
    GDBusMethodInvocation *invocation,
    gpointer user_data)
{
  TcmmdDbus *self = user_data;

  g_print ("UnsetPolicy\n");

  if (self->priv->watch_id != 0)
    {
      g_bus_unwatch_name (self->priv->watch_id);
      self->priv->watch_id = 0;
    }

  g_signal_emit (self, signals[UNSET_POLICY], 0);

  return TRUE;
}

static void
on_bus_acquired (GDBusConnection *connection,
    const gchar *name,
    gpointer user_data)
{
  TcmmdDbus *self = user_data;
  GError *error = NULL;

  self->priv->connection = g_object_ref (connection);

  self->priv->iface = tcmmd_managed_connections_skeleton_new ();
  g_signal_connect (self->priv->iface, "handle-set-policy",
                    G_CALLBACK (handle_set_policy_cb), self);
  g_signal_connect (self->priv->iface, "handle-set-fixed-policy",
                    G_CALLBACK (handle_set_fixed_policy_cb), self);
  g_signal_connect (self->priv->iface, "handle-unset-policy",
                    G_CALLBACK (handle_unset_policy_cb), self);

  if (!g_dbus_interface_skeleton_export (
          G_DBUS_INTERFACE_SKELETON (self->priv->iface),
          self->priv->connection,
          "/org/tcmmd/ManagedConnections", &error))
    {
      g_critical ("Failed to export iface: %s", error->message);
      g_clear_error (&error);
    }<commit_msg>tcmmd: Re-add D-Bus invocation completion calls<commit_after>/*
 * tcmmd - traffic control multimedia daemon
 * Copyright (C) 2014 Collabora Ltd.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser 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 "tcmmd-dbus.h"
#include "tcmmd-generated.h"

G_DEFINE_TYPE (TcmmdDbus, tcmmd_dbus, G_TYPE_OBJECT)

struct _TcmmdDbusPrivate
{
  GDBusConnection *connection;
  TcmmdManagedConnections *iface;
  guint own_name_id;
  guint watch_id;
};

enum
{
  SET_POLICY,
  SET_FIXED_POLICY,
  UNSET_POLICY,
  LAST_SIGNAL
};

static guint signals[LAST_SIGNAL];

static void
name_vanished_cb (GDBusConnection *connection,
    const gchar *name,
    gpointer user_data)
{
  TcmmdDbus *self = user_data;

  g_bus_unwatch_name (self->priv->watch_id);
  self->priv->watch_id = 0;

  g_signal_emit (self, signals[UNSET_POLICY], 0);
}

static void
watch_name (TcmmdDbus *self,
    const gchar *name)
{
  if (self->priv->watch_id != 0)
    g_bus_unwatch_name (self->priv->watch_id);

  self->priv->watch_id = g_bus_watch_name_on_connection (self->priv->connection,
      name, G_BUS_NAME_WATCHER_FLAGS_NONE, NULL,
      name_vanished_cb, self, NULL);
}

static gboolean
handle_set_policy_cb (TcmmdManagedConnections *iface,
    GDBusMethodInvocation *invocation,
    const gchar *src_ip,
    guint src_port,
    const gchar *dest_ip,
    guint dest_port,
    guint bitrate,
    gdouble buffer_fill,
    gpointer user_data)
{
  TcmmdDbus *self = user_data;

  g_print ("SetPolicy: src=%s:%d, dest=%s:%d, bitrate=%d, buffer=%d%%\n",
      src_ip, src_port, dest_ip, dest_port, bitrate, (gint) (buffer_fill * 100.0));

  watch_name (self, g_dbus_method_invocation_get_sender (invocation));

  tcmmd_managed_connections_set_bitrate (self->priv->iface, bitrate);
  tcmmd_managed_connections_set_buffer_fill (self->priv->iface, buffer_fill);

  g_signal_emit (self, signals[SET_POLICY], 0,
      src_ip, src_port, dest_ip, dest_port, bitrate, buffer_fill);

  tcmmd_managed_connections_complete_set_policy (iface, invocation);

  return TRUE;
}

static gboolean
handle_set_fixed_policy_cb (TcmmdManagedConnections *iface,
    GDBusMethodInvocation *invocation,
    const gchar *src_ip,
    guint src_port,
    const gchar *dest_ip,
    guint dest_port,
    guint stream_rate,
    guint background_rate,
    gpointer user_data)
{
  TcmmdDbus *self = user_data;

  g_print ("SetFixedPolicy: %s:%d -> %s:%d stream_rate=%d, background_rate=%d\n",
      src_ip, src_port, dest_ip, dest_port, stream_rate, background_rate);

  watch_name (self, g_dbus_method_invocation_get_sender (invocation));

  g_signal_emit (self, signals[SET_FIXED_POLICY], 0,
      src_ip, src_port, dest_ip, dest_port, stream_rate, background_rate);

  tcmmd_managed_connections_complete_set_fixed_policy (iface, invocation);

  return TRUE;
}

static gboolean
handle_unset_policy_cb (TcmmdManagedConnections *iface,
    GDBusMethodInvocation *invocation,
    gpointer user_data)
{
  TcmmdDbus *self = user_data;

  g_print ("UnsetPolicy\n");

  if (self->priv->watch_id != 0)
    {
      g_bus_unwatch_name (self->priv->watch_id);
      self->priv->watch_id = 0;
    }

  g_signal_emit (self, signals[UNSET_POLICY], 0);

  tcmmd_managed_connections_complete_unset_policy (iface, invocation);

  return TRUE;
}

static void
on_bus_acquired (GDBusConnection *connection,
    const gchar *name,
    gpointer user_data)
{
  TcmmdDbus *self = user_data;
  GError *error = NULL;

  self->priv->connection = g_object_ref (connection);

  self->priv->iface = tcmmd_managed_connections_skeleton_new ();
  g_signal_connect (self->priv->iface, "handle-set-policy",
                    G_CALLBACK (handle_set_policy_cb), self);
  g_signal_connect (self->priv->iface, "handle-set-fixed-policy",
                    G_CALLBACK (handle_set_fixed_policy_cb), self);
  g_signal_connect (self->priv->iface, "handle-unset-policy",
                    G_CALLBACK (handle_unset_policy_cb), self);

  if (!g_dbus_interface_skeleton_export (
          G_DBUS_INTERFACE_SKELETON (self->priv->iface),
          self->priv->connection,
          "/org/tcmmd/ManagedConnections", &error))
    {
      g_critical ("Failed to export iface: %s", error->message);
      g_clear_error (&error);
    }
{'fraction_non_alphanumeric': 0.10050198703200168, 'fraction_numerical': 0.006065676636686885, 'mean_word_length': 4.214285714285714, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 79, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2832755', 'n_tokens_mistral': 3418, 'n_tokens_neox': 3327, 'n_words': 802}