<commit_before>/*
 * filetea-protocol.c
 *
 * FileTea, low-friction file sharing <http://filetea.net>
 *
 * Copyright (C) 2012, Igalia S.L.
 *
 * Authors:
 *   <NAME> <<EMAIL>>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Affero General Public License
 * version 3, or (at your option) any later version as published by
 * the Free Software Foundation.
 *
 * 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
 * Affero General Public License at http://www.gnu.org/licenses/agpl.html
 * for more details.
 */

#include "filetea-protocol.h"

G_DEFINE_TYPE (FileteaProtocol, filetea_protocol, G_TYPE_OBJECT)

#define FILETEA_PROTOCOL_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
                                           FILETEA_TYPE_PROTOCOL, \
                                           FileteaProtocolPrivate))

#define OP_REGISTER "register"

typedef enum
{
  OPERATION_INVALID,
  OPERATION_REGISTER
} FileteaProtocolOperations;

/* private data */
struct _FileteaProtocolPrivate
{
  EvdJsonrpc *rpc;

  FileteaProtocolVTable *vtable;

  gpointer user_data;
  GDestroyNotify user_data_free_func;
};

static void     filetea_protocol_class_init         (FileteaProtocolClass *class);
static void     filetea_protocol_init               (FileteaProtocol *self);

static void     filetea_protocol_finalize           (GObject *obj);
static void     filetea_protocol_dispose            (GObject *obj);

static void     rpc_on_method_called                (EvdJsonrpc  *jsonrpc,
                                                     const gchar *method_name,
                                                     JsonNode    *params,
                                                     guint        invocation_id,
                                                     gpointer     context,
                                                     gpointer     user_data);

static void
filetea_protocol_class_init (FileteaProtocolClass *class)
{
  GObjectClass *obj_class = G_OBJECT_CLASS (class);

  obj_class->dispose = filetea_protocol_dispose;
  obj_class->finalize = filetea_protocol_finalize;

  g_type_class_add_private (obj_class, sizeof (FileteaProtocolPrivate));
}

static void
filetea_protocol_init (FileteaProtocol *self)
{
  FileteaProtocolPrivate *priv;

  priv = FILETEA_PROTOCOL_GET_PRIVATE (self);
  self->priv = priv;

  /* JSON-RPC */
  priv->rpc = evd_jsonrpc_new ();
  evd_jsonrpc_set_method_call_callback (priv->rpc,
                                        rpc_on_method_called,
                                        self);

  priv->vtable = NULL;
}

static void
filetea_protocol_dispose (GObject *obj)
{
  FileteaProtocol *self = FILETEA_PROTOCOL (obj);

  if (self->priv->user_data != NULL && self->priv->user_data_free_func != NULL)
    {
      self->priv->user_data_free_func (self->priv->user_data);
      self->priv->user_data = NULL;
    }

  G_OBJECT_CLASS (filetea_protocol_parent_class)->dispose (obj);
}

static void
filetea_protocol_finalize (GObject *obj)
{
  FileteaProtocol *self = FILETEA_PROTOCOL (obj);

  g_object_unref (self->priv->rpc);

  G_OBJECT_CLASS (filetea_protocol_parent_class)->finalize (obj);
}

typedef struct
{
  FileteaProtocolOperations op;

  guint invocation_id;
  gpointer context;

  JsonNode *result;
  GError *error;
  JsonNode **result_elements;

  guint num_ops;
  guint num_completed_ops;
} AsyncOpData;

typedef struct
{
  AsyncOpData *data;
  guint op_index;
} FileteaProtocolOperation;

static void
op_register_content (FileteaProtocol *self,
                     JsonNode        *params,
                     guint            invocation_id,
                     gpointer         context)
{
  JsonArray *a;
  gint i;
  GError *error = NULL;

  JsonArray *result_arr = NULL;

  if (self->priv->vtable->register_source == NULL)
    {
      g_set_error (&error,
                   G_IO_ERROR,
                   G_IO_ERROR_NOT_SUPPORTED,
                   "'register' operation not implemented");
      goto out;
    }

  if (! JSON_NODE_HOLDS_ARRAY (params))
    {
      g_set_error (&error,
                   G_IO_ERROR,
                   G_IO_ERROR_INVALID_ARGUMENT,
                   "Method register expects an array of objects");
      goto out;
    }

  result_arr = json_array_new ();

  a = json_node_get_array (params);
  for (i=0; i<json_array_get_length (a); i++)
    {<commit_msg>Removes a check for JSON-RPC params to be an array in FileteaProtocol<commit_after>/*
 * filetea-protocol.c
 *
 * FileTea, low-friction file sharing <http://filetea.net>
 *
 * Copyright (C) 2012, Igalia S.L.
 *
 * Authors:
 *   <NAME> <<EMAIL>>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Affero General Public License
 * version 3, or (at your option) any later version as published by
 * the Free Software Foundation.
 *
 * 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
 * Affero General Public License at http://www.gnu.org/licenses/agpl.html
 * for more details.
 */

#include "filetea-protocol.h"

G_DEFINE_TYPE (FileteaProtocol, filetea_protocol, G_TYPE_OBJECT)

#define FILETEA_PROTOCOL_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
                                           FILETEA_TYPE_PROTOCOL, \
                                           FileteaProtocolPrivate))

#define OP_REGISTER "register"

typedef enum
{
  OPERATION_INVALID,
  OPERATION_REGISTER
} FileteaProtocolOperations;

/* private data */
struct _FileteaProtocolPrivate
{
  EvdJsonrpc *rpc;

  FileteaProtocolVTable *vtable;

  gpointer user_data;
  GDestroyNotify user_data_free_func;
};

static void     filetea_protocol_class_init         (FileteaProtocolClass *class);
static void     filetea_protocol_init               (FileteaProtocol *self);

static void     filetea_protocol_finalize           (GObject *obj);
static void     filetea_protocol_dispose            (GObject *obj);

static void     rpc_on_method_called                (EvdJsonrpc  *jsonrpc,
                                                     const gchar *method_name,
                                                     JsonNode    *params,
                                                     guint        invocation_id,
                                                     gpointer     context,
                                                     gpointer     user_data);

static void
filetea_protocol_class_init (FileteaProtocolClass *class)
{
  GObjectClass *obj_class = G_OBJECT_CLASS (class);

  obj_class->dispose = filetea_protocol_dispose;
  obj_class->finalize = filetea_protocol_finalize;

  g_type_class_add_private (obj_class, sizeof (FileteaProtocolPrivate));
}

static void
filetea_protocol_init (FileteaProtocol *self)
{
  FileteaProtocolPrivate *priv;

  priv = FILETEA_PROTOCOL_GET_PRIVATE (self);
  self->priv = priv;

  /* JSON-RPC */
  priv->rpc = evd_jsonrpc_new ();
  evd_jsonrpc_set_method_call_callback (priv->rpc,
                                        rpc_on_method_called,
                                        self);

  priv->vtable = NULL;
}

static void
filetea_protocol_dispose (GObject *obj)
{
  FileteaProtocol *self = FILETEA_PROTOCOL (obj);

  if (self->priv->user_data != NULL && self->priv->user_data_free_func != NULL)
    {
      self->priv->user_data_free_func (self->priv->user_data);
      self->priv->user_data = NULL;
    }

  G_OBJECT_CLASS (filetea_protocol_parent_class)->dispose (obj);
}

static void
filetea_protocol_finalize (GObject *obj)
{
  FileteaProtocol *self = FILETEA_PROTOCOL (obj);

  g_object_unref (self->priv->rpc);

  G_OBJECT_CLASS (filetea_protocol_parent_class)->finalize (obj);
}

typedef struct
{
  FileteaProtocolOperations op;

  guint invocation_id;
  gpointer context;

  JsonNode *result;
  GError *error;
  JsonNode **result_elements;

  guint num_ops;
  guint num_completed_ops;
} AsyncOpData;

typedef struct
{
  AsyncOpData *data;
  guint op_index;
} FileteaProtocolOperation;

static void
op_register_content (FileteaProtocol *self,
                     JsonNode        *params,
                     guint            invocation_id,
                     gpointer         context)
{
  JsonArray *a;
  gint i;
  GError *error = NULL;

  JsonArray *result_arr = NULL;

  if (self->priv->vtable->register_source == NULL)
    {
      g_set_error (&error,
                   G_IO_ERROR,
                   G_IO_ERROR_NOT_SUPPORTED,
                   "'register' operation not implemented");
      goto out;
    }

  result_arr = json_array_new ();

  a = json_node_get_array (params);
  for (i=0; i<json_array_get_length (a); i++)
    {
{'fraction_non_alphanumeric': 0.08073148974130241, 'fraction_numerical': 0.0013380909901873326, 'mean_word_length': 2.354151084517577, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 32, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5418324', 'n_tokens_mistral': 2731, 'n_tokens_neox': 2658, 'n_words': 637}