Exemplo n.º 1
0
JsonNode*
cometd_new_handshake_message(const cometd* h)
{
  gint64 seed = ++(h->conn->msg_id_seed);

  JsonNode*   root = json_node_new(JSON_NODE_OBJECT);
  JsonObject* obj  = json_object_new();

  json_object_set_int_member   (obj, COMETD_MSG_ID_FIELD,          seed);
  json_object_set_string_member(obj, COMETD_MSG_CHANNEL_FIELD,     COMETD_CHANNEL_META_HANDSHAKE);
  json_object_set_string_member(obj, COMETD_MSG_VERSION_FIELD,     COMETD_VERSION);
  json_object_set_string_member(obj, COMETD_MSG_MIN_VERSION_FIELD, COMETD_MIN_VERSION);

  // construct advice - TODO: these values should not be hardcoded
  JsonObject* advice = json_object_new();
  json_object_set_int_member(advice, "timeout",  60000);
  json_object_set_int_member(advice, "interval", 0);
  json_object_set_object_member(obj, COMETD_MSG_ADVICE_FIELD, advice);

  // construct supported transports
  JsonArray* json_transports = json_array_new();

  GList* entry = h->config->transports;
  while (entry){
    cometd_transport* t = entry->data;
    json_array_add_string_element(json_transports, t->name);
    entry = g_list_next(entry);
  }
  json_object_set_array_member(obj, "supportedConnectionTypes", json_transports);

  // call extensions with message - TODO: implement extensions first
  json_node_take_object(root, obj);

  return root;
}
Exemplo n.º 2
0
JsonNode*   cometd_ping_ls(cometd* h, char *target)
{
  JsonObject	*adviceObject = json_object_new();
  JsonNode	*adviceNode = json_node_new(JSON_NODE_OBJECT);
  JsonNode	*adviceRoot = json_node_init_object(adviceNode, adviceObject);

  adviceObject = json_node_get_object(adviceRoot);

  json_object_set_string_member(adviceObject, "folder", "/");

  
  JsonNode*	root = json_node_new(JSON_NODE_OBJECT);
  JsonObject*	obj  = json_object_new();
  gint64	seed = ++(h->conn->msg_id_seed);

  char*  connection_type = cometd_current_transport(h)->name;
  
  json_object_set_int_member   (obj, COMETD_MSG_ID_FIELD,      seed);
  json_object_set_string_member(obj, COMETD_MSG_CHANNEL_FIELD, target);
  json_object_set_string_member(obj, "connectionType",         connection_type);
  json_object_set_member(obj, "data", adviceRoot);
  json_object_set_string_member(obj, "clientId",               cometd_conn_client_id(h->conn));
 
  json_node_take_object(root, obj);

  return(root);
}
Exemplo n.º 3
0
/**
 * json_builder_add_int_value:
 * @builder: a #JsonBuilder
 * @value: the value of the member or element
 *
 * If called after json_builder_set_member_name(), sets @value as member of the
 * most recent opened object, otherwise @value is added as element of the most
 * recent opened array.
 *
 * See also: json_builder_add_value()
 *
 * Return value: (transfer none): the #JsonBuilder, or %NULL if the call was inconsistent
 */
JsonBuilder *
json_builder_add_int_value (JsonBuilder *builder,
                            gint64       value)
{
  JsonBuilderState *state;

  g_return_val_if_fail (JSON_IS_BUILDER (builder), NULL);
  g_return_val_if_fail (!g_queue_is_empty (builder->priv->stack), NULL);
  g_return_val_if_fail (json_builder_is_valid_add_mode (builder), NULL);

  state = g_queue_peek_head (builder->priv->stack);
  switch (state->mode)
    {
    case JSON_BUILDER_MODE_MEMBER:
      json_object_set_int_member (state->data.object, state->member_name, value);
      g_free (state->member_name);
      state->member_name = NULL;
      state->mode = JSON_BUILDER_MODE_OBJECT;
      break;

    case JSON_BUILDER_MODE_ARRAY:
      json_array_add_int_element (state->data.array, value);
      break;

    default:
      g_assert_not_reached ();
    }

  return builder;
}
Exemplo n.º 4
0
JsonNode*
cometd_new_publish_message(const cometd* h,
                           const char* channel,
                           JsonNode* data)
{
  gint64 seed = ++(h->conn->msg_id_seed);

  JsonNode*   root = json_node_new(JSON_NODE_OBJECT);
  JsonObject* obj  = json_object_new();

  json_object_set_int_member(obj, COMETD_MSG_ID_FIELD, seed);

  json_object_set_string_member(obj,
                                COMETD_MSG_CHANNEL_FIELD,
                                channel);

  json_object_set_string_member(obj,
                                COMETD_MSG_CLIENT_ID_FIELD,
                                cometd_conn_client_id(h->conn));

  json_object_set_member(obj,
                         COMETD_MSG_DATA_FIELD,
                         json_node_copy(data));

  json_node_take_object(root, obj);

  return root;
}
Exemplo n.º 5
0
static JsonNode *
melo_jsonrpc_build_error_nodev (MeloJSONRPCError error_code,
                                const char *error_format, va_list args)
{
  gchar *error_message;
  JsonObject *obj;
  JsonNode *node;

  /* Create a new JSON builder */
  obj = json_object_new ();
  if (!obj)
    return NULL;

  /* Generate error message */
  error_message = g_strdup_vprintf (error_format, args);

  /* Begin a new object */
  json_object_set_int_member (obj, "code", error_code);
  json_object_set_string_member (obj, "message", error_message);

  /* Free message */
  g_free (error_message);

  /* Create node */
  node = json_node_new (JSON_NODE_OBJECT);
  json_node_take_object (node, obj);

  return node;
}
Exemplo n.º 6
0
JsonNode*
cometd_new_unsubscribe_message(const cometd* h, const char* channel)
{
  
  gint64 seed = ++(h->conn->msg_id_seed);

  JsonNode*   root = json_node_new(JSON_NODE_OBJECT);
  JsonObject* obj  = json_object_new();

  json_object_set_int_member(obj, COMETD_MSG_ID_FIELD, seed);

  json_object_set_string_member(obj,
                                COMETD_MSG_CHANNEL_FIELD,
                                COMETD_CHANNEL_META_UNSUBSCRIBE);

  json_object_set_string_member(obj,
                                COMETD_MSG_CLIENT_ID_FIELD,
                                cometd_conn_client_id(h->conn));

  json_object_set_string_member(obj,
                                COMETD_MSG_SUBSCRIPTION_FIELD,
                                channel);

  json_node_take_object(root, obj);

  return root;
}
Exemplo n.º 7
0
void
cometd_msg_set_advice(JsonNode* msg, const cometd_advice* advice)
{
    g_return_if_fail(JSON_NODE_HOLDS_OBJECT (msg));

    if (!advice) return;

    JsonObject* advice_obj = json_object_new();

    char* reconnect = NULL;
    switch (advice->reconnect)
    {
    case COMETD_RECONNECT_NONE:
        reconnect = "none";
        break;
    case COMETD_RECONNECT_RETRY:
        reconnect = "retry";
        break;
    case COMETD_RECONNECT_HANDSHAKE:
        reconnect = "handshake";
        break;
    }

    json_object_set_string_member(advice_obj, "reconnect", reconnect);
    json_object_set_int_member(advice_obj, "interval", advice->interval);

    JsonObject* obj = json_node_get_object(msg);
    json_object_set_object_member(obj, COMETD_MSG_ADVICE_FIELD, advice_obj);
}
Exemplo n.º 8
0
static void
send_init_command (CockpitTransport *transport)
{
  const gchar *checksum;
  const gchar *name;
  JsonObject *object;
  GBytes *bytes;

  object = json_object_new ();
  json_object_set_string_member (object, "command", "init");
  json_object_set_int_member (object, "version", 1);

  checksum = cockpit_packages_get_checksum (packages);
  if (checksum)
    json_object_set_string_member (object, "checksum", checksum);

  /* Happens when we're in --interact mode */
  name = cockpit_dbus_internal_name ();
  if (name)
    json_object_set_string_member (object, "bridge-dbus-name", name);

  bytes = cockpit_json_write_bytes (object);
  json_object_unref (object);

  cockpit_transport_send (transport, NULL, bytes);
  g_bytes_unref (bytes);
}
Exemplo n.º 9
0
END_TEST

static void add_foo(const cometd* h, JsonNode* n)
{
    JsonObject* obj = json_node_get_object(n);
    json_object_set_int_member(obj, "foo", 1);
}
Exemplo n.º 10
0
void
couchdb_document_contact_set_phone_numbers (CouchdbDocumentContact *document, GSList *list)
{
	GSList *l;
	JsonObject *phone_numbers;

	g_return_if_fail (COUCHDB_IS_DOCUMENT_CONTACT (document));

	phone_numbers = json_object_new ();
	for (l = list; l != NULL; l = l->next) {
		const gchar *number_str;
		CouchdbStructField *sf = (CouchdbStructField *) l->data;

		number_str = couchdb_document_contact_phone_get_number (sf);
		if (number_str) {
			JsonObject *this_phone;

			this_phone = json_object_new ();
			json_object_set_string_member (this_phone, "number", number_str);
			json_object_set_string_member (this_phone, "description",
						       couchdb_document_contact_phone_get_description (sf));
			json_object_set_int_member (this_phone, "priority",
						    couchdb_document_contact_phone_get_priority (sf));

			json_object_set_object_member (phone_numbers,
						       couchdb_struct_field_get_uuid (sf),
						       this_phone);
		}
	}

	json_object_set_object_member (couchdb_document_get_json_object (COUCHDB_DOCUMENT (document)),
				       "phone_numbers", phone_numbers);
}
void
trg_json_widget_spin_int_save(GtkWidget * widget, JsonObject * obj,
                                 gchar * key)
{
    json_object_set_int_member(obj, key,
                               gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON
                                                                (widget)));
}
/**
 * couchdb_struct_field_set_int_field:
 * @sf: A #CouchdbStructField object
 * @field: Name of the field
 * @calue: Value to set the field to
 *
 * Set the value of an integer field in the given struct field.
 */
void
couchdb_struct_field_set_int_field (CouchdbStructField *sf, const char *field, gint value)
{
	g_return_if_fail (sf != NULL);
	g_return_if_fail (field != NULL);

	json_object_set_int_member (sf->json_object, field, value);
}
Exemplo n.º 13
0
static void
cockpit_channel_actual_send (CockpitChannel *self,
                             GBytes *payload,
                             gboolean trust_is_utf8)
{
  GBytes *validated = NULL;
  guint64 out_sequence;
  JsonObject *ping;
  gsize size;

  g_return_if_fail (self->priv->out_buffer == NULL);
  g_return_if_fail (self->priv->buffer_timeout == 0);

  if (!trust_is_utf8)
    {
      if (!self->priv->binary_ok)
         payload = validated = cockpit_unicode_force_utf8 (payload);
    }

  cockpit_transport_send (self->priv->transport, self->priv->id, payload);

  /* A wraparound of our gint64 size? */
  if (self->priv->flow_control)
    {
      size = g_bytes_get_size (payload);
      g_return_if_fail (G_MAXINT64 - size > self->priv->out_sequence);

      /* How many bytes have been sent (queued) */
      out_sequence = self->priv->out_sequence + size;

      /* Every CHANNEL_FLOW_PING bytes we send a ping */
      if (out_sequence / CHANNEL_FLOW_PING != self->priv->out_sequence / CHANNEL_FLOW_PING)
        {
          ping = json_object_new ();
          json_object_set_int_member (ping, "sequence", out_sequence);
          cockpit_channel_control (self, "ping", ping);
          g_debug ("%s: sending ping with sequence: %" G_GINT64_FORMAT, self->priv->id, out_sequence);
          json_object_unref (ping);
        }

      /* If we've sent more than the window, apply back pressure */
      self->priv->out_sequence = out_sequence;
      if (self->priv->out_sequence > self->priv->out_window)
        {
          g_debug ("%s: sent too much data without acknowledgement, emitting back pressure until %"
                   G_GINT64_FORMAT, self->priv->id, self->priv->out_window);
          cockpit_flow_emit_pressure (COCKPIT_FLOW (self), TRUE);
        }
    }

  if (validated)
    g_bytes_unref (validated);
}
Exemplo n.º 14
0
/**
 * cockpit_channel_close_int_option:
 * @self: a channel
 * @name: the option name
 * @value: the value to add
 *
 * Add a value to the close message for this channel. This must
 * be called before the cockpit_channel_close base class
 * implementation.
 */
void
cockpit_channel_close_int_option (CockpitChannel *self,
                                  const gchar *name,
                                  gint64 value)
{
  g_return_if_fail (COCKPIT_IS_CHANNEL (self));
  g_return_if_fail (name != NULL);

  if (!self->priv->close_options)
    self->priv->close_options = json_object_new ();
  json_object_set_int_member (self->priv->close_options, name, value);
}
Exemplo n.º 15
0
/* Export play list to a json object */
JsonArray *plist_export_to_json( plist_t *pl )
{
	JsonArray *js_plist = json_array_new();
	for ( int i = 0; i < pl->m_len; i ++ )
	{
		song_t *s = pl->m_list[i];
		JsonObject *js_song = json_object_new();

		json_object_set_string_member(js_song, "name", song_get_name(s));
		json_object_set_int_member(js_song, "length", s->m_full_len);
		json_object_set_int_member(js_song, "start_time", s->m_start_time);
		json_object_set_int_member(js_song, "end_time", s->m_end_time);

		if (s->m_default_title)
			json_object_set_string_member(js_song, "title", s->m_default_title);

		if (s->m_info && (s->m_info->m_flags & SI_INITIALIZED))
		{
			song_info_t *si = s->m_info;
			JsonObject *js_si = json_object_new();
			json_object_set_string_member(js_si, "artist",		si->m_artist);
			json_object_set_string_member(js_si, "name",		si->m_name);
			json_object_set_string_member(js_si, "album",		si->m_album);
			json_object_set_string_member(js_si, "year",		si->m_year);
			json_object_set_string_member(js_si, "comments",	si->m_comments);
			json_object_set_string_member(js_si, "track",		si->m_track);
			if (si->m_own_data)
				json_object_set_string_member(js_si, "own_data",	si->m_own_data);
			json_object_set_object_member(js_song, "song_info", js_si);

			if (s->m_flags & SONG_STATIC_INFO)
				json_object_set_int_member(js_song, "static_info", 1);
		}

		json_array_add_object_element(js_plist, js_song);
	}
	return js_plist;
}
Exemplo n.º 16
0
static void
send_init_command (CockpitTransport *transport)
{
  JsonObject *object;
  GBytes *bytes;

  object = json_object_new ();
  json_object_set_string_member (object, "command", "init");
  json_object_set_int_member (object, "version", 1);

  bytes = cockpit_json_write_bytes (object);
  cockpit_transport_send (transport, NULL, bytes);
  g_bytes_unref (bytes);
}
Exemplo n.º 17
0
JsonNode*
cometd_msg_connect_new(const cometd* h) {
    JsonNode*   root = json_node_new(JSON_NODE_OBJECT);
    JsonObject* obj  = json_object_new();

    gint64 seed = ++(h->conn->msg_id_seed);
    char*  connection_type = cometd_current_transport(h)->name;

    json_object_set_int_member   (obj, COMETD_MSG_ID_FIELD,      seed);
    json_object_set_string_member(obj, COMETD_MSG_CHANNEL_FIELD, COMETD_CHANNEL_META_CONNECT);
    json_object_set_string_member(obj, "connectionType",         connection_type);
    json_object_set_string_member(obj, "clientId",               cometd_conn_client_id(h->conn));

    json_node_take_object(root, obj);

    return root;
}
Exemplo n.º 18
0
void trg_prefs_empty_init(TrgPrefs * p)
{
    TrgPrefsPrivate *priv = p->priv;
    JsonArray *profiles = json_array_new();

    priv->user = json_node_new(JSON_NODE_OBJECT);
    priv->userObj = json_object_new();
    json_node_take_object(priv->user, priv->userObj);

    priv->profile = trg_prefs_new_profile_object();

    json_array_add_object_element(profiles, priv->profile);
    json_object_set_array_member(priv->userObj, TRG_PREFS_KEY_PROFILES,
                                 profiles);

    json_object_set_int_member(priv->userObj, TRG_PREFS_KEY_PROFILE_ID, 0);
}
Exemplo n.º 19
0
static void
on_web_socket_open (WebSocketConnection *connection,
                    CockpitWebService *self)
{
  CockpitSocket *socket;
  JsonArray *capabilities;
  GBytes *command;
  JsonObject *object;
  JsonObject *info;

  g_info ("New connection to session from %s", cockpit_creds_get_rhost (self->creds));

  socket = cockpit_socket_lookup_by_connection (&self->sockets, connection);
  g_return_if_fail (socket != NULL);

  object = json_object_new ();
  json_object_set_string_member (object, "command", "init");
  json_object_set_int_member (object, "version", 1);
  json_object_set_string_member (object, "channel-seed", socket->id);
  json_object_set_string_member (object, "host", "localhost");
  json_object_set_string_member (object, "csrf-token", cockpit_creds_get_csrf_token (self->creds));

  capabilities = json_array_new ();
  json_array_add_string_element (capabilities, "multi");
  json_array_add_string_element (capabilities, "credentials");
  json_array_add_string_element (capabilities, "binary");
  json_object_set_array_member (object, "capabilities", capabilities);

  info = json_object_new ();
  json_object_set_string_member (info, "version", PACKAGE_VERSION);
  json_object_set_string_member (info, "build", COCKPIT_BUILD_INFO);
  json_object_set_object_member (object, "system", info);

  command = cockpit_json_write_bytes (object);
  json_object_unref (object);

  web_socket_connection_send (connection, WEB_SOCKET_DATA_TEXT, self->control_prefix, command);
  g_bytes_unref (command);

  /* Do we have an authorize password? if so tell the frontend */
  if (cockpit_creds_get_password (self->creds))
    send_socket_hints (self, "credential", "password");

  g_signal_connect (connection, "message",
                    G_CALLBACK (on_web_socket_message), self);
}
Exemplo n.º 20
0
PurpleCmdRet flist_timeout_cmd(PurpleConversation *convo, const gchar *cmd, gchar **args, gchar **error, void *data) {
    PurpleConnection *pc = purple_conversation_get_gc(convo);
    FListAccount *fla = pc->proto_data;
    gchar **split; guint count;
    const gchar *character, *time, *reason;
    gulong time_parsed; gchar *endptr;
    JsonObject *json;
    FListFlags flags;

    flags = flist_get_flags(fla, NULL, fla->proper_character);
    if(!(flags & (FLIST_FLAG_ADMIN | FLIST_FLAG_GLOBAL_OP))) {
        *error = g_strdup(_("You must be a global operator to timeban."));
        return PURPLE_CMD_STATUS_FAILED;
    }

    split = g_strsplit(args[0], ",", 3);
    count = g_strv_length(split);

    if(count < 3) {
        g_strfreev(split);
        *error = g_strdup(_("You must enter a character, a time, and a reason."));
        return PURPLE_CMD_STATUS_WRONG_ARGS;
    }

    character = split[0];
    time = g_strchug(split[1]);
    reason = g_strchug(split[2]);

    time_parsed = strtoul(time, &endptr, 10);
    if(time_parsed == 0 || endptr != time + strlen(time)) {
        g_strfreev(split);
        *error = g_strdup(_("You must enter a valid length of time."));
        return PURPLE_CMD_STATUS_WRONG_ARGS;
    }
    
    json = json_object_new();
    json_object_set_string_member(json, "character", character);
    json_object_set_string_member(json, "reason", reason);
    json_object_set_int_member(json, "time", time_parsed);
    flist_request(pc, FLIST_GLOBAL_TIMEOUT, json);
    json_object_unref(json);
    g_strfreev(split);
    return PURPLE_CMD_STATUS_OK;
}
Exemplo n.º 21
0
static const gchar *
process_transport_init (CockpitWebService *self,
                        CockpitTransport *transport,
                        JsonObject *options)
{
  JsonObject *object;
  GBytes *payload;
  gint64 version;

  if (!cockpit_json_get_int (options, "version", -1, &version))
    {
      g_warning ("invalid version field in init message");
      return "protocol-error";
    }

  if (version == 1)
    {
      g_debug ("received init message");
      self->init_received = TRUE;
      g_object_set_data_full (G_OBJECT (transport), "init",
                              json_object_ref (options),
                              (GDestroyNotify) json_object_unref);

      /* Always send an init message down the new transport */
      object = cockpit_transport_build_json ("command", "init", NULL);
      json_object_set_int_member (object, "version", 1);
      json_object_set_string_member (object, "host", "localhost");
      payload = cockpit_json_write_bytes (object);
      json_object_unref (object);
      cockpit_transport_send (transport, NULL, payload);
      g_bytes_unref (payload);
    }
  else
    {
      g_message ("unsupported version of cockpit protocol: %" G_GINT64_FORMAT, version);
      return "not-supported";
    }

  g_signal_emit (self, sig_transport_init, 0);
  return NULL;
}
Exemplo n.º 22
0
static void
send_init_command (CockpitTransport *transport)
{
  const gchar *checksum;
  JsonObject *object;
  GBytes *bytes;

  object = json_object_new ();
  json_object_set_string_member (object, "command", "init");
  json_object_set_int_member (object, "version", 1);

  checksum = cockpit_packages_get_checksum (packages);
  if (checksum)
    json_object_set_string_member (object, "checksum", checksum);

  bytes = cockpit_json_write_bytes (object);
  json_object_unref (object);

  cockpit_transport_send (transport, NULL, bytes);
  g_bytes_unref (bytes);
}
Exemplo n.º 23
0
static void
postal_http_reply_error (PostalHttp   *http,
                         SoupMessage  *message,
                         const GError *error)
{
   JsonGenerator *g;
   JsonObject *obj;
   JsonNode *node;
   gchar *json_buf;
   gsize length;

   g_assert(SOUP_IS_MESSAGE(message));
   g_assert(error);

   obj = json_object_new();
   json_object_set_string_member(obj, "message", error->message);
   json_object_set_string_member(obj, "domain", g_quark_to_string(error->domain));
   json_object_set_int_member(obj, "code", error->code);

   node = json_node_new(JSON_NODE_OBJECT);
   json_node_set_object(node, obj);
   json_object_unref(obj);

   g = json_generator_new();
   json_generator_set_indent(g, 2);
   json_generator_set_pretty(g, TRUE);
   json_generator_set_root(g, node);
   json_node_free(node);
   json_buf = json_generator_to_data(g, &length);
   g_object_unref(g);

   soup_message_set_response(message,
                             "application/json",
                             SOUP_MEMORY_TAKE,
                             json_buf,
                             length);
   soup_message_set_status(message, get_status_code(error));
   soup_server_unpause_message(http->priv->server, message);
}
Exemplo n.º 24
0
gchar *
error_to_json (int code, const char *msg, gsize *len)
{
    JsonObject *object = json_object_new ();
    JsonNode *root = json_node_new (JSON_NODE_OBJECT);
    JsonGenerator *generator = json_generator_new ();
    gchar *data;

    json_object_set_int_member (object, "err_code", code);
    json_object_set_string_or_null_member (object, "err_msg", msg);
    
    json_node_take_object (root, object);
    json_generator_set_root (generator, root);

    g_object_set (generator, "pretty", FALSE, NULL);
    data = json_generator_to_data (generator, len);

    json_node_free (root);
    g_object_unref (generator);

    return data;
}
Exemplo n.º 25
0
static void
cockpit_channel_real_close (CockpitChannel *self,
                            const gchar *problem)
{
  const gchar *reason = problem;
  JsonObject *object;
  GBytes *message;

  if (self->priv->closed)
    return;

  self->priv->closed = TRUE;

  if (reason == NULL)
    reason = "";

  if (self->priv->close_options)
    {
      object = self->priv->close_options;
      self->priv->close_options = NULL;
    }
  else
    {
      object = json_object_new ();
    }

  json_object_set_string_member (object, "command", "close");
  json_object_set_int_member (object, "channel", self->priv->number);
  json_object_set_string_member (object, "reason", reason);

  message = cockpit_json_write_bytes (object);
  json_object_unref (object);

  cockpit_transport_send (self->priv->transport, 0, message);
  g_bytes_unref (message);

  g_signal_emit (self, cockpit_channel_sig_closed, 0, problem);
}
Exemplo n.º 26
0
gchar *
searpc_marshal_set_ret_common (JsonObject *object, gsize *len, GError *error)
{
    JsonNode *root = json_node_new (JSON_NODE_OBJECT);
    JsonGenerator *generator = json_generator_new ();
    gchar *data;

    if (error) {
        json_object_set_int_member (object, "err_code", error->code);
        json_object_set_string_or_null_member (object, "err_msg", error->message);
        g_error_free (error);
    }

    json_node_take_object (root, object);
    json_generator_set_root (generator, root);

    g_object_set (generator, "pretty", FALSE, NULL);
    data = json_generator_to_data (generator, len);

    json_node_free (root);
    g_object_unref (generator);
    return data;
}
Exemplo n.º 27
0
static void
send_init_command (CockpitTransport *transport,
                   gboolean interactive)
{
  const gchar *checksum;
  JsonObject *object;
  GBytes *bytes;

  object = json_object_new ();
  json_object_set_string_member (object, "command", "init");
  json_object_set_int_member (object, "version", 1);

  /*
   * When in interactive mode pretend we received an init
   * message, and don't print one out.
   */
  if (interactive)
    {
      json_object_set_string_member (object, "host", "localhost");
    }
  else
    {
      checksum = cockpit_packages_get_checksum (packages);
      if (checksum)
        json_object_set_string_member (object, "checksum", checksum);
    }

  bytes = cockpit_json_write_bytes (object);
  json_object_unref (object);

  if (interactive)
    cockpit_transport_emit_recv (transport, NULL, bytes);
  else
    cockpit_transport_send (transport, NULL, bytes);
  g_bytes_unref (bytes);
}
Exemplo n.º 28
0
JsonNode*
cometd_new_handshake_message(const cometd* h)
{
                                                                                                                                                                                      
  gint64 seed = ++(h->conn->msg_id_seed);
  

  /////////////////Data Concatenation/////////////                                                                                                                                                                                               

  JsonObject* dataObject = json_object_new();
  JsonNode*   dataNode = json_node_new(JSON_NODE_OBJECT);
  JsonNode*   dataRoot = json_node_init_object(dataNode, dataObject);

  dataObject = json_node_get_object(dataRoot);

  json_object_set_string_member(dataObject, "login", "test2");
  json_object_set_string_member(dataObject, "password", "password");

  ////////////////Data Concatenation////////////                                                                                                                                                                                                 

  /////////////////Auth/////////////////                                                                                                                                                                                                         

  // I begin from the most inner JsonObject of my Json File                                                                                                                                                                                      

  //here i initialize my Root JsonNode with an object in it and a JsonObject to be able to get the object from the JsonNode                                                                                                                      

  JsonObject* authObject = json_object_new();
  JsonNode*   authNode = json_node_new(JSON_NODE_OBJECT);
  JsonNode*   authRoot = json_node_init_object(authNode, authObject);

  // Here i retrieve the initialized JsonObject that is inside my JsonNode                                                                                                                                                                       
  authObject = json_node_get_object(authRoot);

  // And here some few insertion of strings in the object                                                                                                                                                                                        
  json_object_set_string_member(authObject, "action", "authenticate");
  json_object_set_string_member(authObject, "type", "GmY-HuzW.KZyH.simple");
  json_object_set_string_member(authObject, "resource", "zetapushTuto");
  json_object_set_member(authObject, "data", dataRoot);

  ///////////////Auth/////////////////                                                                                                                                                                                                           



  //////////////First Concatenation : Authentication///////////////                                                                                                                                                                              
  //Here i make the authentication of my first JsonObject with a upper membrane.                                                                                                                                                                 

  JsonObject *extMembrane = json_object_new();
  JsonNode   *contactMembrane = json_node_new(JSON_NODE_OBJECT);
  JsonNode   *secondRootMembrane = json_node_init_object(contactMembrane, extMembrane);

  extMembrane = json_node_get_object(secondRootMembrane);

  json_object_set_member(extMembrane, "authentication", authRoot);
  /////////////First Concatenation////////////////                                                                                                                                                                                               

  ////////////Second Concatenation : Ext///////////////                                                                                                                                                                                          
  // Here i finnaly concatenante my last JsonObject and encapsulate the overall file inside the root JsonNode                                                                                                                                    
  JsonObject *rootObjectMembrane = json_object_new();
  JsonNode   *initRootMembrane = json_node_new(JSON_NODE_OBJECT);
  JsonNode   *rootMembrane = json_node_init_object(initRootMembrane, rootObjectMembrane);

   rootObjectMembrane = json_node_get_object(rootMembrane);

   json_object_set_member(rootObjectMembrane, "ext", secondRootMembrane);


  // I still needs to create a object under Ext JsonNode, then an JsonArray to supportedConnectionTypes                                                                                                                                          
  //Then add the advice object with "timeout" and "interval"                                                                                                                                                                                     

  json_object_set_int_member(rootObjectMembrane, "id", seed);
  json_object_set_string_member(rootObjectMembrane, "version", "1.0");
  json_object_set_string_member(rootObjectMembrane, "minimumVersion", "1.0");
  json_object_set_string_member(rootObjectMembrane, "channel", "/meta/handshake");

  JsonArray* json_transports = json_array_new();
  GList* entry = h->config->transports;
  while (entry){
    cometd_transport* t = entry->data;
    json_array_add_string_element(json_transports, t->name);
    entry = g_list_next(entry);
}

  //json_array_add_string_element(json_transports, "long-polling");
  json_object_set_array_member(rootObjectMembrane, "supportedConnectionTypes", json_transports);

  //////////Advice////////                                                                                                                                                                                                                       
  JsonObject *adviceMembrane = json_object_new();
  JsonNode   *adviceNodeMembrane = json_node_new(JSON_NODE_OBJECT);
  JsonNode   *adviceRootMembrane = json_node_init_object(adviceNodeMembrane, adviceMembrane);
  gint64 interval = 0;
  gint64 timeout = 60000;
  adviceMembrane = json_node_get_object(adviceRootMembrane);
  json_object_set_int_member(adviceMembrane, "timeout", timeout);
  json_object_set_int_member(adviceMembrane, "interval", interval);
  json_object_set_member(rootObjectMembrane, "advice", adviceRootMembrane);

  /////////Advice////////                                                                                                                                                                                                                        



   ////////////Second Concatenation//////////////
  
  // call extensions with message - TODO: implement extensions first
  //json_node_take_object(root, obj);
  return rootMembrane;
}
Exemplo n.º 29
0
/**
 * push_gcm_client_deliver_async:
 * @client: (in): A #PushGcmClient.
 * @identities: (element-type PushGcmIdentity*): A #GList of #PushGcmIdentity.
 * @message: A #PushGcmMessage.
 * @cancellable: (allow-none): A #GCancellable or %NULL.
 * @callback: A #GAsyncReadyCallback.
 * @user_data: User data for @callback.
 *
 * Asynchronously deliver a #PushGcmMessage to one or more GCM enabled
 * devices.
 */
void
push_gcm_client_deliver_async (PushGcmClient       *client,
                               GList               *identities,
                               PushGcmMessage      *message,
                               GCancellable        *cancellable,
                               GAsyncReadyCallback  callback,
                               gpointer             user_data)
{
   PushGcmClientPrivate *priv;
   GSimpleAsyncResult *simple;
   SoupMessage *request;
   const gchar *registration_id;
   const gchar *collapse_key;
   JsonGenerator *g;
   JsonObject *obj;
   JsonObject *data;
   JsonObject *mdata;
   JsonArray *ar;
   JsonNode *node;
   GList *iter;
   GList *list;
   gchar *str;
   gsize length;
   guint time_to_live;

   ENTRY;

   g_return_if_fail(PUSH_IS_GCM_CLIENT(client));
   g_return_if_fail(identities);
   g_return_if_fail(PUSH_IS_GCM_MESSAGE(message));
   g_return_if_fail(!cancellable || G_IS_CANCELLABLE(cancellable));
   g_return_if_fail(callback);

   priv = client->priv;

   request = soup_message_new("POST", PUSH_GCM_CLIENT_URL);
   ar = json_array_new();

   for (iter = identities; iter; iter = iter->next) {
      g_assert(PUSH_IS_GCM_IDENTITY(iter->data));
      registration_id = push_gcm_identity_get_registration_id(iter->data);
      json_array_add_string_element(ar, registration_id);
   }

   str = g_strdup_printf("key=%s", priv->auth_token);
   soup_message_headers_append(request->request_headers, "Authorization", str);
   g_free(str);

   soup_message_headers_append(request->request_headers,
                               "Accept",
                               "application/json");

   data = json_object_new();

   if ((collapse_key = push_gcm_message_get_collapse_key(message))) {
      json_object_set_string_member(data, "collapse_key", collapse_key);
   }

   json_object_set_boolean_member(data,
                                  "delay_while_idle",
                                  push_gcm_message_get_delay_while_idle(message));

   json_object_set_boolean_member(data,
                                  "dry_run",
                                  push_gcm_message_get_dry_run(message));

   if ((time_to_live = push_gcm_message_get_time_to_live(message))) {
      json_object_set_int_member(data, "time_to_live", time_to_live);
   }

   if ((mdata = push_gcm_message_get_data(message))) {
      json_object_set_object_member(data, "data", mdata);
   }

   obj = json_object_new();
   json_object_set_array_member(obj, "registration_ids", ar);
   json_object_set_object_member(obj, "data", data);

   node = json_node_new(JSON_NODE_OBJECT);
   json_node_set_object(node, obj);
   json_object_unref(obj);

   g = json_generator_new();
   json_generator_set_pretty(g, TRUE);
   json_generator_set_indent(g, 2);
   json_generator_set_root(g, node);
   str = json_generator_to_data(g, &length);
   json_node_free(node);
   g_object_unref(g);

   g_print("REQUEST: \"%s\"\n", str);

   soup_message_set_request(request,
                            "application/json",
                            SOUP_MEMORY_TAKE,
                            str,
                            length);

   simple = g_simple_async_result_new(G_OBJECT(client), callback, user_data,
                                      push_gcm_client_deliver_async);

   /*
    * Keep the list of identities around until we receive our result.
    * We need them to key with the resulting array.
    */
   list = g_list_copy(identities);
   g_list_foreach(list, (GFunc)g_object_ref, NULL);
   g_object_set_data_full(G_OBJECT(simple),
                          "identities",
                          list,
                          _push_gcm_identities_free);

   soup_session_queue_message(SOUP_SESSION(client),
                              request,
                              push_gcm_client_deliver_cb,
                              simple);

   EXIT;
}
Exemplo n.º 30
0
static void
send_meta (CockpitInternalMetrics *self)
{
  JsonArray *metrics;
  JsonObject *metric;
  JsonObject *root;
  struct timeval now_timeval;
  gint64 now;

  gettimeofday (&now_timeval, NULL);
  now = timestamp_from_timeval (&now_timeval);

  root = json_object_new ();
  json_object_set_int_member (root, "timestamp", now);
  json_object_set_int_member (root, "now", now);
  json_object_set_int_member (root, "interval", self->interval);

  metrics = json_array_new ();
  for (int i = 0; i < self->n_metrics; i++)
    {
      MetricInfo *info = &self->metrics[i];
      metric = json_object_new ();

      /* Name and derivation mode
       */
      json_object_set_string_member (metric, "name", info->desc->name);
      if (info->derive)
        json_object_set_string_member (metric, "derive", info->derive);

      /* Instances
       */
      if (info->desc->instanced)
        {
          GHashTableIter iter;
          gpointer key, value;
          int index;
          JsonArray *instances = json_array_new ();

          g_hash_table_iter_init (&iter, info->instances);
          index = 0;
          while (g_hash_table_iter_next (&iter, &key, &value))
            {
              const gchar *name = key;
              InstanceInfo *inst = value;

              /* HACK: We can't use json_builder_add_string_value here since
                 it turns empty strings into 'null' values inside arrays.

                 https://bugzilla.gnome.org/show_bug.cgi?id=730803
              */
              {
                JsonNode *string_element = json_node_alloc ();
                json_node_init_string (string_element, name);
                json_array_add_element (instances, string_element);
              }

              inst->index = index++;
            }
          json_object_set_array_member (metric, "instances", instances);
        }

      /* Units and semantics
       */
      json_object_set_string_member (metric, "units", info->desc->units);
      json_object_set_string_member (metric, "semantics", info->desc->semantics);

      json_array_add_object_element (metrics, metric);
    }

  json_object_set_array_member (root, "metrics", metrics);

  cockpit_metrics_send_meta (COCKPIT_METRICS (self), root, FALSE);

  json_object_unref (root);
}