static void ui_connection_handle_message(UiConnection *self, const gchar *protocol, const gchar *command, JsonObject *payload, SoupWebsocketConnection *ws) { if (g_strcmp0(protocol, "graph") == 0) { handle_graph_message(self, command, payload, ws); } else if (g_strcmp0(protocol, "network") == 0) { handle_network_message(self, command, payload, ws); } else if (g_strcmp0(protocol, "component") == 0 && g_strcmp0(command, "list") == 0) { // Our special Processor component JsonObject *processor = library_processor_component(); send_response(ws, "component", "component", processor); // Components for all available GEGL operations guint no_ops = 0; gchar **operation_names = gegl_list_operations(&no_ops); if (no_ops == 0) { g_warning("No GEGL operations found"); } for (int i=0; i<no_ops; i++) { const gchar *op = operation_names[i]; if (g_strcmp0(op, "gegl:seamless-clone-compose") == 0) { // FIXME: reported by GEGL but cannot be instantiated... continue; } JsonObject *component = library_component(op); send_response(ws, "component", "component", component); } } else if (g_strcmp0(protocol, "runtime") == 0 && g_strcmp0(command, "getruntime") == 0) { JsonObject *runtime = json_object_new(); json_object_set_string_member(runtime, "version", "0.4"); // protocol version json_object_set_string_member(runtime, "type", "imgflo"); JsonArray *capabilities = json_array_new(); json_array_add_string_element(capabilities, "protocol:component"); json_object_set_array_member(runtime, "capabilities", capabilities); send_response(ws, "runtime", "runtime", runtime); } else { g_printerr("Unhandled message: protocol='%s', command='%s'", protocol, command); } }
static void ui_connection_handle_message(UiConnection *self, const gchar *protocol, const gchar *command, JsonObject *payload, SoupWebsocketConnection *ws) { if (g_strcmp0(protocol, "graph") == 0) { handle_graph_message(self, command, payload, ws); } else if (g_strcmp0(protocol, "network") == 0) { handle_network_message(self, command, payload, ws); } else if (g_strcmp0(protocol, "component") == 0 && g_strcmp0(command, "list") == 0) { gint no_components = 0; gchar **operation_names = library_list_components(self->component_lib, &no_components); for (int i=0; i<no_components; i++) { const gchar *op = operation_names[i]; if (op) { JsonObject *component = library_get_component(self->component_lib, op); send_response(ws, "component", "component", component); } } g_strfreev(operation_names); } else if (g_strcmp0(protocol, "component") == 0 && g_strcmp0(command, "source") == 0) { const gchar *name = json_object_get_string_member(payload, "name"); gchar *actual_name = library_set_source(self->component_lib, name, json_object_get_string_member(payload, "code") ); if (actual_name) { JsonObject *component = library_get_component(self->component_lib, name); send_response(ws, "component", "component", component); } else { // TODO: error response } g_free(actual_name); } else if (g_strcmp0(protocol, "component") == 0 && g_strcmp0(command, "getsource") == 0) { const gchar *name = json_object_get_string_member(payload, "name"); JsonObject *source_info = json_object_new(); // TODO: generalize for subgraphs-as-components if (g_strcmp0(name, self->main_network) == 0) { json_object_set_string_member(source_info, "name", "main"); // FIXME: dont hardcode json_object_set_string_member(source_info, "library", "default"); // FIXME: dont hardcode Network *n = g_hash_table_lookup(self->network_map, self->main_network); g_assert(n); JsonObject *g = graph_save_json(n->graph); gsize len = 0; gchar *code = json_stringify(g, &len); g_assert(len); json_object_set_string_member(source_info, "language", "json"); json_object_set_string_member(source_info, "code", code); } else { json_object_set_string_member(source_info, "name", name); gchar *code = library_get_source(self->component_lib, name); json_object_set_string_member(source_info, "library", "imgflo"); json_object_set_string_member(source_info, "language", "c"); json_object_set_string_member(source_info, "code", code); } send_response(ws, "component", "source", source_info); } else if (g_strcmp0(protocol, "runtime") == 0 && g_strcmp0(command, "getruntime") == 0) { JsonObject *runtime = json_object_new(); json_object_set_string_member(runtime, "version", "0.4"); // protocol version json_object_set_string_member(runtime, "type", "imgflo"); json_object_set_string_member(runtime, "graph", self->main_network); JsonArray *capabilities = json_array_new(); json_array_add_string_element(capabilities, "protocol:component"); json_array_add_string_element(capabilities, "protocol:graph"); json_array_add_string_element(capabilities, "protocol:runtime"); json_array_add_string_element(capabilities, "protocol:network"); json_array_add_string_element(capabilities, "component:getsource"); json_array_add_string_element(capabilities, "component:setsource"); json_object_set_array_member(runtime, "capabilities", capabilities); send_response(ws, "runtime", "runtime", runtime); if (self->main_network) { send_ports(g_hash_table_lookup(self->network_map, self->main_network), ws); } } else if (g_strcmp0(protocol, "runtime") == 0 && g_strcmp0(command, "packet") == 0) { gchar *graph_id = g_strdup(json_object_get_string_member(payload, "graph")); const gchar *port = json_object_get_string_member(payload, "port"); const gchar *event = json_object_get_string_member(payload, "event"); if (!graph_id) { // NoFlo RemoteSubGraph currently does not send graph info graph_id = g_strdup(self->main_network); } Network *network = (graph_id) ? g_hash_table_lookup(self->network_map, graph_id) : NULL; g_free(graph_id); g_return_if_fail(network); if (g_strcmp0(event, "data") == 0) { GValue data = G_VALUE_INIT; json_node_get_value(json_object_get_member(payload, "payload"), &data); network_send_packet(network, port, &data); } else { // TODO: support connect/disconnect? imgflo_warning("Unknown runtime:packet event: %s", event); } } else { imgflo_warning("Unhandled message: protocol='%s', command='%s'", protocol, command); } }