static void handle_network_message(UiConnection *self, const gchar *command, JsonObject *payload, SoupWebsocketConnection *ws) { g_return_if_fail(payload); const gchar *graph_id = json_object_get_string_member(payload, "graph"); Network *network = (graph_id) ? g_hash_table_lookup(self->network_map, graph_id) : NULL; g_return_if_fail(network); if (g_strcmp0(command, "start") == 0) { imgflo_info("\tNetwork START\n"); network_set_running(network, TRUE); } else if (g_strcmp0(command, "stop") == 0) { imgflo_info("\tNetwork STOP\n"); network_set_running(network, FALSE); } else if (g_strcmp0(command, "edges") == 0) { // TODO: update subscriptions send_response(ws, "network", "edges", payload); } else if (g_strcmp0(command, "getstatus") == 0) { JsonObject *info = json_object_new(); json_object_set_string_member(info, "graph", graph_id); json_object_set_boolean_member(info, "running", network_is_processing(network)); json_object_set_boolean_member(info, "started", network->running); send_response(ws, "network", "status", info); } else if (g_strcmp0(command, "debug") == 0) { // Ignored, not implemented } else { imgflo_warning("Unhandled message on protocol 'network', command='%s'", command); } }
void cometd_msg_set_boolean_member(JsonNode* node, const char* member, gboolean val) { g_assert(JSON_NODE_HOLDS_OBJECT (node)); JsonObject* obj = json_node_get_object(node); json_object_set_boolean_member(obj, member, val); }
void cockpit_channel_socket_open (CockpitWebService *service, JsonObject *open, const gchar *original_path, const gchar *path, GIOStream *io_stream, GHashTable *headers, GByteArray *input_buffer, gboolean for_tls_proxy) { CockpitChannelSocket *self = NULL; WebSocketDataType data_type; CockpitTransport *transport; gchar **protocols = NULL; gchar *id = NULL; if (!cockpit_web_service_parse_external (open, NULL, NULL, NULL, &protocols) || !cockpit_web_service_parse_binary (open, &data_type)) { respond_with_error (original_path, path, io_stream, for_tls_proxy, headers, 400, "Bad channel request"); goto out; } transport = cockpit_web_service_get_transport (service); if (!transport) { respond_with_error (original_path, path, io_stream, for_tls_proxy, headers, 502, "Failed to open channel transport"); goto out; } json_object_set_boolean_member (open, "flow-control", TRUE); id = cockpit_web_service_unique_channel (service); self = g_object_new (COCKPIT_TYPE_CHANNEL_SOCKET, "transport", transport, "options", open, "id", id, NULL); self->data_type = data_type; self->socket = cockpit_web_service_create_socket ((const gchar **)protocols, original_path, io_stream, headers, input_buffer, for_tls_proxy); self->socket_open = g_signal_connect (self->socket, "open", G_CALLBACK (on_socket_open), self); self->socket_message = g_signal_connect (self->socket, "message", G_CALLBACK (on_socket_message), self); self->socket_close = g_signal_connect (self->socket, "close", G_CALLBACK (on_socket_close), self); /* Unref when the channel closes */ g_signal_connect_after (self, "closed", G_CALLBACK (g_object_unref), NULL); /* Tell the channel to throttle based on back pressure from socket */ cockpit_flow_throttle (COCKPIT_FLOW (self), COCKPIT_FLOW (self->socket)); /* Tell the socket peer's output to throttle based on back pressure */ cockpit_flow_throttle (COCKPIT_FLOW (self->socket), COCKPIT_FLOW (self)); out: g_free (id); g_free (protocols); }
/** * json_builder_add_boolean_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_boolean_value (JsonBuilder *builder, gboolean 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_boolean_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_boolean_element (state->data.array, value); break; default: g_assert_not_reached (); } return builder; }
/** * cockpit_fslist_open: * @transport: the transport to send/receive messages on * @channel_id: the channel id * @path: the path name of the file to read * @watch: boolean, watch the directoy as well? * * This function is mainly used by tests. The usual way * to get a #CockpitFslist is via cockpit_channel_open() * * Returns: (transfer full): the new channel */ CockpitChannel * cockpit_fslist_open (CockpitTransport *transport, const gchar *channel_id, const gchar *path, const gboolean watch) { CockpitChannel *channel; JsonObject *options; g_return_val_if_fail (channel_id != NULL, NULL); options = json_object_new (); json_object_set_string_member (options, "path", path); json_object_set_string_member (options, "payload", "fslist1"); json_object_set_boolean_member (options, "watch", watch); channel = g_object_new (COCKPIT_TYPE_FSLIST, "transport", transport, "id", channel_id, "options", options, NULL); json_object_unref (options); return channel; }
/** * couchdb_struct_field_set_boolean_field: * @sf: A #CouchdbStructField object * @field: Name of the field * @calue: Value to set the field to * * Set the value of a boolean field in the given struct field. */ void couchdb_struct_field_set_boolean_field (CouchdbStructField *sf, const char *field, gboolean value) { g_return_if_fail (sf != NULL); g_return_if_fail (field != NULL); json_object_set_boolean_member (sf->json_object, field, value); }
void trg_json_widget_check_save(GtkWidget * widget, JsonObject * obj, gchar * key) { gboolean active = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)); json_object_set_boolean_member(obj, key, active); }
JsonNode *torrent_set_location(JsonArray * array, gchar * location, gboolean move) { JsonNode *req = generic_request(METHOD_TORRENT_SET_LOCATION, array); JsonObject *args = node_get_arguments(req); json_object_set_boolean_member(args, FIELD_MOVE, move); json_object_set_string_member(args, FIELD_LOCATION, location); return req; }
JsonNode *torrent_add_url(const gchar * url, gboolean paused) { JsonNode *root = base_request(METHOD_TORRENT_ADD); JsonObject *args = node_get_arguments(root); json_object_set_string_member(args, PARAM_FILENAME, url); json_object_set_boolean_member(args, PARAM_PAUSED, paused); request_set_tag(root, TORRENT_GET_TAG_MODE_FULL); return root; }
void ui_net_state_changed(Network *network, gboolean running, gboolean processing, gpointer user_data) { g_return_if_fail(network); g_assert(user_data); UiConnection *self = (UiConnection *)user_data; // TODO: send timestamp/uptime JsonObject *info = json_object_new(); json_object_set_string_member(info, "graph", network->graph->id); json_object_set_boolean_member(info, "started", running); json_object_set_boolean_member(info, "running", processing); const gchar * cmd = (running) ? "started" : "stopped"; if (self->connection) { send_response(self->connection, "network", cmd, info); } }
void send_ports(Network *network, SoupWebsocketConnection *ws) { g_return_if_fail(network); g_return_if_fail(network->graph); GHashTableIter iter; gpointer key = NULL; gpointer value = NULL; JsonObject *payload = json_object_new(); json_object_set_string_member(payload, "graph", network->graph->id); // Inports JsonArray *inports = json_array_new(); json_object_set_array_member(payload, "inPorts", inports); g_hash_table_iter_init(&iter, network->graph->inports); while (g_hash_table_iter_next(&iter, &key, &value)) { const gchar *exported_port = (const gchar *)(key); JsonObject *port = json_object_new(); json_object_set_string_member(port, "id", exported_port); json_object_set_string_member(port, "type", "any"); // TODO: should be that of the target json_object_set_string_member(port, "description", ""); json_object_set_boolean_member(port, "addressable", FALSE); json_object_set_boolean_member(port, "required", FALSE); json_array_add_object_element(inports, port); } // Outports JsonArray *outports = json_array_new(); json_object_set_array_member(payload, "outPorts", outports); g_hash_table_iter_init(&iter, network->graph->outports); while (g_hash_table_iter_next(&iter, &key, &value)) { const gchar *exported_port = (const gchar *)(key); JsonObject *port = json_object_new(); json_object_set_string_member(port, "id", exported_port); json_object_set_string_member(port, "type", "any"); // TODO: should be that of the target json_object_set_string_member(port, "description", ""); json_object_set_boolean_member(port, "addressable", FALSE); json_object_set_boolean_member(port, "required", FALSE); json_array_add_object_element(outports, port); } send_response(ws, "runtime", "ports", payload); }
JsonNode *torrent_remove(JsonArray * array, gboolean removeData) { JsonNode *root = base_request(METHOD_TORRENT_REMOVE); JsonObject *args = node_get_arguments(root); json_object_set_array_member(args, PARAM_IDS, array); json_object_set_boolean_member(args, PARAM_DELETE_LOCAL_DATA, removeData); request_set_tag(root, TORRENT_GET_TAG_MODE_FULL); return root; }
JsonNode *torrent_add(gchar * target, gint flags) { JsonNode *root; JsonObject *args; gboolean isMagnet = is_magnet(target); gboolean isUri = isMagnet || is_url(target); gchar *encodedFile; if (!isUri && !g_file_test(target, G_FILE_TEST_IS_REGULAR)) { g_message("file \"%s\" does not exist.", target); return NULL; } root = base_request(METHOD_TORRENT_ADD); args = node_get_arguments(root); if (isUri) { json_object_set_string_member(args, PARAM_FILENAME, target); } else { encodedFile = trg_base64encode(target); if (encodedFile) { json_object_set_string_member(args, PARAM_METAINFO, encodedFile); g_free(encodedFile); } else { g_error("unable to base64 encode file \"%s\".", target); return NULL; } } json_object_set_boolean_member(args, PARAM_PAUSED, (flags & TORRENT_ADD_FLAG_PAUSED)); if ((flags & TORRENT_ADD_FLAG_DELETE)) g_unlink(target); return root; }
static void add_page_to_environment (JsonObject *object) { static gint page_login_to = -1; JsonObject *page; const gchar *value; page = json_object_new (); value = cockpit_conf_string ("WebService", "LoginTitle"); if (value) json_object_set_string_member (page, "title", value); if (page_login_to < 0) { page_login_to = cockpit_conf_bool ("WebService", "LoginTo", g_file_test (cockpit_ws_ssh_program, G_FILE_TEST_IS_EXECUTABLE)); } json_object_set_boolean_member (page, "connect", page_login_to); json_object_set_object_member (object, "page", page); }
gint main (gint argc, gchar ** argv) { JSGlobalContextRef ctx; JSStringRef str; JSObjectRef func; JSValueRef result; JSValueRef exception = NULL; JsonObject *obj; JsonNode *node; g_type_init(); gchar *buffer; if (argc != 2) { printf ("Usage: %s <script>\n", argv[0]); return 1; } ctx = JSGlobalContextCreate (NULL); str = JSStringCreateWithUTF8CString ("emitIntermediate"); func = JSObjectMakeFunctionWithCallback (ctx, str, js_emitIntermediate); JSObjectSetProperty (ctx, JSContextGetGlobalObject (ctx), str, func, kJSPropertyAttributeNone, NULL); JSStringRelease (str); str = JSStringCreateWithUTF8CString ("emit"); func = JSObjectMakeFunctionWithCallback (ctx, str, js_emit); JSObjectSetProperty (ctx, JSContextGetGlobalObject (ctx), str, func, kJSPropertyAttributeNone, NULL); JSStringRelease (str); str = JSStringCreateWithUTF8CString (argv[1]); result = JSEvaluateScript (ctx, str, NULL, NULL, 0, &exception); JSStringRelease (str); obj = json_object_new (); if (!result || exception) { js_value (ctx, exception, &node); json_object_set_member (obj, "error", node); json_object_set_boolean_member (obj, "status", FALSE); JSGlobalContextRelease (ctx); } else { json_object_set_boolean_member (obj, "status", TRUE); } JsonNode *node1 = json_node_new (JSON_NODE_OBJECT); if (node1 == NULL) { json_object_unref (obj); JSGlobalContextRelease (ctx); return 1; } json_node_set_object (node1, obj); JsonGenerator *gen = json_generator_new(); if (gen == NULL) { json_node_free (node1); JSGlobalContextRelease (ctx); return 1; } json_generator_set_root (gen, node1 ); buffer = json_generator_to_data (gen,NULL); if (buffer == NULL) { json_node_free (node1); JSGlobalContextRelease (ctx); return 1; } json_node_free (node1); puts (buffer); g_free (buffer); JSGlobalContextRelease (ctx); return 0; }
/* Not much point adding a default of FALSE, as that's the fallback */ void trg_prefs_add_default_bool_true(TrgPrefs * p, const gchar * key) { TrgPrefsPrivate *priv = p->priv; json_object_set_boolean_member(priv->defaultsObj, key, TRUE); }
static int pb_send_im(PurpleConnection *pc, const gchar *who, const gchar *message, PurpleMessageFlags flags) { PushBulletAccount *pba = pc->proto_data; gchar *stripped, *postdata; gchar *guid; if (g_str_has_prefix(message, "?OTR")) return 0; if (PB_IS_SMS(who)) { JsonObject *root = json_object_new(); JsonObject *data = json_object_new(); JsonArray *addresses = json_array_new(); json_array_add_string_element(addresses, who); json_object_set_array_member(data, "addresses", addresses); guid = pb_get_next_id(pba); json_object_set_string_member(data, "guid", guid); json_object_set_string_member(data, "target_device_iden", pba->main_sms_device); json_object_set_boolean_member(data, "encrypted", FALSE); stripped = g_strstrip(purple_markup_strip_html(message)); json_object_set_string_member(data, "message", stripped); g_free(stripped); json_object_set_object_member(root, "data", data); postdata = pb_jsonobj_to_string(root); pb_fetch_url(pba, "https://api.pushbullet.com/v3/create-text", postdata, NULL, NULL); g_free(postdata); json_object_unref(root); g_hash_table_insert(pba->sent_messages_hash, guid, guid); return 1; } if (!strchr(who, '@')) { return -1; } //<IMG ID="5"> - embedded image i.e. MMS /* Image flow: POST to https://api.pushbullet.com/v3/start-upload {"name":"imagename.jpg","size":12345,"type":"image/jpeg"} => {"id":"abcde","piece_size":5242880,"piece_urls":["https://upload.pushbullet.com/upload-piece/12345/0"]} POST data in chunks to the pieces_urls POST to https://api.pushbullet.com/v3/finish-upload {"id":"abcde"} (from earlier) => {"file_name":"imagename.jpg","file_type":"image/jpeg","file_url":"..urltoimage..."} POST to https://api.pushbullet.com/v2/pushes {"type":"file","file_name":"filename.jpg","file_url":"..urltoimage...","file_type":"image/jpeg","email":"touser"} */ { JsonObject *root = json_object_new(); guid = pb_get_next_id(pba); json_object_set_string_member(root, "guid", guid); json_object_set_string_member(root, "type", "note"); json_object_set_string_member(root, "title", ""); json_object_set_string_member(root, "url", ""); json_object_set_string_member(root, "email", who); stripped = g_strstrip(purple_markup_strip_html(message)); json_object_set_string_member(root, "body", stripped); g_free(stripped); postdata = pb_jsonobj_to_string(root); pb_fetch_url(pba, "https://api.pushbullet.com/v2/pushes", postdata, NULL, NULL); g_free(postdata); json_object_unref(root); g_hash_table_insert(pba->sent_messages_hash, guid, guid); return 1; } return -1; }
/* Params utils */ static gboolean melo_jsonrpc_add_node (JsonNode *node, JsonObject *schema, JsonObject *obj, JsonArray *array) { GType vtype = G_TYPE_INVALID; const gchar *s_name; const gchar *s_type; JsonNodeType type; /* Get name and type from schema */ s_name = json_object_get_string_member (schema, "name"); s_type = json_object_get_string_member (schema, "type"); if (!s_name || !s_type) return FALSE; /* Get type */ type = json_node_get_node_type (node); if (type == JSON_NODE_VALUE) vtype = json_node_get_value_type (node); /* Check type: * We check only first letter of the type string. */ switch (s_type[0]) { case 'b': /* Boolean: check type */ if (vtype != G_TYPE_BOOLEAN) return FALSE; /* Add to object / array */ if (obj || array) { gboolean v; v = json_node_get_boolean (node); if (obj) json_object_set_boolean_member (obj, s_name, v); else json_array_add_boolean_element (array, v); break; } break; case 'i': /* Integer: check type */ if (vtype != G_TYPE_INT64) return FALSE; /* Add to object / array */ if (obj || array) { gint64 v; v = json_node_get_int (node); if (obj) json_object_set_int_member (obj, s_name, v); else json_array_add_int_element (array, v); } break; case 'd': /* Double: check type */ if (vtype != G_TYPE_DOUBLE) return FALSE; /* Add to object / array */ if (obj || array) { gdouble v; v = json_node_get_double (node); if (obj) json_object_set_double_member (obj, s_name, v); else json_array_add_double_element (array, v); } break; case 's': /* String: check type */ if (vtype != G_TYPE_STRING) return FALSE; /* Add to object / array */ if (obj || array) { const gchar *v; v = json_node_get_string (node); if (obj) json_object_set_string_member (obj, s_name, v); else json_array_add_string_element (array, v); } break; case 'o': /* Object: check type */ if (type != JSON_NODE_OBJECT) return FALSE; /* Add to object / array */ if (obj || array) { JsonObject *v; v = json_node_dup_object (node); if (obj) json_object_set_object_member (obj, s_name, v); else json_array_add_object_element (array, v); } break; case 'a': /* Array: check type */ if (type != JSON_NODE_ARRAY) return FALSE; /* Add to object / array */ if (obj || array) { JsonArray *v; v = json_node_dup_array (node); if (obj) json_object_set_array_member (obj, s_name, v); else json_array_add_array_element (array, v); } break; default: return FALSE; } return TRUE; }
/** * 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; }
} END_TEST /* FIXME: more tests required for: * * - object containing an array (of various types). * - object containing an object (containing various types). * - object containing a double. */ START_TEST(test_cc_oci_json_obj_to_string) { gchar *str; JsonObject *obj; gboolean ret; gchar **fields; gchar *value; ck_assert (! cc_oci_json_obj_to_string (NULL, false, NULL)); ck_assert (! cc_oci_json_obj_to_string (NULL, true, NULL)); obj = json_object_new (); /* empty object, non-pretty */ str = cc_oci_json_obj_to_string (obj, false, NULL); ck_assert (str); ret = g_regex_match_simple ( /* pair of braces containing optional * whitespace. */ "{\\s*}", str, 0, 0); ck_assert (ret); g_free (str); /* empty object, pretty */ str = cc_oci_json_obj_to_string (obj, true, NULL); ck_assert (str); ret = g_regex_match_simple ( /* pair of braces containing optional * whitespace. */ "{\\s*}", str, 0, 0); ck_assert (ret); g_free (str); /* non-empty object, non-pretty */ json_object_set_string_member (obj, "test-string-set", "foo"); json_object_set_string_member (obj, "test-string-empty", ""); json_object_set_boolean_member (obj, "test-bool-false", false); json_object_set_boolean_member (obj, "test-bool-true", true); json_object_set_int_member (obj, "test-int-max", G_MAXINT64); json_object_set_int_member (obj, "test-int-min", G_MININT64); json_object_set_null_member (obj, "test-null"); str = cc_oci_json_obj_to_string (obj, false, NULL); ck_assert (str); fields = g_strsplit (str, ",", -1); ck_assert (fields); g_free (str); ck_assert (strv_contains_regex (fields, "test-string-set\\S*foo")); ck_assert (strv_contains_regex (fields, "test-string-empty\\S*\"\"")); ck_assert (strv_contains_regex (fields, "test-bool-true\\S*true")); ck_assert (strv_contains_regex (fields, "test-bool-false\\S*false")); value = g_strdup_printf ("test-int-max\\S*" "%" G_GINT64_FORMAT, G_MAXINT64); ck_assert (value); ck_assert (strv_contains_regex (fields, value)); g_free (value); value = g_strdup_printf ("test-int-min\\S*" "%" G_GINT64_FORMAT, G_MININT64); ck_assert (value); ck_assert (strv_contains_regex (fields, value)); g_free (value); g_strfreev (fields); /* clean up */ json_object_unref (obj); } END_TEST