<commit_before>/*
 * Gstreamer Daemon - Gst Launch under steroids
 * Copyright (C) 2015 <NAME> <<EMAIL>>
 *
 * This file is part of Gstd.
 *
 * Gstd 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 3 of the License, or
 * (at your option) any later version.
 *
 * Gstd 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 Gstd.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <string.h>
#include <stdio.h>
#include <gst/gst.h>

#include "gstd_session.h"
#include "gstd_list.h"
#include "gstd_tcp.h"
#include "gstd_pipeline_creator.h"
#include "gstd_pipeline_deleter.h"

/* Gstd Session debugging category */
GST_DEBUG_CATEGORY_STATIC(gstd_session_debug);
#define GST_CAT_DEFAULT gstd_session_debug

#define GSTD_DEBUG_DEFAULT_LEVEL GST_LEVEL_INFO

enum {
  PROP_PIPELINES = 1,
  PROP_PORT,
  N_PROPERTIES // NOT A PROPERTY
};

#define GSTD_SESSION_DEFAULT_PIPELINES NULL

struct _GstdSession
{
  GstdObject parent;
  
  /**
   * The list of GstdPipelines created by the user
   */
  GstdList *pipelines;

  guint16 port;
  GSocketService *service;
};

struct _GstdSessionClass
{
  GstdObjectClass parent_class;
};

G_DEFINE_TYPE (GstdSession, gstd_session, GSTD_TYPE_OBJECT)

/* VTable */
static void
gstd_session_set_property (GObject *, guint, const GValue *, GParamSpec *);
static void
gstd_session_get_property (GObject *, guint, GValue *, GParamSpec *);
static void
gstd_session_dispose (GObject *);
static void
gstd_session_constructed (GObject *);

static void
gstd_session_class_init (GstdSessionClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  GParamSpec *properties[N_PROPERTIES] = { NULL, };
  guint debug_color;

  object_class->set_property = gstd_session_set_property;
  object_class->get_property = gstd_session_get_property;
  object_class->dispose = gstd_session_dispose;
  object_class->constructed = gstd_session_constructed;

  properties[PROP_PIPELINES] =
    g_param_spec_object ("pipelines",
			 "Pipelines",
			 "The pipelines created by the user",
			 GSTD_TYPE_LIST,
			 G_PARAM_READWRITE |
			 G_PARAM_STATIC_STRINGS |
			 GSTD_PARAM_CREATE |
			 GSTD_PARAM_READ |
			 GSTD_PARAM_DELETE);

  properties[PROP_PORT] =
    g_param_spec_uint ("port",
		       "Port",
		       "The port to start listening to",
		       0,
		       G_MAXINT,
		       GSTD_TCP_DEFAULT_PORT,
		       G_PARAM_READWRITE |
		       G_PARAM_CONSTRUCT_ONLY |
		       G_PARAM_STATIC_STRINGS |
		       GSTD_PARAM_READ);

  g_object_class_install_properties (object_class,
                                     N_PROPERTIES,
                                     properties);
  
  /* Initialize debug category with nice colors */
  debug_color = GST_DEBUG_FG_BLACK | GST_DEBUG_BOLD | GST_DEBUG_BG_WHITE;
  GST_DEBUG_CATEGORY_INIT (gstd_session_debug, "gstdsession", debug_color,
			   "Gstd Session category");
}

static void
gstd_session_init (GstdSession *self)
{
  GST_INFO_OBJECT(self, "Initializing gstd session");

  self->pipelines = GSTD_LIST(g_object_new(GSTD_TYPE_LIST, "name", "pipelines",
					   "node-type", GSTD_TYPE_PIPELINE, "flags",
					   GSTD_PARAM_CREATE | GSTD_PARAM_READ |
					   GSTD_PARAM_UPDATE | GSTD_PARAM_DELETE, NULL));

  gstd_list_set_creator(self->pipelines,
      g_object_new (GSTD_TYPE_PIPELINE_CREATOR,NULL));

  gstd_list_set_deleter(self->pipelines,
      g_object_new (GSTD_TYPE_PIPELINE_DELETER,NULL));

  self->port = GSTD_TCP_DEFAULT_PORT;
  self->service = NULL;
}

static void
gstd_session_get_property (GObject        *object,
			guint           property_id,
			GValue         *value,
			GParamSpec     *pspec)
{
  GstdSession *self = GSTD_SESSION(object);

  gstd_object_set_code (GSTD_OBJECT(self), GSTD_EOK);
  
  switch (property_id) {
  case PROP_PIPELINES:
    GST_DEBUG_OBJECT(self, "Returning pipeline list %p", self->pipelines);
    g_value_set_object (value, self->pipelines);
    break;
  case PROP_PORT:
    GST_DEBUG_OBJECT(self, "Returning post %u", self->port);
    g_value_set_uint (value, self->port);
    break;
    
  default:
    /* We don't have any other property... */
    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
    gstd_object_set_code (GSTD_OBJECT(self), GSTD_NO_RESOURCE);
    break;
  }<commit_msg>Add initial singleton implementation for gstd_session class.<commit_after>/*
 * Gstreamer Daemon - Gst Launch under steroids
 * Copyright (C) 2015 RidgeRun Engineering <<EMAIL>>
 *
 * This file is part of Gstd.
 *
 * Gstd 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 3 of the License, or
 * (at your option) any later version.
 *
 * Gstd 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 Gstd.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <string.h>
#include <stdio.h>
#include <gst/gst.h>
#include <glib.h>

#include "gstd_session.h"
#include "gstd_list.h"
#include "gstd_tcp.h"
#include "gstd_pipeline_creator.h"
#include "gstd_pipeline_deleter.h"

/* Gstd Session debugging category */
GST_DEBUG_CATEGORY_STATIC(gstd_session_debug);
#define GST_CAT_DEFAULT gstd_session_debug

#define GSTD_DEBUG_DEFAULT_LEVEL GST_LEVEL_INFO

GMutex singletonMutex;

enum {
  PROP_PIPELINES = 1,
  PROP_PORT,
  N_PROPERTIES // NOT A PROPERTY
};

#define GSTD_SESSION_DEFAULT_PIPELINES NULL

struct _GstdSession
{
  GstdObject parent;
  
  /**
   * The list of GstdPipelines created by the user
   */
  GstdList *pipelines;

  guint16 port;
  GSocketService *service;
};

struct _GstdSessionClass
{
  GstdObjectClass parent_class;
};

G_DEFINE_TYPE (GstdSession, gstd_session, GSTD_TYPE_OBJECT)

/* VTable */
static void
gstd_session_set_property (GObject *, guint, const GValue *, GParamSpec *);
static void
gstd_session_get_property (GObject *, guint, GValue *, GParamSpec *);
static void
gstd_session_dispose (GObject *);
static void
gstd_session_constructed (GObject *);
static GObject* 
gstd_session_constructor(GType, guint, GObjectConstructParam *); 

static GObject *session = NULL;

static GObject*
gstd_session_constructor(GType type, guint n_construct_params,
            GObjectConstructParam *construct_params)
{
  g_mutex_lock(&singletonMutex);
  GObject* object = NULL;
  g_print("Before instantiation \n");
  if (session == NULL)
  {
    g_print("Instantiating singleton \n");
    object = G_OBJECT_CLASS(gstd_session_parent_class)->constructor(type, n_construct_params, construct_params);
    session = object;
  }
  g_mutex_unlock(&singletonMutex);
  object = g_object_ref (G_OBJECT (session));
  return session;
}

static void
gstd_session_class_init (GstdSessionClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  GParamSpec *properties[N_PROPERTIES] = { NULL, };
  guint debug_color;

  object_class->set_property = gstd_session_set_property;
  object_class->get_property = gstd_session_get_property;
  object_class->dispose = gstd_session_dispose;
  object_class->constructed = gstd_session_constructed;
  object_class->constructor = gstd_session_constructor;

  properties[PROP_PIPELINES] =
    g_param_spec_object ("pipelines",
			 "Pipelines",
			 "The pipelines created by the user",
			 GSTD_TYPE_LIST,
			 G_PARAM_READWRITE |
			 G_PARAM_STATIC_STRINGS |
			 GSTD_PARAM_CREATE |
			 GSTD_PARAM_READ |
			 GSTD_PARAM_DELETE);

  properties[PROP_PORT] =
    g_param_spec_uint ("port",
		       "Port",
		       "The port to start listening to",
		       0,
		       G_MAXINT,
		       GSTD_TCP_DEFAULT_PORT,
		       G_PARAM_READWRITE |
		       G_PARAM_CONSTRUCT_ONLY |
		       G_PARAM_STATIC_STRINGS |
		       GSTD_PARAM_READ);

  g_object_class_install_properties (object_class,
                                     N_PROPERTIES,
                                     properties);
  
  /* Initialize debug category with nice colors */
  debug_color = GST_DEBUG_FG_BLACK | GST_DEBUG_BOLD | GST_DEBUG_BG_WHITE;
  GST_DEBUG_CATEGORY_INIT (gstd_session_debug, "gstdsession", debug_color,
			   "Gstd Session category");
}

static void
gstd_session_init (GstdSession *self)
{
    GST_INFO_OBJECT(self, "Initializing gstd session");

    self->pipelines = GSTD_LIST(g_object_new(GSTD_TYPE_LIST, "name", "pipelines",
              "node-type", GSTD_TYPE_PIPELINE, "flags",
              GSTD_PARAM_CREATE | GSTD_PARAM_READ |
              GSTD_PARAM_UPDATE | GSTD_PARAM_DELETE, NULL));

    gstd_list_set_creator(self->pipelines,
        g_object_new (GSTD_TYPE_PIPELINE_CREATOR,NULL));

    gstd_list_set_deleter(self->pipelines,
        g_object_new (GSTD_TYPE_PIPELINE_DELETER,NULL));

    self->port = GSTD_TCP_DEFAULT_PORT;
    self->service = NULL;
}

static void
gstd_session_get_property (GObject        *object,
			guint           property_id,
			GValue         *value,
			GParamSpec     *pspec)
{
  GstdSession *self = GSTD_SESSION(object);

  gstd_object_set_code (GSTD_OBJECT(self), GSTD_EOK);
  
  switch (property_id) {
  case PROP_PIPELINES:
    GST_DEBUG_OBJECT(self, "Returning pipeline list %p", self->pipelines);
    g_value_set_object (value, self->pipelines);
    break;
  case PROP_PORT:
    GST_DEBUG_OBJECT(self, "Returning post %u", self->port);
    g_value_set_uint (value, self->port);
    break;
    
  default:
    /* We don't have any other property... */
    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
    gstd_object_set_code (GSTD_OBJECT(self), GSTD_NO_RESOURCE);
    break;
  }
{'fraction_non_alphanumeric': 0.08755985536988176, 'fraction_numerical': 0.001759014951627089, 'mean_word_length': 3.939189189189189, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5244058', 'n_tokens_mistral': 3674, 'n_tokens_neox': 3425, 'n_words': 804}