daemon: handle apps with same name in different vgraphs
[ladish.git] / daemon / cmd_remove_app.c
blob0afaea1bb3e79982ec62aab6089dbf1b172c7fec
1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
2 /*
3 * LADI Session Handler (ladish)
5 * Copyright (C) 2010 Nedko Arnaudov <nedko@arnaudov.name>
7 **************************************************************************
8 * This file contains implementation of the "remove app" command
9 **************************************************************************
11 * LADI Session Handler is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * LADI Session Handler is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with LADI Session Handler. If not, see <http://www.gnu.org/licenses/>
23 * or write to the Free Software Foundation, Inc.,
24 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include <ctype.h>
28 #include "cmd.h"
29 #include "studio.h"
30 #include "../dbus/error.h"
31 #include "../proxies/notify_proxy.h"
32 #include "virtualizer.h"
34 struct ladish_command_remove_app
36 struct ladish_command command; /* must be the first member */
37 char * opath;
38 uint64_t id;
41 #define cmd_ptr ((struct ladish_command_remove_app *)context)
43 static bool run(void * context)
45 ladish_app_supervisor_handle supervisor;
46 ladish_app_handle app;
47 const char * app_name;
48 uuid_t app_uuid;
50 ASSERT(cmd_ptr->command.state == LADISH_COMMAND_STATE_PENDING);
52 log_info("remove app command. opath='%s'", cmd_ptr->opath);
54 supervisor = ladish_studio_find_app_supervisor(cmd_ptr->opath);
55 if (supervisor == NULL)
57 log_error("cannot find supervisor '%s' to remove app from", cmd_ptr->opath);
58 ladish_notify_simple(LADISH_NOTIFY_URGENCY_HIGH, "Cannot remove app because of internal error (unknown supervisor)", NULL);
59 return false;
62 app = ladish_app_supervisor_find_app_by_id(supervisor, cmd_ptr->id);
63 if (app == NULL)
65 log_error("App with ID %"PRIu64" not found and (remove)", cmd_ptr->id);
66 ladish_notify_simple(LADISH_NOTIFY_URGENCY_HIGH, "Cannot remove app because it is not found", NULL);
67 return false;
70 app_name = ladish_app_get_name(app);
71 ladish_app_get_uuid(app, app_uuid);
73 if (ladish_app_is_running(app))
75 if (cmd_ptr->command.state == LADISH_COMMAND_STATE_PENDING)
77 log_info("App %s is not stopped as it should", app_name);
78 ladish_notify_simple(LADISH_NOTIFY_URGENCY_HIGH, "Cannot remove app because it is not stopped (because of internal error)", NULL);
80 return false;
83 /* remove jclient and vclient for this app */
84 log_info("Removing graph objects for app '%s'", app_name);
85 ladish_virtualizer_remove_app(ladish_studio_get_jack_graph(), app_uuid, app_name);
87 ladish_app_supervisor_remove_app(supervisor, app);
88 cmd_ptr->command.state = LADISH_COMMAND_STATE_DONE;
89 return true;
92 static void destructor(void * context)
94 log_info("remove_app command destructor");
95 free(cmd_ptr->opath);
98 #undef cmd_ptr
100 bool ladish_command_remove_app(void * call_ptr, struct ladish_cqueue * queue_ptr, const char * opath, uint64_t id)
102 struct ladish_command_remove_app * cmd_ptr;
103 char * opath_dup;
105 if (!ladish_command_change_app_state(call_ptr, queue_ptr, opath, id, LADISH_APP_STATE_STOPPED))
107 goto fail;
110 opath_dup = strdup(opath);
111 if (opath_dup == NULL)
113 lash_dbus_error(call_ptr, LASH_DBUS_ERROR_GENERIC, "strdup('%s') failed.", opath);
114 goto fail_drop_stop_command;
117 cmd_ptr = ladish_command_new(sizeof(struct ladish_command_remove_app));
118 if (cmd_ptr == NULL)
120 lash_dbus_error(call_ptr, LASH_DBUS_ERROR_GENERIC, "ladish_command_new() failed.");
121 goto fail_free_opath;
124 cmd_ptr->command.run = run;
125 cmd_ptr->command.destructor = destructor;
126 cmd_ptr->opath = opath_dup;
127 cmd_ptr->id = id;
129 if (!ladish_cqueue_add_command(queue_ptr, &cmd_ptr->command))
131 lash_dbus_error(call_ptr, LASH_DBUS_ERROR_GENERIC, "ladish_cqueue_add_command() failed.");
132 goto fail_destroy_command;
135 return true;
137 fail_destroy_command:
138 free(cmd_ptr);
139 fail_free_opath:
140 free(opath_dup);
141 fail_drop_stop_command:
142 ladish_cqueue_drop_command(queue_ptr);
143 fail:
144 return false;